Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
wrapper script for tei->html. flexible output redir when running exec…
…utables.
  • Loading branch information
EsGeh authored and EsGeh committed Apr 8, 2019
1 parent 1793c57 commit 779f189
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 8 deletions.
103 changes: 103 additions & 0 deletions tei2html.py
@@ -0,0 +1,103 @@
#!/usr/bin/env python3
# -*- coding: utf-8; mode: python -*-

"A converter from TEI to HTML."

__version__ = "1.0"
__date__ = "20190408"
__author__ = "sgfroerer@mpiwg-berlin.mpg.de"

from utils.load_config import load_config, check_executable, exec_command, ToFile
import utils.libeoaconvert as libeoaconvert

import logging
import argparse
from pathlib import Path

# things to be done
# assign ids top to bottom for the following elements:
# div1 div2 div3 note item table EOAfigure EOAequation formula theorem

BASE_DIR = Path( __file__ ).resolve().parent
SCRIPT_NAME = Path( __file__).stem

def check_executables():
check_executable( "saxon" )

def main(
input_file,
root_dir
):
"""Main function"""

exec_command(
"saxon -t -s:{input_file} -xsl:{xslt_file} domain={root_dir}".format(
xslt_file = (BASE_DIR / SCRIPT_NAME) . with_suffix( ".xsl" ),
input_file = input_file,
root_dir = root_dir
),
)

# def main ends here

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-file",
default = Path("logs", SCRIPT_NAME).with_suffix(".log"),
help="logfile"
)
parser.add_argument(
"--log-level",
default = "INFO",
help="log level: choose between DEBUG, INFO, WARNING, ERROR, CRITICAL"
)
parser.add_argument(
"--root-dir",
default = Path.cwd(),
help="internal html links on the page will use this location as a prefix"
)
parser.add_argument(
"-f", "--filename",
required = True,
help="Name of main EOA-TEI file"
)

'''
TODO: support output directory
parser.add_argument(
"-o", "--output-dir",
default = ".",
help="where to dump all output files"
)
'''

args = parser.parse_args()

CONFIG_FILE = args.config

print("The configfile is '%s'." % CONFIG_FILE)
print("The logfile is '%s'." % args.log_file)

CONFIG = load_config(
CONFIG_FILE,
args.log_level,
args.log_file,
)

check_executables()

# run main:
main(
input_file = args.filename,
root_dir = args.root_dir
)

# finis
35 changes: 27 additions & 8 deletions utils/load_config.py
Expand Up @@ -24,10 +24,18 @@ def copy_dir_overwrite( src, dst ):
shutil.rmtree( dst )
shutil.copytree( src, dst)

class ToFile:
def __init__( self, filename ):
self.filename = filename

class ToLog:
pass

def exec_command(
command,
error_msg = "ERROR while running {command}",
wd = None,
output_to = ToLog(),
log_level = "INFO",
ignore_fail = False
):
Expand All @@ -37,24 +45,35 @@ def exec_command(
)

arguments = shlex.split(command)

if isinstance( output_to, ToFile ):
stdout_file = open( output_to.filename, "w")
if isinstance( output_to, ToLog ):
stdout_file = PIPE
else:
stdout_file = None
process = subprocess.Popen(
arguments,
cwd = wd,
stdout=PIPE,
stderr=PIPE
stdout= stdout_file,
stderr= STDOUT,
)

def check_io():
def check_io(stream):
while True:
output = process.stdout.readline()
output = stream.readline()
if output:
logging.debug( output.decode().strip() )
logging.debug( "> " + output.decode().strip() )
else:
break

# keep checking stdout/stderr until the child exits
while process.poll() is None:
check_io()
logging.debug( f"{command} output:" )
if isinstance( output_to, ToFile ):
stdout_file.close()
elif isinstance( output_to, ToLog ):
while process.poll() is None:
check_io(process.stdout)

ret = process.wait() # 0 means success
if (not ignore_fail) and ret != 0:
Expand Down

0 comments on commit 779f189

Please sign in to comment.