當前位置: 首頁>>代碼示例>>Python>>正文


Python magic.MagicException方法代碼示例

本文整理匯總了Python中magic.MagicException方法的典型用法代碼示例。如果您正苦於以下問題:Python magic.MagicException方法的具體用法?Python magic.MagicException怎麽用?Python magic.MagicException使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在magic的用法示例。


在下文中一共展示了magic.MagicException方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_filetype

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import MagicException [as 別名]
def get_filetype(data):
    """There are two versions of python-magic floating around, and annoyingly, the interface 
    changed between versions, so we try one method and if it fails, then we try the other.
    NOTE: you may need to alter the magic_file for your system to point to the magic file."""
    if sys.modules.has_key('magic'):
        try:
            ms = magic.open(magic.MAGIC_NONE) 
            ms.load() 
            return ms.buffer(data)
        except:
            try:
                return magic.from_buffer(data)
            except magic.MagicException:
                magic_custom = magic.Magic(magic_file='C:\windows\system32\magic')
                return magic_custom.from_buffer(data)
    return '' 
開發者ID:omriher,項目名稱:CapTipper,代碼行數:18,代碼來源:pescanner.py

示例2: clean_file

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import MagicException [as 別名]
def clean_file(self):
        file = self.cleaned_data['file']

        if not magic:
           raise forms.ValidationError(_("The file could not be validated"))

        # Won't ever raise. Has at most one '.' so lstrip is fine here
        ext = os.path.splitext(file.name)[1].lstrip('.').lower()
        if ext not in settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE:
            raise forms.ValidationError(
                _("Unsupported file extension %(extension)s. "
                  "Supported extensions are %(supported)s.") % {
                    'extension': ext,
                    'supported': ", ".join(
                        sorted(settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE.keys()))})

        try:
            if isinstance(file, TemporaryUploadedFile):
                file_mime = magic.from_file(file.temporary_file_path(), mime=True)
            else:  # In-memory file
                file_mime = magic.from_buffer(file.read(), mime=True)
        except magic.MagicException as e:
            logger.exception(e)
            raise forms.ValidationError(_("The file could not be validated"))

        mime = settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE.get(ext, None)
        if mime != file_mime:
            raise forms.ValidationError(
                _("Unsupported file mime type %(mime)s. "
                  "Supported types are %(supported)s.") % {
                    'mime': file_mime,
                    'supported': ", ".join(
                        sorted(settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE.values()))})

        return file 
開發者ID:nitely,項目名稱:Spirit,代碼行數:37,代碼來源:forms.py

示例3: MIME_TYPE

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import MagicException [as 別名]
def MIME_TYPE(data, mime=True):
    try:
        return magic.from_buffer(data, mime=mime)
    except magic.MagicException:
        return "none/none" 
開發者ID:codexgigassys,項目名稱:codex-backend,代碼行數:7,代碼來源:InfoExtractor.py

示例4: skip_lib

# 需要導入模塊: import magic [as 別名]
# 或者: from magic import MagicException [as 別名]
def skip_lib(main, lib_path):
    logger.debug("Checking lib %s", lib_path)
    try:
        if not os.path.isfile(lib_path):
            logger.error("skipping %s: not a file", lib_path)
            return True

        elif not os.path.exists(lib_path):
            logger.error("skipping %s: doesn't exist", lib_path)
            return True

        elif main.ignore_scanned and main.rrc and main.rrc.handle().exists(lib_path):
            logger.error("Skipping processed lib_path %s", lib_path)
            return True

        try:
            import magic
            lib_info = magic.from_file(lib_path).split(',')
            lib_type = lib_info[0]
            if lib_type != "ELF 32-bit LSB shared object" and "ELF 32-bit" not in lib_type:
                logger.error("skipping %s: not a ELF 32-bit shared object", lib_path)
                return True
            lib_arch = lib_info[1]
            if lib_arch != " ARM":
                logger.error("skipping %s: not an ARM exectable", lib_path)
                return True

            return False

        except ImportError as ie:
            logger.error("skipping %s: %s", lib_path, str(ie))
            return True

        except magic.MagicException as me:
            logger.error("skipping %s: magic exception %s", lib_path, str(me))
            return True

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        logger.error("[%s, %s, %s] skipping %s: %s", exc_type, fname, exc_tb.tb_lineno, lib_path, str(e))
        return True


###########################################################
# Lookup items by features
########################################################### 
開發者ID:osssanitizer,項目名稱:osspolice,代碼行數:49,代碼來源:searching.py


注:本文中的magic.MagicException方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。