Skip to content

Commit

Permalink
Introducing argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaus Thoden committed Mar 2, 2018
1 parent c643b40 commit 28fbff9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions prepare_tei.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from lxml import etree
from datetime import datetime
import bibtexparser
import argparse

logging.basicConfig(level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s')

Expand All @@ -29,10 +30,6 @@
ns_tei = "http://www.tei-c.org/ns/1.0"
NS_MAP = {"t" : ns_tei}

bibtexfile = "03_daston/03_daston.bib"
teifile = "03_daston/03_daston.xml"
fig_directory = "03_daston/images"

def parse_bibtex(bibfile):
"""Parse the bibtex file, return a dict"""

Expand Down Expand Up @@ -153,7 +150,7 @@ def convert_figures(string):
return string
# def convert_figures ends here

def make_figure_elements(list_of_figures):
def make_figure_elements(list_of_figures, figure_directory):
"""Construct the figure element."""

for figure in list_of_figures:
Expand All @@ -168,7 +165,7 @@ def make_figure_elements(list_of_figures):
if len(parts) == 3:
logging.info("This figure contains hyperimage directions")
elif len(parts) == 2:
available_images = os.listdir(fig_directory)
available_images = os.listdir(figure_directory)

for image in available_images:
if image.startswith(parts[0]):
Expand Down Expand Up @@ -269,7 +266,7 @@ def fix_tei_header(xml_tree):
# licence_text = etree.SubElement(licence, "p").text = """Distributed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany License."""

source_desc = xml_tree.xpath("//t:sourceDesc", namespaces=NS_MAP)[0]
bibfile = etree.SubElement(source_desc, "listBibl", source=bibtexfile, type="monograph")
bibfile = etree.SubElement(source_desc, "listBibl", source=str(args.bibfile), type="monograph")

profile_desc = etree.SubElement(xml_tree, "profileDesc")
langusage = etree.SubElement(profile_desc, "langUsage")
Expand All @@ -280,14 +277,20 @@ def fix_tei_header(xml_tree):
def main():
"""The main bit"""

with open(teifile, 'r') as xmlfile:
parser = argparse.ArgumentParser()
parser.add_argument("teifile", help="Output from oxgarage/metypeset, an TEI XML file.")
parser-add_argument("bibfile", help="The bibliography database of the publication.")
parser-add_argument("figdir", help="The directory that contains the figures belonging to the publication.")
args = parser.parse_args()

with open(args.teifile, 'r') as xmlfile:
xml_tree = etree.parse(xmlfile)

################
# bibliography #
################
# bibtexparser
bibdata = parse_bibtex(bibtexfile)
bibdata = parse_bibtex(args.bibfile)

xml_cleaned = cleanup_xml(xml_tree)
xml_cleaned.write("cleaned.xml", pretty_print=True, xml_declaration=True, encoding="utf-8")
Expand All @@ -309,7 +312,7 @@ def main():
xml_tree2 = etree.fromstring(mod_string2)

all_figures = xml_tree2.xpath("//t:graphic", namespaces=NS_MAP)
make_figure_elements(all_figures)
make_figure_elements(all_figures, args.figdir)

all_references = xml_tree2.xpath("//t:bibl", namespaces=NS_MAP)
parse_cited_range(all_references)
Expand Down

0 comments on commit 28fbff9

Please sign in to comment.