Commit f4955997 authored by Jason R. Coombs's avatar Jason R. Coombs

Update ContextualZipFile to use a single constructor

parent 24d18354
...@@ -80,14 +80,13 @@ class ContextualZipFile(zipfile.ZipFile): ...@@ -80,14 +80,13 @@ class ContextualZipFile(zipfile.ZipFile):
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
self.close() self.close()
@classmethod def __new__(cls, *args, **kwargs):
def compat(cls, *args, **kwargs):
""" """
Construct a ZipFile or ContextualZipFile as appropriate Construct a ZipFile or ContextualZipFile as appropriate
""" """
zf_has_exit = hasattr(zipfile.ZipFile, '__exit__') if hasattr(zipfile.ZipFile, '__exit__'):
class_ = zipfile.ZipFile if zf_has_exit else cls return zipfile.ZipFile(*args, **kwargs)
return class_(*args, **kwargs) return super(ContextualZipFile, cls).__new__(cls, *args, **kwargs)
@contextlib.contextmanager @contextlib.contextmanager
...@@ -98,7 +97,7 @@ def archive_context(filename): ...@@ -98,7 +97,7 @@ def archive_context(filename):
old_wd = os.getcwd() old_wd = os.getcwd()
try: try:
os.chdir(tmpdir) os.chdir(tmpdir)
with ContextualZipFile.compat(filename) as archive: with ContextualZipFile(filename) as archive:
archive.extractall() archive.extractall()
# going in the directory # going in the directory
......
...@@ -1551,7 +1551,7 @@ def build_zipmanifest(path): ...@@ -1551,7 +1551,7 @@ def build_zipmanifest(path):
* [7] - zipinfo.CRC * [7] - zipinfo.CRC
""" """
zipinfo = dict() zipinfo = dict()
with ContextualZipFile.compat(path) as zfile: with ContextualZipFile(path) as zfile:
for zitem in zfile.namelist(): for zitem in zfile.namelist():
zpath = zitem.replace('/', os.sep) zpath = zitem.replace('/', os.sep)
zipinfo[zpath] = zfile.getinfo(zitem) zipinfo[zpath] = zfile.getinfo(zitem)
...@@ -1570,14 +1570,13 @@ class ContextualZipFile(zipfile.ZipFile): ...@@ -1570,14 +1570,13 @@ class ContextualZipFile(zipfile.ZipFile):
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
self.close() self.close()
@classmethod def __new__(cls, *args, **kwargs):
def compat(cls, *args, **kwargs):
""" """
Construct a ZipFile or ContextualZipFile as appropriate Construct a ZipFile or ContextualZipFile as appropriate
""" """
zf_has_exit = hasattr(zipfile.ZipFile, '__exit__') if hasattr(zipfile.ZipFile, '__exit__'):
class_ = zipfile.ZipFile if zf_has_exit else cls return zipfile.ZipFile(*args, **kwargs)
return class_(*args, **kwargs) return super(ContextualZipFile, cls).__new__(cls, *args, **kwargs)
class ZipProvider(EggProvider): class ZipProvider(EggProvider):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment