diff options
| -rw-r--r-- | object.py | 171 | ||||
| -rw-r--r-- | plugin.py | 45 | ||||
| -rw-r--r-- | user.py | 12 |
3 files changed, 134 insertions, 94 deletions
@@ -8,97 +8,94 @@ import zlib import datetime import fileinput from permission import * +import logging -OBJECT_ROOT = 'objects/' +OBJECT_ROOT = 'users/' class Object : - def _getPath( self ) : + 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 ) - + 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 ) : + 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 = 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 ) : - + 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.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() ) ) + + elif isinstance(init, str) : + if (os.path.exists(str)) : + file = open(self.path + '/object', 'r') + contentStream = StringIO.StringIO(zlib.decompress(file.read())) data = {} - + for line in contentStream : - if ( line == "#\n") : + 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 = open(self.path + "/childs", 'a') + file.write(child.name + "\n") file.close() - + @staticmethod - def getPermissionByHash( hash, user ) : + def getPermissionByHash(hash, user) : path = hash[:2] + '/' + hash[2:] try: - file = open( path + '/permissions', 'r' ) + file = open(path + '/permissions', 'r') except IOError : print 'cannot open', path else : @@ -109,81 +106,97 @@ class Object : return 0 - def getPermission( self, user ) : - - file = open( self.path + '/permissions', 'r' ) - + 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 ) : + + def setPermission(self, user, permission) : if not self.saved : self.save() - + sentinel = False - for line in fileinput.input( self.path + "/permissions", inplace=1 ) : + 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' ) + sys.stdout.write(name + ' ' + str(permission) + '\n') sentinel = True else : - sys.stdout.write( line ) - + sys.stdout.write(line) + if not sentinel : - file = open( self.path + '/permissions', 'a' ) - file.write( user + ' ' + str( permission ) + '\n' ) + file = open(self.path + '/permissions', 'a') + file.write(user + ' ' + str(permission) + '\n') file.close() class ObjectHandler : - - def __init__( self, user ) : + + def __init__(self, user) : self.user = user - self.root = OBJECT_ROOT + user + '/' - - def getDirectory( self, hash ) : - directory = self.root + hash[:2] + '/' + hash[2:] - if ( os.path.exists( directory ) ) : + self.root_directory = OBJECT_ROOT + user + '/' + self.root_object = hashlib.sha1(user).hexdigest() + + if not os.path.exists(self.root_directory) : + logging.error("User %s root doesn't exist" % self.user) + + def get_object_directory(self, hash) : + directory = self.root_directory + hash[:2] + '/' + hash[2:] + if (os.path.exists(directory)) : return directory else : return None + def getObject(self, hash) : + directory = self.get_object_directory(hash) + if directory is None : + logging.error("Object %s doesn't exist" % hash) + else : + return Object(directory) + #return a list of hash,permission pairs - def getChildList( self, hash, user) : - directory = self.getDirectory( hash ) - if ( directory ) : + def get_child_list(self, hash, user) : + directory = self.get_object_directory(hash) + if (directory) : result = [] - file = open( directory + "/childs", 'r' ) + file = open(directory + "/childs", 'r') for line in file : - name, sep, perm = line.rstrip('\n').partition(' ') - if name == user : - result.append( ( name, int(perm) ) ) + name = line.rstrip('\n') + result.append(name) file.close() return result else : return None -if __name__== '__main__' : + def create_home_node(self) : + pass + + def get_home_node(self): + return self.root_object + +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 ) + 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") print Object.getPermissionByHash(x.name, "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'] - - - + + + @@ -1,32 +1,49 @@ +import logging +import base64 +from xml.etree import cElementTree as ET + from sleekxmpp.xmlstream.stanzabase import ElementBase, registerStanzaPlugin from sleekxmpp.plugins import base from sleekxmpp.xmlstream.handler.callback import Callback from sleekxmpp.xmlstream.matcher.xpath import MatchXPath from sleekxmpp.stanza.iq import Iq -import logging -class AliasQuery(ElementBase) : +import object + +class AliasQuery(ElementBase): namespace = 'alias:query' name = 'query' - plugin_attrib = 'query' - interfaces = set(('node', 'type', 'items','object')) + plugin_attrib = 'alias' + interfaces = set(('node', 'type', 'items', 'object')) sub_interfaces = set(('items')) - -class AliasPlugin(base.base_plugin) : - + +class AliasPlugin(base.base_plugin): + def plugin_init(self) : self.description = 'Plugin to handle alias queries' registerStanzaPlugin(Iq, AliasQuery) - + self.xmpp.registerHandler( Callback('Alias queries', - MatchXPath('{%s}iq/{%s}query' % (self.xmpp.default_ns, + MatchXPath('{%s}iq/{%s}query' % (self.xmpp.default_ns, AliasQuery.namespace)), self.handle_alias_query)) - - def handle_alias_query(self, iq) : - iq.reply().send() - - + + def handle_alias_query(self, iq): + receiver = base64.b64decode(iq['to'].user) + handler = object.ObjectHandler(receiver) + node = iq['alias']['node'] + if node == "" : + node = handler.get_home_node() + if iq['alias']['type'] == 'items': + logging.debug('childs of {} requested'.format(node)) + childs = handler.get_child_list(node, iq['alias']['from']) + test = ET.Element("test") + for child in childs : + test.append(ET.Element("item", {'node':child})) + iq.reply().set_payload(test).send() + + + @@ -1,6 +1,8 @@ import os import os.path import shutil +import object +import hashlib class UserHandler : def __init__(self,root): @@ -10,6 +12,8 @@ class UserHandler : userDir = self.root+'/'+name if not os.path.exists(userDir) : os.mkdir(userDir) + handler = object.ObjectHandler(name) + handler.createHomeNode() def registered(self,name): return os.path.exists(self.root+'/'+name) @@ -18,4 +22,10 @@ class UserHandler : shutil.rmtree(self.root+'/'+name) def getUserList(self): - return os.listdir(self.root)
\ No newline at end of file + return os.listdir(self.root) + +class User : + def __init__(self, name) : + self.name = name + self.rootObject = hashlib.sha1( name ).hexdigest() +
\ No newline at end of file |
