2 """Automatically disambiguate location identifiers
4 This is an unmaintained one-shot script, only included in the repo for reference.
7 Disambiguates identifiers that aren't unique, Routes and Sea Routes, and
8 generic names like 'villa' or 'game corner' that could appear in future
11 Does this by prepending the region name, and if that isn't enough, appends
17 from collections
import defaultdict
19 from pokedex
.db
import connect
, tables
21 ambiguous_re
= re
.compile(r
'^(sea-)?route-\d+$')
23 ambiguous_set
= set('foreign-building game-corner global-terminal lighthouse '
24 'restaurant flower-shop cycle-shop cafe shopping-mall villa'.split())
29 location_dict
= defaultdict(list)
30 for location
in session
.query(tables
.Location
).order_by(tables
.Location
.id):
31 location_dict
[location
.identifier
].append(location
)
34 for identifier
, locations
in sorted(location_dict
.items()):
37 ambiguous_re
.match(identifier
),
38 identifier
in ambiguous_set
,
40 print len(locations
), ' *'[disambiguate
], identifier
,
43 print u
'→'.encode('utf-8'),
44 by_region
= defaultdict(list)
45 for location
in locations
:
47 by_region
[location
.region
.identifier
].append(location
)
49 by_region
[None].append(location
)
50 for region_identifier
, region_locations
in by_region
.items():
52 new_identifier
= '%s-%s' %
(region_identifier
, identifier
)
55 new_identifier
= identifier
56 if len(region_locations
) == 1:
57 location
= region_locations
[0]
58 # The region was enough
60 location
.identifier
= new_identifier
62 # Need to number the locations :(
63 for i
, location
in enumerate(region_locations
, start
=1):
64 numbered_identifier
= '%s-%s' %
(new_identifier
, i
)
65 print numbered_identifier
,
66 location
.identifier
= numbered_identifier
70 if argv
and argv
[0] == '--commit':
74 print 'Run with --commit to commit changes'
76 print 'No changes needed'
79 if __name__
== '__main__':