# -*- coding: utf-8 -*- import StringIO import hashlib import sys import os import os.path import zlib import datetime import fileinput from permission import * class Object : def _getPath( self ) : return self.name[:2] + '/' + self.name[2:] def _createDir( 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._createDir() 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.data['content'] file = open( self.path + "/object", 'w' ) file.write( zlib.compress( store ) ) file.close() os.mknod(self.path + "/childs") os.mknod(self.path + "/permissions") self.saved = True ## # Class constructor. def __init__( self, init ) : if isinstance( init, dict ) : 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._getPath() self.saved = False elif isinstance( init, str ) : self.name = init self.path = self._getPath() self.saved = True if ( os.path.exists( self.path ) ) : file = open( self.path + '/object','r') contentStream = StringIO.StringIO( zlib.decompress( file.read() ) ) data = {} for line in contentStream : if ( line == "#\n") : data['content'] = contentStream.read() break else : key, sep, value = line.rstrip('\n').partition(' ') data[key] = value self.data = data file.close() def appendTo(self, father) : father.appendChild(self) def appendChild(self, child) : if not self.saved : self.save() file = open( self.path + "/childs", 'a' ) file.write( child.name + "\n") file.close() def getPermission( self, user ) : file = open( self.path + '/permissions', 'r' ) for line in file : name, sep, perm = line.rstrip('\n').partition(' ') if name == user : return perm return 0 def setPermission(self, user, permission ) : if not self.saved : self.save() sentinel = False for line in fileinput.input( self.path + "/permissions", inplace=1 ) : name, sep, perm = line.rstrip('\n').partition(' ') if name == user : sys.stdout.write( name + ' ' + str( permission ) + '\n' ) sentinel = True else : sys.stdout.write( line ) if not sentinel : file = open( self.path + '/permissions', 'a' ) file.write( user + ' ' + str( permission ) + '\n' ) file.close() if __name__== '__main__' : x = Object({'parent' : 'toto', 'author' : 'Zaran'}) x.setPermission("test", READ ) x.setPermission( "toto", READ | MODIFY ) x.setPermission( "toto", READ | MODIFY | APPEND ) x.setPermission( "toto2", READ | MODIFY | APPEND ) print x.getPermission("toto") child = Object({'parent' : x.name, 'author' : 'Zaran'}) child.save() x.appendChild(child) y = Object(x.name) print y.data['author'] print y.data['parent'] print y.data['content'] print y.data['created']