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
157 lines (130 sloc)
4 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, | |
overwrite = False | |
): | |
def copy_cmd(src, dst): | |
if overwrite: | |
copy_dir_overwrite( src, dst ) | |
else: | |
shutil.copytree( src, dst ) | |
for f in input_dir.iterdir(): | |
logging.debug( f"copy dir: {f}" ) | |
if f.is_dir(): | |
copy_cmd( | |
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( | |
"-l", "--log-dir", | |
default = DEFAULT_OUTPUT_DIR / "logs", | |
help="logfiles go into this directory" | |
) | |
parser.add_argument( | |
"--log-level", | |
default = "INFO", | |
help="log level: choose between DEBUG, INFO, WARNING, ERROR, CRITICAL" | |
) | |
parser.add_argument( | |
"-f", "--filename", | |
required = True, | |
# default = DEFAULT_INPUT_DIR / "tei", | |
help="the TEI file" | |
) | |
parser.add_argument( | |
"-o", "--output-dir", | |
default = DEFAULT_OUTPUT_DIR / "from_tei/eoatex", | |
metavar = "OUTPUT_DIR", | |
help="output directory", | |
type = Path | |
) | |
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" | |
) | |
args = parser.parse_args() | |
CONFIG_FILE = args.config | |
log_dir = Path(args.log_dir) | |
CONFIG = load_config( | |
CONFIG_FILE, | |
args.log_level, | |
(log_dir / SCRIPT_NAME) . with_suffix( ".log" ), | |
# args.log_file, | |
) | |
OUTPUT_DIR = args.output_dir | |
check_executable( "saxon" ) | |
input_file = Path( args.filename ) | |
output_file = OUTPUT_DIR / "main.tex" | |
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 | |
) | |
copy_files( | |
input_dir = input_file.parent, | |
output_dir = output_file.parent, | |
overwrite = args.overwrite, | |
) | |
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" ) | |
}, | |
) |