aboutsummaryrefslogtreecommitdiffstats
path: root/server/object.py
diff options
context:
space:
mode:
Diffstat (limited to 'server/object.py')
-rw-r--r--server/object.py211
1 files changed, 211 insertions, 0 deletions
diff --git a/server/object.py b/server/object.py
new file mode 100644
index 0000000..866f7bb
--- /dev/null
+++ b/server/object.py
@@ -0,0 +1,211 @@
+# -*- coding: utf-8 -*-
+import StringIO
+import hashlib
+import sys
+import os
+import os.path
+import zlib
+import datetime
+import fileinput
+import logging
+
+from permission import *
+from config import config
+
+class Object:
+
+ def __get_path(self):
+ return self.name[:2] + '/' + self.name[2:]
+
+ def __create_dir(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.__create_dir()
+
+ 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.__get_path()
+ self.saved = False
+
+ elif isinstance(init, str):
+ self.name = init
+ self.path = self.__get_path()
+ 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()
+ else:
+ logging.error("Object {} root doesn't exist".format(init))
+
+ def append_to(self, father):
+ father.append_child(self)
+
+ def append_child(self, child):
+ if not self.saved:
+ self.save()
+
+ file = open(self.path + "/childs", 'a')
+ file.write(child.name + "\n")
+ file.close()
+
+ @staticmethod
+ def get_permission_by_hash(hash, user):
+ path = hash[:2] + '/' + hash[2:]
+ try:
+ file = open(path + '/permissions', 'r')
+ except IOError:
+ print 'cannot open', path
+ else:
+ for line in file:
+ name, sep, perm = line.rstrip('\n').partition(' ')
+ if name == user:
+ return perm
+
+ return 0
+
+ def get_permission(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 set_permission(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()
+
+class ObjectHandler:
+
+ def __init__(self, user):
+ self.user = user
+ self.root_object = hashlib.sha1(user).hexdigest()
+
+ if not os.path.exists(self.user):
+ logging.error("User {} root doesn't exist".format(self.user))
+
+ def get_object_directory(self, hash):
+ directory = self.user + '/' + hash[:2] + '/' + hash[2:]
+ if (os.path.exists(directory)):
+ return directory
+ else:
+ return None
+
+ def get_object(self, hash):
+ os.chdir(self.user)
+ temp = Object(hash)
+ os.chdir('..')
+ return temp
+
+ #return a list of hash,permission pairs
+ def get_child_list(self, hash, user):
+ directory = self.get_object_directory(hash)
+ if directory:
+ result = []
+ file = open(directory + "/childs", 'r')
+ for line in file :
+ name = line.rstrip('\n')
+ result.append(name)
+ file.close()
+ return result
+ else :
+ return None
+
+ def create_home_node(self):
+ pass
+
+ def get_home_node(self):
+ return self.root_object
+
+if __name__ == '__main__':
+ config.root = os.path.abspath('object_tests')
+ if not(os.path.exists(config.root)):
+ os.mkdir(config.root)
+ os.chdir(config.root)
+ if not(os.path.exists('guillaume')):
+ os.mkdir('guillaume')
+
+ guillaume_stuff = ObjectHandler('guillaume')
+ os.chdir('guillaume')
+ x = Object({'parent' : 'toto', 'author' : 'Zaran'})
+ x.set_permission("test", READ)
+ x.set_permission("toto", READ | MODIFY)
+ x.set_permission("toto", READ | MODIFY | APPEND)
+ x.set_permission("toto2", READ | MODIFY | APPEND)
+ print x.get_permission("toto")
+ print Object.get_permission_by_hash(x.name, "toto")
+ child = Object({'parent' : x.name, 'author' : 'Zaran'})
+ child.save()
+
+ x.append_child(child)
+ os.chdir(config.root)
+
+ y = guillaume_stuff.get_object(x.name)
+ pomme = guillaume_stuff.get_child_list(x.name, None)
+ print pomme
+ print y.data['author']
+ print y.data['parent']
+ print y.data['content']
+ print y.data['created']
+
+
+