Fix formatting

This commit is contained in:
Ines Montani 2018-11-26 13:27:41 +01:00
parent 7601ae0cff
commit 048416f265
2 changed files with 53 additions and 39 deletions

View File

@ -39,11 +39,11 @@ copy_reg = copy_reg
CudaStream = CudaStream CudaStream = CudaStream
cupy = cupy cupy = cupy
copy_array = copy_array copy_array = copy_array
izip = getattr(itertools, 'izip', zip) izip = getattr(itertools, "izip", zip)
is_windows = sys.platform.startswith('win') is_windows = sys.platform.startswith("win")
is_linux = sys.platform.startswith('linux') is_linux = sys.platform.startswith("linux")
is_osx = sys.platform == 'darwin' is_osx = sys.platform == "darwin"
# See: https://github.com/benjaminp/six/blob/master/six.py # See: https://github.com/benjaminp/six/blob/master/six.py
is_python2 = sys.version_info[0] == 2 is_python2 = sys.version_info[0] == 2
@ -55,8 +55,10 @@ if is_python2:
unicode_ = unicode # noqa: F821 unicode_ = unicode # noqa: F821
basestring_ = basestring # noqa: F821 basestring_ = basestring # noqa: F821
input_ = raw_input # noqa: F821 input_ = raw_input # noqa: F821
json_dumps = lambda data: ujson.dumps(data, indent=2, escape_forward_slashes=False).decode('utf8') json_dumps = lambda data: ujson.dumps(
path2str = lambda path: str(path).decode('utf8') data, indent=2, escape_forward_slashes=False
).decode("utf8")
path2str = lambda path: str(path).decode("utf8")
elif is_python3: elif is_python3:
bytes_ = bytes bytes_ = bytes
@ -71,35 +73,41 @@ def b_to_str(b_str):
if is_python2: if is_python2:
return b_str return b_str
# important: if no encoding is set, string becomes "b'...'" # important: if no encoding is set, string becomes "b'...'"
return str(b_str, encoding='utf8') return str(b_str, encoding="utf8")
def getattr_(obj, name, *default): def getattr_(obj, name, *default):
if is_python3 and isinstance(name, bytes): if is_python3 and isinstance(name, bytes):
name = name.decode('utf8') name = name.decode("utf8")
return getattr(obj, name, *default) return getattr(obj, name, *default)
def symlink_to(orig, dest): def symlink_to(orig, dest):
if is_windows: if is_windows:
import subprocess import subprocess
subprocess.call(['mklink', '/d', path2str(orig), path2str(dest)], shell=True)
subprocess.call(["mklink", "/d", path2str(orig), path2str(dest)], shell=True)
else: else:
orig.symlink_to(dest) orig.symlink_to(dest)
def symlink_remove(link): def symlink_remove(link):
# https://stackoverflow.com/questions/26554135/cant-delete-unlink-a-symlink-to-directory-in-python-windows # https://stackoverflow.com/q/26554135/6400719
if( os.path.isdir(path2str(link)) and is_windows ): # this should only be on Py2.7 and windows if os.path.isdir(path2str(link)) and is_windows:
# this should only be on Py2.7 and windows
os.rmdir(path2str(link)) os.rmdir(path2str(link))
else: else:
os.unlink(path2str(link)) os.unlink(path2str(link))
def is_config(python2=None, python3=None, windows=None, linux=None, osx=None): def is_config(python2=None, python3=None, windows=None, linux=None, osx=None):
return (python2 in (None, is_python2) and return (
python3 in (None, is_python3) and python2 in (None, is_python2)
windows in (None, is_windows) and and python3 in (None, is_python3)
linux in (None, is_linux) and and windows in (None, is_windows)
osx in (None, is_osx)) and linux in (None, is_linux)
and osx in (None, is_osx)
)
def normalize_string_keys(old): def normalize_string_keys(old):
@ -107,7 +115,7 @@ def normalize_string_keys(old):
new = {} new = {}
for key, value in old.items(): for key, value in old.items():
if isinstance(key, bytes_): if isinstance(key, bytes_):
new[key.decode('utf8')] = value new[key.decode("utf8")] = value
else: else:
new[key] = value new[key] = value
return new return new
@ -117,19 +125,21 @@ def import_file(name, loc):
loc = str(loc) loc = str(loc)
if is_python_pre_3_5: if is_python_pre_3_5:
import imp import imp
return imp.load_source(name, loc) return imp.load_source(name, loc)
else: else:
import importlib.util import importlib.util
spec = importlib.util.spec_from_file_location(name, str(loc)) spec = importlib.util.spec_from_file_location(name, str(loc))
module = importlib.util.module_from_spec(spec) module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) spec.loader.exec_module(module)
return module return module
def locale_escape(string, errors='replace'): def locale_escape(string, errors="replace"):
''' """
Mangle non-supported characters, for savages with ascii terminals. Mangle non-supported characters, for savages with ascii terminals.
''' """
encoding = locale.getpreferredencoding() encoding = locale.getpreferredencoding()
string = string.encode(encoding, errors).decode('utf8') string = string.encode(encoding, errors).decode("utf8")
return string return string

View File

@ -1,36 +1,40 @@
# coding: utf-8
from __future__ import unicode_literals
import pytest
import os import os
from pathlib import Path from pathlib import Path
from ..compat import symlink_to, symlink_remove, path2str from ..compat import symlink_to, symlink_remove, path2str
import pytest
def target_local_path(): def target_local_path():
return './foo-target' return "./foo-target"
def link_local_path(): def link_local_path():
return './foo-symlink' return "./foo-symlink"
@pytest.fixture(scope='function') @pytest.fixture(scope="function")
def setup_target(request): def setup_target(request):
target = Path(target_local_path()) target = Path(target_local_path())
if not target.exists(): if not target.exists():
os.mkdir( path2str(target) ) os.mkdir(path2str(target))
# yield -- need to cleanup even if assertion fails # yield -- need to cleanup even if assertion fails
# https://github.com/pytest-dev/pytest/issues/2508#issuecomment-309934240 # https://github.com/pytest-dev/pytest/issues/2508#issuecomment-309934240
def cleanup(): def cleanup():
symlink_remove( Path(link_local_path() ) ) symlink_remove(Path(link_local_path()))
os.rmdir( target_local_path() ) os.rmdir(target_local_path())
request.addfinalizer(cleanup) request.addfinalizer(cleanup)
def test_create_symlink_windows(setup_target): def test_create_symlink_windows(setup_target):
target = Path(target_local_path()) target = Path(target_local_path())
link = Path(link_local_path()) link = Path(link_local_path())
assert target.exists() assert target.exists()
symlink_to(link, target) symlink_to(link, target)
assert link.exists() assert link.exists()