-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
109 lines (93 loc) · 2.67 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from moira_query import moira_query
"""
Several utilities (i.e. helper functions) for use
in the Flask Moira API
"""
def get_ace_use(ace_type, name):
res = moira_query('get_ace_use', ace_type, name)
return [
{
'type': entry['use_type'].lower(),
'name': entry['use_name'],
}
for entry in res
]
def conditional_recursive_type(ace_type, recursive):
if recursive:
return 'R' + ace_type
else:
return ace_type
"""
Parse a GET parameter as bool
"""
def parse_bool(param):
if isinstance(param, bool):
return param
if param == 1 or param == '1' or param.lower() == 'true':
return True
elif param == 0 or param == '0' or param.lower() == 'false':
return False
else:
raise Exception(f'invalid boolean: {param}')
"""
Parses a mailing list entry dict
into the names we want for our API
"""
def parse_list_dict(entry):
return {
'name': entry['list_name'],
'active': parse_bool(entry['active']),
'public': parse_bool(entry['publicflg']),
'hidden': parse_bool(entry['hidden']),
'is_mailing_list': parse_bool(entry['maillist']),
'is_afs_group': parse_bool(entry['grouplist']),
}
"""
Parse a parameter as a bool-like "1" or "0"
for use in Moira queries
"""
def parse_bool_for_moira(param):
if param == True or param == 1 or param == '1':
return '1'
elif param == False or param == 0 or param == '0':
return '0'
else:
raise Exception(f'invalid boolean: {param}')
"""
Converts from REST API format of member type
to the format Moira wants
(user to USER, email to STRING, etc)
"""
def serialize_member_type(member_type):
if member_type == 'email':
member_type = 'string'
return member_type.upper()
"""
Converts to Moira format to format REST API
defines (USER to user, STRING to email, etc)
"""
def parse_member_type(member_type):
if member_type == 'STRING':
member_type = 'EMAIL'
return member_type.lower()
"""
From the output of get_list_info, prepare it as input
for update_list. If this is passed without modification
to update_list, it should be a no-op.
"""
def create_update_list_input(list_name):
# Get current attributes
attributes = moira_query('get_list_info', list_name)[0]
# Delete modified attributes
del attributes['modtime']
del attributes['modby']
del attributes['modwith']
# Set name format
del attributes['name']
# Order matters! We want name and new name at the beginning
input = {}
input['name'] = list_name
input['newname'] = list_name
for k, v in attributes.items():
input[k] = v
return input