-
Notifications
You must be signed in to change notification settings - Fork 1
/
countries.py
executable file
·80 lines (62 loc) · 2.17 KB
/
countries.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
#!/usr/bin/env python
import sys
import textwrap
from argparse import ArgumentParser
import DXEntity
def clist():
dxcc = DXEntity.DXCC()
countries = dxcc.entities
for _country in sorted(countries):
print(_country)
def get_prefix(prefix):
# pylint: disable=no-member
dxcc = DXEntity.DXCC()
prefix = prefix.upper()
match, result = dxcc.get_prefix(prefix)
print(f"Call prefix: {match} > {result.prefix} = {result.country} - Continent: "
f"{result.continent}, CQZone: "
f"{result.cqzone}, ITUZone: {result.ituzone}")
def check(ctry):
dxcc = DXEntity.DXCC()
ctry = ctry.upper()
countries = {k.upper(): k for k in dxcc.entities}
if ctry not in countries:
raise KeyError(f'The country "{ctry}" cannot be found.')
print(f'Country "{countries[ctry]}" found.')
def country(ctry):
dxcc = DXEntity.DXCC()
wrapper = textwrap.TextWrapper()
wrapper.subsequent_indent = wrapper.initial_indent = " > "
ctry = ctry.upper()
countries = {k.upper(): k for k in dxcc.entities}
if ctry not in countries:
raise KeyError(f'The country "{countries[ctry]}" cannot be found.')
ctry = countries[ctry]
prefixes = ', '.join(dxcc.get_entity(ctry))
print('\n'.join(wrapper.wrap(prefixes)))
def main():
parser = ArgumentParser(description="DXCC entities lookup")
x_grp = parser.add_mutually_exclusive_group(required=True)
x_grp.add_argument("-l", "--list", action="store_true", default=False,
help="List all the countries")
x_grp.add_argument("-c", "--country", type=str,
help="Find the country from a callsign")
x_grp.add_argument("-C", "--check", type=str,
help="Check if the country from a exists")
x_grp.add_argument("-p", "--prefix", type=str,
help="List all the prefixes for a given country")
opts = parser.parse_args()
try:
if opts.list:
clist()
elif opts.country:
country(opts.country)
elif opts.check:
check(opts.check)
elif opts.prefix:
get_prefix(opts.prefix)
except KeyError as err:
print(f"Error: {err}", file=sys.stderr)
raise SystemExit('Argument Error') from None
if __name__ == "__main__":
main()