aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--object.py84
1 files changed, 63 insertions, 21 deletions
diff --git a/object.py b/object.py
index a89b020..7538b4c 100644
--- a/object.py
+++ b/object.py
@@ -1,28 +1,70 @@
# -*- coding: utf-8 -*-
-#TODO : dissociate the db connexion and the objecthandler (probably pass a db connexion as init argument)
-import sqlite3
-from version import VERSION
+import StringIO
+import hashlib
+import os
+import os.path
+import zlib
-class ObjectHandler :
+class Object :
- def __init__(self,root,dbhandler) :
- self.rootDirectory = root
- self.db = dbhandler
- if isinstance(self.db,sqlite3.Connection) :
- self.db.text_factory = str
- self.sql = self.db.cursor()
+ ##
+ # Save the object on the disk.
+ def save( self ) :
+ header = "author " + self.author + "\n"
+ header += "parent " + self.parent + "\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 os.path.exists( name[:2] ) ) :
+ os.mkdir( name[:2] )
+
+ if ( not os.path.exists( path ) ) :
+ os.mkdir( path )
+
+ file = open( path + "/object", 'w' )
+ file.write( zlib.compress( store ) )
+ file.close()
+ return 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() ) )
+
+ for line in contentStream :
+ if ( line == "#\n") :
+ self.content = contentStream.read()
+ break
+ key, sep, value = line.rstrip('\n').partition(' ')
+ setattr(self, key, value)
+
+ def appendTo(self) :
+ pass
+
+ def appendChild(self) :
+ pass
+
+if __name__== '__main__' :
+ x = Object()
+ x.parent = "test parent"
+ x.author = "test author"
+ x.content = "test content"
+ name = x.save()
- def childList(self,hash) :
- self.sql.execute("select son from structure where hash=?",(hash,))
- l = []
- for son in self.cursor :
- l.append(son[0])
- return l
+ y = Object(name)
+ print y.author
+ print y.parent
+ print y.content
- def fileName(self,hash) :
- return self.rootDirectory+'objects/'+hash[:2]+'/'+hash[2:]
- def appendChild(self,father,son) :
- self.sql.execute("insert into structure values (?,?)",(father,son))
- self.db.commit()