From 6576acaf198f1299be776b3337912d9162cc2ddc Mon Sep 17 00:00:00 2001 From: Klaus Thoden Date: Thu, 13 Sep 2018 15:17:22 +0200 Subject: [PATCH] Adding publcation.cfg checker --- imxml2django.py | 58 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/imxml2django.py b/imxml2django.py index 2b19662..6c73051 100755 --- a/imxml2django.py +++ b/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 @@ -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 # @@ -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"] @@ -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") @@ -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") @@ -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() @@ -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 @@ -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