changeset 59d55a97f35a in modules/country:default
details: https://hg.tryton.org/modules/country?cmd=changeset&node=59d55a97f35a
description:
Manage common user's input error when loading postal codes
issue10577
review363571002
diffstat:
scripts/import_postal_codes.py | 13 ++++++++++---
1 files changed, 10 insertions(+), 3 deletions(-)
diffs (38 lines):
diff -r ee4d7565f16f -r 59d55a97f35a scripts/import_postal_codes.py
--- a/scripts/import_postal_codes.py Thu Jul 22 00:05:32 2021 +0200
+++ b/scripts/import_postal_codes.py Fri Aug 06 09:46:32 2021 +0200
@@ -7,8 +7,9 @@
import sys
try:
from urllib.request import urlopen
+ from urllib.error import HTTPError
except ImportError:
- from urllib2 import urlopen
+ from urllib2 import urlopen, HTTPError
import zipfile
from argparse import ArgumentParser
@@ -37,7 +38,10 @@
def fetch(code):
sys.stderr.write('Fetching')
url = 'https://download.geonames.org/export/zip/%s.zip' % code
- responce = urlopen(url)
+ try:
+ responce = urlopen(url)
+ except HTTPError as e:
+ sys.exit("\nError downloading %s: %s" % (code, e.reason))
data = responce.read()
with zipfile.ZipFile(BytesIO(data)) as zf:
data = zf.read('%s.txt' % code)
@@ -54,7 +58,10 @@
def get_country(code):
country = countries.get(code)
if not country:
- country, = Country.find([('code', '=', code)])
+ try:
+ country, = Country.find([('code', '=', code)])
+ except ValueError:
+ sys.exit("Error missing country with code %s" % code)
countries[code] = country
return country
countries = {}