Skip to content

Commit

Permalink
Drop other code for python 2. Add tests for zip extract and to cover …
Browse files Browse the repository at this point in the history
…the progress argument
  • Loading branch information
marianoeramirez committed May 8, 2020
1 parent 2845dfe commit e1648d5
Show file tree
Hide file tree
Showing 16 changed files with 344 additions and 37 deletions.
11 changes: 5 additions & 6 deletions cities_light/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
from django.contrib import admin
from django.contrib.admin.views.main import ChangeList

from .forms import *
from .settings import *
from .abstract_models import to_search
from . import forms
from .loading import get_cities_models

Country, Region, SubRegion, City = get_cities_models()
Expand Down Expand Up @@ -38,7 +37,7 @@ class CountryAdmin(admin.ModelAdmin):
list_filter = (
'continent',
)
form = CountryForm
form = forms.CountryForm


admin.site.register(Country, CountryAdmin)
Expand All @@ -62,7 +61,7 @@ class RegionAdmin(admin.ModelAdmin):
'country',
'geoname_id',
)
form = RegionForm
form = forms.RegionForm


admin.site.register(Region, RegionAdmin)
Expand All @@ -89,7 +88,7 @@ class SubRegionAdmin(admin.ModelAdmin):
'region',
'geoname_id',
)
form = SubRegionForm
form = forms.SubRegionForm


admin.site.register(SubRegion, SubRegionAdmin)
Expand Down Expand Up @@ -126,7 +125,7 @@ class CityAdmin(admin.ModelAdmin):
'country',
'timezone'
)
form = CityForm
form = forms.CityForm

def get_changelist(self, request, **kwargs):
return CityChangeList
Expand Down
9 changes: 3 additions & 6 deletions cities_light/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
import time
import os

try:
from urllib.request import urlopen
from urllib.parse import urlparse
except ImportError:
from urllib import urlopen
from urlparse import urlparse
from urllib.request import urlopen
from urllib.parse import urlparse


from .exceptions import SourceFileDoesNotExist

Expand Down
14 changes: 2 additions & 12 deletions cities_light/geonames.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import unicode_literals

import six
import os.path
import zipfile
import logging
Expand Down Expand Up @@ -61,16 +60,10 @@ def extract(self, zip_path, file_name):
zip_file.extract(file_name, DATA_DIR)

def parse(self):
if not six.PY3:
file = open(self.file_path, 'r')
else:
file = open(self.file_path, encoding='utf-8', mode='r')
file = open(self.file_path, encoding='utf-8', mode='r')
line = True

for line in file:
if not six.PY3:
# In python3 this is already an unicode
line = line.decode('utf8')

line = line.strip()
# If the line is blank/empty or a comment, skip it and continue
Expand All @@ -80,7 +73,4 @@ def parse(self):
yield [e.strip() for e in line.split('\t')]

def num_lines(self):
if not six.PY3:
return sum(1 for line in open(self.file_path))
else:
return sum(1 for line in open(self.file_path, encoding='utf-8'))
return sum(1 for line in open(self.file_path, encoding='utf-8'))
10 changes: 2 additions & 8 deletions cities_light/loading.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import django
from .settings import CITIES_LIGHT_APP_NAME


if django.VERSION < (1, 7):
from django.db.models import get_model
else:
from django.apps import apps
get_model = apps.get_model
from django.apps import apps
get_model = apps.get_model


def get_cities_model(model_name, *args, **kwargs):
Expand Down
11 changes: 6 additions & 5 deletions cities_light/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TestImportBase(test.TransactionTestCase):
maxDiff = 100000
reset_sequences = True

def import_data(self, srcdir, countries, regions, subregions, cities, trans, **options):
def import_data(self, srcdir, countries, regions, subregions, cities, trans, file_type="txt", **options):
"""Helper method to import Geonames data.
Patch *_SOURCES settings and call 'cities_light' command with
Expand All @@ -58,18 +58,19 @@ def import_data(self, srcdir, countries, regions, subregions, cities, trans, **o
trans - values for TRANSLATION_SOURCES
**options - passed to call_command() as is
"""

def _s2l(param):
return param if isinstance(param, list) else [param]

def _patch(setting, *values):
setting_to_patch = (
'cities_light.management.commands.cities_light.%s_SOURCES' %
setting.upper()
'cities_light.management.commands.cities_light.%s_SOURCES' %
setting.upper()
)

return mock.patch(
setting_to_patch,
['file://%s.txt' % srcdir.get_file_path(v) for v in values]
['file://%s.%s' % (srcdir.get_file_path(v), file_type) for v in values]
)

m_country = _patch('country', *_s2l(countries))
Expand All @@ -78,6 +79,6 @@ def _patch(setting, *values):
m_city = _patch('city', *_s2l(cities))
m_tr = _patch('translation', *_s2l(trans))
with m_country, m_region, m_subregion, m_city, m_tr:
management.call_command('cities_light',
management.call_command('cities_light', progress=True,
force_import_all=True,
**options)
1 change: 1 addition & 0 deletions cities_light/tests/fixtures/import_zip/angouleme_city.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3037598 Angoulême Angouleme ANG,An'nkoulem,Angouleme,Angoulême,Angulem,Angulema,Angulemas,Angulemo,Engoleime,Engolesme,Engolisma,Gorad Angulem,Montagne-Charente,Ongulem,ang gu lai mu,ang-gullem,anghwlym,anguremu,angwlm,Ανγκουλέμ,Ангулем,Горад Ангулем,Онгулем,אנגולם,آنگولم,أنغوليم,アングレーム,昂古莱姆,앙굴렘 45.65 0.15 P PPLA2 FR 75 16 161 16015 49468 103 Europe/Paris 2018-02-06
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FR FRA 250 FR France Paris 547030 64768389 EU .fr EUR Euro 33 ##### ^(\d{5})$ fr-FR,frp,br,co,ca,eu,oc 3017382 CH,DE,BE,LU,IT,AD,MC,ES
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FR.75 Nouvelle-Aquitaine Nouvelle-Aquitaine 11071620
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FR.75.16 Charente Charente 3026646
Binary file not shown.
Loading

0 comments on commit e1648d5

Please sign in to comment.