aboutsummaryrefslogtreecommitdiffstats
path: root/permission.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2010-07-30 09:29:24 +0200
committerThibaut Horel <thibaut.horel@gmail.com>2010-07-30 09:29:24 +0200
commit7945c0daad7d2e3b8380c18b7d080e54e0a948ed (patch)
treeebe1932b3d2aeb979570c977fc9947f8891336cf /permission.py
downloadalias-7945c0daad7d2e3b8380c18b7d080e54e0a948ed.tar.gz
Project creation.
Diffstat (limited to 'permission.py')
-rw-r--r--permission.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/permission.py b/permission.py
new file mode 100644
index 0000000..310cef2
--- /dev/null
+++ b/permission.py
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+#TODO : move the globals to a config file (use the config file parser module)
+PERM_LIST=['READ','MODIFY','APPEND'] #just add a string here to define a new permission
+PERMS = len(PERM_LIST)
+MAX = (1<<PERMS)-1
+
+class PermissionError(Exception) :
+ def __init__(self,error_string) :
+ self.error = error_string
+
+ def __str__(self) :
+ str(self.error)
+
+class Permission :
+
+ def __init__(self,init) :
+ if isinstance(init,int) :
+ if (init > MAX) or (init < 0) :
+ raise PermissionError('Permission int not in correct range')
+ result = []
+ for shift in range(PERMS-1,-1,-1) :
+ if init>>shift :
+ result.append(PERM_LIST[shift])
+ init = init-(1<<shift)
+ self.perm_list = result
+ elif isinstance(init,list) :
+ if (set(init) <= set(PERM_LIST)) :
+ self.perm_list = init
+ else :
+ raise PermissionError('Undefined permission')
+
+ def __str__(self) :
+ return str(self.perm_list)
+
+ def toInt(self) :
+ result = 0
+ for i,perm in enumerate(PERM_LIST) :
+ if perm in self.perm_list :
+ result += 1<<i
+ return result
+
+if __name__== '__main__' :
+ for i in range(MAX+1) :
+ print i, Permission(i), Permission(i).toInt()
+ \ No newline at end of file