-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_compass.py
102 lines (55 loc) · 2.03 KB
/
export_compass.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
# coding=utf-8
"""Online Scout Manager Interface.
Usage:
export_compass.py [-d] [--term=<term>] <apiid> <token> <outdir> <section>...
export_compass.py (-h | --help)
export_compass.py --version
Options:
<section> Section to export.
<outdir> Output directory for vcard files.
--term=<term> Which OSM term to use [default: current].
-d,--debug Turn on debug output.
-h,--help Show this screen.
--version Show version.
"""
import os.path
import logging
from docopt import docopt
import osm
import csv
from group import Group
from update import MAPPING
from compass import member2compass
from compass import check
from compass import compass_headings
log = logging.getLogger(__name__)
DEF_CACHE = "osm.cache"
DEF_CREDS = "osm.creds"
def _main(osm, auth, sections, outdir, term):
assert os.path.exists(outdir) and os.path.isdir(outdir)
group = Group(osm, auth, MAPPING.keys(), term)
for section in sections:
assert section in group.SECTIONIDS.keys(), \
"section must be in {!r}.".format(group.SECTIONIDS.keys())
for section in sections:
entries = [member2compass(member, section) for
member in group.section_yp_members_without_leaders(section)]
[check(entry, section) for entry in entries]
with open(os.path.join(outdir, section + ".csv"), "w") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=compass_headings)
writer.writeheader()
[writer.writerow(entry) for entry in entries]
if __name__ == '__main__':
args = docopt(__doc__, version='OSM 2.0')
if args['--debug']:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(level=level)
log.debug("Debug On\n")
if args['--term'] in [None, 'current']:
args['--term'] = None
auth = osm.Authorisor(args['<apiid>'], args['<token>'])
auth.load_from_file(open(DEF_CREDS, 'r'))
_main(osm, auth, args['<section>'], args['<outdir>'],
args['--term'])