Exemplo n.º 1
0
def extractMetadata(parser, quality=QUALITY_NORMAL, **kwargs):
    """
    Create a Metadata class from a parser. Returns None if no metadata
    extractor does exist for the parser class.
    """
    try:
        extractor = extractors[parser.__class__]
    except KeyError:
        return None
    metadata = extractor(quality)
    meta_extract_error = True
    try:
        if 'scan_index' in kwargs:
            metadata.extract(parser, scan_index=kwargs['scan_index'])
        else:
            metadata.extract(parser)
        meta_extract_error = False
    except (BaseException, Exception) as err:
        error("Error during metadata extraction: %s" % ex(err))

    if meta_extract_error:
        try:
            parser.stream._input.close()
        except (BaseException, Exception):
            pass
        return None

    if metadata:
        metadata.mime_type = parser.mime_type
        metadata.endian = endian_name[parser.endian]
    return metadata
Exemplo n.º 2
0
 def _getContentSize(self):
     if not hasattr(self, "_content_size"):
         try:
             self._content_size = self.createContentSize()
         except Exception as err:
             error("Unable to compute %s content size: %s" %
                   (self.__class__.__name__, err))
             self._content_size = None
     return self._content_size
Exemplo n.º 3
0
 def _getDescription(self):
     if self._description is None:
         try:
             self._description = self.createDescription()
             if isinstance(self._description, str):
                 self._description = makeUnicode(self._description)
         except Exception as err:
             error("Error getting description of %s: %s" \
                   % (self.path, unicode(err)))
             self._description = self.PARSER_TAGS["description"]
     return self._description
Exemplo n.º 4
0
 def _getMimeType(self):
     if not self._mime_type:
         try:
             self._mime_type = self.createMimeType()
         except Exception as err:
             error("Error when creating MIME type: %s" % unicode(err))
         if not self._mime_type \
                 and self.createMimeType != Parser.createMimeType:
             self._mime_type = Parser.createMimeType(self)
         if not self._mime_type:
             self._mime_type = u"application/octet-stream"
     return self._mime_type
Exemplo n.º 5
0
    def add(self, parser):
        tags = parser.getParserTags()
        err = self.validParser(parser, tags)
        if err:
            error("Skip parser %s: %s" % (parser.__name__, err))
            return

        _tags = []
        for tag in tags.iteritems():
            tag = self.translate(*tag)
            if isinstance(tag, tuple):
                _tags.append(tag)
            elif tag is not True:
                error("[%s] %s" % (parser.__name__, tag))
                return

        self.parser_list.append(parser)

        for name, values in _tags:
            byname = self.bytag.setdefault(name, {})
            for value in values:
                byname.setdefault(value, []).append(parser)