Skip to content

Commit

Permalink
Merge branch 'karlrl-support-new-python-and-django'
Browse files Browse the repository at this point in the history
  • Loading branch information
mjumbewu committed Feb 10, 2023
2 parents 4f44a0a + dfdb96a commit ce5129b
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 163 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
language: python
python:
- "2.7"
- "3.4"
- "3.7"
- "3.8"
- "3.9"
- "3.10"

env:
- DJANGO="django<1.9" # 1.8.x
- DJANGO="django<3" # 2.2.x
- DJANGO="django<4" # 3.2.x
- DJANGO="git+git://github.com/django/django.git@master#egg=django"
Expand Down
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
CHANGES
=======

2.0.0
-----

Thank you to @karlrl for the following changes:

- Support Python 3.7-3.10
- Support Django 2.2-4.1
- Drop support for Python<3.7 and Django<3.2

1.3.9
-----

Expand Down
2 changes: 1 addition & 1 deletion jstemplate/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.9"
__version__ = "2.0.0"
12 changes: 4 additions & 8 deletions jstemplate/finders.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from __future__ import unicode_literals

import glob
import os
import re
import sys
import warnings
import glob, os, sys, re
import six

from importlib import import_module

from .conf import conf



class BaseFinder(object):
def find(self, name):
raise NotImplementedError()
Expand Down Expand Up @@ -115,8 +113,6 @@ def _get_app_template_dirs():
for dirname in conf.JSTEMPLATE_APP_DIRNAMES:
template_dir = os.path.join(app_dir, dirname)
if os.path.isdir(template_dir):
if not six.PY3:
template_dir = template_dir.decode(fs_encoding)
ret.append(template_dir)
return ret

Expand Down
6 changes: 1 addition & 5 deletions jstemplate/loading.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from __future__ import unicode_literals

import six
from django.core.exceptions import ImproperlyConfigured
from importlib import import_module

from .conf import conf



def find(name):
all_matches = {}

Expand All @@ -16,7 +12,7 @@ def find(name):

# <finder>.find may return a single string. The name of the template
# will then be the name given to 'find'
if isinstance(matches, six.text_type):
if isinstance(matches, str):
filepath = matches
if name not in all_matches:
all_matches[name] = filepath
Expand Down
13 changes: 6 additions & 7 deletions jstemplate/preprocessors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import unicode_literals

import re
from django.utils.translation import ugettext
from django.utils.translation import gettext
from .conf import conf


class I18nPreprocessor(object):
@property
def tagnames(self):
Expand All @@ -13,7 +12,7 @@ def tagnames(self):
def short_trans_re(self):
# Should match strings like: {{ _ "Hello, world! }}
tagnames = '|'.join(['(?:{0})'.format(t) for t in self.tagnames])

left_side = r'''(?P<left>\{\{\s*(?P<tag>(?:''' + tagnames + r''')\s+)(?P<quote>['"]))'''
right_side = r'''(?P<right>(?P=quote)\s*\}\})'''

Expand All @@ -23,7 +22,7 @@ def short_trans_re(self):
def long_trans_re(self):
# Should match strings like: {{# _ }}Hello, {{ name }}.{{/ _ }}
tagnames = '|'.join(['(?:{0})'.format(t) for t in self.tagnames])

start_tag = r'\{\{#\s*(?P<tag>' + tagnames + r')\s*\}\}'
end_tag = r'\{\{\/\s*(?P=tag)\s*\}\}'

Expand All @@ -33,14 +32,14 @@ def translate_short_form(self, match):
"""Translate a result of matching the compiled trans_re pattern."""
tag = match.group('tag')
msg = match.group('msg')
msg = ugettext(msg) if len(msg) > 0 else ''
msg = gettext(msg) if len(msg) > 0 else ''
string = match.group('left').replace(tag, '', 1) + msg + match.group('right')
return string

def translate_long_form(self, match):
"""Translate a result of matching the compiled trans_re pattern."""
msg = match.group('msg')
string = ugettext(msg) if len(msg) > 0 else ''
string = gettext(msg) if len(msg) > 0 else ''
return string

def process(self, content):
Expand Down
14 changes: 3 additions & 11 deletions jstemplate/templatetags/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import unicode_literals

import six
from django import template
from ..conf import conf
from ..loading import find, preprocess, JSTemplateNotFound
Expand Down Expand Up @@ -49,13 +46,8 @@ def read_template_file_contents(self, filepath):
# Django 3.1, UTF-8 is always expected.
encoding = getattr(conf, 'FILE_CHARSET', 'utf-8')

if six.PY3:
with open(filepath, "r", encoding=encoding) as fp:
template_text = fp.read()
else:
with open(filepath, "r") as fp:
template_text = fp.read().decode(encoding)

with open(filepath, "r", encoding=encoding) as fp:
template_text = fp.read()
template_text = self.preprocess(template_text)
return template_text

Expand All @@ -75,4 +67,4 @@ def jstemplate_tag_helper(tagname, TagNodeClass, parser, token):
"'%s' tag takes at least one argument: the name/id of "
"the template, or a pattern matching a set of templates. "
% tagname)
return TagNodeClass(*bits[1:])
return TagNodeClass(*bits[1:])
4 changes: 1 addition & 3 deletions jstemplate/templatetags/handlebarsjs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from __future__ import unicode_literals
from django import template
from ..conf import conf
from .base import BaseJSTemplateNode, jstemplate_tag_helper


Expand Down Expand Up @@ -49,7 +47,7 @@ def handlebarsjs(parser, token):
Finds the Handlebars template for the given name and renders it surrounded
by the requisite Handlebars <script> tags.
We don't use the jstemplate_tag_helper here, since we can take an
We don't use the jstemplate_tag_helper here, since we can take an
additional parameter denoting whether to register partials inline.
"""
Expand Down
18 changes: 1 addition & 17 deletions jstemplate/tests/project/project/urls.py
Original file line number Diff line number Diff line change
@@ -1,17 +1 @@
from django.conf.urls import *

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = [
# Examples:
# url(r'^$', 'project.views.home', name='home'),
# url(r'^project/', include('project.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
]
urlpatterns = []
2 changes: 1 addition & 1 deletion jstemplate/tests/project/project/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.utils.translation import ugettext as _
from django.utils.translation import gettext as _

def f():
return _('View message')
16 changes: 7 additions & 9 deletions jstemplate/tests/test_finders.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import os

from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.test import override_settings, TestCase

from mock import patch

from .utils import override_settings
from unittest.mock import patch



Expand Down Expand Up @@ -181,8 +178,9 @@ def test_get_app_template_dirs(self):
self.assertEqual(self.func(), [os.path.join(here, "templates")])


@override_settings(INSTALLED_APPS=["jstemplate.nonexistent"])
def test_bad_app(self):
with patch('warnings.warn') as warn:
self.func()
self.assertEqual(warn.call_count, 1)
with self.assertRaises(ModuleNotFoundError):
# Adding the non-existent app to INSTALLED_APPS triggers a
# ModuleNotFoundError
with override_settings(INSTALLED_APPS=["jstemplate.nonexistent"]):
pass
5 changes: 2 additions & 3 deletions jstemplate/tests/test_loading.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.test import override_settings, TestCase

from mock import patch
from unittest.mock import patch

from .mockfinders import MockFinder
from .utils import override_settings



Expand Down
7 changes: 1 addition & 6 deletions jstemplate/tests/test_makemessages.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import mock
import os.path

from django.template import Template, Context, TemplateSyntaxError
from django.test import TestCase
from mock import patch
from django.test import override_settings, TestCase

from jstemplate.management.commands import makemessages

from .utils import override_settings

DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "project", "project", "jstemplates")

class MonkeyPatchedTemplatizeTest (TestCase):
Expand Down
23 changes: 5 additions & 18 deletions jstemplate/tests/test_translation.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from __future__ import unicode_literals

import os.path
import six

from django.test import TestCase
from django.test import override_settings, TestCase
from django.utils import translation
from jstemplate.preprocessors import I18nPreprocessor

from .utils import override_settings



__all__ = [
"I18nTest",
Expand All @@ -34,13 +28,9 @@ def new_func(*args, **kwargs):
translation.activate("fr")
fake_translation = translation.trans_real._active.value

# wrap the ugettext and ungettext functions so that 'XXX ' will prefix each translation
if six.PY3:
self.original_gettext = fake_translation.gettext
fake_translation.gettext = wrap_with_xxx(fake_translation.gettext)
else:
self.original_ugettext = fake_translation.ugettext
fake_translation.ugettext = wrap_with_xxx(fake_translation.ugettext)
# wrap the gettext and ungettext functions so that 'XXX ' will prefix each translation
self.original_gettext = fake_translation.gettext
fake_translation.gettext = wrap_with_xxx(fake_translation.gettext)

# Turn back on our old translations
translation.activate(self.native_lang)
Expand All @@ -49,10 +39,7 @@ def tearDown(self):
# Restore the french translation function
translation.activate("fr")
fake_translation = translation.trans_real._active.value
if six.PY3:
fake_translation.gettext = self.original_gettext
else:
fake_translation.ugettext = self.original_ugettext
fake_translation.gettext = self.original_gettext

# Turn back on our old translations
translation.activate(self.native_lang)
Expand Down
9 changes: 1 addition & 8 deletions jstemplate/tests/test_ttag.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import mock
import os.path

from django.template import Template, Context, TemplateSyntaxError
from django.test import TestCase
from mock import patch
import six

from .utils import override_settings

from django.test import override_settings, TestCase


__all__ = [
Expand Down
45 changes: 0 additions & 45 deletions jstemplate/tests/utils.py

This file was deleted.

6 changes: 2 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
django>=1.8
six
mock
coverage
django>=2.2
coverage
Loading

0 comments on commit ce5129b

Please sign in to comment.