aboutsummaryrefslogtreecommitdiffstats
path: root/permission.py
diff options
context:
space:
mode:
authorguillaume <guillaume@localhost.localdomain>2010-08-10 16:27:37 -0400
committerguillaume <guillaume@localhost.localdomain>2010-08-10 16:27:37 -0400
commit724c296e54714a949ce79c5056229e00fb7c15c2 (patch)
treed505860e67513dcb76141ebecb86a50ac00ad70d /permission.py
parent9e8fd4662da1688cc0c8fec2e9194b1606f30295 (diff)
downloadalias-724c296e54714a949ce79c5056229e00fb7c15c2.tar.gz
First code commit, with very few things.
* Beginning of object class * Beginning of permission class
Diffstat (limited to 'permission.py')
-rwxr-xr-xpermission.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/permission.py b/permission.py
new file mode 100755
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