1
0
Fork 0
mirror of https://github.com/veekun/pokedex.git synced 2024-08-20 18:16:34 +00:00

Resolve some Python 3 compatability issues with decoding

This commit is contained in:
unknown 2018-08-17 16:31:28 -04:00
parent ffb0638ef5
commit ef9a9e3b84
2 changed files with 16 additions and 6 deletions

View file

@ -210,7 +210,10 @@ def load(session, tables=[], directory=None, drop_tables=False, verbose=False, s
try: try:
csvpath = "%s/%s.csv" % (directory, table_name) csvpath = "%s/%s.csv" % (directory, table_name)
csvfile = open(csvpath, 'r') if six.PY2:
csvfile = open(csvpath, 'r')
else:
csvfile = open(csvpath, 'r', encoding="utf8")
except IOError: except IOError:
# File doesn't exist; don't load anything! # File doesn't exist; don't load anything!
print_done('missing?') print_done('missing?')
@ -389,7 +392,7 @@ def dump(session, tables=[], directory=None, verbose=False, langs=None):
""" """
# First take care of verbosity # First take care of verbosity
print_start, print_status, print_done = _get_verbose_prints(verbose) print_start, print_done = _get_verbose_prints(verbose)
languages = dict((l.id, l) for l in session.query(pokedex.db.tables.Language)) languages = dict((l.id, l) for l in session.query(pokedex.db.tables.Language))
@ -416,7 +419,7 @@ def dump(session, tables=[], directory=None, verbose=False, langs=None):
# CSV module only works with bytes on 2 and only works with text on 3! # CSV module only works with bytes on 2 and only works with text on 3!
if six.PY3: if six.PY3:
writer = csv.writer(open(filename, 'w', newline=''), lineterminator='\n') writer = csv.writer(open(filename, 'w', newline='', encoding="utf8"), lineterminator='\n')
columns = [col.name for col in table.columns] columns = [col.name for col in table.columns]
else: else:
writer = csv.writer(open(filename, 'wb'), lineterminator='\n') writer = csv.writer(open(filename, 'wb'), lineterminator='\n')

View file

@ -262,11 +262,15 @@ class Translations(object):
def reader_for_class(self, cls, reader_class=csv.reader): def reader_for_class(self, cls, reader_class=csv.reader):
tablename = cls.__table__.name tablename = cls.__table__.name
csvpath = os.path.join(self.csv_directory, tablename + '.csv') csvpath = os.path.join(self.csv_directory, tablename + '.csv')
return reader_class(open(csvpath, 'r'), lineterminator='\n') if six.PY2:
read = open(csvpath, 'r')
else:
read = open(csvpath, 'r', encoding='utf-8')
return reader_class(read, lineterminator='\n')
def writer_for_lang(self, lang): def writer_for_lang(self, lang):
csvpath = os.path.join(self.translation_directory, '%s.csv' % lang) csvpath = os.path.join(self.translation_directory, '%s.csv' % lang)
return csv.writer(io.open(csvpath, 'w', newline=''), lineterminator='\n') return csv.writer(io.open(csvpath, 'w', newline='', encoding="utf8"), lineterminator='\n')
def yield_source_messages(self, language_id=None): def yield_source_messages(self, language_id=None):
"""Yield all messages from source CSV files """Yield all messages from source CSV files
@ -307,7 +311,10 @@ class Translations(object):
""" """
path = os.path.join(self.csv_directory, 'translations', '%s.csv' % lang) path = os.path.join(self.csv_directory, 'translations', '%s.csv' % lang)
try: try:
file = open(path, 'r') if six.PY2:
file = open(path, 'r')
else:
file = open(path, 'r', encoding="utf8")
except IOError: except IOError:
return () return ()
return yield_translation_csv_messages(file) return yield_translation_csv_messages(file)