def _fixFeedError(self, exception): """ Try to fix a feeding error. Returns False if error can't be fixed, otherwise returns new field if any, or None. """ if self._size is None or not self.autofix: return False self.warning(makeUnicode(exception)) return self._fixLastField()
def _createItem(self, value, text=None): if text is None: if isinstance(value, unicode): text = value elif self.text_handler: text = self.text_handler(value) assert isinstance(text, unicode) else: text = makeUnicode(value) return DataValue(value, text)
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
def exportPlaintext(self, priority=None, human=True, line_prefix=u"- ", title=None): r""" Convert metadata to multi-line Unicode string and skip datas with priority lower than specified priority. Default priority is Metadata.MAX_PRIORITY. If human flag is True, data key are translated to better human name (eg. "bit_rate" becomes "Bit rate") which may be translated using gettext. If priority is too small, metadata are empty and so None is returned. >>> print RootMetadata().exportPlaintext() None >>> meta = RootMetadata() >>> meta.copyright = unicode("© Hachoir", "UTF-8") >>> print repr(meta.exportPlaintext()) [u'Metadata:', u'- Copyright: \xa9 Hachoir'] @see __str__() and __unicode__() """ if priority is not None: priority = max(priority, MIN_PRIORITY) priority = min(priority, MAX_PRIORITY) else: priority = MAX_PRIORITY if not title: title = self.header text = ["%s:" % title] for data in sorted(self): if priority < data.priority: break if not data.values: continue if human: title = data.description else: title = data.key for item in data.values: if human: value = item.text else: value = makeUnicode(item.value) text.append("%s%s: %s" % (line_prefix, title, value)) if 1 < len(text): return text else: return None
def __init__(self, stream, **args): validate = args.pop("validate", False) self._mime_type = None while validate: nbits = self.getParserTags()["min_size"] if stream.sizeGe(nbits): res = self.validate() if res is True: break res = makeUnicode(res) else: res = _("stream is smaller than %s.%s bytes" % divmod(nbits, 8)) raise ValidateError(res or _("no reason given")) self._autofix = True
def useExtDescItem(self, desc, data): if desc["type"].value == ASF_Descriptor.TYPE_BYTE_ARRAY: # Skip binary data return key = desc["name"].value if "/" in key: # Replace "WM/ToolName" with "ToolName" key = key.split("/", 1)[1] if key in self.SKIP_EXT_DESC: # Skip some keys return value = desc["value"].value if not value: return value = makeUnicode(value) data[key] = value
def extractAVIVideo(self, header, meta): meta.compression = "%s (fourcc:\"%s\")" \ % (header["fourcc"].display, makeUnicode(header["fourcc"].value)) if header["rate"].value and header["scale"].value: fps = float(header["rate"].value) / header["scale"].value meta.frame_rate = fps if 0 < fps: self.duration = meta.duration = timedelta( seconds=float(header["length"].value) / fps) if "../stream_fmt/width" in header: format = header["../stream_fmt"] meta.width = format["width"].value meta.height = format["height"].value meta.bits_per_pixel = format["depth"].value else: meta.width = header["right"].value - header["left"].value meta.height = header["bottom"].value - header["top"].value
def parseIPTC(self, iptc): datestr = hourstr = None for field in iptc: # Skip incomplete field if "tag" not in field or "content" not in field: continue # Get value value = field["content"].value if isinstance(value, (str, unicode)): value = value.replace("\r", " ") value = value.replace("\n", " ") # Skip unknown tag tag = field["tag"].value if tag == 55: datestr = value continue if tag == 60: hourstr = value continue if tag not in self.IPTC_KEY: if tag != 0: self.warning("Skip IPTC key %s: %s" % (field["tag"].display, makeUnicode(value))) continue setattr(self, self.IPTC_KEY[tag], value) if datestr and hourstr: try: year = int(datestr[0:4]) month = int(datestr[4:6]) day = int(datestr[6:8]) hour = int(hourstr[0:2]) min = int(hourstr[2:4]) sec = int(hourstr[4:6]) self.creation_date = datetime(year, month, day, hour, min, sec) except ValueError: pass
def exportDictionary(self, priority=None, human=True, title=None): r""" Convert metadata to python Dictionary and skip datas with priority lower than specified priority. Default priority is Metadata.MAX_PRIORITY. If human flag is True, data key are translated to better human name (eg. "bit_rate" becomes "Bit rate") which may be translated using gettext. If priority is too small, metadata are empty and so None is returned. """ if priority is not None: priority = max(priority, MIN_PRIORITY) priority = min(priority, MAX_PRIORITY) else: priority = MAX_PRIORITY if not title: title = self.header text = {} text[title] = {} for data in sorted(self): if priority < data.priority: break if not data.values: continue if human: field = data.description else: field = data.key text[title][field] = {} for item in data.values: if human: value = item.text else: value = makeUnicode(item.value) text[title][field] = value return text
def createMimeType(self): if self["file[0]/filename"].value == "mimetype": return makeUnicode(self["file[0]/data"].value) else: return u"application/zip"