[damned-lies] Improved 'variable in file' reading code



commit 9ad4df90a62d46b894dcec7d41adeb8ffe731e91
Author: Claude Paroz <claude 2xlibre net>
Date:   Wed Sep 7 20:50:22 2016 +0200

    Improved 'variable in file' reading code
    
    Variable $(some_var) included in other variables can now be expanded.

 stats/tests/help_docbook/Makefile.am |    9 ++++++
 stats/tests/tests.py                 |    3 ++
 stats/utils.py                       |   49 +++++++++++++++++++--------------
 3 files changed, 40 insertions(+), 21 deletions(-)
---
diff --git a/stats/tests/help_docbook/Makefile.am b/stats/tests/help_docbook/Makefile.am
index 753b673..99507ec 100644
--- a/stats/tests/help_docbook/Makefile.am
+++ b/stats/tests/help_docbook/Makefile.am
@@ -11,3 +11,12 @@ DOC_FIGURES = \
     $(NULL)
 
 DOC_LINGUAS = id
+
+
+help_files_1 = \
+       rnusers.xml
+
+help_files_2 = \
+       rnlookingforward.xml
+
+HELP_FILES = $(help_files_1) $(help_files_2)
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index 874304b..dcd2dc6 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -536,6 +536,9 @@ class UtilsTests(TestCase):
         var_content = utils.search_variable_in_file(file_path, "DOC_INCLUDES")
         self.assertEqual(var_content.split(), ['rnusers.xml', 'rnlookingforward.xml', '$(NULL)'])
 
+        var_content = utils.search_variable_in_file(file_path, "HELP_FILES")
+        self.assertEqual(var_content.split(), ['rnusers.xml', 'rnlookingforward.xml'])
+
     def test_generate_doc_potfile_docbook(self):
         """
         Test Docbook-style help
diff --git a/stats/utils.py b/stats/utils.py
index ddd38a3..5727a8c 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -282,33 +282,40 @@ def search_variable_in_file(file_path, variable):
     """ Search for a variable value in a file, and return content if found
         Return None if variable not found in file (or file does not exist)
     """
+    non_terminated_content = ""
+    found = None
     try:
-        file = open(file_path, "r")
+        with open(file_path, "r") as fh:
+            for line in fh:
+                line = line.strip()
+                if non_terminated_content:
+                    line = non_terminated_content + " " + line
+                # Test if line is non terminated (end with \ or even quote count)
+                if (len(line) > 2 and line[-1] == "\\") or line.count('"') % 2 == 1:
+                    if line[-1] == "\\":
+                        # Remove trailing backslash
+                        line = line[:-1]
+                    non_terminated_content = line
+                else:
+                    non_terminated_content = ""
+                    # Search for variable
+                    match = re.search('%s\s*[=,]\s*"?([^"]*)"?' % variable, line)
+                    if match:
+                        found = match.group(1)
+                        break
     except IOError:
         return None
 
-    non_terminated_content = ""
-    found = None
-    for line in file:
-        line = line.strip()
-        if non_terminated_content:
-            line = non_terminated_content + " " + line
-        # Test if line is non terminated (end with \ or even quote count)
-        if (len(line)>2 and line[-1] == "\\") or line.count('"') % 2 == 1:
-            if line[-1] == "\\":
-                # Remove trailing backslash
-                line = line[:-1]
-            non_terminated_content = line
-        else:
-            non_terminated_content = ""
-            # Search for variable
-            match = re.search('%s\s*[=,]\s*"?([^"]*)"?' % variable, line)
-            if match:
-                found = match.group(1)
-                break
-    file.close()
+    # Try to expand '$(var)' variables
+    if found:
+        for element in found.split():
+            if element.startswith('$(') and element.endswith(')') and element != '$(NULL)':
+                result = search_variable_in_file(file_path, element[2:-1])
+                if result:
+                    found = found.replace(element, result)
     return found
 
+
 def pot_diff_status(pota, potb):
     (status, output, errs) = run_shell_command('diff "%s" "%s"|wc -l' % (pota, potb))
     # POT generation date always change and produce a 4 line diff


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