Skip to content

Commit

Permalink
typing extensions separate for 3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
madhur-ob committed May 15, 2024
1 parent 9f230ab commit 1990686
Show file tree
Hide file tree
Showing 12 changed files with 3,423 additions and 60 deletions.
20 changes: 0 additions & 20 deletions metaflow/_vendor/pip.LICENSE

This file was deleted.

2 changes: 0 additions & 2 deletions metaflow/_vendor/v3_5/zipp.LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Copyright Jason R. Coombs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
Expand Down
178 changes: 141 additions & 37 deletions metaflow/_vendor/v3_5/zipp.py → metaflow/_vendor/v3_5/zipp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import zipfile
import itertools
import contextlib
import sys
import pathlib
import re
import sys

if sys.version_info < (3, 7):
from collections import OrderedDict
else:
OrderedDict = dict
from .compat.py310 import text_encoding
from .glob import Translator


__all__ = ['Path']
Expand Down Expand Up @@ -56,7 +55,7 @@ def _ancestry(path):
path, tail = posixpath.split(path)


_dedupe = OrderedDict.fromkeys
_dedupe = dict.fromkeys
"""Deduplicate an iterable in original order"""


Expand All @@ -68,10 +67,33 @@ def _difference(minuend, subtrahend):
return itertools.filterfalse(set(subtrahend).__contains__, minuend)


class CompleteDirs(zipfile.ZipFile):
class InitializedState:
"""
Mix-in to save the initialization state for pickling.
"""

def __init__(self, *args, **kwargs):
self.__args = args
self.__kwargs = kwargs
super().__init__(*args, **kwargs)

def __getstate__(self):
return self.__args, self.__kwargs

def __setstate__(self, state):
args, kwargs = state
super().__init__(*args, **kwargs)


class CompleteDirs(InitializedState, zipfile.ZipFile):
"""
A ZipFile subclass that ensures that implied directories
are always included in the namelist.
>>> list(CompleteDirs._implied_dirs(['foo/bar.txt', 'foo/bar/baz.txt']))
['foo/', 'foo/bar/']
>>> list(CompleteDirs._implied_dirs(['foo/bar.txt', 'foo/bar/baz.txt', 'foo/bar/']))
['foo/']
"""

@staticmethod
Expand All @@ -81,7 +103,7 @@ def _implied_dirs(names):
return _dedupe(_difference(as_dirs, names))

def namelist(self):
names = super(CompleteDirs, self).namelist()
names = super().namelist()
return names + list(self._implied_dirs(names))

def _name_set(self):
Expand All @@ -97,6 +119,17 @@ def resolve_dir(self, name):
dir_match = name not in names and dirname in names
return dirname if dir_match else name

def getinfo(self, name):
"""
Supplement getinfo for implied dirs.
"""
try:
return super().getinfo(name)
except KeyError:
if not name.endswith('/') or name not in self._name_set():
raise
return zipfile.ZipInfo(filename=name)

@classmethod
def make(cls, source):
"""
Expand All @@ -107,7 +140,7 @@ def make(cls, source):
return source

if not isinstance(source, zipfile.ZipFile):
return cls(_pathlib_compat(source))
return cls(source)

# Only allow for FastLookup when supplied zipfile is read-only
if 'r' not in source.mode:
Expand All @@ -116,6 +149,16 @@ def make(cls, source):
source.__class__ = cls
return source

@classmethod
def inject(cls, zf: zipfile.ZipFile) -> zipfile.ZipFile:
"""
Given a writable zip file zf, inject directory entries for
any directories implied by the presence of children.
"""
for name in cls._implied_dirs(zf.namelist()):
zf.writestr(name, b"")
return zf


class FastLookup(CompleteDirs):
"""
Expand All @@ -126,25 +169,21 @@ class FastLookup(CompleteDirs):
def namelist(self):
with contextlib.suppress(AttributeError):
return self.__names
self.__names = super(FastLookup, self).namelist()
self.__names = super().namelist()
return self.__names

def _name_set(self):
with contextlib.suppress(AttributeError):
return self.__lookup
self.__lookup = super(FastLookup, self)._name_set()
self.__lookup = super()._name_set()
return self.__lookup


def _pathlib_compat(path):
"""
For path-like objects, convert to a filename for compatibility
on Python 3.6.1 and earlier.
"""
try:
return path.__fspath__()
except AttributeError:
return str(path)
def _extract_text_encoding(encoding=None, *args, **kwargs):
# compute stack level so that the caller of the caller sees any warning.
is_pypy = sys.implementation.name == 'pypy'
stack_level = 3 + is_pypy
return text_encoding(encoding, stack_level), args, kwargs


class Path:
Expand All @@ -169,13 +208,13 @@ class Path:
Path accepts the zipfile object itself or a filename
>>> root = Path(zf)
>>> path = Path(zf)
From there, several path operations are available.
Directory iteration (including the zip file itself):
>>> a, b = root.iterdir()
>>> a, b = path.iterdir()
>>> a
Path('mem/abcde.zip', 'a.txt')
>>> b
Expand All @@ -196,7 +235,7 @@ class Path:
Read text:
>>> c.read_text()
>>> c.read_text(encoding='utf-8')
'content of c'
existence:
Expand All @@ -213,16 +252,38 @@ class Path:
'mem/abcde.zip/b/c.txt'
At the root, ``name``, ``filename``, and ``parent``
resolve to the zipfile. Note these attributes are not
valid and will raise a ``ValueError`` if the zipfile
has no filename.
resolve to the zipfile.
>>> root.name
>>> str(path)
'mem/abcde.zip/'
>>> path.name
'abcde.zip'
>>> str(root.filename).replace(os.sep, posixpath.sep)
'mem/abcde.zip'
>>> str(root.parent)
>>> path.filename == pathlib.Path('mem/abcde.zip')
True
>>> str(path.parent)
'mem'
If the zipfile has no filename, such attribtues are not
valid and accessing them will raise an Exception.
>>> zf.filename = None
>>> path.name
Traceback (most recent call last):
...
TypeError: ...
>>> path.filename
Traceback (most recent call last):
...
TypeError: ...
>>> path.parent
Traceback (most recent call last):
...
TypeError: ...
# workaround python/cpython#106763
>>> pass
"""

__repr = "{self.__class__.__name__}({self.root.filename!r}, {self.at!r})"
Expand All @@ -240,6 +301,18 @@ def __init__(self, root, at=""):
self.root = FastLookup.make(root)
self.at = at

def __eq__(self, other):
"""
>>> Path(zipfile.ZipFile(io.BytesIO(), 'w')) == 'foo'
False
"""
if self.__class__ is not other.__class__:
return NotImplemented
return (self.root, self.at) == (other.root, other.at)

def __hash__(self):
return hash((self.root, self.at))

def open(self, mode='r', *args, pwd=None, **kwargs):
"""
Open this entry as text or binary following the semantics
Expand All @@ -256,30 +329,36 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
if args or kwargs:
raise ValueError("encoding args invalid for binary operation")
return stream
return io.TextIOWrapper(stream, *args, **kwargs)
# Text mode:
encoding, args, kwargs = _extract_text_encoding(*args, **kwargs)
return io.TextIOWrapper(stream, encoding, *args, **kwargs)

def _base(self):
return pathlib.PurePosixPath(self.at or self.root.filename)

@property
def name(self):
return pathlib.Path(self.at).name or self.filename.name
return self._base().name

@property
def suffix(self):
return pathlib.Path(self.at).suffix or self.filename.suffix
return self._base().suffix

@property
def suffixes(self):
return pathlib.Path(self.at).suffixes or self.filename.suffixes
return self._base().suffixes

@property
def stem(self):
return pathlib.Path(self.at).stem or self.filename.stem
return self._base().stem

@property
def filename(self):
return pathlib.Path(self.root.filename).joinpath(self.at)

def read_text(self, *args, **kwargs):
with self.open('r', *args, **kwargs) as strm:
encoding, args, kwargs = _extract_text_encoding(*args, **kwargs)
with self.open('r', encoding, *args, **kwargs) as strm:
return strm.read()

def read_bytes(self):
Expand Down Expand Up @@ -307,14 +386,39 @@ def iterdir(self):
subs = map(self._next, self.root.namelist())
return filter(self._is_child, subs)

def match(self, path_pattern):
return pathlib.PurePosixPath(self.at).match(path_pattern)

def is_symlink(self):
"""
Return whether this path is a symlink. Always false (python/cpython#82102).
"""
return False

def glob(self, pattern):
if not pattern:
raise ValueError(f"Unacceptable pattern: {pattern!r}")

prefix = re.escape(self.at)
tr = Translator(seps='/')
matches = re.compile(prefix + tr.translate(pattern)).fullmatch
names = (data.filename for data in self.root.filelist)
return map(self._next, filter(matches, names))

def rglob(self, pattern):
return self.glob(f'**/{pattern}')

def relative_to(self, other, *extra):
return posixpath.relpath(str(self), str(other.joinpath(*extra)))

def __str__(self):
return posixpath.join(self.root.filename, self.at)

def __repr__(self):
return self.__repr.format(self=self)

def joinpath(self, *other):
next = posixpath.join(self.at, *map(_pathlib_compat, other))
next = posixpath.join(self.at, *other)
return self._next(self.root.resolve_dir(next))

__truediv__ = joinpath
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions metaflow/_vendor/v3_5/zipp/compat/py310.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sys
import io


def _text_encoding(encoding, stacklevel=2, /): # pragma: no cover
return encoding


text_encoding = (
io.text_encoding if sys.version_info > (3, 10) else _text_encoding # type: ignore
)
Loading

0 comments on commit 1990686

Please sign in to comment.