aboutsummaryrefslogtreecommitdiffstats
path: root/object.py
diff options
context:
space:
mode:
Diffstat (limited to 'object.py')
-rw-r--r--object.py84
1 files changed, 59 insertions, 25 deletions
diff --git a/object.py b/object.py
index 7538b4c..0bf71c7 100644
--- a/object.py
+++ b/object.py
@@ -4,6 +4,7 @@ import hashlib
import os
import os.path
import zlib
+import datetime
class Object :
@@ -12,16 +13,19 @@ class Object :
def save( self ) :
header = "author " + self.author + "\n"
header += "parent " + self.parent + "\n"
+ header += "created " + self.created + "\n"
header += "#\n" #end of header
store = header + self.content
# if object was never stored, it doesn't have a name attribute
# so we hash it contents
- name = getattr( self, "name", hashlib.sha1( store ).hexdigest() )
- path = name[:2] + '/' + name[2:]
+ if not hasattr( self, "name" ) :
+ self.name = hashlib.sha1( store ).hexdigest()
- if ( not os.path.exists( name[:2] ) ) :
- os.mkdir( name[:2] )
+ path = self.name[:2] + '/' + self.name[2:]
+
+ if ( not os.path.exists( self.name[:2] ) ) :
+ os.mkdir( self.name[:2] )
if ( not os.path.exists( path ) ) :
os.mkdir( path )
@@ -29,42 +33,72 @@ class Object :
file = open( path + "/object", 'w' )
file.write( zlib.compress( store ) )
file.close()
- return name
+ return self.name
##
# Class constructor.
- # If a name is provided, try to load the object from the disk
- def __init__( self, name = None ) :
- if ( name != None ) :
- path = name[:2] + '/' + name[2:] + '/object'
- if ( os.path.exists( path ) ) :
- file = open( name[:2] + '/' + name[2:] + '/object','r')
- contentStream = StringIO.StringIO( zlib.decompress( file.read() ) )
+ def __init__( self, author = None, parent = None, content = None ) :
+ self.author = str(author)
+ self.parent = str(parent)
+ self.created = str( datetime.datetime.now() )
+ self.content = str(content)
+ self.name = hashlib.sha1( self.author + self.parent + self.created ).hexdigest()
+
+ ##
+ # Create an object from a file object
+ @staticmethod
+ def load( name ) :
+ result = Object()
+ path = name[:2] + '/' + name[2:] + '/object'
+
+ if ( os.path.exists( path ) ) :
+
+ file = open( name[:2] + '/' + name[2:] + '/object','r')
+ contentStream = StringIO.StringIO( zlib.decompress( file.read() ) )
+
+ for line in contentStream :
+
+ if ( line == "#\n") :
+ result.content = contentStream.read()
+ break
+
+ key, sep, value = line.rstrip('\n').partition(' ')
+ setattr(result, key, value)
- for line in contentStream :
- if ( line == "#\n") :
- self.content = contentStream.read()
- break
- key, sep, value = line.rstrip('\n').partition(' ')
- setattr(self, key, value)
+ return result
def appendTo(self) :
pass
- def appendChild(self) :
- pass
+ def appendChild(self, child) :
+ if not hasattr(self, "name") :
+ pass
+
+ if child.parent != self.name :
+ pass
+
+ if ( not os.path.exists( self.name[:2] ) ) :
+ os.mkdir( self.name[:2] )
+
+ if ( not os.path.exists( path ) ) :
+ os.mkdir( path )
+
+ file = open( path + "/childs", 'w+' )
+ file.write( child.name )
+ file.close()
+
+
+
if __name__== '__main__' :
- x = Object()
- x.parent = "test parent"
- x.author = "test author"
- x.content = "test content"
+ x = Object("test parent", "test author")
name = x.save()
- y = Object(name)
+ y = Object.load(name)
print y.author
print y.parent
print y.content
+ print y.created