Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(59)

Delta Between Two Patch Sets: Lib/zipfile.py

Issue 22217: Reprs for zipfile classes
Left Patch Set: Created 5 years, 5 months ago
Right Patch Set: Created 5 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_zipfile.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """ 1 """
2 Read and write ZIP files. 2 Read and write ZIP files.
3 3
4 XXX references to utf-8 need further investigation. 4 XXX references to utf-8 need further investigation.
5 """ 5 """
6 import io 6 import io
7 import os 7 import os
8 import re 8 import re
9 import importlib.util 9 import importlib.util
10 import sys 10 import sys
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 357
358 def __repr__(self): 358 def __repr__(self):
359 result = ['<%s filename=%r' % (self.__class__.__name__, self.filename)] 359 result = ['<%s filename=%r' % (self.__class__.__name__, self.filename)]
360 if self.compress_type != ZIP_STORED: 360 if self.compress_type != ZIP_STORED:
361 result.append(' compress_type=%s' % 361 result.append(' compress_type=%s' %
362 (compressor_names.get(self.compress_type, 362 (compressor_names.get(self.compress_type,
363 self.compress_type),)) 363 self.compress_type),))
364 hi = self.external_attr >> 16 364 hi = self.external_attr >> 16
365 lo = self.external_attr & 0xFFFF 365 lo = self.external_attr & 0xFFFF
366 if hi: 366 if hi:
367 result.append(' filemode=%s' % (stat.filemode(hi),)) 367 result.append(' filemode=%r' % (stat.filemode(hi),))
berkerpeksag 2014/08/20 12:17:45 Can filemode also be %r here? <ZipInfo filena
berkerpeksag 2014/10/01 01:40:47 Passing a tuple is not necessary here. ' file
storchaka 2014/10/08 19:42:09 I were too paranoid here. When right argument of t
368 if lo: 368 if lo:
369 result.append(' external_attr=%#x' % (lo,)) 369 result.append(' external_attr=%#x' % (lo,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
370 isdir = self.filename[-1:] == '/' 370 isdir = self.filename[-1:] == '/'
371 if not isdir or self.file_size: 371 if not isdir or self.file_size:
372 result.append(' file_size=%r' % (self.file_size,)) 372 result.append(' file_size=%r' % (self.file_size,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
373 if ((not isdir or self.compress_size) and 373 if ((not isdir or self.compress_size) and
374 (self.compress_type != ZIP_STORED or 374 (self.compress_type != ZIP_STORED or
375 self.file_size != self.compress_size)): 375 self.file_size != self.compress_size)):
376 result.append(' compress_size=%r' % (self.compress_size,)) 376 result.append(' compress_size=%r' % (self.compress_size,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
377 result.append('>') 377 result.append('>')
378 return ''.join(result) 378 return ''.join(result)
379 379
380 def FileHeader(self, zip64=None): 380 def FileHeader(self, zip64=None):
381 """Return the per-file header as a string.""" 381 """Return the per-file header as a string."""
382 dt = self.date_time 382 dt = self.date_time
383 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2] 383 dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
384 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2) 384 dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
385 if self.flag_bits & 0x08: 385 if self.flag_bits & 0x08:
386 # Set these to zero because we write them after the file data 386 # Set these to zero because we write them after the file data
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 self._expected_crc = None 694 self._expected_crc = None
695 695
696 def __repr__(self): 696 def __repr__(self):
697 result = ['<%s.%s' % (self.__class__.__module__, 697 result = ['<%s.%s' % (self.__class__.__module__,
698 self.__class__.__qualname__)] 698 self.__class__.__qualname__)]
699 if not self.closed: 699 if not self.closed:
700 result.append(' name=%r mode=%r' % (self.name, self.mode)) 700 result.append(' name=%r mode=%r' % (self.name, self.mode))
701 if self._compress_type != ZIP_STORED: 701 if self._compress_type != ZIP_STORED:
702 result.append(' compress_type=%s' % 702 result.append(' compress_type=%s' %
703 (compressor_names.get(self._compress_type, 703 (compressor_names.get(self._compress_type,
704 self._compress_type),)) 704 self._compress_type),))
berkerpeksag 2014/10/01 01:40:47 Same as above.
705 else: 705 else:
706 result.append(' [closed]') 706 result.append(' [closed]')
707 result.append('>') 707 result.append('>')
708 return ''.join(result) 708 return ''.join(result)
709 709
710 def readline(self, limit=-1): 710 def readline(self, limit=-1):
711 """Read and return a line from the stream. 711 """Read and return a line from the stream.
712 712
713 If limit is specified, at most limit bytes will be read. 713 If limit is specified, at most limit bytes will be read.
714 """ 714 """
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
1001 return self 1001 return self
1002 1002
1003 def __exit__(self, type, value, traceback): 1003 def __exit__(self, type, value, traceback):
1004 self.close() 1004 self.close()
1005 1005
1006 def __repr__(self): 1006 def __repr__(self):
1007 result = ['<%s.%s' % (self.__class__.__module__, 1007 result = ['<%s.%s' % (self.__class__.__module__,
1008 self.__class__.__qualname__)] 1008 self.__class__.__qualname__)]
1009 if self.fp is not None: 1009 if self.fp is not None:
1010 if self._filePassed: 1010 if self._filePassed:
1011 result.append(' file=%r' % (self.fp,)) 1011 result.append(' file=%r' % (self.fp,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
1012 elif self.filename is not None: 1012 elif self.filename is not None:
1013 result.append(' filename=%r' % (self.filename,)) 1013 result.append(' filename=%r' % (self.filename,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
1014 result.append(' mode=%r' % (self.mode,)) 1014 result.append(' mode=%r' % (self.mode,))
berkerpeksag 2014/10/01 01:40:47 Same as above.
1015 else: 1015 else:
1016 result.append(' [closed]') 1016 result.append(' [closed]')
1017 result.append('>') 1017 result.append('>')
1018 return ''.join(result) 1018 return ''.join(result)
1019 1019
1020 def _RealGetContents(self): 1020 def _RealGetContents(self):
1021 """Read in the table of contents for the ZIP file.""" 1021 """Read in the table of contents for the ZIP file."""
1022 fp = self.fp 1022 fp = self.fp
1023 try: 1023 try:
1024 endrec = _EndRecData(fp) 1024 endrec = _EndRecData(fp)
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after
1807 if badfile: 1807 if badfile:
1808 print("The following enclosed file is corrupted: {!r}".format(badfil e)) 1808 print("The following enclosed file is corrupted: {!r}".format(badfil e))
1809 print("Done testing") 1809 print("Done testing")
1810 1810
1811 elif args[0] == '-e': 1811 elif args[0] == '-e':
1812 if len(args) != 3: 1812 if len(args) != 3:
1813 print(USAGE) 1813 print(USAGE)
1814 sys.exit(1) 1814 sys.exit(1)
1815 1815
1816 with ZipFile(args[1], 'r') as zf: 1816 with ZipFile(args[1], 'r') as zf:
1817 out = args[2] 1817 zf.extractall(args[2])
1818 for path in zf.namelist():
1819 if path.startswith('./'):
1820 tgt = os.path.join(out, path[2:])
1821 else:
1822 tgt = os.path.join(out, path)
1823
1824 tgtdir = os.path.dirname(tgt)
1825 if not os.path.exists(tgtdir):
1826 os.makedirs(tgtdir)
1827 with open(tgt, 'wb') as fp:
1828 fp.write(zf.read(path))
1829 1818
1830 elif args[0] == '-c': 1819 elif args[0] == '-c':
1831 if len(args) < 3: 1820 if len(args) < 3:
1832 print(USAGE) 1821 print(USAGE)
1833 sys.exit(1) 1822 sys.exit(1)
1834 1823
1835 def addToZip(zf, path, zippath): 1824 def addToZip(zf, path, zippath):
1836 if os.path.isfile(path): 1825 if os.path.isfile(path):
1837 zf.write(path, zippath, ZIP_DEFLATED) 1826 zf.write(path, zippath, ZIP_DEFLATED)
1838 elif os.path.isdir(path): 1827 elif os.path.isdir(path):
1839 for nm in os.listdir(path): 1828 for nm in os.listdir(path):
1840 addToZip(zf, 1829 addToZip(zf,
1841 os.path.join(path, nm), os.path.join(zippath, nm)) 1830 os.path.join(path, nm), os.path.join(zippath, nm))
1842 # else: ignore 1831 # else: ignore
1843 1832
1844 with ZipFile(args[1], 'w') as zf: 1833 with ZipFile(args[1], 'w') as zf:
1845 for src in args[2:]: 1834 for src in args[2:]:
1846 addToZip(zf, src, os.path.basename(src)) 1835 addToZip(zf, src, os.path.basename(src))
1847 1836
1848 if __name__ == "__main__": 1837 if __name__ == "__main__":
1849 main() 1838 main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+