/usr/lib/python3/dist-packages/zict/zip.py is in python3-zict 0.1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | from __future__ import absolute_import, division, print_function
from collections import MutableMapping
import sys
import zipfile
class Zip(MutableMapping):
""" Mutable Mapping interface to a Zip file
Keys must be strings, values must be bytes
Parameters
----------
filename: string
mode: string, ('r', 'w', 'a'), defaults to 'a'
Examples
--------
>>> z = Zip('myfile.zip') # doctest: +SKIP
>>> z['x'] = b'123' # doctest: +SKIP
>>> z['x'] # doctest: +SKIP
b'123'
>>> z.flush() # flush and write metadata to disk # doctest: +SKIP
"""
def __init__(self, filename, mode='a'):
self.filename = filename
self.mode = mode
self._file = None
@property
def file(self):
if self.mode == 'closed':
raise IOError("File closed")
if not self._file or not self._file.fp:
self._file = zipfile.ZipFile(self.filename, mode=self.mode)
return self._file
def __getitem__(self, key):
return self.file.read(key)
def __setitem__(self, key, value):
self.file.writestr(key, to_bytes(value))
def keys(self):
return (zi.filename for zi in self.file.filelist)
def values(self):
return map(self.file.read, self.keys())
def items(self):
return ((zi.filename, self.file.read(zi.filename))
for zi in self.file.filelist)
def __iter__(self):
return self.keys()
def __delitem__(self, key):
raise NotImplementedError("Not supported by stdlib zipfile")
def __len__(self):
return len(self.file.filelist)
def flush(self):
self.file.fp.flush()
self.file.close()
def close(self):
self.flush()
self.mode = 'closed'
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
if sys.version_info[0] == 2:
def to_bytes(x):
if isinstance(x, bytearray):
return bytes(x)
return x
else:
to_bytes = lambda x: x
|