mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-11 07:18:28 +01:00
Merge pull request #3030 from mroutis/remove-utils-compat
py3: remove csv_reader and makedirs from utils/compat.py
This commit is contained in:
@@ -10,7 +10,6 @@ from jsonpath_ng.ext import parse
|
||||
from dvc.exceptions import NoMetricsError
|
||||
from dvc.exceptions import OutputNotFoundError
|
||||
from dvc.repo import locked
|
||||
from dvc.utils.compat import csv_reader
|
||||
from dvc.utils.compat import open
|
||||
|
||||
NO_METRICS_FILE_AT_REFERENCE_WARNING = (
|
||||
@@ -101,7 +100,7 @@ def _format_csv(content, delimiter):
|
||||
"0.67528 0.289545 testing\n"
|
||||
"0.671502 0.297848 validation\n"
|
||||
"""
|
||||
reader = csv_reader(io.StringIO(content), delimiter=delimiter)
|
||||
reader = csv.reader(io.StringIO(content), delimiter=delimiter)
|
||||
rows = [row for row in reader]
|
||||
max_widths = [max(map(len, column)) for column in zip(*rows)]
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ from shortuuid import uuid
|
||||
|
||||
from dvc.utils.compat import fspath
|
||||
from dvc.utils.compat import fspath_py35
|
||||
from dvc.utils.compat import makedirs as _makedirs
|
||||
from dvc.utils.compat import open
|
||||
|
||||
|
||||
@@ -142,7 +141,7 @@ def makedirs(path, exist_ok=False, mode=None):
|
||||
path = fspath_py35(path)
|
||||
|
||||
if mode is None:
|
||||
_makedirs(path, exist_ok=exist_ok)
|
||||
os.makedirs(path, exist_ok=exist_ok)
|
||||
return
|
||||
|
||||
# utilize umask to set proper permissions since Python 3.7 the `mode`
|
||||
@@ -150,7 +149,7 @@ def makedirs(path, exist_ok=False, mode=None):
|
||||
# newly-created intermediate-level directories.
|
||||
umask = os.umask(0o777 - mode)
|
||||
try:
|
||||
_makedirs(path, exist_ok=exist_ok)
|
||||
os.makedirs(path, exist_ok=exist_ok)
|
||||
finally:
|
||||
os.umask(umask)
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
"""Handle import compatibility between Python 2 and Python 3"""
|
||||
|
||||
import errno
|
||||
import os
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
|
||||
@@ -15,60 +14,6 @@ is_py2 = _ver[0] == 2
|
||||
is_py3 = _ver[0] == 3
|
||||
|
||||
|
||||
def csv_reader(unicode_csv_data, dialect=None, **kwargs):
|
||||
"""csv.reader doesn't support Unicode input, so need to use some tricks
|
||||
to work around this.
|
||||
|
||||
Source: https://docs.python.org/2/library/csv.html#csv-examples
|
||||
"""
|
||||
import csv
|
||||
|
||||
dialect = dialect or csv.excel
|
||||
|
||||
if is_py3:
|
||||
# Python3 supports encoding by default, so just return the object
|
||||
for row in csv.reader(unicode_csv_data, dialect=dialect, **kwargs):
|
||||
yield [cell for cell in row]
|
||||
|
||||
else:
|
||||
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
|
||||
reader = csv.reader(
|
||||
utf_8_encoder(unicode_csv_data), dialect=dialect, **kwargs
|
||||
)
|
||||
for row in reader:
|
||||
# decode UTF-8 back to Unicode, cell by cell:
|
||||
yield [unicode(cell, "utf-8") for cell in row] # noqa: F821
|
||||
|
||||
|
||||
def utf_8_encoder(unicode_csv_data):
|
||||
"""Source: https://docs.python.org/2/library/csv.html#csv-examples"""
|
||||
for line in unicode_csv_data:
|
||||
yield line.encode("utf-8")
|
||||
|
||||
|
||||
def _makedirs(name, mode=0o777, exist_ok=False):
|
||||
"""Source: https://github.com/python/cpython/blob/
|
||||
3ce3dea60646d8a5a1c952469a2eb65f937875b3/Lib/os.py#L196-L226
|
||||
"""
|
||||
head, tail = os.path.split(name)
|
||||
if not tail:
|
||||
head, tail = os.path.split(head)
|
||||
if head and tail and not os.path.exists(head):
|
||||
try:
|
||||
_makedirs(head, exist_ok=exist_ok)
|
||||
except OSError as e:
|
||||
if e.errno != errno.EEXIST:
|
||||
raise
|
||||
cdir = os.curdir
|
||||
if tail == cdir:
|
||||
return
|
||||
try:
|
||||
os.mkdir(name, mode)
|
||||
except OSError:
|
||||
if not exist_ok or not os.path.isdir(name):
|
||||
raise
|
||||
|
||||
|
||||
@contextmanager
|
||||
def ignore_file_not_found():
|
||||
try:
|
||||
@@ -94,7 +39,6 @@ if is_py2:
|
||||
numeric_types = (int, long, float) # noqa: F821
|
||||
integer_types = (int, long) # noqa: F821
|
||||
input = raw_input # noqa: F821
|
||||
makedirs = _makedirs
|
||||
range = xrange # noqa: F821
|
||||
FileNotFoundError = IOError
|
||||
JSONDecodeError = ValueError
|
||||
@@ -119,7 +63,6 @@ if is_py2:
|
||||
|
||||
elif is_py3:
|
||||
import pathlib # noqa: F401
|
||||
from os import makedirs # noqa: F401
|
||||
from urllib.parse import ( # noqa: F401
|
||||
urlparse, # noqa: F401
|
||||
urlunparse, # noqa: F401
|
||||
|
||||
Reference in New Issue
Block a user