Refactor code to clean it up a bit.

This commit is contained in:
Craig Maloney 2018-03-14 20:39:27 -04:00
parent ef77c8faed
commit c28e26d5cc
3 changed files with 87 additions and 87 deletions

View file

@ -1,4 +1,4 @@
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from events.models.locale import Country, SPR, City from events.models.locale import Country, SPR, City
@ -26,6 +26,7 @@ MODIFICATION_DATE=18
COUNTRY_CACHE = dict() COUNTRY_CACHE = dict()
SPR_CACHE = dict() SPR_CACHE = dict()
class Command(BaseCommand): class Command(BaseCommand):
help = 'Loads city data from GeoNames database file' help = 'Loads city data from GeoNames database file'
@ -39,7 +40,7 @@ class Command(BaseCommand):
COUNTRY_CACHE[country.code] = country COUNTRY_CACHE[country.code] = country
for spr in SPR.objects.all(): for spr in SPR.objects.all():
SPR_CACHE["%s.%s" % (spr.country.code, spr.code)] = spr SPR_CACHE["%s.%s" % (spr.country.code, spr.code)] = spr
cities_file = open(options['file'], 'r') with open(options['file'], 'r') as cities_file:
for city_line in cities_file.readlines(): for city_line in cities_file.readlines():
city = city_line.split("\t") city = city_line.split("\t")
if len(city) == 19: if len(city) == 19:
@ -48,12 +49,14 @@ class Command(BaseCommand):
spr = SPR_CACHE.get("%s.%s" % (city[COUNTRY_CODE], city[ADMIN1])) spr = SPR_CACHE.get("%s.%s" % (city[COUNTRY_CODE], city[ADMIN1]))
if country is not None and spr is not None: if country is not None and spr is not None:
try: try:
City.objects.update_or_create(name=city[NAME], spr=spr, defaults={'tz':city[TIMEZONE], 'longitude':city[LONGITUDE], 'latitude':city[LATITUDE]}) City.objects.update_or_create(
except: name=city[NAME],
print("Warning: Failed to load city %s for %s" % (city[NAME], spr)) spr=spr,
defaults={'tz': city[TIMEZONE], 'longitude': city[LONGITUDE], 'latitude': city[LATITUDE]})
except Exception as e:
print("Warning: Failed to load city %s for %s (%s)" % (city[NAME], spr, e))
else: else:
print("Short line (%s): %s" % (len(city), city_line)) print("Short line (%s): %s" % (len(city), city_line))
cities_file.close()
else: else:
print("No File in options!") print("No File in options!")

View file

@ -1,4 +1,4 @@
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from events.models.locale import Country from events.models.locale import Country
@ -23,6 +23,7 @@ GEONAMEID=16
NEIGHBOURS = 17 NEIGHBOURS = 17
EQUIVALENTFIPSCODE = 18 EQUIVALENTFIPSCODE = 18
class Command(BaseCommand): class Command(BaseCommand):
help = 'Loads country data from GeoNames database file' help = 'Loads country data from GeoNames database file'
@ -31,7 +32,7 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
if 'file' in options: if 'file' in options:
countries_file = open(options['file'], 'r') with open(options['file'], 'r') as countries_file:
for country_line in countries_file.readlines(): for country_line in countries_file.readlines():
if country_line.startswith("#"): if country_line.startswith("#"):
continue continue
@ -41,7 +42,5 @@ class Command(BaseCommand):
Country.objects.get_or_create(name=country[COUNTRY], code=country[ISO]) Country.objects.get_or_create(name=country[COUNTRY], code=country[ISO])
else: else:
print("Short line (%s): %s" % (len(country), country_line)) print("Short line (%s): %s" % (len(country), country_line))
countries_file.close()
else: else:
print("No File in options!") print("No File in options!")

View file

@ -22,7 +22,7 @@ class Command(BaseCommand):
# Preload country cache # Preload country cache
for country in Country.objects.all(): for country in Country.objects.all():
COUNTRY_CACHE[country.code] = country COUNTRY_CACHE[country.code] = country
spr_file = open(options['file'], 'r') with open(options['file'], 'r') as spr_file:
for spr_line in spr_file.readlines(): for spr_line in spr_file.readlines():
if spr_line.startswith("#"): if spr_line.startswith("#"):
continue continue
@ -35,7 +35,5 @@ class Command(BaseCommand):
SPR.objects.get_or_create(name=spr[NAME], code=SPR_CODE, country=country) SPR.objects.get_or_create(name=spr[NAME], code=SPR_CODE, country=country)
else: else:
print("Short line (%s): %s" % (len(spr), spr_line)) print("Short line (%s): %s" % (len(spr), spr_line))
spr_file.close()
else: else:
print("No File in options!") print("No File in options!")