Ejemplo n.º 1
0
    def detect(self):
        """Detect charset with MagicLib. A charset is detected from up to
        1 megabytes of data from the beginning of file."""
        messages = []
        errors = []
        charset = magic_analyze(MAGIC_LIB, MAGIC_LIB.MAGIC_MIME_ENCODING,
                                self.filename)

        if charset is None or charset.upper() == "BINARY":
            errors.append("Unable to detect character encoding.")
        elif charset.upper() == "US-ASCII":
            self.charset = "UTF-8"
        elif charset.upper() == "ISO-8859-1":
            self.charset = "ISO-8859-15"
        elif charset.upper() == "UTF-16LE" or \
                charset.upper() == "UTF-16BE":
            self.charset = "UTF-16"
        else:
            self.charset = charset.upper()
        if not errors:
            messages.append("Character encoding detected as %s" % self.charset)

        self.info = {
            "class": self.__class__.__name__,
            "messages": messages,
            "errors": errors,
            "tools": []
        }
Ejemplo n.º 2
0
 def detect(self):
     """Detect mimetype."""
     mimetype = magic_analyze(MAGIC_LIB, MAGIC_LIB.MAGIC_MIME_TYPE,
                              self.filename)
     if mimetype in MIMETYPE_DICT:
         self.mimetype = MIMETYPE_DICT[mimetype]
     else:
         self.mimetype = six.text_type(mimetype)
     self.info = {"class": self.__class__.__name__,
                  "messages": [],
                  "errors": []}
Ejemplo n.º 3
0
    def _magic_call(self):
        """Fetch three values from file with using magic.
        These are: mimetype, info line (for version) and encoding.

        :returns: Python dict of the three fetched values from magic
        """
        magicdict = {
            "magic_mime_type": MAGIC_LIB.MAGIC_MIME_TYPE,
            "magic_none": MAGIC_LIB.MAGIC_NONE,
            "magic_mime_encoding": MAGIC_LIB.MAGIC_MIME_ENCODING
        }

        magic_result = {}
        for key, value in magicdict.items():
            magic_result[key] = magic_analyze(MAGIC_LIB, value, self.filename)
        return magic_result