From 9feb45e1653058e7f6f966fcc1414f3deb54912d Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 24 Dec 2020 16:02:20 +0100 Subject: [PATCH] load only folder now --- src/rougail/objspace.py | 12 ++++++--- src/rougail/xmlreflector.py | 52 ++++++------------------------------- tests/test_1_flattener.py | 4 +-- 3 files changed, 18 insertions(+), 50 deletions(-) diff --git a/src/rougail/objspace.py b/src/rougail/objspace.py index bcb2ac36..94e39c61 100644 --- a/src/rougail/objspace.py +++ b/src/rougail/objspace.py @@ -19,6 +19,9 @@ The RougailObjSpace The visit/annotation stage is a complex step that corresponds to the Rougail procedures. """ + +from typing import List + from .i18n import _ from .xmlreflector import XMLReflector from .annotator import SpaceAnnotator @@ -140,12 +143,13 @@ class RougailObjSpace: return '"' + '", "'.join(xmlfiles[:-1]) + '"' + ' and ' + '"' + xmlfiles[-1] + '"' def create_or_populate_from_xml(self, - namespace, - xmlfolders, - ): + namespace: str, + xmlfolders: List[str], + ) -> List[str]: """Parses a bunch of XML files and populates the RougailObjSpace """ - for xmlfile, document in self.xmlreflector.load_xml_from_folders(xmlfolders): + for xmlfile in self.xmlreflector.load_xml_from_folders(xmlfolders): + document = self.xmlreflector.parse_xmlfile(xmlfile) self.redefine_variables = [] self.xml_parse_document(xmlfile, document, diff --git a/src/rougail/xmlreflector.py b/src/rougail/xmlreflector.py index 37fbd16f..77d75811 100644 --- a/src/rougail/xmlreflector.py +++ b/src/rougail/xmlreflector.py @@ -1,7 +1,7 @@ # coding: utf-8 -from os.path import join, isfile, basename, isdir +from typing import List +from os.path import join, isfile from os import listdir -#from io import BytesIO from lxml.etree import DTD, parse, tostring, XMLSyntaxError @@ -42,51 +42,15 @@ class XMLReflector(object): raise DictConsistencyError(_(f'"{xmlfile}" not a valid XML file: {self.dtd.error_log.filter_from_errors()[0]}'), 43) return document.getroot() - def load_xml_from_folders(self, xmlfolders): + def load_xml_from_folders(self, + xmlfolders: List[str], + ): """Loads all the XML files located in the xmlfolders' list :param xmlfolders: list of full folder's name """ - documents = [] - if not isinstance(xmlfolders, list): - xmlfolders = [xmlfolders] for xmlfolder in xmlfolders: - if isinstance(xmlfolder, list) or isinstance(xmlfolder, tuple): - # directory group : collect files from each - # directory and sort them before loading - group_files = [] - for idx, subdir in enumerate(xmlfolder): - if isdir(subdir): - for filename in listdir(subdir): - group_files.append((filename, idx, subdir)) - else: - group_files.append(basename(subdir), idx, dirname(subdir)) - def sort_group(file1, file2): - if file1[0] == file2[0]: - # sort by initial xmlfolder order if same name - return file1[1].__cmp__(file2[1]) - # sort by filename - elif file1[0] > file2[0]: - return 1 - else: - return -1 - group_files.sort(sort_group) - filenames = [join(f[2], f[0]) for f in group_files] - elif isdir(xmlfolder): - filenames = [] - for filename in listdir(xmlfolder): - filenames.append(join(xmlfolder, filename)) - filenames.sort() - else: - filenames = [xmlfolder] + filenames = [join(xmlfolder, filename) for filename in listdir(xmlfolder) if filename.endswith('.xml')] + filenames.sort() for xmlfile in filenames: - if xmlfile.endswith('.xml'): - #xmlfile_path = join(xmlfolder, xmlfile) - documents.append((xmlfile, self.parse_xmlfile(xmlfile))) - return documents - - def save_xmlfile(self, xmlfilename, xml): # pylint: disable=R0201 - """Write a bunch of XML on the disk - """ - with open(xmlfilename, 'w') as xmlfh: - xmlfh.write(tostring(xml, pretty_print=True, encoding="UTF-8", xml_declaration=True).decode('utf8')) + yield xmlfile diff --git a/tests/test_1_flattener.py b/tests/test_1_flattener.py index 764a8d81..3d3e8c0c 100644 --- a/tests/test_1_flattener.py +++ b/tests/test_1_flattener.py @@ -55,10 +55,10 @@ def test_dir_error(request): def launch_flattener(test_dir, test_ok=False): eolobj = objspace.RougailObjSpace(Config['dtdfilename']) - dirs = test_dir + dirs = [test_dir] subfolder = join(test_dir, 'subfolder') if isdir(subfolder): - dirs = [dirs, subfolder] + dirs.append(subfolder) eolobj.create_or_populate_from_xml(Config['variable_namespace'], dirs) subfolder = join(test_dir, 'extra_dirs', 'extra') if isdir(subfolder):