Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding publcation.cfg checker
  • Loading branch information
Klaus Thoden committed Sep 13, 2018
1 parent 81b8c79 commit 6576aca
Showing 1 changed file with 47 additions and 11 deletions.
58 changes: 47 additions & 11 deletions imxml2django.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8; mode: python -*-
# Time-stamp: <2018-08-24 17:37:51 (kthoden)>
# Time-stamp: <2018-09-13 15:15:48 (kthoden)>

"""
Create an XML file that can be inserted into the Django database
Expand Down Expand Up @@ -30,13 +30,14 @@
#####################
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", dest="CONFIG_FILE", help="Name of configuration file", metavar="CONFIGURATION")
parser.add_argument("-p", "--checkpublicationcfg", help="Check the publication.cfg for completeness.", action="store_true")

args = parser.parse_args()

if args.CONFIG_FILE is not None:
CONFIG_FILE = os.path.abspath(args.CONFIG_FILE)
else:
CONFIG_FILE = os.path.dirname(sys.argv[0]) + "/config/eoaconvert.cfg"
CONFIG_FILE = os.path.dirname(sys.argv[0]) + os.path.sep + "config" + os.path.sep + "eoaconvert.cfg"

##################################
# Reading the configuration file #
Expand All @@ -63,7 +64,7 @@
###########################################
# Loading data from first conversion step #
###########################################
with open('tmp_files/data.pickle', 'rb') as f:
with open('tmp_files' + os.path.sep + 'data.pickle', 'rb') as f:
data = pickle.load(f)

dictChapters = data["chapterdict"]
Expand All @@ -79,7 +80,7 @@
if not os.path.exists(os.getcwd() + os.path.sep + "debug"):
os.mkdir(os.getcwd() + os.path.sep + "debug")

xmlTree = etree.parse("tmp_files/IntermediateXMLFile.xml")
xmlTree = etree.parse("tmp_files" + os.path.sep + "IntermediateXMLFile.xml")

libeoaconvert.debug_xml_here(xmlTree, "fresh")

Expand All @@ -89,11 +90,11 @@
############################################################################
""")
# Create django File Structure
if os.path.exists(os.getcwd() + "/CONVERT/django") == False:
os.mkdir(os.getcwd() + "/CONVERT/django")
os.mkdir(os.getcwd() + "/CONVERT/django/images")
os.mkdir(os.getcwd() + "/CONVERT/django/images/embedded")
os.mkdir(os.getcwd() + "/CONVERT/django/files")
if os.path.exists(os.getcwd() + os.path.sep + "CONVERT" + os.path.sep + "django") == False:
os.mkdir(os.getcwd() + os.path.sep + "CONVERT" + os.path.sep + "django")
os.mkdir(os.getcwd() + os.path.sep + "CONVERT" + os.path.sep + "django" + os.path.sep + "images")
os.mkdir(os.getcwd() + os.path.sep + "CONVERT" + os.path.sep + "django" + os.path.sep + "images" + os.path.sep + "embedded")
os.mkdir(os.getcwd() + os.path.sep + "CONVERT" + os.path.sep + "django" + os.path.sep + "files")

# Create empty xmlTree
xmlEOAdocument = etree.Element("EOAdocument")
Expand All @@ -103,7 +104,7 @@
etree.strip_tags(xmlTree, "temp")
libeoaconvert.debug_xml_here(xmlTree, "afterstriptags")
# Write Temporary XML-Maintree
ergebnisdatei = open("tmp_files/Devel_django.xml", "w")
ergebnisdatei = open("tmp_files" + os.path.sep + "Devel_django.xml", "w")
ergebnis = etree.tostring(xmlTree, pretty_print=True, encoding="unicode")
ergebnisdatei.write(ergebnis)
ergebnisdatei.close()
Expand Down Expand Up @@ -788,6 +789,37 @@ def djangoParseHeadline(xmlElement):
return xmlElement
# def djangoParseHeadline ends here

def check_publication_cfg(configuration_file):
"""Check the configuration file before uploading
This function is adapted from the publicationimport script.
"""

logging.debug("Checking configuration file %s.", configuration_file)

config = configparser.ConfigParser()

try:
config.read("CONVERT" + os.path.sep + configuration_file)
except configparser.ParsingError as err:
logging.error(err)

technical_items = ["Serie", "Number", "Title", "Subtitle", "PublicationDate", "Language", "License", "ISBN", "Price", "Shoplink"]
general_items = ["BriefDescription", "DetailedDescription", "Submitter", "EditorialCoordination", "Copyediting", "Translator", "Dedication"]
authors_items = ["Author1", "Author2", "Author3", "Author4", "Author5", "Zusatz"]

categories = {"Technical" : technical_items, "General" : general_items, "Authors" : authors_items}

for cat in categories:
for item in categories[cat]:
try:
config[cat][item]
except KeyError:
logging.error("%s is missing in configuration.", item)

return
# def check_publication_cfg ends here

# Iterate over Chapters, Sections, Subsections, and Subsubsections and
# Put all on one level: EOAchapter
intChapterNumber = 1
Expand Down Expand Up @@ -1565,5 +1597,9 @@ def bring_footnote_down_django(footnote, fragment, footnote_number, object_numbe
tmpResult = etree.tostring(xmlDjangoTree, pretty_print=True, encoding="unicode")
tmpFile.write(tmpResult)
tmpFile.close()

logging.debug("Wrote Django.xml")

if args.checkpublicationcfg:
check_publication_cfg("publication.cfg")
else:
pass

0 comments on commit 6576aca

Please sign in to comment.