Use io instead of codecs

This commit is contained in:
Jannis Leidel 2016-09-20 11:34:28 +02:00
parent 44c040523a
commit 8c1e33de72
4 changed files with 15 additions and 16 deletions

View file

@ -1,5 +1,5 @@
import io
import os
import codecs
from django.contrib.sites.models import Site
from django.core.management.base import CommandError, BaseCommand
from django.template.utils import get_app_template_dirs
@ -87,8 +87,8 @@ class Command(BaseCommand):
"database.\nCreate it with '%s'?"
" (y/[n]): """ % (name, path))
if force or confirm.lower().startswith('y'):
t = Template(name=name,
content=codecs.open(path, "r").read())
with io.open(path, encoding='utf-8') as f:
t = Template(name=name, content=f.read())
t.save()
t.sites.add(site)
else:
@ -105,9 +105,10 @@ class Command(BaseCommand):
if confirm in ('', FILES_TO_DATABASE,
DATABASE_TO_FILES):
if confirm == FILES_TO_DATABASE:
t.content = codecs.open(path, 'r').read()
t.save()
t.sites.add(site)
with io.open(path, encoding='utf-8') as f:
t.content = f.read()
t.save()
t.sites.add(site)
if delete:
try:
os.remove(path)
@ -115,11 +116,8 @@ class Command(BaseCommand):
raise CommandError(
u"Couldn't delete %s" % path)
elif confirm == DATABASE_TO_FILES:
f = codecs.open(path, 'w', 'utf-8')
try:
with io.open(path, 'w', encoding='utf-8') as f:
f.write(t.content)
finally:
f.close()
if delete:
t.delete()
break

View file

@ -1,4 +1,4 @@
import codecs
import io
import os
import shutil
import tempfile
@ -102,7 +102,7 @@ class DbTemplatesTestCase(TestCase):
old_template_dirs = settings.TEMPLATES[0].get('DIRS', [])
temp_template_dir = tempfile.mkdtemp('dbtemplates')
temp_template_path = os.path.join(temp_template_dir, 'temp_test.html')
temp_template = codecs.open(temp_template_path, 'w')
temp_template = io.open(temp_template_path, 'w', encoding='utf-8')
try:
temp_template.write('temp test')
settings.TEMPLATES[0]['DIRS'] = (temp_template_dir,)
@ -124,7 +124,8 @@ class DbTemplatesTestCase(TestCase):
call_command('sync_templates', force=True,
verbosity=0, overwrite=DATABASE_TO_FILES)
self.assertTrue(
'modified' in codecs.open(temp_template_path, 'utf-8').read().decode('utf-8'))
'modified' in io.open(temp_template_path,
encoding='utf-8').read().decode('utf-8'))
call_command('sync_templates', force=True, verbosity=0,
delete=True, overwrite=DATABASE_TO_FILES)

View file

@ -1,2 +1,2 @@
flake8<3
flake8
coverage

View file

@ -1,6 +1,6 @@
import ast
import os
import codecs
import io
from setuptools import setup, find_packages
@ -15,7 +15,7 @@ class VersionFinder(ast.NodeVisitor):
def read(*parts):
filename = os.path.join(os.path.dirname(__file__), *parts)
with codecs.open(filename, encoding='utf-8') as fp:
with io.open(filename, encoding='utf-8') as fp:
return fp.read()