mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Open CSVs with io.open, rather than using six.
This commit is contained in:
parent
65127e83e0
commit
8406878eee
2 changed files with 7 additions and 17 deletions
pokedex/db
|
@ -5,6 +5,7 @@ import csv
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import os.path
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
|
from io import open
|
||||||
|
|
||||||
import six
|
import six
|
||||||
import sqlalchemy.sql.util
|
import sqlalchemy.sql.util
|
||||||
|
@ -16,7 +17,6 @@ from pokedex.defaults import get_default_csv_dir
|
||||||
from pokedex.db.dependencies import find_dependent_tables
|
from pokedex.db.dependencies import find_dependent_tables
|
||||||
from pokedex.db.oracle import rewrite_long_table_names
|
from pokedex.db.oracle import rewrite_long_table_names
|
||||||
|
|
||||||
|
|
||||||
def _get_table_names(metadata, patterns):
|
def _get_table_names(metadata, patterns):
|
||||||
"""Returns a list of table names from the given metadata. If `patterns`
|
"""Returns a list of table names from the given metadata. If `patterns`
|
||||||
exists, only tables matching one of the patterns will be returned.
|
exists, only tables matching one of the patterns will be returned.
|
||||||
|
@ -210,10 +210,7 @@ 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)
|
||||||
if six.PY2:
|
csvfile = open(csvpath, 'r', encoding='utf-8')
|
||||||
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?')
|
||||||
|
@ -419,7 +416,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='', encoding="utf8"), lineterminator='\n')
|
writer = csv.writer(open(filename, 'w', newline='', encoding='utf-8'), 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')
|
||||||
|
|
|
@ -25,10 +25,10 @@ from __future__ import print_function
|
||||||
|
|
||||||
import binascii
|
import binascii
|
||||||
import csv
|
import csv
|
||||||
import io
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from io import open
|
||||||
|
|
||||||
import six
|
import six
|
||||||
from six.moves import zip
|
from six.moves import zip
|
||||||
|
@ -262,15 +262,11 @@ 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')
|
||||||
if six.PY2:
|
return reader_class(open(csvpath, 'r', encoding='utf-8'), lineterminator='\n')
|
||||||
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='', encoding="utf8"), lineterminator='\n')
|
return csv.writer(open(csvpath, 'w', encoding='utf-8', newline=''), 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
|
||||||
|
@ -311,10 +307,7 @@ 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:
|
||||||
if six.PY2:
|
file = open(path, 'r', encoding='utf-8')
|
||||||
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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue