py3: dvc/utils/compat.py -> dvc/compat.py

This commit is contained in:
Mr. Outis
2019-12-31 23:42:07 -06:00
parent 27f581cf4e
commit 086acf6404
19 changed files with 60 additions and 148 deletions
+42
View File
@@ -0,0 +1,42 @@
"""Python version compatibility code"""
# Backport os.fspath() from Python 3.6
try:
from os import fspath # noqa: F821
fspath_py35 = lambda s: s # noqa: E731
except ImportError:
def fspath(path):
"""Return the path representation of a path-like object.
If str or bytes is passed in, it is returned unchanged. Otherwise the
os.PathLike interface is used to get the path representation. If the
path representation is not str or bytes, TypeError is raised. If the
provided path is not str, bytes, or os.PathLike, TypeError is raised.
"""
if isinstance(path, (str, bytes)):
return path
# Work from the object's type to match method resolution of other magic
# methods.
path_type = type(path)
try:
path_repr = path_type.__fspath__(path)
except AttributeError:
if hasattr(path_type, "__fspath__"):
raise
else:
raise TypeError(
"expected str, bytes or os.PathLike object, "
"not " + path_type.__name__
)
if isinstance(path_repr, (str, bytes)):
return path_repr
else:
raise TypeError(
"expected {}.__fspath__() to return str or bytes, "
"not {}".format(path_type.__name__, type(path_repr).__name__)
)
fspath_py35 = fspath
+1 -1
View File
@@ -7,7 +7,7 @@ from dvc.istextfile import istextfile
from dvc.output.base import OutputBase
from dvc.remote.local import RemoteLOCAL
from dvc.utils import relpath
from dvc.utils.compat import fspath_py35
from dvc.compat import fspath_py35
from dvc.utils.fs import path_isin
+1 -1
View File
@@ -28,7 +28,7 @@ from dvc.utils import makedirs
from dvc.utils import relpath
from dvc.utils import tmp_fname
from dvc.utils import walk_files
from dvc.utils.compat import fspath_py35
from dvc.compat import fspath_py35
from dvc.utils.fs import move
from dvc.utils.fs import remove
+1 -1
View File
@@ -5,7 +5,7 @@ from functools import wraps
from itertools import chain
from dvc.ignore import CleanTree
from dvc.utils.compat import fspath_py35
from dvc.compat import fspath_py35
from funcy import cached_property
+1 -1
View File
@@ -1,6 +1,6 @@
import os
from dvc.utils.compat import fspath
from dvc.compat import fspath
class BaseTree(object):
+1 -1
View File
@@ -11,7 +11,7 @@ from dvc.exceptions import DvcException
from dvc.utils import current_timestamp
from dvc.utils import relpath
from dvc.utils import to_chunks
from dvc.utils.compat import fspath_py35
from dvc.compat import fspath_py35
from dvc.utils.fs import get_inode
from dvc.utils.fs import get_mtime_and_size
from dvc.utils.fs import remove
+1 -1
View File
@@ -3,7 +3,7 @@ import logging
import os
import shutil
from dvc.utils.compat import fspath
from dvc.compat import fspath
logger = logging.getLogger(__name__)
+2 -2
View File
@@ -15,7 +15,7 @@ import nanotime
from ruamel.yaml import YAML
from shortuuid import uuid
from dvc.utils.compat import fspath, fspath_py35
from dvc.compat import fspath, fspath_py35
logger = logging.getLogger(__name__)
@@ -387,7 +387,7 @@ def env2bool(var, undefined=False):
def resolve_output(inp, out):
from dvc.utils.compat import urlparse
from urllib.parse import urlparse
name = os.path.basename(urlparse(inp).path)
if not out:
-129
View File
@@ -1,129 +0,0 @@
"""Handle import compatibility between Python 2 and Python 3"""
import errno
import sys
from contextlib import contextmanager
# Syntax sugar.
_ver = sys.version_info
#: Python 2.x?
is_py2 = _ver[0] == 2
#: Python 3.x?
is_py3 = _ver[0] == 3
@contextmanager
def ignore_file_not_found():
try:
yield
except IOError as exc:
if exc.errno != errno.ENOENT:
raise
if is_py2:
from urlparse import urlparse, urlunparse, urljoin # noqa: F401
from urllib import urlencode # noqa: F401
import ConfigParser # noqa: F401
from io import open # noqa: F401
import pathlib2 as pathlib # noqa: F401
from collections import Mapping # noqa: F401
from contextlib2 import ExitStack # noqa: F401
builtin_str = str # noqa: F821
bytes = str # noqa: F821
str = unicode # noqa: F821
basestring = basestring # noqa: F821
numeric_types = (int, long, float) # noqa: F821
integer_types = (int, long) # noqa: F821
input = raw_input # noqa: F821
range = xrange # noqa: F821
FileNotFoundError = IOError
JSONDecodeError = ValueError
import StringIO
import io
class StringIO(StringIO.StringIO):
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
class BytesIO(io.BytesIO):
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
elif is_py3:
import pathlib # noqa: F401
from urllib.parse import ( # noqa: F401
urlparse, # noqa: F401
urlunparse, # noqa: F401
urlencode, # noqa: F401
urljoin, # noqa: F401
)
from io import StringIO, BytesIO # noqa: F401
import configparser as ConfigParser # noqa: F401
from collections.abc import Mapping # noqa: F401
from contextlib import ExitStack # noqa: F401
from json.decoder import JSONDecodeError # noqa: F401
builtin_str = str # noqa: F821
str = str # noqa: F821
bytes = bytes # noqa: F821
basestring = (str, bytes) # noqa: F821
numeric_types = (int, float) # noqa: F821
integer_types = (int,) # noqa: F821
input = input # noqa: F821
open = open # noqa: F821
range = range # noqa: F821
FileNotFoundError = FileNotFoundError
# Backport os.fspath() from Python 3.6
try:
from os import fspath # noqa: F821
fspath_py35 = lambda s: s # noqa: E731
except ImportError:
def fspath(path):
"""Return the path representation of a path-like object.
If str or bytes is passed in, it is returned unchanged. Otherwise the
os.PathLike interface is used to get the path representation. If the
path representation is not str or bytes, TypeError is raised. If the
provided path is not str, bytes, or os.PathLike, TypeError is raised.
"""
if isinstance(path, (str, bytes)):
return path
# Work from the object's type to match method resolution of other magic
# methods.
path_type = type(path)
try:
path_repr = path_type.__fspath__(path)
except AttributeError:
if hasattr(path_type, "__fspath__"):
raise
else:
raise TypeError(
"expected str, bytes or os.PathLike object, "
"not " + path_type.__name__
)
if isinstance(path_repr, (str, bytes)):
return path_repr
else:
raise TypeError(
"expected {}.__fspath__() to return str or bytes, "
"not {}".format(path_type.__name__, type(path_repr).__name__)
)
fspath_py35 = fspath
+1 -1
View File
@@ -50,7 +50,7 @@ import pytest
from funcy.py3 import lmap, retry
from dvc.utils import makedirs
from dvc.utils.compat import fspath, fspath_py35
from dvc.compat import fspath, fspath_py35
__all__ = ["tmp_dir", "scm", "dvc", "repo_template", "run_copy", "erepo_dir"]
+1 -1
View File
@@ -23,7 +23,7 @@ from dvc.system import System
from dvc.utils import file_md5
from dvc.utils import LARGE_DIR_SIZE
from dvc.utils import relpath
from dvc.utils.compat import fspath
from dvc.compat import fspath
from dvc.utils.fs import path_isin
from dvc.utils.stage import load_stage_file
from tests.basic_env import TestDvc
+1 -1
View File
@@ -1,7 +1,7 @@
import mock
from dvc.main import main
from dvc.utils.compat import fspath
from dvc.compat import fspath
@mock.patch("dvc.analytics.send")
+1 -1
View File
@@ -11,7 +11,7 @@ from dvc.exceptions import FileMissingError
from dvc.main import main
from dvc.path_info import URLInfo
from dvc.remote.config import RemoteConfig
from dvc.utils.compat import fspath
from dvc.compat import fspath
from tests.remotes import Azure, GCP, HDFS, Local, OSS, S3, SSH
+1 -1
View File
@@ -10,7 +10,7 @@ from dvc.repo.get import GetDVCFileError, PathMissingError
from dvc.repo import Repo
from dvc.system import System
from dvc.utils import makedirs
from dvc.utils.compat import fspath
from dvc.compat import fspath
from tests.utils import trees_equal
+1 -2
View File
@@ -7,8 +7,7 @@ from dvc.exceptions import DvcIgnoreInCollectedDirError
from dvc.ignore import DvcIgnore, DvcIgnoreDirs, DvcIgnorePatterns, CleanTree
from dvc.scm.tree import WorkingTree
from dvc.utils import relpath
from dvc.utils.compat import fspath_py35
from dvc.utils.compat import fspath
from dvc.compat import fspath_py35, fspath
from dvc.utils.fs import get_mtime_and_size
from dvc.remote import RemoteLOCAL
+1 -1
View File
@@ -13,7 +13,7 @@ from dvc.exceptions import NoOutputInExternalRepoError
from dvc.stage import Stage
from dvc.system import System
from dvc.utils import makedirs
from dvc.utils.compat import fspath
from dvc.compat import fspath
from tests.utils import trees_equal
+1 -1
View File
@@ -11,7 +11,7 @@ from dvc.main import main
from dvc.path_info import PathInfo
from dvc.remote import RemoteLOCAL, RemoteConfig
from dvc.remote.base import RemoteBASE
from dvc.utils.compat import fspath
from dvc.compat import fspath
from tests.basic_env import TestDvc
from tests.remotes import get_local_url, get_local_storagepath
+1 -1
View File
@@ -4,7 +4,7 @@ from dvc.scm.git.tree import GitTree
from dvc.cache import Cache
from dvc.repo import Repo
from dvc.system import System
from dvc.utils.compat import fspath
from dvc.compat import fspath
def test_destroy(tmp_dir, dvc):
+1 -1
View File
@@ -9,7 +9,7 @@ from dvc.rwlock import (
RWLockFileCorruptedError,
)
from dvc.lock import LockError
from dvc.utils.compat import fspath
from dvc.compat import fspath
from dvc.path_info import PathInfo