Commits

abudden  committed 09dded3

Added python support for INCLUDE directive (fixes #71).

  • Participants
  • Parent commits d6873be

Comments (0)

Files changed (1)

File plugin/TagHighlight/module/loaddata.py

 import glob
 import re
 
+try:
+    from .debug import Debug
+except ValueError:
+    def Debug(text, level):
+        print(text)
+
 data_directory = None
 
 leadingTabRE = re.compile(r'^\t*')
+includeRE = re.compile(r'^%INCLUDE (?P<filename>.*)$')
+envRE = re.compile(r'\$\{(?P<envname>[A-Za-z0-9_]+)\}')
 
 def SetLoadDataDirectory(directory):
     global data_directory
 def ParseEntries(entries, indent_level=0):
     index = 0
     while index < len(entries):
-        line = entries[index]
+        line = entries[index].rstrip()
         m = leadingTabRE.match(line)
         this_indent_level = len(m.group(0))
         unindented = line[this_indent_level:]
                 except NameError:
                     result = {}
                 if not isinstance(result, dict):
-                    raise TypeError("Dictionary/List mismatch")
+                    raise TypeError("Dictionary/List mismatch in '%s'" % key)
                 if len(parts) > 1:
                     result[key] = parts[1]
             else:
                 except NameError:
                     result = []
                 if not isinstance(result, list):
-                    raise TypeError("Dictionary/List mismatch")
+                    raise TypeError("Dictionary/List mismatch: %r" % result)
                 result += [unindented]
         else:
             sublist = entries[index:]
         result = {}
     return {'Index': index, 'Result': result}
 
-def LoadFile(filename, ):
+def LoadFile(filename):
     fh = open(filename, 'r')
     entries = fh.readlines()
+    index = 0
+    while index < len(entries):
+        m = includeRE.match(entries[index])
+        if m is not None:
+            # Include line
+            inc_file = m.group('filename').strip()
+            e = envRE.search(inc_file)
+            try:
+                while e is not None:
+                    inc_file = inc_file[:e.start()] + \
+                            os.environ[e.group('envname')] + \
+                            inc_file[e.end():]
+                    e = envRE.search(inc_file)
+            except KeyError:
+                raise
+                pass
+            if os.path.exists(inc_file):
+                fhinc = open(inc_file, 'r')
+                extra_entries = fhinc.readlines()
+                fhinc.close()
+                entries = entries[:index] + extra_entries + entries[index+1:]
+            else:
+                Debug("No such file: '%s'" % inc_file, "Warning")
+        index += 1
     fh.close()
     return ParseEntries(entries)['Result']
 
 def GlobData(matcher):
     files = glob.glob(os.path.join(data_directory, matcher))
     return [os.path.relpath(i,data_directory) for i in files]
+
+if __name__ == "__main__":
+    import pprint
+    pprint.pprint(LoadFile('testfile.txt'))