diff --git a/dbtemplates/management/commands/sync_templates.py b/dbtemplates/management/commands/sync_templates.py index c4b616b..5a93b4f 100644 --- a/dbtemplates/management/commands/sync_templates.py +++ b/dbtemplates/management/commands/sync_templates.py @@ -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 diff --git a/dbtemplates/test_cases.py b/dbtemplates/test_cases.py index 67a6ddb..4229418 100644 --- a/dbtemplates/test_cases.py +++ b/dbtemplates/test_cases.py @@ -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) diff --git a/requirements/tests.txt b/requirements/tests.txt index 630566f..25152de 100644 --- a/requirements/tests.txt +++ b/requirements/tests.txt @@ -1,2 +1,2 @@ -flake8<3 +flake8 coverage diff --git a/setup.py b/setup.py index 73d13e1..e4c532f 100644 --- a/setup.py +++ b/setup.py @@ -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()