当前位置: 首页>>代码示例>>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;未经允许,请勿转载。