当前位置: 首页>>代码示例>>Python>>正文


Python zipfile.ZipExtFile方法代码示例

本文整理汇总了Python中zipfile.ZipExtFile方法的典型用法代码示例。如果您正苦于以下问题:Python zipfile.ZipExtFile方法的具体用法?Python zipfile.ZipExtFile怎么用?Python zipfile.ZipExtFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块zipfile的用法示例。

在下文中一共展示了zipfile.ZipExtFile方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: open

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import ZipExtFile [as 别名]
def open(self, path_or_handle, mode='r'):
        """
        Don't re-open files that are passed as handles, but for easy
        use-cases, this'll work normally with regular paths
        """
        #TODO: we have to be smarter here about whether we're being passed a file
        # or whether we should hunt down for a particular filename
        # based on what each state spits out
        if mode == 'r' and isinstance(path_or_handle, zipfile.ZipExtFile):
            # see pa.py for an example of needing zipfile support
            return TextIOWrapper(path_or_handle,
                                 encoding='utf8',
                                 errors='ignore', line_buffering=True)
        elif hasattr(path_or_handle, 'mode'): #py2/3 file/buffer type
            return path_or_handle
        else:
            return open(path_or_handle, mode, errors='ignore') 
开发者ID:national-voter-file,项目名称:national-voter-file,代码行数:19,代码来源:base.py


示例2: test_container_zip

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import ZipExtFile [as 别名]
def test_container_zip(self):
        """Check the type zip of the container of an archive"""

        mbox = MBoxArchive(self.cfiles['zip'])
        container = mbox.container
        self.assertIsInstance(container, zipfile.ZipExtFile)
        container.close()

        with zipfile.ZipFile(self.cfiles['zip'], 'w') as f_out:
            f_out.write(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/mbox/mbox_single.mbox'))
            f_out.write(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/mbox/mbox_no_fields.mbox'))

        mbox = MBoxArchive(self.cfiles['zip'])
        with self.assertLogs(logger, level='ERROR') as cm:
            container = mbox.container
            container.close()
            self.assertEqual(cm.output[0], 'ERROR:perceval.backends.core.mbox:Zip %s contains more than one file, '
                                           'only the first uncompressed' % mbox.filepath) 
开发者ID:chaoss,项目名称:grimoirelab-perceval,代码行数:20,代码来源:test_mbox.py


示例3: test_verifying_zipfile

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import ZipExtFile [as 别名]
def test_verifying_zipfile():
    if not hasattr(zipfile.ZipExtFile, '_update_crc'):
        pytest.skip('No ZIP verification. Missing ZipExtFile._update_crc.')
    
    sio = StringIO()
    zf = zipfile.ZipFile(sio, 'w')
    zf.writestr("one", b"first file")
    zf.writestr("two", b"second file")
    zf.writestr("three", b"third file")
    zf.close()
    
    # In default mode, VerifyingZipFile checks the hash of any read file
    # mentioned with set_expected_hash(). Files not mentioned with
    # set_expected_hash() are not checked.
    vzf = wheel.install.VerifyingZipFile(sio, 'r')
    vzf.set_expected_hash("one", hashlib.sha256(b"first file").digest())
    vzf.set_expected_hash("three", "blurble")
    vzf.open("one").read()
    vzf.open("two").read()
    try:
        vzf.open("three").read()
    except wheel.install.BadWheelFile:
        pass
    else:
        raise Exception("expected exception 'BadWheelFile()'")
    
    # In strict mode, VerifyingZipFile requires every read file to be
    # mentioned with set_expected_hash().
    vzf.strict = True
    try:
        vzf.open("two").read()
    except wheel.install.BadWheelFile:
        pass
    else:
        raise Exception("expected exception 'BadWheelFile()'")
        
    vzf.set_expected_hash("two", None)
    vzf.open("two").read() 
开发者ID:jpush,项目名称:jbox,代码行数:40,代码来源:test_wheelfile.py


示例4: process_extracted_file

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import ZipExtFile [as 别名]
def process_extracted_file(self, extracted_file, extracted_filename):
        """
        Processes an individual file extracted from the zip

        Args:
            extracted_file (zipfile.ZipExtFile): the extracted file-like or iterable object
            extracted_filename (str): the filename of the extracted file

        Returns:
            (bool, list(str)): bool is True if file processed successfuly, error messages are returned in the list
        """

        if extracted_filename.startswith(PEARSON_FILE_TYPES.VCDC):
            # We send Pearson CDD files and get the results as VCDC files
            return self.process_vcdc_file(extracted_file)
        elif extracted_filename.startswith(PEARSON_FILE_TYPES.EAC):
            # We send Pearson EAD files and get the results as EAC files
            return self.process_eac_file(extracted_file)
        elif extracted_filename.startswith(PEARSON_FILE_TYPES.EXAM):
            return self.process_exam_file(extracted_file)
        elif any(extracted_filename.startswith(file_type) for file_type in PEARSON_INTENDED_SKIP_FILE_TYPES):
            # for files we don't care about, act like we processed them
            # so they don't cause us to leave the zip file on the server
            # this would cause us to reprocess these zip files forever
            return True, []

        return False, [] 
开发者ID:mitodl,项目名称:micromasters,代码行数:29,代码来源:download.py


示例5: process_vcdc_file

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import ZipExtFile [as 别名]
def process_vcdc_file(self, extracted_file):
        """
        Processes a VCDC file extracted from the zip

        Args:
            extracted_file (zipfile.ZipExtFile): the extracted file-like object

        Returns:
            (bool, list(str)): bool is True if file processed successfuly, error messages are returned in the list
        """
        log.debug('Found VCDC file: %s', extracted_file)
        results, invalid_rows = VCDCReader().read(extracted_file)
        messages = self.get_invalid_row_messages(invalid_rows)
        for result in results:
            try:
                exam_profile = ExamProfile.objects.get(profile__student_id=result.client_candidate_id)
            except ExamProfile.DoesNotExist:
                messages.append(format_and_log_error(
                    'Unable to find an ExamProfile record:',
                    client_candidate_id=result.client_candidate_id,
                    error=result.message,
                ))
                continue

            if result.status == EAC_SUCCESS_STATUS and 'WARNING' not in result.message:
                exam_profile.status = ExamProfile.PROFILE_SUCCESS
            else:
                exam_profile.status = ExamProfile.PROFILE_FAILED
                messages.append(format_and_log_error(
                    'ExamProfile sync failed:',
                    client_candidate_id=result.client_candidate_id,
                    username=exam_profile.profile.user.username,
                    error=result.message,
                ))

            exam_profile.save()

        return True, messages 
开发者ID:mitodl,项目名称:micromasters,代码行数:40,代码来源:download.py


示例6: process_eac_file

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import ZipExtFile [as 别名]
def process_eac_file(self, extracted_file):
        """
        Processes a EAC file extracted from the zip

        Args:
            extracted_file (zipfile.ZipExtFile): the extracted file-like object

        Returns:
            (bool, list(str)): bool is True if file processed successfuly, error messages are returned in the list
        """
        log.debug('Found EAC file: %s', extracted_file)
        results, invalid_rows = EACReader().read(extracted_file)
        messages = self.get_invalid_row_messages(invalid_rows)
        for result in results:
            try:
                exam_authorization = ExamAuthorization.objects.get(id=result.client_authorization_id)
            except ExamAuthorization.DoesNotExist:
                messages.append(format_and_log_error(
                    'Unable to find a matching ExamAuthorization record:',
                    client_candidate_id=result.client_candidate_id,
                    client_authorization_id=result.client_authorization_id,
                    error=result.message,
                ))
                continue

            if result.status == VCDC_SUCCESS_STATUS:
                exam_authorization.status = ExamAuthorization.STATUS_SUCCESS
            else:
                exam_authorization.status = ExamAuthorization.STATUS_FAILED
                messages.append(format_and_log_error(
                    'ExamAuthorization sync failed:',
                    username=exam_authorization.user.username,
                    client_authorization_id=result.client_authorization_id,
                    error=result.message,
                ))

            exam_authorization.save()

        return True, messages 
开发者ID:mitodl,项目名称:micromasters,代码行数:41,代码来源:download.py


示例7: read_roi_file

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import ZipExtFile [as 别名]
def read_roi_file(fpath):
    """
    """

    if isinstance(fpath, zipfile.ZipExtFile):
        data = fpath.read()
        name = os.path.splitext(os.path.basename(fpath.name))[0]
    elif isinstance(fpath, str):
        fp = open(fpath, 'rb')
        data = fp.read()
        fp.close()
        name = os.path.splitext(os.path.basename(fpath))[0]
    else:
        logging.error("Can't read {}".format(fpath))
        return None

    logging.debug("Read ROI for \"{}\"".format(name))

    roi, (hdr2Offset, n_coordinates, roi_type, channel, slice, frame, position, version, subtype, size) = extract_basic_roi_data(data)
    roi['name'] = name

    if version >= 218:
        # Not implemented
        # Read stroke width, stroke color and fill color
        pass

    if version >= 218 and subtype == SUBTYPES['TEXT']:
        # Not implemented
        # Read test ROI
        pass

    if version >= 218 and subtype == SUBTYPES['IMAGE']:
        # Not implemented
        # Get image ROI
        pass

    if version >= 224:
        # Not implemented
        # Get ROI properties
        pass

    if version >= 227 and roi['type'] == 'point':
        # Get "point counters" (includes a "counter" and a "position" (slice, i.e. z position)
        tmp = get_point_counters(data, hdr2Offset, n_coordinates, size)
        if tmp is not None:
            counters, positions = tmp
            if counters:
                roi.update(dict(counters=counters, slices=positions))

    roi['position'] = position
    if channel > 0 or slice > 0 or frame > 0:
        roi['position'] = dict(channel=channel, slice=slice, frame=frame)

    return {name: roi} 
开发者ID:hadim,项目名称:read-roi,代码行数:56,代码来源:_read_roi.py



注:本文中的zipfile.ZipExtFile方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。