From 374b2adb8a7ee55c5cad87955add772096f73999 Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Sat, 20 Nov 2010 22:54:01 +0100 Subject: Add config file feature. New -c switch to specify a config file on the command line. See config.ini.sample for an example of a config file. The configuration options can then be shared across modules : just import config module. --- object.py | 74 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'object.py') diff --git a/object.py b/object.py index 3b4f067..8faa317 100644 --- a/object.py +++ b/object.py @@ -7,26 +7,26 @@ import os.path import zlib import datetime import fileinput -from permission import * import logging -OBJECT_ROOT = 'users/' +from permission import * +from config import config -class Object : +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])) : + if (not os.path.exists(self.name[:2])): os.mkdir(self.name[:2]) - if (not os.path.exists(self.path)) : + 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" @@ -44,9 +44,9 @@ class Object : ## # Class constructor. - def __init__(self, init) : + def __init__(self, init): - if isinstance(init, dict) : + if isinstance(init, dict): if 'author' not in init : init['author'] = 'None' @@ -61,30 +61,30 @@ class Object : self.path = self._getPath() self.saved = False - elif isinstance(init, str) : - if (os.path.exists(str)) : + 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 : + for line in contentStream: - if (line == "#\n") : + if (line == "#\n"): data['content'] = contentStream.read() break - else : + else: key, sep, value = line.rstrip('\n').partition(' ') data[key] = value self.data = data file.close() - def appendTo(self, father) : + def appendTo(self, father): father.appendChild(self) def appendChild(self, child) : - if not self.saved : + if not self.saved: self.save() file = open(self.path + "/childs", 'a') @@ -92,16 +92,16 @@ class Object : file.close() @staticmethod - def getPermissionByHash(hash, user) : + def getPermissionByHash(hash, user): path = hash[:2] + '/' + hash[2:] try: file = open(path + '/permissions', 'r') - except IOError : + except IOError: print 'cannot open', path - else : - for line in file : + else: + for line in file: name, sep, perm = line.rstrip('\n').partition(' ') - if name == user : + if name == user: return perm return 0 @@ -110,49 +110,49 @@ class Object : file = open(self.path + '/permissions', 'r') - for line in file : + for line in file: name, sep, perm = line.rstrip('\n').partition(' ') - if name == user : + if name == user: return perm return 0 - def setPermission(self, user, permission) : - if not self.saved : + 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 : + if name == user: sys.stdout.write(name + ' ' + str(permission) + '\n') sentinel = True - else : + else: sys.stdout.write(line) - if not sentinel : + if not sentinel: file = open(self.path + '/permissions', 'a') file.write(user + ' ' + str(permission) + '\n') file.close() -class ObjectHandler : +class ObjectHandler: - def __init__(self, user) : + def __init__(self, user): self.user = user - self.root_directory = OBJECT_ROOT + 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) : + 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) : + def getObject(self, hash): directory = self.get_object_directory(hash) if directory is None : logging.error("Object %s doesn't exist" % hash) @@ -160,7 +160,7 @@ class ObjectHandler : return Object(directory) #return a list of hash,permission pairs - def get_child_list(self, hash, user) : + def get_child_list(self, hash, user): directory = self.get_object_directory(hash) if (directory) : result = [] @@ -173,13 +173,13 @@ class ObjectHandler : else : return None - def create_home_node(self) : + def create_home_node(self): pass def get_home_node(self): return self.root_object -if __name__ == '__main__' : +if __name__ == '__main__': x = Object({'parent' : 'toto', 'author' : 'Zaran'}) x.setPermission("test", READ) x.setPermission("toto", READ | MODIFY) -- cgit v1.2.3-70-g09d2