Commits

oliver_g committed 00d3b6e Draft

allow loading additional parsers from other packages (outside of Hachoir repo)

The additional parser package names must be specified in colon-separated format
in optional HACHOIR_PARSERS env var. The packages must be located in Python
search path (sys.path).

  • Participants
  • Parent commits c5fa998
  • Branches external-parsers

Comments (0)

Files changed (1)

hachoir-parser/hachoir_parser/parser_list.py

 from hachoir_core.i18n import _
 from hachoir_parser import Parser, HachoirParser
 import sys
+import os
 
 ### Parser list ################################################################
 
         if self.parser_list:
             return self.parser_list
 
+        parserModules = ["hachoir_parser"]
+        if os.environ.has_key('HACHOIR_PARSERS'):
+            parserModules += os.environ['HACHOIR_PARSERS'].split(':')
+
+        for moduleName in parserModules:
+            if moduleName:
+                self._loadModule(moduleName)
+
+        assert 1 <= len(self.parser_list)
+        return self.parser_list
+
+    def _loadModule(self, modName):
         todo = []
-        module = __import__("hachoir_parser")
+        module = __import__(modName)
         for attrname in dir(module):
             attr = getattr(module, attrname)
             if isinstance(attr, types.ModuleType):
                 and issubclass(attr, HachoirParser) \
                 and attr not in (Parser, HachoirParser):
                     self.add(attr)
-        assert 1 <= len(self.parser_list)
-        return self.parser_list