Commit 818c5e71 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[library] Pre-commit cleanup

parent 900b4f01
......@@ -67,14 +67,14 @@ class Storage(utils.CodeStorage):
def __init__(self, prefix, name, language=None):
if name.count('/') != 2:
if name.count("/") != 2:
raise RuntimeError("invalid library name: `%s'" % name)
self.username, self.name, self.version = name.split('/')
self.username, self.name, self.version = name.split("/")
self.fullname = name
self.prefix = prefix
path = utils.hashed_or_simple(self.prefix, 'libraries', name, suffix='.json')
path = utils.hashed_or_simple(self.prefix, "libraries", name, suffix=".json")
path = path[:-5]
super(Storage, self).__init__(path, language)
......@@ -144,10 +144,9 @@ class Library(object):
try:
self._load(name, library_cache)
finally:
if self._name is not None: #registers it into the cache, even if failed
if self._name is not None: # registers it into the cache, even if failed
library_cache[self._name] = self
def _load(self, data, library_cache):
"""Loads the library"""
......@@ -156,16 +155,16 @@ class Library(object):
self.storage = Storage(self.prefix, data)
json_path = self.storage.json.path
if not self.storage.exists():
self.errors.append('Library declaration file not found: %s' % json_path)
self.errors.append("Library declaration file not found: %s" % json_path)
return
with open(json_path, 'rb') as f:
self.data = simplejson.loads(f.read().decode('utf-8'))
with open(json_path, "rb") as f:
self.data = simplejson.loads(f.read().decode("utf-8"))
self.code_path = self.storage.code.path
# if no errors so far, make sense out of the library data
self.data.setdefault('uses', {})
self.data.setdefault("uses", {})
if self.uses is not None:
for name, value in self.uses.items():
......@@ -173,11 +172,10 @@ class Library(object):
self.libraries[self._name] = self
def uses_dict(self):
"""Returns the usage dictionary for all dependent modules"""
if self.data['language'] == 'unknown':
if self.data["language"] == "unknown":
raise RuntimeError("library has no programming language set")
if not self._name:
......@@ -189,95 +187,85 @@ class Library(object):
for name, value in self.uses.items():
retval[name] = dict(
path=self.libraries[value].storage.code.path,
uses=self.libraries[value].uses_dict(),
)
path=self.libraries[value].storage.code.path,
uses=self.libraries[value].uses_dict(),
)
return retval
def load(self):
"""Loads the Python module for this library resolving all references
Returns the loaded Python module.
"""
if self.data['language'] == 'unknown':
if self.data["language"] == "unknown":
raise RuntimeError("library has no programming language set")
if not self._name:
raise RuntimeError("library has no name")
return loader.load_module(self.name.replace(os.sep, '_'),
self.storage.code.path, self.uses_dict())
return loader.load_module(
self.name.replace(os.sep, "_"), self.storage.code.path, self.uses_dict()
)
@property
def name(self):
"""Returns the name of this object"""
return self._name or '__unnamed_library__'
return self._name or "__unnamed_library__"
@name.setter
def name(self, value):
if self.data['language'] == 'unknown':
if self.data["language"] == "unknown":
raise RuntimeError("library has no programming language set")
self._name = value
self.storage = Storage(self.prefix, value, self.data['language'])
self.storage = Storage(self.prefix, value, self.data["language"])
@property
def schema_version(self):
"""Returns the schema version"""
return self.data.get('schema_version', 1)
return self.data.get("schema_version", 1)
@property
def language(self):
"""Returns the current language set for the library code"""
return self.data['language']
return self.data["language"]
@language.setter
def language(self, value):
"""Sets the current executable code programming language"""
if self.storage: self.storage.language = value
self.data['language'] = value
if self.storage:
self.storage.language = value
self.data["language"] = value
self._check_language_consistence()
@property
def valid(self):
"""A boolean that indicates if this library is valid or not"""
return not bool(self.errors)
@property
def uses(self):
return self.data.get('uses')
return self.data.get("uses")
@uses.setter
def uses(self, value):
self.data['uses'] = value
self.data["uses"] = value
return value
@property
def description(self):
"""The short description for this object"""
return self.data.get('description', None)
return self.data.get("description", None)
@description.setter
def description(self, value):
"""Sets the short description for this object"""
self.data['description'] = value
self.data["description"] = value
@property
def documentation(self):
......@@ -290,7 +278,6 @@ class Library(object):
return self.storage.doc.load()
return None
@documentation.setter
def documentation(self, value):
"""Sets the full-length description for this object"""
......@@ -298,12 +285,11 @@ class Library(object):
if not self._name:
raise RuntimeError("library has no name")
if hasattr(value, 'read'):
if hasattr(value, "read"):
self.storage.doc.save(value.read())
else:
self.storage.doc.save(value)
def hash(self):
"""Returns the hexadecimal hash for the current library"""
......@@ -312,7 +298,6 @@ class Library(object):
return self.storage.hash()
def json_dumps(self, indent=4):
"""Dumps the JSON declaration of this object in a string
......@@ -328,14 +313,11 @@ class Library(object):
"""
return simplejson.dumps(self.data, indent=indent,
cls=utils.NumpyJSONEncoder)
return simplejson.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder)
def __str__(self):
return self.json_dumps()
def write(self, storage=None):
"""Writes contents to prefix location.
......@@ -347,17 +329,16 @@ class Library(object):
"""
if self.data['language'] == 'unknown':
if self.data["language"] == "unknown":
raise RuntimeError("library has no programming language set")
if storage is None:
if not self._name:
raise RuntimeError("library has no name")
storage = self.storage #overwrite
storage = self.storage # overwrite
storage.save(str(self), self.code, self.description)
def export(self, prefix):
"""Recursively exports itself into another prefix
......@@ -387,8 +368,9 @@ class Library(object):
raise RuntimeError("library is not valid")
if prefix == self.prefix:
raise RuntimeError("Cannot export library to the same prefix ("
"%s)" % (prefix))
raise RuntimeError(
"Cannot export library to the same prefix (" "%s)" % (prefix)
)
for k in self.libraries.values():
k.export(prefix)
......
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