class Identifiers(object): "Aggregate identifier for textual, mimetype and encoding identificaiton" def __init__(self, path): "Initialise all identifiers to None" self.path = None if not path else path.split(':') self.textual = None self.mimetype = None self.encoding = None def __enter__(self): "__enter__() -> self." self.textual = Magic(paths=self.path, flags=MAGIC_NONE) self.mimetype = Magic(paths=self.path, flags=MAGIC_MIME_TYPE) self.encoding = Magic(paths=self.path, flags=MAGIC_MIME_ENCODING) return self def __exit__(self, exc_type, exc_value, traceback): "__exit__(*excinfo) -> None. Closes libmagic resources." if self.textual is not None: self.textual.close() if self.mimetype is not None: self.mimetype.close() if self.encoding is not None: self.encoding.close() def id_filename(self, filename): identity = odict() identity['textual'] = self.textual.id_filename(filename) identity['mimetype'] = self.mimetype.id_filename(filename) identity['encoding'] = self.encoding.id_filename(filename) return identity
def __enter__(self): "__enter__() -> self." self.textual = Magic(paths=self.path, flags=MAGIC_NONE) self.mimetype = Magic(paths=self.path, flags=MAGIC_MIME_TYPE) self.encoding = Magic(paths=self.path, flags=MAGIC_MIME_ENCODING) return self