Skip to content
Snippets Groups Projects

Create a base class for Storage and CodeStorage

Merged Samuel GAIST requested to merge 23_clean_storage_classes into master
1 file
+ 51
25
Compare changes
  • Side-by-side
  • Inline
+ 51
25
@@ -216,8 +216,7 @@ class File(object):
# ----------------------------------------------------------
class Storage(object):
"""Resolves paths for objects that provide only a description"""
class AbstractStorage(object):
asset_type = None
asset_folder = None
@@ -233,37 +232,64 @@ class Storage(object):
self.json = File(self.path + ".json")
self.doc = File(self.path + ".rst")
def hash(self, description="description"):
"""The 64-character hash of the database declaration JSON"""
return hash.hashJSONFile(self.json.path, description)
def exists(self):
"""If the database declaration file exists"""
return self.json.exists()
def remove(self):
"""Removes the object from the disk"""
self.json.remove()
self.doc.remove()
def hash(self):
"""The 64-character hash of the database declaration JSON"""
raise NotImplementedError
def load(self):
"""Loads the JSON declaration as a file"""
raise NotImplementedError
def save(self):
"""Saves the JSON declaration as files"""
raise NotImplementedError
class Storage(AbstractStorage):
"""Resolves paths for objects that provide only a description"""
def __init__(self, path):
super(Storage, self).__init__(path)
def hash(self, description="description"):
"""Re-imp"""
return hash.hashJSONFile(self.json.path, description)
def load(self):
"""Re-imp"""
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"""
"""Re-imp"""
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)
def remove(self):
"""Removes the object from the disk"""
self.json.remove()
self.doc.remove()
# ----------------------------------------------------------
class CodeStorage(object):
class CodeStorage(AbstractStorage):
"""Resolves paths for objects that provide a description and code
Parameters:
@@ -273,10 +299,7 @@ class CodeStorage(object):
"""
def __init__(self, path, language=None):
self.path = path
self.json = File(self.path + ".json")
self.doc = File(self.path + ".rst")
super(CodeStorage, self).__init__(path)
self._language = language or self.__auto_discover_language()
self.code = File(
@@ -304,7 +327,7 @@ class CodeStorage(object):
)
def hash(self):
"""The 64-character hash of the database declaration JSON"""
"""Re-imp"""
if self.code.exists():
return hash.hash(
@@ -319,18 +342,21 @@ class CodeStorage(object):
)
def exists(self):
"""If the database declaration file exists"""
return self.json.exists() and self.code.exists()
"""Re-imp"""
return super(CodeStorage, self).exists() and self.code.exists()
def load(self):
"""Loads the JSON declaration as a file"""
"""Re-imp"""
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"""
"""Re-imp"""
if description:
self.doc.save(description.encode("utf8"))
@@ -344,9 +370,9 @@ class CodeStorage(object):
self.code.save(code)
def remove(self):
"""Removes the object from the disk"""
self.json.remove()
self.doc.remove()
"""Re-imp"""
super(CodeStorage, self).remove()
self.code.remove()
Loading