aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2010-11-20 22:54:01 +0100
committerThibaut Horel <thibaut.horel@gmail.com>2010-11-20 22:54:01 +0100
commit374b2adb8a7ee55c5cad87955add772096f73999 (patch)
treebffb6cb3018e74b7a43b1c4b69b2ecd30ac871ea
parentf3fbfe116584393f5b373d98541c803c695f6ffb (diff)
downloadalias-374b2adb8a7ee55c5cad87955add772096f73999.tar.gz
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.
-rw-r--r--.gitignore1
-rw-r--r--config.ini.sample6
-rw-r--r--config.py15
-rw-r--r--object.py74
-rw-r--r--server.py28
-rw-r--r--user.py27
6 files changed, 95 insertions, 56 deletions
diff --git a/.gitignore b/.gitignore
index d8a5dcf..ad519ae 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@
.project
.pydevproject
.settings/
+config.ini
diff --git a/config.ini.sample b/config.ini.sample
new file mode 100644
index 0000000..b25b281
--- /dev/null
+++ b/config.ini.sample
@@ -0,0 +1,6 @@
+[component]
+name = object.alias.fr.nf
+secret = xxxx
+host = alias.fr.nf
+port = 5347
+root = users/
diff --git a/config.py b/config.py
new file mode 100644
index 0000000..d50d4d7
--- /dev/null
+++ b/config.py
@@ -0,0 +1,15 @@
+import ConfigParser
+
+filename = None
+
+class AliasConfigParser(ConfigParser.RawConfigParser):
+
+ def read(self, filenames):
+ ConfigParser.RawConfigParser.read(self, filenames)
+ self.name = self.get("component", "name")
+ self.root = self.get("component", "root")
+ self.host = self.get("component", "host")
+ self.secret = self.get("component", "secret")
+ self.port = self.getint("component", "port")
+
+config = AliasConfigParser()
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)
diff --git a/server.py b/server.py
index 187aec7..0395799 100644
--- a/server.py
+++ b/server.py
@@ -1,11 +1,11 @@
import logging
-import ConfigParser
from argparse import ArgumentParser
from sleekxmpp.componentxmpp import ComponentXMPP
from sleekxmpp.xmlstream.xmlstream import XMLStream
from user import UserHandler
+from config import filename, config
class ObjectComponent(ComponentXMPP):
@@ -58,20 +58,36 @@ if __name__ == '__main__':
help = 'Name the component will have')
commandline.add_argument('-r', '--root',
help = 'Root directory of the user files')
+ commandline.add_argument('-c', '--config',
+ help = 'Name of the config file to use')
commandline.add_argument('-d', '--debug',
help = 'Set log level to DEBUG',
action = 'store_const',
const = logging.DEBUG,
default = logging.INFO)
- commandline.add_argument('host',
+ commandline.add_argument('-o', '--host',
help = 'Host to connect to')
+
args = commandline.parse_args()
- logging.basicConfig(level = args.debug)
- component = ObjectComponent(args.name, args.secret,
- args.host, args.port, args.root)
+
+ if args.config is None:
+ logging.basicConfig(level = args.debug)
+ config.name = args.name
+ config.port = args.port
+ config.secret = args.secret
+ config.root = args.root
+ config.host = args.host
+ else:
+ filename = args.config
+ logging.basicConfig(level = args.debug)
+ config.read(filename)
+
+ component = ObjectComponent(config.name, config.secret,
+ config.host, config.port,
+ config.root)
if component.connect():
- logging.info('Component {} connected'.format(args.name))
+ logging.info('Component {} connected'.format(component.boundjid))
component.process(False)
else :
print "Couldn't connect"
diff --git a/user.py b/user.py
index c1d8188..0be0070 100644
--- a/user.py
+++ b/user.py
@@ -4,28 +4,29 @@ import shutil
import object
import hashlib
-class UserHandler :
- def __init__(self,root):
+class UserHandler:
+
+ def __init__(self, root):
self.root = root
- def register(self,name):
- userDir = self.root+'/'+name
+ def register(self, name):
+ 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)
-
- def unregister(self,name):
- shutil.rmtree(self.root+'/'+name)
-
+ def registered(self, name):
+ return os.path.exists(self.root + '/' + name)
+
+ def unregister(self, name):
+ shutil.rmtree(self.root + '/' + name)
+
def getUserList(self):
return os.listdir(self.root)
class User :
- def __init__(self, name) :
+
+ def __init__(self, name):
self.name = name
- self.rootObject = hashlib.sha1( name ).hexdigest()
- \ No newline at end of file
+ self.rootObject = hashlib.sha1(name).hexdigest()