diff options
Diffstat (limited to 'permission.py')
| -rw-r--r-- | permission.py | 45 |
1 files changed, 3 insertions, 42 deletions
diff --git a/permission.py b/permission.py index 310cef2..fe93928 100644 --- a/permission.py +++ b/permission.py @@ -1,45 +1,6 @@ # -*- 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() +READ = 1 << 0 +MODIFY = 1 << 1 +APPEND = 1 << 2
\ No newline at end of file |
