-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathoverridepositions.py
More file actions
129 lines (104 loc) · 5.23 KB
/
overridepositions.py
File metadata and controls
129 lines (104 loc) · 5.23 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
from geocode import myclub
import googlemaps
import re
from googleapiclient import discovery
from pprint import pprint
from simpleclub import Club
def normalizespaces(s):
# Removes extra spaces; turns non-breaking spaces into spaces
return re.sub(r'\s+',' ',s.strip().replace('\xa0',' '))
def overrideClubPositions(clubs, overridefile, apikey, log=False,
ignorefields=[], donotlog=[], suspendedClubs=[],
createnewclubs=False):
""" Updates 'clubs' with information from the override (or workingalignment) spreadsheet
Note: the same apikey is used for Google Maps and the Google spreadsheet """
from geocode import myclub
gmaps = googlemaps.Client(key=apikey)
myclub.setgmaps(gmaps)
# Get the data from the spreadsheet
# We may be passed a whole URL or just the key
if '/' in overridefile:
# Have a whole URL; get the key
overridefile = re.search(r'/spreadsheets/d/([a-zA-Z0-9-_]+)', overridefile).groups()[0]
# Connect to the spreadsheet and get the values
service = discovery.build('sheets', 'v4', developerKey=apikey)
request = service.spreadsheets().values().get(spreadsheetId=overridefile, range='a1:zz999')
values = request.execute()['values']
keys = [''.join(k.lower().split()) for k in values[0]] # Ensure lowercase, one word
# Make sure all clubs have all the keys in our override, plus "touchedby"
requiredkeys = list(keys)
requiredkeys.append('touchedby')
# We may be creating new keys; the default for each is None.
for c in clubs:
for k in requiredkeys:
club = clubs[c]
if k not in club.__dict__:
club.__dict__[k] = None
# Now, work through the overrides
linenum = 0
for line in values[1:]:
linenum += 1
# Convert the values to a dictionary, based on the keys.
row = {}
for i, key in enumerate(keys):
try:
# Normalize whitespace, including non-breaking spaces
row[key] = normalizespaces(line[i])
except IndexError:
row[key] = ''
# Now, process the data.
clubnumber = row['clubnumber'].strip()
if not clubnumber:
clubnumber = '%s' % (0 - linenum)
row['clubnumber'] = clubnumber
if clubnumber not in clubs and createnewclubs:
club = Club([normalizespaces(f) for f in line], fieldnames=keys, fillall=True)
club.clubnumber = clubnumber
clubs[clubnumber] = club
if log and clubnumber not in suspendedClubs:
print(("%8s/%s *** New or unlisted Club ***" % (club.clubnumber, club.clubname)))
if clubnumber in clubs:
club = clubs[clubnumber]
# Indicate we touched this club
club.touchedby = overridefile
# Override anything specified; add anything new
for key in keys:
if key not in ignorefields and (row[key] or key not in club.__dict__):
if log and key not in donotlog and key in club.__dict__ and club.__dict__[key] != row[key]:
print(("%8s/%s: Updating '%s' from '%s' to '%s'" % (club.clubnumber, club.clubname, key, club.__dict__[key], row[key])))
club.__dict__[key] = row[key]
# Now, compute latitude and longitude if need be
try:
# Use explicit coordinates if we find them
club.latitude = float(club.latitude)
club.longitude = float(club.longitude)
# Both are specified, so we use them
except: # latitude and longitude not both specified
if row['address']:
parts = ['address', 'city', 'state', 'zip', 'country']
address = ', '.join([row[p] for p in parts])
# No coordinates provided but an address is here; use it.
gres = gmaps.geocode(address) # Includes city, state, zip, country if needed
# Make a dummy club entry for geocoding
reloinfo = myclub(clubnumber, club.clubname, club.place, club.address, club.city, club.state, club.zip, club.country, 0.0, 0.0)
# TODO: get rid of the need for a dummy club entry - have geocode return coordinates.
reloinfo.process(gres)
club.latitude = reloinfo.latitude
club.longitude = reloinfo.longitude
if __name__ == '__main__':
#!/usr/bin/env python3
""" If called as a main program, prints the current overrides """
import tmutil, sys
import tmglobals
myglobals = tmglobals.tmglobals()
import tmparms
# Establish parameters
parms = tmparms.tmparms()
# Add other parameters here
# Do global setup
myglobals.setup(parms)
curs = myglobals.curs
conn = myglobals.conn
clubs = Club.getClubsOn(curs)
print(('Using %s as the override spreadsheet' % parms.mapoverride))
overrideClubPositions(clubs, parms.mapoverride, parms.googlesheetsapikey, log=True)