Skip to content

Commit 89bba48

Browse files
Improve Python 3 compatibility.
1 parent e2ac254 commit 89bba48

File tree

10 files changed

+65
-53
lines changed

10 files changed

+65
-53
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ env:
77
- TOX_ENV=py27-1.4
88
- TOX_ENV=py27-1.5
99
- TOX_ENV=py27-1.6
10+
- TOX_ENV=py27-1.7
1011
install:
1112
- pip install tox
1213
script:

requirements.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Requirements for using tower
2-
Django==1.4.5
2+
Django>=1.4.5,<1.8
33
babel==1.3
44
jinja2
5+
six>=1.9.0
56
translate-toolkit
67
-e git://github.com/jbalogh/jingo.git#egg=jingo
78

89
# Requirements for running the tests
9-
django-nose==1.1
10-
mock==1.0.1
10+
django-nose>=1.1
11+
mock>=1.0.1
1112

1213
# Requirements to build the docs
1314
Sphinx

run_tests.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
import os
44
import sys
55

6+
if __name__ == "__main__":
7+
# Set up the environment for our test project.
8+
ROOT = os.path.abspath(os.path.dirname(__file__))
69

7-
# Set up the environment for our test project.
8-
ROOT = os.path.abspath(os.path.dirname(__file__))
10+
os.environ['DJANGO_SETTINGS_MODULE'] = 'tower-project.settings'
11+
sys.path.insert(0, os.path.join(ROOT, 'examples'))
912

10-
os.environ['DJANGO_SETTINGS_MODULE'] = 'tower-project.settings'
11-
sys.path.insert(0, os.path.join(ROOT, 'examples'))
13+
from django.core.management import execute_from_command_line
1214

13-
# This can't be imported until after we've fiddled with the
14-
# environment.
15-
from django.test.utils import setup_test_environment
16-
setup_test_environment()
15+
# Run the equivalent of "django-admin.py test"
16+
sys.argv.insert(1, 'test')
1717

18-
from django.core.management import call_command
19-
20-
# Run the equivalent of "django-admin.py test"
21-
call_command('test')
18+
execute_from_command_line(sys.argv)

tower/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55
import django
66
from django.conf import settings
77
from django.utils.functional import lazy
8-
from django.utils.importlib import import_module
98
from django.utils.translation import (trans_real as django_trans,
109
ugettext as django_ugettext,
1110
ungettext as django_nugettext)
11+
try:
12+
from importlib import import_module
13+
except ImportError:
14+
from django.utils.importlib import import_module
15+
16+
try:
17+
from django.utils import six
18+
except ImportError:
19+
import six
1220

1321
from babel.messages.extract import extract_python
1422
from jinja2 import ext
@@ -52,8 +60,8 @@ def ungettext(singular, plural, number, context=None):
5260
return plural_stripped
5361
return ret
5462

55-
ugettext_lazy = lazy(ugettext, unicode)
56-
ungettext_lazy = lazy(ungettext, unicode)
63+
ugettext_lazy = lazy(ugettext, six.text_type)
64+
ungettext_lazy = lazy(ungettext, six.text_type)
5765

5866

5967
def add_context(context, message):

tower/management/commands/amalgamate.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def handle(self, *args, **options):
5858
'messages.po')
5959

6060
if not os.path.isfile(r_messages):
61-
print " Can't find (%s). Skipping..." % (r_messages)
61+
print(" Can't find (%s). Skipping..." % (r_messages))
6262
continue
6363

64-
print "Mushing python strings into messages.po for %s" % (locale)
64+
print("Mushing python strings into messages.po for %s" % (locale))
6565

6666
# Step 3: Merge our new combined .pot with the .po file
6767
if locale == "en_US":
@@ -90,7 +90,7 @@ def handle(self, *args, **options):
9090
# commands in the middle of Step 3.
9191
for domain in standalone_domains:
9292

93-
print "Merging %s strings to each locale..." % domain
93+
print("Merging %s strings to each locale..." % domain)
9494
z_domain_keys = os.path.join(locale_dir, 'z-%s.pot' % domain)
9595
if not os.path.isfile(z_domain_keys):
9696
sys.exit("Can't find z-%s.pot" % domain)
@@ -104,11 +104,11 @@ def handle(self, *args, **options):
104104
'z-%s.po' % domain)
105105

106106
if not os.path.isfile(z_domain_messages):
107-
print " Can't find (%s). Creating..." % (z_domain_messages)
107+
print(" Can't find (%s). Creating..." % (z_domain_messages))
108108
t = open(z_domain_messages, 'w')
109109
t.close()
110110

111-
print "Merging z-%s.po for %s" % (domain, locale)
111+
print("Merging z-%s.po for %s" % (domain, locale))
112112

113113
z_domain_keys_file = open(z_domain_keys)
114114

@@ -133,6 +133,6 @@ def handle(self, *args, **options):
133133

134134
p4.communicate()
135135
mergeme.close()
136-
print "Domain %s finished" % domain
136+
print("Domain %s finished" % domain)
137137

138-
print "All finished"
138+
print("All finished")

tower/management/commands/extract.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ def handle(self, *args, **options):
113113

114114
if not os.path.isdir(outputdir):
115115
if not options.get('create'):
116-
print ("Output directory must exist (%s) unless -c option is "
117-
"given. "
118-
"Specify one with --output-dir" % outputdir)
116+
print("Output directory must exist (%s) unless -c option is "
117+
"given. "
118+
"Specify one with --output-dir" % outputdir)
119119
return "FAILURE\n"
120120
else:
121121
os.makedirs(outputdir)
@@ -129,11 +129,11 @@ def handle(self, *args, **options):
129129

130130
def callback(filename, method, options):
131131
if method != 'ignore':
132-
print " %s" % filename
132+
print(" %s" % filename)
133133

134134
for domain in domains:
135135

136-
print "Extracting all strings in domain %s..." % (domain)
136+
print("Extracting all strings in domain %s..." % (domain))
137137

138138
methods = settings.DOMAIN_METHODS[domain]
139139
extracted = extract_from_dir(root,
@@ -154,8 +154,8 @@ def callback(filename, method, options):
154154
pot_files.append(os.path.join(outputdir, '%s.pot' % i))
155155

156156
if len(pot_files) > 1:
157-
print ("Concatenating the non-standalone domains into %s.pot" %
158-
TEXT_DOMAIN)
157+
print("Concatenating the non-standalone domains into %s.pot" %
158+
TEXT_DOMAIN)
159159

160160
final_out = os.path.join(outputdir, '%s.pot' % TEXT_DOMAIN)
161161

@@ -179,4 +179,4 @@ def callback(filename, method, options):
179179
for i in [x for x in domains if x not in standalone_domains]:
180180
os.remove(os.path.join(outputdir, '%s.pot' % i))
181181

182-
print 'done'
182+
print('done')

tower/management/commands/merge.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def handle(self, *args, **options):
5050

5151
for domain in domains:
5252

53-
print "Merging %s strings to each locale..." % domain
53+
print("Merging %s strings to each locale..." % domain)
5454
domain_pot = os.path.join(locale_dir, 'templates', 'LC_MESSAGES',
5555
'%s.pot' % domain)
5656
if not os.path.isfile(domain_pot):
@@ -69,7 +69,7 @@ def handle(self, *args, **options):
6969
'%s.po' % domain)
7070

7171
if not os.path.isfile(domain_po):
72-
print " Can't find (%s). Creating..." % (domain_po)
72+
print(" Can't find (%s). Creating..." % (domain_po))
7373
if not call(["which", "msginit"], stdout=PIPE) == 0:
7474
raise Exception("You do not have gettext installed.")
7575
p1 = Popen(["msginit",
@@ -80,7 +80,7 @@ def handle(self, *args, **options):
8080
"--width=200",])
8181
p1.communicate()
8282

83-
print "Merging %s.po for %s" % (domain, locale)
83+
print("Merging %s.po for %s" % (domain, locale))
8484

8585
domain_pot_file = open(domain_pot)
8686

@@ -100,13 +100,13 @@ def handle(self, *args, **options):
100100
domain_po,
101101
"-"]
102102
if os.path.isfile(compendium):
103-
print "(using a compendium)"
103+
print("(using a compendium)")
104104
command.insert(1, '--compendium=%s' % compendium)
105105
p3 = Popen(command, stdin=mergeme)
106106
p3.communicate()
107107
mergeme.close()
108-
print "Domain %s finished" % domain
108+
print("Domain %s finished" % domain)
109109

110-
print "All finished"
110+
print("All finished")
111111

112112
Command.help = Command.__doc__

tower/tests/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def fake_extract_from_dir(filename, fileobj, method,
1616
options=OPTIONS_MAP, keywords=TOWER_KEYWORDS,
1717
comment_tags=COMMENT_TAGS):
1818
""" We use Babel's exctract_from_dir() to pull out our gettext
19-
strings. In the tests, I don't have a directory of files, I have StringIO
19+
strings. In the tests, I don't have a directory of files, I have BytesIO
2020
objects. So, we fake the original function with this one."""
2121
for lineno, message, comments, context in extract(method, fileobj, keywords,
2222
comment_tags, options):

tower/tests/test_l10n.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from cStringIO import StringIO
1+
from io import BytesIO
22

33
import django
44
from django.utils import translation
@@ -262,7 +262,7 @@ def test_template_gettext_functions():
262262

263263

264264
def test_extract_tower_python():
265-
fileobj = StringIO(TEST_PO_INPUT)
265+
fileobj = BytesIO(TEST_PO_INPUT)
266266
method = 'tower.extract_tower_python'
267267
output = fake_extract_from_dir(filename="filename", fileobj=fileobj,
268268
method=method)
@@ -272,7 +272,7 @@ def test_extract_tower_python():
272272

273273

274274
def test_extract_tower_template():
275-
fileobj = StringIO(TEST_TEMPLATE_INPUT)
275+
fileobj = BytesIO(TEST_TEMPLATE_INPUT)
276276
method = 'tower.extract_tower_template'
277277
output = fake_extract_from_dir(filename="filename", fileobj=fileobj,
278278
method=method)
@@ -282,7 +282,7 @@ def test_extract_tower_template():
282282

283283

284284
def test_extract_tower_python_backwards_compatible():
285-
fileobj = StringIO(TEST_PO_INPUT)
285+
fileobj = BytesIO(TEST_PO_INPUT)
286286
method = 'tower.management.commands.extract.extract_tower_python'
287287
output = fake_extract_from_dir(filename="filename", fileobj=fileobj,
288288
method=method)
@@ -292,7 +292,7 @@ def test_extract_tower_python_backwards_compatible():
292292

293293

294294
def test_extract_tower_template_backwards_compatible():
295-
fileobj = StringIO(TEST_TEMPLATE_INPUT)
295+
fileobj = BytesIO(TEST_TEMPLATE_INPUT)
296296
method = 'tower.management.commands.extract.extract_tower_template'
297297
output = fake_extract_from_dir(filename="filename", fileobj=fileobj,
298298
method=method)

tox.ini

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py26-1.4, py26-1.5, py26-1.6, py27-1.4, py27-1.5, py27-1.6
2+
envlist = py26-1.4, py26-1.5, py26-1.6, py27-1.4, py27-1.5, py27-1.6, py27-1.7
33
toxworkdir = {homedir}/.tox-tower
44

55
[testenv]
@@ -15,36 +15,41 @@ deps = -egit+https://github.com/jbalogh/jingo.git#egg=jingo
1515
[testenv:py26-1.4]
1616
basepython = python2.6
1717
deps =
18-
Django==1.4.10
18+
Django==1.4.20
1919
{[testenv]deps}
2020

2121
[testenv:py26-1.5]
2222
basepython = python2.6
2323
deps =
24-
Django==1.5.5
24+
Django==1.5.12
2525
{[testenv]deps}
2626

2727
[testenv:py26-1.6]
2828
basepython = python2.6
2929
deps =
30-
Django==1.6
30+
Django==1.6.11
3131
{[testenv]deps}
3232

3333
[testenv:py27-1.4]
3434
basepython = python2.7
3535
deps =
36-
Django==1.4.10
36+
Django==1.4.20
3737
{[testenv]deps}
3838

39-
4039
[testenv:py27-1.5]
4140
basepython = python2.7
4241
deps =
43-
Django==1.5.5
42+
Django==1.5.12
4443
{[testenv]deps}
4544

4645
[testenv:py27-1.6]
4746
basepython = python2.7
4847
deps =
49-
Django==1.6
48+
Django==1.6.11
49+
{[testenv]deps}
50+
51+
[testenv:py27-1.7]
52+
basepython = python2.7
53+
deps =
54+
Django==1.7.7
5055
{[testenv]deps}

0 commit comments

Comments
 (0)