-
Notifications
You must be signed in to change notification settings - Fork 10
/
generate_lcd.py
executable file
·132 lines (107 loc) · 4.86 KB
/
generate_lcd.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
#!/usr/bin/env python3
# Script to generate LCD tables for the Nortel Millennium Payphone
#
# www.github.com/hharte/mm_manager
#
# (c) 2020-2022, Howard M. Harte
#
# USA: https://nationalnanpa.com/reports/reports_cocodes_assign.html
# Canada: http://www.cnac.ca/co_codes/co_code_status.htm
#
# All NPA-NXX in the USA: wget https://nationalnanpa.com/nanp1/allutlzd.zip
# All NPA-NXX in Canada: wget http://www.cnac.ca/data/COCodeStatus_ALL.zip
#
# usage: generate_lcd.py [-h] [--debug DEBUG] [--country COUNTRY] [--state STATE]
# [--npa NPA] [--datafile DATAFILE]
# --ratecenters RATECENTERS [RATECENTERS ...]
#
# For example, to generate LCD tables for the San Jose, California, USA rate center:
# python3 generate_lcd.py --state CA --npa 408 --ratecenters "SNJS NORTH" "SNJS WEST" "SNJS SOUTH" CAMPBELL SARATOGA SUNNYVALE "LOS GATOS"
#
# To generate LCD tables for Ottawa, Canada:
# python3 generate_lcd.py --country CA --npa 613 --ratecenters Ottawa-Hull
import argparse
import pandas
from mm_lcd import generate_lcd_tables
parser = argparse.ArgumentParser()
parser.add_argument("--debug", help="display debug output")
parser.add_argument("--country", help="Country 'US' or 'CA'", default='US')
parser.add_argument("--state", help="State (only for Country 'US')", default='CA')
parser.add_argument("--npa", help="Terminal's NPA", default='408')
parser.add_argument("--datafile", help="NPA-NXX source data file")
parser.add_argument("--ratecenters", nargs='+', help='List of rate centers', required=True)
args = parser.parse_args()
print("LCD Table Generator for the Nortel Millennium Payphone")
print("(c) 2020-2022, Howard M. Harte\n")
print("Terminal NPA: " + args.npa)
if args.country == 'US':
source_filename = 'allutlzd.txt'
if args.datafile: source_filename = args.datafile
print("Reading " + args.country + " data from " + source_filename)
try:
if ".csv" in source_filename: # Import as CSV
df = pandas.read_csv(source_filename, dtype={'NPA-NXX': object}, engine='python')
else: # Import as TSV
df = pandas.read_csv(source_filename, dtype={'NPA-NXX': object}, sep=' *\t', engine='python')
except OSError:
print("Error: Cannot read NPA-NXX data file.")
print("Please download https://nationalnanpa.com/nanp1/allutlzd.zip and unzip it into the current directory.")
exit()
df["NPA-NXX"].fillna("0", inplace = True)
df["State"].fillna("Unknown", inplace = True)
df["Use"].fillna("Unknown", inplace = True)
df["RateCenter"].fillna("None", inplace = True)
# Split NPA-NXX into separate columns in dataframe.
df[['NPA','NXX']] = df['NPA-NXX'].str.split("-",expand=True)
# Filter out by state.
sc = df[df['State'].str.match(args.state)]
else:
source_filename = 'COCodeStatus_ALL.csv'
if args.datafile: source_filename = args.datafile
print("Reading " + args.country + " data from " + source_filename)
try:
df = pandas.read_csv(source_filename, sep=',', dtype={'NPA': object, 'CO Code (NXX)': object}, engine='python')
except OSError:
print("Error: Cannot read NPA-NXX data file.")
print("Please download http://www.cnac.ca/data/COCodeStatus_ALL.zip and unzip it into the current directory.")
exit()
df = df.rename(columns={'CO Code (NXX)': 'NXX', 'Status': 'Use', "Exchange Area": 'RateCenter'})
df["NPA"].fillna("0", inplace = True)
df["NXX"].fillna("0", inplace = True)
df["RateCenter"].fillna("None", inplace = True)
sc = df
if args.debug: print(sc[['NPA', 'NXX', 'RateCenter']])
def prepRateForRegex(r):
return r'^' + r + '$'
rate_centers = map(prepRateForRegex,args.ratecenters)
rc = sc[sc['RateCenter'].str.match('|'.join(rate_centers))]
if args.debug: print(rc[['NPA', 'NXX', 'RateCenter']])
# Get list of unique NPAs that have NXX in matching rate centers.
npas = rc['NPA'].unique()
lcd_npas = list()
# Convert list of NPAs to array, starting with Terminal's own NPA.
for i, npa in enumerate(npas):
if npa == args.npa:
lcd_npas.insert(0, npa)
else:
lcd_npas.append(npa)
print("Rate Centers: " + str(args.ratecenters))
print("NPAs for these rate centers: " + str(lcd_npas))
npa_dict = {}
df1 = df[['NPA', 'NXX', 'Use', 'RateCenter']]
for key, row in df1.iterrows():
npanxx = str(row['NPA']) + '-' + row['NXX']
if (str(row['Use']) == "AS") | (str(row['Use']) == "In Service"): # NXX is Assigned
# Either local or LD based on rate center.
if row['RateCenter'] in args.ratecenters:
flag = 0 # local
else:
flag = 2 # LD
else: # UA NXX is "Unassigned"
flag = 3 # UA is invalid
npa_dict[npanxx] = flag
status = generate_lcd_tables(npa_dict, lcd_npas, args.npa)
if status == 0:
print("Successfully completed generating LCD tables.")
else:
print("LCD table limit exceeded for one or more MTR versions.")