-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgc_cli.py
256 lines (209 loc) · 7.71 KB
/
gc_cli.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""Gateway Clients CLI"""
import argparse
import logging
import phonenumbers
from phonenumbers import carrier, geocoder
from playhouse.shortcuts import model_to_dict
from src.models import GatewayClients
from mccmnc import find_matches, update
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger("[GC CLI]")
def get_plmn(country_code, operator, refresh=False):
"""
Get PLMN (Public Land Mobile Network) information based on country code and operator.
Args:
country_code (int): The country code.
operator (str): The operator name.
refresh (bool, optional): Whether to force a refresh of PLMN data. Defaults to False.
Returns:
dict: PLMN information.
"""
if refresh:
update()
network = operator.split()[0].lower()
try:
plmn = find_matches(user_cc=country_code, user_network=network)
except FileNotFoundError:
update()
plmn = find_matches(user_cc=country_code, user_network=network)
return list(plmn.keys())[0]
def get_operator_information(msisdn):
"""
Get country and operator information from MSISDN
(Mobile Station International Subscriber Directory Number).
Args:
msisdn (str): The MSISDN of the client.
Returns:
tuple: A tuple containing country, operator, and operator code.
- country (str): The country name.
- operator (str): The operator name.
- operator_code (str): The PLMN (Public Land Mobile Network) code.
"""
try:
number = phonenumbers.parse(msisdn, None)
country = geocoder.description_for_number(number, "en")
country_code = number.country_code
operator = carrier.name_for_number(number, "en") or "N/A"
operator_code = get_plmn(country_code, operator) if operator != "N/A" else "N/A"
return country, operator, operator_code
# pylint: disable=W0718
except Exception:
logger.exception("Failed to parse MSISDN.")
return None, None, None
def create_client(msisdn, protocols):
"""
Create a new gateway client.
Args:
msisdn (str): The MSISDN of the client.
protocols (str): The protocol(s) of the client (comma separated).
Returns:
None
"""
try:
country, operator, operator_code = get_operator_information(msisdn)
if not all((country, operator, operator_code)):
logger.error(
"Failed to retrieve complete operator information for the provided MSISDN."
)
if not country:
logger.error("Country information is missing.")
if not operator:
logger.error("Operator information is missing.")
if not operator_code:
logger.error("Operator code information is missing.")
return
# pylint: disable=W0212,E1101
with GatewayClients._meta.database.atomic():
client = GatewayClients.create(
msisdn=msisdn,
country=country,
operator=operator,
operator_code=operator_code,
protocols=protocols,
)
logger.info("Client created successfully.")
print("-" * 60)
print(f"{'Client Details':=^60}")
for key, value in model_to_dict(client).items():
print(f"{key.upper()}: {value}")
# pylint: disable=W0718
except Exception:
logger.error("Failed to create client.", exc_info=True)
def view_client(msisdn=None):
"""
View gateway client(s).
Args:
msisdn (str, optional): The MSISDN of the client to view. If None,
all clients will be displayed.
Returns:
None
"""
# pylint: disable=W0212,E1101
with GatewayClients._meta.database.atomic():
try:
query = GatewayClients.select().dicts()
if msisdn:
query = query.where(GatewayClients.msisdn == msisdn).dicts()
if not query:
logger.info("No clients found.")
return
print(f"{'Clients':=^60}")
for test in query:
print("-" * 60)
for key, value in test.items():
print(f"{key.upper()}: {value}")
# pylint: disable=W0718
except Exception:
logger.error("Failed to get client(s).", exc_info=True)
def update_client(msisdn, country=None, operator=None, protocols=None):
"""
Update an existing gateway client.
Args:
msisdn (str): The MSISDN of the client to update.
country (str, optional): The new country value for the client.
operator (str, optional): The new operator value for the client.
protocols (str, optional): The new protocol(s) value for the client (comma separated).
Returns:
None
"""
# pylint: disable=W0212,E1101
with GatewayClients._meta.database.atomic():
try:
client = GatewayClients.get_or_none(msisdn=msisdn)
if client:
if country:
client.country = country
if operator:
client.operator = operator
if protocols:
client.protocols = protocols
client.save()
logger.info("Client updated successfully.")
else:
logger.info("No client found with MSISDN: %s", msisdn)
# pylint: disable=W0718
except Exception:
logger.error("Failed to update record.", exc_info=True)
def delete_client(msisdn):
"""
Delete an existing gateway client.
Args:
msisdn (str): The MSISDN of the client to delete.
"""
with GatewayClients._meta.database.atomic():
try:
client = GatewayClients.get_or_none(msisdn=msisdn)
if client:
client.delete_instance()
logger.info("Client deleted successfully.")
else:
logger.info("No client found with MSISDN: %s", msisdn)
# pylint: disable=W0718
except Exception:
logger.exception("Failed to delete record.")
def main():
"""
Parse command line arguments and execute corresponding actions.
"""
parser = argparse.ArgumentParser(description="Gateway Clients CLI")
parser.add_argument(
"action",
choices=["create", "view", "update", "delete"],
help="Action to perform",
)
parser.add_argument("--msisdn", help="MSISDN of the client")
parser.add_argument("--country", help="Country of the client")
parser.add_argument("--operator", help="Operator of the client")
parser.add_argument(
"--protocols", help="Protocol(s) of the client (comma separated)"
)
args = parser.parse_args()
if not args.action:
parser.error("Please specify an action to perform (create, view, update)")
if args.action == "create":
if not all([args.msisdn, args.protocols]):
parser.error(
"For 'create' action, all arguments are required: --msisdn, --protocols"
)
elif args.action == "update":
if not args.msisdn:
parser.error(f"For '{args.action}' action, --msisdn is required")
if args.action == "create":
create_client(args.msisdn, args.protocols)
elif args.action == "view":
view_client(args.msisdn)
elif args.action == "update":
update_client(
args.msisdn,
args.country,
args.operator,
args.protocols,
)
elif args.action == "delete":
delete_client(args.msisdn)
if __name__ == "__main__":
main()