Commit 6c5084f5 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[utils] pre-coommit cleanup

parent b0200612
......@@ -55,11 +55,11 @@ from . import hash
# ----------------------------------------------------------
def hashed_or_simple(prefix, what, path, suffix='.json'):
def hashed_or_simple(prefix, what, path, suffix=".json"):
"""Returns a hashed path or simple path depending on where the resource is
"""
username, right_bit = path.split('/', 1)
username, right_bit = path.split("/", 1)
hashed_prefix = hash.toUserPath(username)
candidate = os.path.join(prefix, what, hashed_prefix, right_bit) + suffix
......@@ -119,20 +119,13 @@ def extension_for_language(language):
"""
return dict(
unknown = '',
cxx = '.so',
matlab = '.m',
python = '.py',
r = '.r',
)[language]
return dict(unknown="", cxx=".so", matlab=".m", python=".py", r=".r")[language]
# ----------------------------------------------------------
class Prefix(object):
def __init__(self, paths=None):
if isinstance(paths, list):
self.paths = paths
......@@ -159,44 +152,38 @@ class Prefix(object):
class File(object):
"""User helper to read and write file objects"""
def __init__(self, path, binary=False):
self.path = path
self.binary = binary
def exists(self):
return os.path.exists(self.path)
def load(self):
mode = 'rb' if self.binary else 'rt'
mode = "rb" if self.binary else "rt"
with open(self.path, mode) as f:
return f.read()
def try_load(self):
if os.path.exists(self.path):
return self.load()
return None
def backup(self):
if not os.path.exists(self.path):
return #no point in backing-up
return # no point in backing-up
backup = self.path + '~'
backup = self.path + "~"
if os.path.exists(backup):
os.remove(backup)
shutil.copy(self.path, backup)
def save(self, contents):
d = os.path.dirname(self.path)
......@@ -206,25 +193,24 @@ class File(object):
if os.path.exists(self.path):
self.backup()
mode = 'wb' if self.binary else 'wt'
mode = "wb" if self.binary else "wt"
if self.binary:
mode = 'wb'
mode = "wb"
if isinstance(contents, six.string_types):
contents = contents.encode('utf-8')
contents = contents.encode("utf-8")
else:
mode = 'wt'
mode = "wt"
if not isinstance(contents, six.string_types):
contents = contents.decode('utf-8')
contents = contents.decode("utf-8")
with open(self.path, mode) as f:
f.write(contents)
def remove(self):
safe_rmfile(self.path)
safe_rmfile(self.path + '~') #backup
safe_rmdir(self.path) #remove containing directory
safe_rmfile(self.path + "~") # backup
safe_rmdir(self.path) # remove containing directory
# ----------------------------------------------------------
......@@ -236,10 +222,10 @@ class Storage(object):
def __init__(self, path):
self.path = path
self.json = File(self.path + '.json')
self.doc = File(self.path + '.rst')
self.json = File(self.path + ".json")
self.doc = File(self.path + ".rst")
def hash(self, description='description'):
def hash(self, description="description"):
"""The 64-character hash of the database declaration JSON"""
return hash.hashJSONFile(self.json.path, description)
......@@ -249,12 +235,13 @@ class Storage(object):
def load(self):
"""Loads the JSON declaration as a file"""
tp = collections.namedtuple('Storage', ['declaration', 'description'])
tp = collections.namedtuple("Storage", ["declaration", "description"])
return tp(self.json.load(), self.doc.try_load())
def save(self, declaration, description=None):
"""Saves the JSON declaration as files"""
if description: self.doc.save(description.encode('utf8'))
if description:
self.doc.save(description.encode("utf8"))
if not isinstance(declaration, six.string_types):
declaration = simplejson.dumps(declaration, indent=4)
self.json.save(declaration)
......@@ -280,21 +267,22 @@ class CodeStorage(object):
def __init__(self, path, language=None):
self.path = path
self.json = File(self.path + '.json')
self.doc = File(self.path + '.rst')
self.json = File(self.path + ".json")
self.doc = File(self.path + ".rst")
self._language = language or self.__auto_discover_language()
self.code = File(self.path + \
extension_for_language(self._language), binary=True)
self.code = File(
self.path + extension_for_language(self._language), binary=True
)
def __auto_discover_language(self, json=None):
"""Discovers and sets the language from its own JSON descriptor"""
try:
text = json or self.json.load()
json = simplejson.loads(text)
return json['language']
return json["language"]
except IOError:
return 'unknown'
return "unknown"
@property
def language(self):
......@@ -303,21 +291,24 @@ class CodeStorage(object):
@language.setter
def language(self, value):
self._language = value
self.code = File(self.path + extension_for_language(self._language),
binary=True)
self.code = File(
self.path + extension_for_language(self._language), binary=True
)
def hash(self):
"""The 64-character hash of the database declaration JSON"""
if self.code.exists():
return hash.hash(dict(
json=hash.hashJSONFile(self.json.path, 'description'),
code=hash.hashFileContents(self.code.path),
))
return hash.hash(
dict(
json=hash.hashJSONFile(self.json.path, "description"),
code=hash.hashFileContents(self.code.path),
)
)
else:
return hash.hash(dict(
json=hash.hashJSONFile(self.json.path, 'description'),
))
return hash.hash(
dict(json=hash.hashJSONFile(self.json.path, "description"))
)
def exists(self):
"""If the database declaration file exists"""
......@@ -325,21 +316,22 @@ class CodeStorage(object):
def load(self):
"""Loads the JSON declaration as a file"""
tp = collections.namedtuple('CodeStorage',
['declaration', 'code', 'description'])
tp = collections.namedtuple(
"CodeStorage", ["declaration", "code", "description"]
)
return tp(self.json.load(), self.code.try_load(), self.doc.try_load())
def save(self, declaration, code=None, description=None):
"""Saves the JSON declaration and the code as files"""
if description:
self.doc.save(description.encode('utf8'))
self.doc.save(description.encode("utf8"))
if not isinstance(declaration, six.string_types):
declaration = simplejson.dumps(declaration, indent=4)
self.json.save(declaration)
if code:
if self._language == 'unknown':
if self._language == "unknown":
self.language = self.__auto_discover_language(declaration)
self.code.save(code)
......@@ -366,7 +358,8 @@ class NumpyJSONEncoder(simplejson.JSONEncoder):
if isinstance(obj, numpy.ndarray) or isinstance(obj, numpy.generic):
return obj.tolist()
elif isinstance(obj, numpy.dtype):
if obj.name == 'str': return 'string'
if obj.name == "str":
return "string"
return obj.name
return simplejson.JSONEncoder.default(self, obj)
......@@ -377,10 +370,12 @@ class NumpyJSONEncoder(simplejson.JSONEncoder):
def has_argument(method, argument):
try:
from inspect import signature
sig = signature(method)
params = sig.parameters
except ImportError:
from inspect import getargspec
params = getargspec(method).args
return argument in params
Supports Markdown
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