Skip to content

Commit

Permalink
fixup! Update 'add_ons_to_gss' command to support CSVReader for Python 2
Browse files Browse the repository at this point in the history
Refactor the two uses of the same CSV opening technique into a function.
  • Loading branch information
dracos committed Jun 29, 2016
1 parent 15b0fdf commit 2e341a0
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions mapit_gb/management/commands/mapit_UK_add_ons_to_gss.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
from mapit.models import Area, CodeType
from psycopg2 import IntegrityError

python_version = sys.version_info[0]


def open_csv(filename):
if python_version < 3:
o = open(filename)
else:
o = codecs.open(filename, 'r', 'latin-1')
mapping = csv.reader(o)
next(mapping)
return mapping


def process(new_code, old_code):
try:
Expand All @@ -31,30 +43,10 @@ class Command(NoArgsCommand):
help = 'Inserts the old ONS codes into mapit'

def handle_noargs(self, **options):
python_version = sys.version_info[0]

code_change_file_path = os.path.join(
os.path.dirname(__file__),
'../../data/BL-2010-10-code-change.csv')
if python_version < 3:
mapping = csv.reader(open(code_change_file_path))
else:
code_changes = codecs.open(code_change_file_path, 'r', 'latin-1')
mapping = csv.reader(code_changes)
next(mapping)
for row in mapping:
for row in open_csv(os.path.join(os.path.dirname(__file__), '../../data/BL-2010-10-code-change.csv')):
new_code, old_code = row[0], row[3]
process(new_code, old_code)

missing_codes_file_path = os.path.join(
os.path.dirname(__file__),
'../../data/BL-2010-10-missing-codes.csv')
if python_version < 3:
mapping = csv.reader(open(missing_codes_file_path))
else:
missing_codes = codecs.open(missing_codes_file_path, 'r', 'latin-1')
mapping = csv.reader(missing_codes)
next(mapping)
for row in mapping:
for row in open_csv(os.path.join(os.path.dirname(__file__), '../../data/BL-2010-10-missing-codes.csv')):
new_code, old_code = row[1], row[2]
process(new_code, old_code)

0 comments on commit 2e341a0

Please sign in to comment.