Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
EOASkripts/src/tei2eoatex.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
146 lines (123 sloc)
4.04 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""eoatei -> eoatex""" | |
__version__= "1.0" | |
__author__ = "samuel" | |
__date__="20191114" | |
from utils.load_config import load_config, exec_command, check_executable, copy_dir_overwrite, ToLog, ToFile | |
from utils.run_xslt import run_xslt | |
import logging | |
import argparse | |
from pathlib import Path | |
import os | |
import shutil | |
BASE_DIR = Path( __file__ ).resolve().parent | |
SCRIPT_PATH = Path( __file__ ) | |
SCRIPT_NAME = SCRIPT_PATH.stem | |
DEFAULT_INPUT_DIR = \ | |
Path(os.environ['INPUT_DIR'] if 'INPUT_DIR' in os.environ else './input') | |
DEFAULT_OUTPUT_DIR = \ | |
Path(os.environ['OUTPUT_DIR'] if 'OUTPUT_DIR' in os.environ else './output') | |
DEFAULT_DEPENDENCIES_DIR = \ | |
Path(os.environ['DEPENDENCIES_DIR'] if 'DEPENDENCIES_DIR' in os.environ else './dependencies') | |
EOA_SCRIPTS_DIR = \ | |
Path(os.environ['EOA_SCRIPTS_DIR']) | |
def copy_files( | |
input_dir, | |
output_dir, | |
): | |
for f in input_dir.iterdir(): | |
logging.debug( f"copy dir: {f}" ) | |
if f.is_dir(): | |
shutil.copytree( | |
f, | |
output_dir / f.name | |
) | |
else: | |
shutil.copy( | |
f, | |
output_dir / f.name | |
) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter | |
) | |
parser.add_argument( | |
"-c", "--config", | |
default = BASE_DIR / "config" / "eoaconvert.cfg", | |
help="Name of config file" | |
) | |
parser.add_argument( | |
"--log-level", | |
default = "INFO", | |
help="log level: choose between DEBUG, INFO, WARNING, ERROR, CRITICAL" | |
) | |
parser.add_argument( | |
"-o", "--output-dir", | |
help = f"output directory. default: {DEFAULT_OUTPUT_DIR}/PUBLICATION_NAME/eoatex", | |
type = Path, | |
) | |
parser.add_argument( | |
"-f", "--filename", | |
default = Path("*.xml"), | |
type = Path, | |
help = "xml file inside PUBLICATION_DIR, or absolute path. Patterns like '*.xml' are also acceptable" | |
) | |
parser.add_argument( | |
"-p", "--param", | |
action = 'append', | |
default = [], | |
help="xslt params" | |
) | |
parser.add_argument( | |
"-!", "--overwrite", | |
action = "store_true", | |
default = False, | |
help="overwrite files at OUTPUT_DIR" | |
) | |
parser.add_argument( | |
"PUBLICATION_DIR", | |
help = "directory containing the publication (including resources like pictures, etc.)", | |
type = Path, | |
) | |
args = parser.parse_args() | |
publ_dir = args.PUBLICATION_DIR | |
input_file = \ | |
args.filename if args.filename . is_absolute() else list(publ_dir . glob( str(args.filename) ))[0] | |
OUTPUT_DIR = \ | |
args.output_dir if args.output_dir is not None else (DEFAULT_OUTPUT_DIR / publ_dir.resolve().stem) / "eoatex" | |
log_dir = OUTPUT_DIR / "log" | |
if not input_file.is_file(): | |
import errno | |
raise( Exception( | |
f"not a valid input file: {input_file}" | |
) ) | |
if OUTPUT_DIR.exists(): | |
if args.overwrite: | |
shutil.rmtree( OUTPUT_DIR ) | |
else: | |
raise( Exception( f"output directory already existing: '{OUTPUT_DIR}'!" ) ) | |
if not OUTPUT_DIR.exists(): | |
OUTPUT_DIR.mkdir( | |
parents=True | |
) | |
CONFIG_FILE = args.config | |
CONFIG = load_config( | |
CONFIG_FILE, | |
args.log_level, | |
(log_dir / SCRIPT_NAME) . with_suffix( ".log" ), | |
) | |
check_executable( "saxon" ) | |
output_file = OUTPUT_DIR / "main.tex" | |
copy_files( | |
input_dir = input_file.parent, | |
output_dir = output_file.parent, | |
) | |
run_xslt( | |
input_file = input_file, | |
xslt_file = EOA_SCRIPTS_DIR / "stylesheets/tei2eoatex.xsl", | |
output_file = output_file, | |
params = args.param, | |
exec_command_args = { | |
'output_to' : ToFile( log_dir / SCRIPT_NAME / "saxon.log" ) | |
}, | |
) |