diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2010-10-25 00:35:36 +0200 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2010-10-25 00:35:36 +0200 |
| commit | 9479e2e8fb7624ec89783badca6e4e510d71680f (patch) | |
| tree | 81cd8ff3f3ea145392abf827d0133f933eea4dfe /permission.py | |
| parent | 18e3610d219300c8f8e19ab137f91fff837a95c8 (diff) | |
| download | alias-9479e2e8fb7624ec89783badca6e4e510d71680f.tar.gz | |
Simplification of permission system
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 |
