aboutsummaryrefslogtreecommitdiffstats
path: root/object.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2010-11-17 03:52:54 +0100
committerThibaut Horel <thibaut.horel@gmail.com>2010-11-17 03:52:54 +0100
commitc494741c09f03268d950b8da27952917518db446 (patch)
tree1fbbd798240db8b996fe9aa5b015930a7dd00b7c /object.py
parentb8106bd66fad5bafdec50da344e6a23f2dc78c00 (diff)
downloadalias-c494741c09f03268d950b8da27952917518db446.tar.gz
Example of alias query handling.
The request must be of the form : <iq to='xxx@object.alias.fr.nf'> <query xmlns='alias:query' type='items'/> </iq> where xxx is the base64 encoding of the buddy's jabber id whom profile you are requesting. Also tried to make some files more pep8 compliant (coding style)
Diffstat (limited to 'object.py')
-rw-r--r--object.py171
1 files changed, 92 insertions, 79 deletions
diff --git a/object.py b/object.py
index 6702a6c..3b4f067 100644
--- a/object.py
+++ b/object.py
@@ -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']
-
-
-
+
+
+