Commits

haypo  committed cb9d9ae

* Split ParserList in two class: ParserList and HachoirParserList,
HachoirParserList uses a singleton and automatically load all
Hachoir parsers
* Fix hachoir-urwid '--parser-list' command line option

  • Participants
  • Parent commits ee09fa5

Comments (0)

Files changed (5)

File hachoir-metadata/hachoir-metadata

     from hachoir_core.benchmark import Benchmark
     from hachoir_core.stream import InputStreamError
     from hachoir_core.tools import makePrintable
-    from hachoir_parser import createParser,printParserList
+    from hachoir_parser import createParser, ParserList
 except ImportError, err:
     raise
     print >>sys.stderr, "Unable to import an Hachoir module: %s" % err
 
 
 def displayParserList(*args):
-    extractor_list = metadata_extractors
-    printParserList(extractor_list, _("List of Hachoir parsers with extractor"))
+    parser_list = ParserList()
+    for parser in metadata_extractors.keys():
+        parser_list.add(parser)
+    parser_list.print_(_("List of metadata extractors."))
     sys.exit(0)
 
 def displayVersion(*args):

File hachoir-parser/hachoir_parser/__init__.py

 __version__ = "0.8.0"
 
 from hachoir_parser.parser import Parser
-from hachoir_parser.parser_list import ParserList
+from hachoir_parser.parser_list import ParserList, HachoirParserList
 from hachoir_parser.guess import (guessParser,
     parseStream, createParser, createEditor)
 from hachoir_parser import (archive, audio, container,

File hachoir-parser/hachoir_parser/guess.py

 
 import os
 from hachoir_core.error import warning, info, HACHOIR_ERRORS
-from hachoir_parser import Parser
+from hachoir_parser import Parser, HachoirParserList
 from hachoir_core.stream import FileInputStream, InputSubStream
 from hachoir_core.i18n import _
 from hachoir_core.tools import makePrintable
 
 def _guessParsers(stream, file_ext=None, force_mime=None):
     # Filter by minimum size
-    parser_list = [ parser for parser in ParserList() \
+    parser_list = [ parser for parser in HachoirParserList() \
         if "min_size" not in parser.tags or stream.sizeGe(parser.tags["min_size"]) ]
 
     # Guess my MIME type

File hachoir-parser/hachoir_parser/parser_list.py

     VALID_CATEGORY = ("archive", "audio", "container", "file_system",
         "game", "image", "misc", "program", "video")
     ID_REGEX = re.compile("^[a-z0-9][a-z0-9_]{2,}$")
-    _instance = None
-
-    def __new__(cls):
-        if cls._instance is None:
-            cls._instance = object.__new__(cls)
-        return cls._instance
 
     def __init__(self):
         self.parser_list = []
         self.byid = {}
         self.bycategory = {}
-        self._load()
 
     def add(self, parser):
         # Check parser
     def __iter__(self):
         return iter(self.parser_list)
 
-    def _load(self):
-        """
-        Load all parsers from "hachoir.parser" module.
-
-        Return the list of loaded parsers.
-        """
-        # Parser list is already loaded?
-        if self.parser_list:
-            return self.parser_list
-
-        todo = []
-        module = __import__("hachoir_parser")
-        for attrname in dir(module):
-            attr = getattr(module, attrname)
-            if isinstance(attr, types.ModuleType):
-                parser = attr
-                todo.append(attr)
-
-        for module in todo:
-            attributes = ( getattr(module, name) for name in dir(module) )
-            parsers = list( attr for attr in attributes \
-                if isinstance(attr, type) \
-                   and issubclass(attr, Parser) \
-                   and hasattr(attr, "tags"))
-            for parser in parsers:
-                self.add(parser)
-        assert 1 <= len(self.parser_list)
-        return self.parser_list
-
     def checkParser(self, parser):
         """
         Check 'tags' attribute of a parser.
             print _("List of Hachoir parsers.")
 
         # Create parser list sorted by module
-        for category in self.bycategory.iterkeys():
-            print "\n[%s]" % category
+        for category in sorted(self.bycategory.iterkeys()):
+            print
+            print "[%s]" % category
             parser_list = sorted(self.bycategory[category],
                 key=lambda parser: parser.tags["id"])
             for parser in parser_list:
                 self.printParser(parser, verbose)
         print
+        print "Total: %s parsers" % len(self.parser_list)
 
     def __getitem__(self, key):
         return self.byid[key]
 
+class HachoirParserList(ParserList):
+    _instance = None
+
+    def __new__(cls):
+        if cls._instance is None:
+            cls._instance = object.__new__(cls)
+        return cls._instance
+
+    def __init__(self):
+        ParserList.__init__(self)
+        self._load()
+
+    def _load(self):
+        """
+        Load all parsers from "hachoir.parser" module.
+
+        Return the list of loaded parsers.
+        """
+        # Parser list is already loaded?
+        if self.parser_list:
+            return self.parser_list
+
+        todo = []
+        module = __import__("hachoir_parser")
+        for attrname in dir(module):
+            attr = getattr(module, attrname)
+            if isinstance(attr, types.ModuleType):
+                parser = attr
+                todo.append(attr)
+
+        for module in todo:
+            attributes = ( getattr(module, name) for name in dir(module) )
+            parsers = list( attr for attr in attributes \
+                if isinstance(attr, type) \
+                   and issubclass(attr, Parser) \
+                   and hasattr(attr, "tags"))
+            for parser in parsers:
+                self.add(parser)
+        assert 1 <= len(self.parser_list)
+        return self.parser_list
+

File hachoir-urwid/hachoir-urwid

     configureHachoir, unicodeFilename)
 from hachoir_core.stream import InputStreamError, FileInputStream
 from hachoir_core.i18n import _
-from hachoir_parser import createParser, parseStream, ParserList
+from hachoir_parser import createParser, parseStream, HachoirParserList
 from hachoir_urwid import exploreFieldSet
 from optparse import OptionGroup, OptionParser
 import sys
     sys.exit(0)
 
 def displayParserList(*args):
-    ParserList().print_()
+    HachoirParserList().print_()
     sys.exit(0)
 
 def parseOptions():
         type="int", action="store", default=3)
     common.add_option("--path", help=_("Initial path to focus on"),
         type="str", action="store", default=None)
-    common.add_option("--force-parser", help=_("Force a parser by its identifier"),
+    common.add_option("--parser", help=_("Use the specified parser (use its identifier)"),
         type="str", action="store", default=None)
     common.add_option("--offset", help=_("Skip first bytes of input file"),
         type="long", action="store", default=None)
     finally:
         unlink(profile_filename)
 
-def openParser(force_parser, filename):
+def openParser(parser_id, filename, offset, size):
     try:
         filename, realname = unicodeFilename(filename), filename
-        if force_parser:
+        if parser_id:
             try:
-                parser_cls = ParserList()[force_parser]
+                parser_cls = HachoirParserList()[parser_id]
             except KeyError:
-                error(_("No parser with identifier: %s") % force_parser)
+                error(_("No parser with identifier: %s") % parser_id)
                 sys.exit(1)
             stream = FileInputStream(filename, real_filename=realname)
+            # TODO: Use offset and size
             return parseStream(parser_cls, stream, False)
         else:
             parser = createParser(filename,
-                offset=values.offset, size=values.size, real_filename=realname)
+                offset=offset, size=size, real_filename=realname)
     except InputStreamError, err:
         return None, _("Unable to open file: %s") % err
     if not parser:
     configureHachoir(values)
 
     # Open file and create parser
-    parser, err = openParser(values.force_parser, filename)
+    parser, err = openParser(values.parser, filename, values.offset, values.size)
     if err:
         print err
         sys.exit(1)