-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_users.py
More file actions
152 lines (129 loc) · 5.16 KB
/
generate_users.py
File metadata and controls
152 lines (129 loc) · 5.16 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import csv
import logging
import os
import subprocess
logging.basicConfig(
filename='auto_run_generate_users_script.log',
filemode='a',
format='%(asctime)s - %(levelname)s - %(filename)s - %(message)s',
level=logging.INFO,
)
logger = logging.getLogger(__name__)
input_users_dirpath="input-users"
output_users_dirpath="output-users"
users_teplate = """
<include>
{users}
</include>
""".strip()
intercom_template = """
<user id="{extension}">
<params>
<param name="password" value="{password}"/>
</params>
<variables>
<variable name="toll_allow" value="domestic,international,local"/>
<variable name="accountcode" value="{extension}"/>
<variable name="user_context" value="intercoms-main"/>
<variable name="effective_caller_id_name" value="{extension}"/>
<variable name="effective_caller_id_number" value="{extension}"/>
<variable name="outbound_caller_id_name" value="$${{outbound_caller_name}}"/>
<variable name="outbound_caller_id_number" value="$${{outbound_caller_id}}"/>
<variable name="sip-force-expires" value="30"/>
<variable name="X-Doma-AI-Type" value="intercom"/>
</variables>
</user>
"""
apartment_template = """
<user id="{extension}">
<params>
<param name="password" value="{password}"/>
<param name="vm-password" value="{extension}"/>
</params>
<variables>
<variable name="absolute_codec_string" value="OPUS,G722,PCMU,PCMA,VP8"/>
<variable name="toll_allow" value="domestic,international,local"/>
<variable name="accountcode" value="{extension}"/>
<variable name="user_context" value="rooms-main"/>
<variable name="effective_caller_id_name" value="room house 1 apart 1"/>
<variable name="effective_caller_id_number" value="room-1-1"/>
<variable name="outbound_caller_id_name" value="$${{outbound_caller_name}}"/>
<variable name="outbound_caller_id_number" value="$${{outbound_caller_id}}"/>
<variable name="sip-force-expires" value="30"/>
<variable name="X-Doma-AI-Type" value="apartment"/>
</variables>
</user>
"""
def fs_cli(cmd):
password = os.getenv('SOCKET_PASSWORD').strip()
try:
subprocess.run([
'docker', 'compose', 'exec', '-Td', 'sip-server',
'/usr/local/freeswitch/bin/fs_cli', '-rRS', '--password', password, '-x',
cmd
])
except Exception as e:
logger.error('Exception while calling fs_cli: {e}')
def reload_xml():
logger.info('Reloading XML...')
fs_cli("reloadxml")
def transliterate(s):
replacements = {
'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'G', 'Д': 'D', 'Е': 'E', 'Ё': 'Yo',
'Ж': 'Zh', 'З': 'Z', 'И': 'I', 'Й': 'Y', 'К': 'K', 'Л': 'L', 'М': 'M',
'Н': 'N', 'О': 'O', 'П': 'P', 'Р': 'R', 'С': 'S', 'Т': 'T', 'У': 'U',
'Ф': 'F', 'Х': 'Kh', 'Ц': 'Ts', 'Ч': 'Ch', 'Ш': 'Sh', 'Щ': 'Shch',
'Ъ': '', 'Ы': 'Y', 'Ь': '', 'Э': 'E', 'Ю': 'Yu', 'Я': 'Ya',
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e', 'ё': 'yo',
'ж': 'zh', 'з': 'z', 'и': 'i', 'й': 'y', 'к': 'k', 'л': 'l', 'м': 'm',
'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u',
'ф': 'f', 'х': 'kh', 'ц': 'ts', 'ч': 'ch', 'ш': 'sh', 'щ': 'shch',
'ъ': '', 'ы': 'y', 'ь': '', 'э': 'e', 'ю': 'yu', 'я': 'ya',
' .,;:-!?*&@#$%^()[]{}|\\\'"<>': '_',
}
for replace_from, replace_to in replacements.items():
for c in replace_from:
s = s.replace(c, replace_to)
# Original script had a bug, which adds a _ to the end of the string
return s + '_'
def import_dotenv():
if not os.path.exists('.env'):
return
logger.info('Importing .env...')
with open('.env') as e:
for line in e:
try:
key, *rest = line.split('=')
os.environ[key.strip()] = '='.join(rest).strip()
except: pass
def read_input_users():
if not os.path.isdir(input_users_dirpath):
return {}
logger.info('Reading input users...')
result = {}
for group in os.listdir(input_users_dirpath):
fname = os.path.join(input_users_dirpath, group)
output_fname = transliterate(os.path.splitext(group)[0])
with open(fname) as f:
reader = csv.DictReader(f, delimiter=';')
result[output_fname] = list(reader)
return result
def generate_configs(users):
logger.info('Generating configs...')
for fname, group in users.items():
group_users = []
for user in group:
if user['kvartira'] == 'x':
user_xml = intercom_template.format(**user)
else:
user_xml = apartment_template.format(**user)
group_users.append(user_xml)
with open(os.path.join(output_users_dirpath, fname + '.xml'), 'w') as f:
f.write(users_teplate.format(users='\n'.join(group_users)))
def generate_users():
import_dotenv()
input_users = read_input_users()
generate_configs(input_users)
reload_xml()
if __name__ == '__main__':
generate_users()