diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2010-10-23 17:59:39 +0200 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2010-10-23 17:59:39 +0200 |
| commit | 18e3610d219300c8f8e19ab137f91fff837a95c8 (patch) | |
| tree | 9d25166ccabf9d9e233d10a01efd1e12d9235205 /object.py | |
| parent | c97e9c9102c5352639f7e8338b178d2d06a3432c (diff) | |
| download | alias-18e3610d219300c8f8e19ab137f91fff837a95c8.tar.gz | |
Make object attributes a dictionnary to avoid conflicts with class attributes
Diffstat (limited to 'object.py')
| -rw-r--r-- | object.py | 96 |
1 files changed, 48 insertions, 48 deletions
@@ -8,7 +8,7 @@ import datetime class Object : - def __create_dir(self): + def _create_dir(self): if ( not os.path.exists( self.name[:2] ) ) : os.mkdir( self.name[:2] ) @@ -18,59 +18,60 @@ class Object : ## # Save the object on the disk. def save( self ) : - self.__create_dir() + self._create_dir() - header = "author " + self.author + "\n" - header += "parent " + self.parent + "\n" - header += "created " + self.created + "\n" + header = "author " + self.data['author'] + "\n" + header += "parent " + self.data['parent'] + "\n" + header += "created " + self.data['created'] + "\n" header += "#\n" #end of header - store = header + self.content + store = header + self.data['content'] file = open( self.path + "/object", 'w' ) file.write( zlib.compress( store ) ) file.close() self.saved = True - return self.name ## # Class constructor. - 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() - self.path = self.name[:2] + '/' + self.name[2:] - self.saved = False - - ## - # Create an object from a file object - # @param name The name of the object to load - @staticmethod - def load( name ) : - result = Object() - result.name = name - result.path = name[:2] + '/' + name[2:] - result.saved = True + def __init__( self, init ) : + + if isinstance( init, dict ) : - if ( os.path.exists( result.path ) ) : + if 'author' not in init : + init['author'] = 'None' + if 'parent' not in init : + init['parent'] = 'None' + if 'content' not in init : + init['content'] = 'None' + + self.data = init + self.data['created'] = str( datetime.datetime.now() ) + self.name = hashlib.sha1( str( self.data ) ).hexdigest() + self.path = self.name[:2] + '/' + self.name[2:] + self.saved = False - file = open( result.path + '/object','r') - contentStream = StringIO.StringIO( zlib.decompress( file.read() ) ) + elif isinstance( init, str ) : + self.name = init + self.path = self.name[:2] + '/' + self.name[2:] + self.saved = True - for line in contentStream : - - if ( line == "#\n") : - result.content = contentStream.read() - break + if ( os.path.exists( self.path ) ) : + file = open( self.path + '/object','r') + contentStream = StringIO.StringIO( zlib.decompress( file.read() ) ) + data = {} + + for line in contentStream : - key, sep, value = line.rstrip('\n').partition(' ') - setattr(result, key, value) + if ( line == "#\n") : + data['content'] = contentStream.read() + break + else : + key, sep, value = line.rstrip('\n').partition(' ') + data[key] = value - file.close() + self.data = data + file.close() - return result - def appendTo(self, father) : father.appendChild(self) @@ -79,6 +80,7 @@ class Object : if not self.saved : self.save() + file = open( self.path + "/childs", 'a' ) file.write( child.name + "\n") file.close() @@ -87,20 +89,18 @@ class Object : if __name__== '__main__' : - x = Object("test parent", "Zaran", "father") - name = x.save() - - child = Object(name,"Zaran", "child") + x = Object({'parent' : 'toto', 'author' : 'Zaran'}) + + child = Object({'parent' : x.name, 'author' : 'Zaran'}) child.save() x.appendChild(child) - print name - y = Object.load(name) - print y.author - print y.parent - print y.content - print y.created + y = Object(x.name) + print y.data['author'] + print y.data['parent'] + print y.data['content'] + print y.data['created'] |
