本文整理匯總了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')
示例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)
示例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()
示例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, []
示例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
示例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
示例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}