mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Function documentation
This commit is contained in:
parent
49447517e3
commit
e34f6e0b4f
2 changed files with 87 additions and 10 deletions
|
@ -9,6 +9,14 @@ import getpass
|
||||||
|
|
||||||
|
|
||||||
def colorize(astr, color):
|
def colorize(astr, color):
|
||||||
|
"""
|
||||||
|
Print with style ;)
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
astr -- String to colorize
|
||||||
|
color -- Name of the color
|
||||||
|
|
||||||
|
"""
|
||||||
color_dict = {
|
color_dict = {
|
||||||
'red' : '31',
|
'red' : '31',
|
||||||
'green' : '32',
|
'green' : '32',
|
||||||
|
@ -18,10 +26,19 @@ def colorize(astr, color):
|
||||||
}
|
}
|
||||||
return "\033["+ color_dict[color] +"m\033[1m" + astr + "\033[m"
|
return "\033["+ color_dict[color] +"m\033[1m" + astr + "\033[m"
|
||||||
|
|
||||||
|
|
||||||
def win_msg(astr):
|
def win_msg(astr):
|
||||||
|
"""
|
||||||
|
Display a success message if isatty
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
astr -- Win message to display
|
||||||
|
|
||||||
|
"""
|
||||||
if os.isatty(1):
|
if os.isatty(1):
|
||||||
print('\n' + colorize(_("Success: "), 'green') + astr + '\n')
|
print('\n' + colorize(_("Success: "), 'green') + astr + '\n')
|
||||||
|
|
||||||
|
|
||||||
def str_to_func(astr):
|
def str_to_func(astr):
|
||||||
"""
|
"""
|
||||||
Call a function from a string name
|
Call a function from a string name
|
||||||
|
@ -49,7 +66,14 @@ def str_to_func(astr):
|
||||||
|
|
||||||
|
|
||||||
class YunoHostError(Exception):
|
class YunoHostError(Exception):
|
||||||
""" Custom exception """
|
"""
|
||||||
|
Custom exception
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
code -- Integer error code
|
||||||
|
message -- Error message to display
|
||||||
|
|
||||||
|
"""
|
||||||
def __init__(self, code, message):
|
def __init__(self, code, message):
|
||||||
code_dict = {
|
code_dict = {
|
||||||
1 : _('Fail'),
|
1 : _('Fail'),
|
||||||
|
@ -76,8 +100,12 @@ class YunoHostLDAP:
|
||||||
""" Specific LDAP functions for YunoHost """
|
""" Specific LDAP functions for YunoHost """
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
""" Connect to LDAP base """
|
"""
|
||||||
|
Connect to LDAP base
|
||||||
|
|
||||||
|
Initialize to localhost, base yunohost.org, prompt for password
|
||||||
|
|
||||||
|
"""
|
||||||
self.conn = ldap.initialize('ldap://localhost:389')
|
self.conn = ldap.initialize('ldap://localhost:389')
|
||||||
self.base = 'dc=yunohost,dc=org'
|
self.base = 'dc=yunohost,dc=org'
|
||||||
self.pwd = getpass.getpass(_('LDAP Admin Password: '))
|
self.pwd = getpass.getpass(_('LDAP Admin Password: '))
|
||||||
|
@ -86,9 +114,15 @@ class YunoHostLDAP:
|
||||||
except ldap.INVALID_CREDENTIALS:
|
except ldap.INVALID_CREDENTIALS:
|
||||||
raise YunoHostError(13, _('Invalid credentials'))
|
raise YunoHostError(13, _('Invalid credentials'))
|
||||||
|
|
||||||
def disconnect(self):
|
|
||||||
""" Unbind from LDAP """
|
|
||||||
|
|
||||||
|
def disconnect(self):
|
||||||
|
"""
|
||||||
|
Unbind from LDAP
|
||||||
|
|
||||||
|
Returns
|
||||||
|
Boolean | YunoHostError
|
||||||
|
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
self.conn.unbind_s()
|
self.conn.unbind_s()
|
||||||
except:
|
except:
|
||||||
|
@ -96,9 +130,20 @@ class YunoHostLDAP:
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def search(self, base=None, filter='(objectClass=*)', attrs=['dn']):
|
|
||||||
""" Search in LDAP base """
|
|
||||||
|
|
||||||
|
def search(self, base=None, filter='(objectClass=*)', attrs=['dn']):
|
||||||
|
"""
|
||||||
|
Search in LDAP base
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
base -- Base to search into
|
||||||
|
filter -- LDAP filter
|
||||||
|
attrs -- Array of attributes to fetch
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Boolean | Dict
|
||||||
|
|
||||||
|
"""
|
||||||
if not base:
|
if not base:
|
||||||
base = self.base
|
base = self.base
|
||||||
|
|
||||||
|
@ -117,9 +162,19 @@ class YunoHostLDAP:
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def add(self, rdn, attr_dict):
|
|
||||||
""" Add LDAP entry """
|
|
||||||
|
|
||||||
|
def add(self, rdn, attr_dict):
|
||||||
|
"""
|
||||||
|
Add LDAP entry
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
rdn -- DN without domain
|
||||||
|
attr_dict -- Dictionnary of attributes/values to add
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Boolean | YunoHostError
|
||||||
|
|
||||||
|
"""
|
||||||
dn = rdn + ',' + self.base
|
dn = rdn + ',' + self.base
|
||||||
ldif = modlist.addModlist(attr_dict)
|
ldif = modlist.addModlist(attr_dict)
|
||||||
|
|
||||||
|
@ -132,6 +187,16 @@ class YunoHostLDAP:
|
||||||
|
|
||||||
|
|
||||||
def validate(self, regex_dict):
|
def validate(self, regex_dict):
|
||||||
|
"""
|
||||||
|
Validate attributes with a pattern
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
regex_dict -- Dictionnary of values/pattern to check
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Boolean | YunoHostError
|
||||||
|
|
||||||
|
"""
|
||||||
for attr, pattern in regex_dict.items():
|
for attr, pattern in regex_dict.items():
|
||||||
if re.match(pattern, attr):
|
if re.match(pattern, attr):
|
||||||
continue
|
continue
|
||||||
|
@ -139,11 +204,21 @@ class YunoHostLDAP:
|
||||||
raise YunoHostError(22, _('Invalid attribute') + ' ' + attr)
|
raise YunoHostError(22, _('Invalid attribute') + ' ' + attr)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def validate_uniqueness(self, value_dict):
|
def validate_uniqueness(self, value_dict):
|
||||||
|
"""
|
||||||
|
Check uniqueness of values
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
value_dict -- Dictionnary of attributes/values to check
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Boolean | YunoHostError
|
||||||
|
|
||||||
|
"""
|
||||||
for attr, value in value_dict.items():
|
for attr, value in value_dict.items():
|
||||||
if not self.search(filter=attr + '=' + value):
|
if not self.search(filter=attr + '=' + value):
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(17, _('Attribute already exists') + ' "' + attr + '=' + value + '"')
|
raise YunoHostError(17, _('Attribute already exists') + ' "' + attr + '=' + value + '"')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,14 @@ import string
|
||||||
import getpass
|
import getpass
|
||||||
from yunohost import YunoHostError, YunoHostLDAP, win_msg
|
from yunohost import YunoHostError, YunoHostLDAP, win_msg
|
||||||
|
|
||||||
|
|
||||||
# Initialize LDAP
|
# Initialize LDAP
|
||||||
yldap = YunoHostLDAP()
|
yldap = YunoHostLDAP()
|
||||||
|
|
||||||
|
|
||||||
def user_list(args): # TODO : fix
|
def user_list(args): # TODO : fix
|
||||||
result = yldap.search()
|
result = yldap.search()
|
||||||
print(result)
|
#print(result)
|
||||||
|
|
||||||
|
|
||||||
def user_add(args):
|
def user_add(args):
|
||||||
|
|
Loading…
Add table
Reference in a new issue