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

View file

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

View file

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

View file

@ -1,6 +1,6 @@
import ast import ast
import os import os
import codecs import io
from setuptools import setup, find_packages from setuptools import setup, find_packages
@ -15,7 +15,7 @@ class VersionFinder(ast.NodeVisitor):
def read(*parts): def read(*parts):
filename = os.path.join(os.path.dirname(__file__), *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() return fp.read()