From c97e9c9102c5352639f7e8338b178d2d06a3432c Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Sat, 23 Oct 2010 02:17:03 +0200 Subject: Append child --- object.py | 67 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) (limited to 'object.py') diff --git a/object.py b/object.py index 16b16bd..ad76c41 100644 --- a/object.py +++ b/object.py @@ -8,31 +8,28 @@ import datetime class Object : + def __create_dir(self): + if ( not os.path.exists( self.name[:2] ) ) : + os.mkdir( self.name[:2] ) + + if ( not os.path.exists( self.path ) ) : + os.mkdir( self.path ) + ## # Save the object on the disk. def save( self ) : + self.__create_dir() + 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 - if not hasattr( self, "name" ) : - self.name = hashlib.sha1( store ).hexdigest() - - 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 ) - - file = open( path + "/object", 'w' ) + file = open( self.path + "/object", 'w' ) file.write( zlib.compress( store ) ) file.close() + self.saved = True return self.name ## @@ -43,6 +40,8 @@ class Object : self.created = str( datetime.datetime.now() ) self.content = str(content) self.name = hashlib.sha1( self.author + self.parent + self.created ).hexdigest() + self.path = self.name[:2] + '/' + self.name[2:] + self.saved = False ## # Create an object from a file object @@ -50,11 +49,13 @@ class Object : @staticmethod def load( name ) : result = Object() - path = name[:2] + '/' + name[2:] + '/object' + result.name = name + result.path = name[:2] + '/' + name[2:] + result.saved = True - if ( os.path.exists( path ) ) : + if ( os.path.exists( result.path ) ) : - file = open( name[:2] + '/' + name[2:] + '/object','r') + file = open( result.path + '/object','r') contentStream = StringIO.StringIO( zlib.decompress( file.read() ) ) for line in contentStream : @@ -66,35 +67,35 @@ class Object : key, sep, value = line.rstrip('\n').partition(' ') setattr(result, key, value) + file.close() + return result - def appendTo(self) : - pass + def appendTo(self, father) : + father.appendChild(self) 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 ) + if not self.saved : + self.save() + + file = open( self.path + "/childs", 'a' ) + file.write( child.name + "\n") file.close() if __name__== '__main__' : - x = Object("test parent", "test author") + x = Object("test parent", "Zaran", "father") name = x.save() + child = Object(name,"Zaran", "child") + child.save() + + x.appendChild(child) + + print name y = Object.load(name) print y.author print y.parent -- cgit v1.2.3-70-g09d2