# -*- 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 _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): 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"): 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() @staticmethod def getPermissionByHash(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 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() class ObjectHandler: def __init__(self, user): self.user = user self.root_directory = config.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 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__': 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") 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']