[damned-lies] Refactored part of generate_doc_pot_file into DocFormat class



commit 1708d5e4eae11ae87633590928d8165d89527a51
Author: Claude Paroz <claude 2xlibre net>
Date:   Tue Mar 6 11:18:59 2018 +0100

    Refactored part of generate_doc_pot_file into DocFormat class

 stats/tests/tests.py |    2 +-
 stats/utils.py       |   67 +++++++++++++++++++++++++++++++------------------
 2 files changed, 43 insertions(+), 26 deletions(-)
---
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index 691c7bd..ab8693f 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -588,8 +588,8 @@ class UtilsTests(TestCase):
         help_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_mallard")
         self.addCleanup(os.remove, os.path.join(help_path, 'C', 'gnome-hello.pot'))
         potfile, errs, doc_format = utils.generate_doc_pot_file(help_path, 'gnome-hello', 'gnome-hello')
-        self.assertEqual(potfile, os.path.join(help_path, 'C', 'gnome-hello.pot'))
         self.assertEqual(errs, [])
+        self.assertEqual(potfile, os.path.join(help_path, 'C', 'gnome-hello.pot'))
         self.assertEqual(doc_format.tool, 'itstool')
         self.assertEqual(doc_format.format, 'mallard')
         res = utils.get_fig_stats(potfile, doc_format=doc_format)
diff --git a/stats/utils.py b/stats/utils.py
index f05fa7c..b7f2714 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -38,10 +38,41 @@ ITS_DATA_DIR = os.path.join(settings.SCRATCHDIR, 'data', 'its')
 class DocFormat:
     itstool_regex = re.compile("^msgid \"external ref=\'(?P<path>[^\']*)\' md5=\'(?P<hash>[^\']*)\'\"")
     xml2po_regex = re.compile("^msgid \"@@image: \'(?P<path>[^\']*)\'; md5=(?P<hash>[^\"]*)\"")
-    def __init__(self, is_itstool, is_mallard, use_meson):
-        self.format = is_mallard and "mallard" or "docbook"
-        self.tool = is_itstool and "itstool" or "xml2po"
-        self.use_meson = use_meson
+
+    def __init__(self, makefile):
+        self.makefile = makefile
+        doc_id = makefile.read_variable("HELP_ID", "yelp.project_id")
+        uses_itstool = doc_id is not None or isinstance(makefile, MesonfileWrapper)
+        has_page_files = any(f.endswith('.page') for f in self.source_files(force_all=True))
+        self.format = 'mallard' if has_page_files else 'docbook'
+        self.tool = 'itstool' if uses_itstool else 'xml2po'
+
+    @property
+    def use_meson(self):
+        return isinstance(self.makefile, MesonfileWrapper)
+
+    def source_files(self, force_all=False):
+        """Return absolute paths to help source files."""
+        base_path = os.path.dirname(self.makefile.path)
+        if force_all:
+            source_dir = os.path.join(base_path, 'C')
+            return os.listdir(source_dir)
+        else:
+            sources = self.makefile.read_variable(self.include_var)
+            if not sources:
+                if self.format == "mallard":
+                    # fallback to directory listing
+                    sources = [
+                        f for f in self.source_files(force_all=True) if f.endswith(".page")
+                    ]
+                if not sources:
+                    return []
+            if isinstance(sources, str):
+                sources = sources.split()
+            return [
+                os.path.join(base_path, 'C', src) for src in sources
+                if src not in ("", "$(NULL)")
+            ]
 
     def command(self, potfile, files):
         if self.tool == "itstool":
@@ -375,15 +406,7 @@ def generate_doc_pot_file(vcs_path, potbase, moduleid):
         )
         return "", errors, None
 
-    doc_id = makefile.read_variable("HELP_ID", "yelp.project_id")
-    uses_itstool = doc_id is not None or isinstance(makefile, MesonfileWrapper)
-    all_C_files = os.listdir(os.path.join(vcs_path, "C"))
-    has_page_files = any(f.endswith('.page') for f in all_C_files)
-    doc_format = DocFormat(
-        is_itstool=uses_itstool, is_mallard=has_page_files,
-        use_meson=isinstance(makefile, MesonfileWrapper)
-    )
-
+    doc_format = DocFormat(makefile)
     files = []
     if doc_format.format == "docbook":
         modulename = makefile.read_variable(doc_format.module_var)
@@ -395,25 +418,19 @@ def generate_doc_pot_file(vcs_path, potbase, moduleid):
                 break
         if not files:
             # Last try: only one xml file in C/...
-            xml_files = [f for f in all_C_files if f.endswith(".xml")]
+            xml_files = [f for f in doc_format.source_files(force_all=True) if f.endswith(".xml")]
             if len(xml_files) == 1:
                 files.append(os.path.basename(xml_files[0]))
             else:
                 errors.append(("error", ugettext_noop("%s doesn’t point to a real file, probably a macro.") 
% doc_format.module_var))
                 return "", errors, doc_format
 
-    includes = makefile.read_variable(doc_format.include_var)
-    if includes:
-        if isinstance(includes, str):
-            includes = includes.split()
-        files.extend(filter(lambda x:x not in ("", "$(NULL)"), includes))
-    elif includes is None and doc_format.format == "mallard":
-        # fallback to directory listing
-        files.extend([f for f in all_C_files if f.endswith(".page")])
-
+    files.extend(doc_format.source_files())
     potfile = os.path.join(vcs_path, "C", potbase + ".pot")
-    command = doc_format.command(potfile, [os.path.join("C", f) for f in files])
-    (status, output, errs) = run_shell_command(command, cwd=vcs_path)
+    command = doc_format.command(potfile, [
+        os.path.join("C", f) if not os.path.isabs(f) else f for f in files
+    ])
+    status, output, errs = run_shell_command(command, cwd=vcs_path)
 
     if status != STATUS_OK:
         errors.append(("error",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]