Skip to content

Commit 8c1e33d

Browse files
committed
Use io instead of codecs
1 parent 44c0405 commit 8c1e33d

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

dbtemplates/management/commands/sync_templates.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import io
12
import os
2-
import codecs
33
from django.contrib.sites.models import Site
44
from django.core.management.base import CommandError, BaseCommand
55
from django.template.utils import get_app_template_dirs
@@ -87,8 +87,8 @@ def handle(self, **options):
8787
"database.\nCreate it with '%s'?"
8888
" (y/[n]): """ % (name, path))
8989
if force or confirm.lower().startswith('y'):
90-
t = Template(name=name,
91-
content=codecs.open(path, "r").read())
90+
with io.open(path, encoding='utf-8') as f:
91+
t = Template(name=name, content=f.read())
9292
t.save()
9393
t.sites.add(site)
9494
else:
@@ -105,21 +105,19 @@ def handle(self, **options):
105105
if confirm in ('', FILES_TO_DATABASE,
106106
DATABASE_TO_FILES):
107107
if confirm == FILES_TO_DATABASE:
108-
t.content = codecs.open(path, 'r').read()
109-
t.save()
110-
t.sites.add(site)
108+
with io.open(path, encoding='utf-8') as f:
109+
t.content = f.read()
110+
t.save()
111+
t.sites.add(site)
111112
if delete:
112113
try:
113114
os.remove(path)
114115
except OSError:
115116
raise CommandError(
116117
u"Couldn't delete %s" % path)
117118
elif confirm == DATABASE_TO_FILES:
118-
f = codecs.open(path, 'w', 'utf-8')
119-
try:
119+
with io.open(path, 'w', encoding='utf-8') as f:
120120
f.write(t.content)
121-
finally:
122-
f.close()
123121
if delete:
124122
t.delete()
125123
break

dbtemplates/test_cases.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import codecs
1+
import io
22
import os
33
import shutil
44
import tempfile
@@ -102,7 +102,7 @@ def test_sync_templates(self):
102102
old_template_dirs = settings.TEMPLATES[0].get('DIRS', [])
103103
temp_template_dir = tempfile.mkdtemp('dbtemplates')
104104
temp_template_path = os.path.join(temp_template_dir, 'temp_test.html')
105-
temp_template = codecs.open(temp_template_path, 'w')
105+
temp_template = io.open(temp_template_path, 'w', encoding='utf-8')
106106
try:
107107
temp_template.write('temp test')
108108
settings.TEMPLATES[0]['DIRS'] = (temp_template_dir,)
@@ -124,7 +124,8 @@ def test_sync_templates(self):
124124
call_command('sync_templates', force=True,
125125
verbosity=0, overwrite=DATABASE_TO_FILES)
126126
self.assertTrue(
127-
'modified' in codecs.open(temp_template_path, 'utf-8').read().decode('utf-8'))
127+
'modified' in io.open(temp_template_path,
128+
encoding='utf-8').read().decode('utf-8'))
128129

129130
call_command('sync_templates', force=True, verbosity=0,
130131
delete=True, overwrite=DATABASE_TO_FILES)

requirements/tests.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
flake8<3
1+
flake8
22
coverage

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ast
22
import os
3-
import codecs
3+
import io
44
from setuptools import setup, find_packages
55

66

@@ -15,7 +15,7 @@ def visit_Assign(self, node):
1515

1616
def read(*parts):
1717
filename = os.path.join(os.path.dirname(__file__), *parts)
18-
with codecs.open(filename, encoding='utf-8') as fp:
18+
with io.open(filename, encoding='utf-8') as fp:
1919
return fp.read()
2020

2121

0 commit comments

Comments
 (0)