Skip to content

Commit

Permalink
Modernize code
Browse files Browse the repository at this point in the history
- remove python 2 support
- add github workflows
- remove disable unicode
- cleanup compat file
- modernize setup
- use pep517

Change-Id: Ic38dbf478046cec5d0815b468f0c235b4ea5e20c
  • Loading branch information
CaselIT committed Oct 23, 2021
1 parent 09cf4f6 commit a1d70af
Show file tree
Hide file tree
Showing 57 changed files with 611 additions and 1,327 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/run-on-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Run tests on a pr

on:
# run on pull request to master excluding changes that are only on doc or example folders
pull_request:
branches:
- master
paths-ignore:
- "doc/**"

jobs:
run-test:
name: ${{ matrix.python-version }}-${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
# run this job using this matrix
matrix:
os:
- "ubuntu-latest"
python-version:
- "3.9"

fail-fast: false

# steps to run in each job. Some are github actions, others run shell commands
steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Set up python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade tox setuptools
pip list
- name: Run tests
run: tox
51 changes: 51 additions & 0 deletions .github/workflows/run-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Run tests

on:
# run on push in master or rel_* branches excluding changes are only on doc or example folders
push:
branches:
- master
- "rel_*"
# branches used to test the workflow
- "workflow_test_*"
paths-ignore:
- "docs/**"

jobs:
run-test:
name: ${{ matrix.python-version }}-${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
# run this job using this matrix
matrix:
os:
- "ubuntu-latest"
- "windows-latest"
- "macos-latest"
python-version:
- "3.6"
- "3.7"
- "3.8"
- "3.9"

fail-fast: false

# steps to run in each job. Some are github actions, others run shell commands
steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Set up python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade tox setuptools
pip list
- name: Run tests
run: tox
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
/man
.tox/
.cache/
.vscode
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2006-2020 the Mako authors and contributors <see AUTHORS file>.
Copyright 2006-2021 the Mako authors and contributors <see AUTHORS file>.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
4 changes: 1 addition & 3 deletions doc/build/filtering.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ In addition to the ``expression_filter`` argument, the
:class:`.TemplateLookup` can specify filtering for all expression tags
at the programmatic level. This array-based argument, when given
its default argument of ``None``, will be internally set to
``["unicode"]`` (or ``["str"]`` on Python 3), except when
``disable_unicode=True`` is set in which case it defaults to
``["str"]``:
``["unicode"]`` (or ``["str"]`` on Python 3):

.. sourcecode:: python

Expand Down
90 changes: 2 additions & 88 deletions doc/build/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,7 @@ the storage format for strings.

The "pass through encoded data" scheme is what template
languages like Cheetah and earlier versions of Myghty do by
default. Mako as of version 0.2 also supports this mode of
operation when using Python 2, using the ``disable_unicode=True``
flag. However, when using Mako in its default mode of
unicode-aware, it requires explicitness when dealing with
non-ASCII encodings. Additionally, if you ever need to handle
unicode strings and other kinds of encoding conversions more
intelligently, the usage of raw byte-strings quickly becomes a
nightmare, since you are sending the Python interpreter
collections of bytes for which it can make no intelligent
decisions with regards to encoding. In Python 3 Mako only allows
default. In Python 3 Mako only allows
usage of native, unicode strings.

In normal Mako operation, all parsed template constructs and
Expand Down Expand Up @@ -255,82 +246,5 @@ which cannot handle encoding of non-ASCII ``unicode`` objects
buffering. Otherwise, a custom Mako class called
``FastEncodingBuffer`` is used, which essentially is a super
dumbed-down version of ``StringIO`` that gathers all strings into
a list and uses ``u''.join(elements)`` to produce the final output
a list and uses ``''.join(elements)`` to produce the final output
-- it's markedly faster than ``StringIO``.

.. _unicode_disabled:

Saying to Heck with It: Disabling the Usage of Unicode Entirely
===============================================================

Some segments of Mako's userbase choose to make no usage of
Unicode whatsoever, and instead would prefer the "pass through"
approach; all string expressions in their templates return
encoded byte-strings, and they would like these strings to pass
right through. The only advantage to this approach is that
templates need not use ``u""`` for literal strings; there's an
arguable speed improvement as well since raw byte-strings
generally perform slightly faster than unicode objects in
Python. For these users, assuming they're sticking with Python
2, they can hit the ``disable_unicode=True`` flag as so:

.. sourcecode:: python

# -*- coding:utf-8 -*-
from mako.template import Template

t = Template("drôle de petite voix m’a réveillé.", disable_unicode=True, input_encoding='utf-8')
print(t.code)

The ``disable_unicode`` mode is strictly a Python 2 thing. It is
not supported at all in Python 3.

The generated module source code will contain elements like
these:

.. sourcecode:: python

# -*- coding:utf-8 -*-
# ...more generated code ...

def render_body(context,**pageargs):
context.caller_stack.push_frame()
try:
__M_locals = dict(pageargs=pageargs)
# SOURCE LINE 1
context.write('dr\xc3\xb4le de petite voix m\xe2\x80\x99a r\xc3\xa9veill\xc3\xa9.')
return ''
finally:
context.caller_stack.pop_frame()

Where above that the string literal used within :meth:`.Context.write`
is a regular byte-string.

When ``disable_unicode=True`` is turned on, the ``default_filters``
argument which normally defaults to ``["unicode"]`` now defaults
to ``["str"]`` instead. Setting ``default_filters`` to the empty list
``[]`` can remove the overhead of the ``str`` call. Also, in this
mode you **cannot** safely call :meth:`~.Template.render_unicode` -- you'll get
unicode/decode errors.

The ``h`` filter (HTML escape) uses a less performant pure Python
escape function in non-unicode mode. This because
MarkupSafe only supports Python unicode objects for non-ASCII
strings.

.. versionchanged:: 0.3.4
In prior versions, it used ``cgi.escape()``, which has been replaced
with a function that also escapes single quotes.

Rules for using ``disable_unicode=True``
----------------------------------------

* Don't use this mode unless you really, really want to and you
absolutely understand what you're doing.
* Don't use this option just because you don't want to learn to
use Unicode properly; we aren't supporting user issues in this
mode of operation. We will however offer generous help for the
vast majority of users who stick to the Unicode program.
* Python 3 is unicode by default, and the flag is not available
when running on Python 3.

4 changes: 4 additions & 0 deletions doc/build/unreleased/disable_unicode.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. change::
:tags: py3k

Removed ``disable_unicode`` flag, that's no longer used in Python 3.
4 changes: 4 additions & 0 deletions doc/build/unreleased/py2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. change::
:tags: py3k

Remove support for Python 2. Mako now requires Python >= 3.6
22 changes: 5 additions & 17 deletions examples/bench/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,10 @@
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from cgi import escape
import os
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from io import StringIO
import sys
import timeit

def u(stringlit):
if sys.version_info >= (3,):
return stringlit
else:
return stringlit.decode('latin1')

__all__ = ['mako', 'mako_inheritance', 'jinja2', 'jinja2_inheritance',
'cheetah', 'django', 'myghty', 'genshi', 'kid']
Expand All @@ -50,7 +40,6 @@ def u(stringlit):
TITLE = 'Just a test'
USER = 'joe'
ITEMS = ['Number %d' % num for num in range(1, 15)]
U_ITEMS = [u(item) for item in ITEMS]

def genshi(dirname, verbose=False):
from genshi.template import TemplateLoader
Expand Down Expand Up @@ -79,11 +68,10 @@ def render():
def mako(dirname, verbose=False):
from mako.template import Template
from mako.lookup import TemplateLookup
disable_unicode = (sys.version_info < (3,))
lookup = TemplateLookup(directories=[dirname], filesystem_checks=False, disable_unicode=disable_unicode)
lookup = TemplateLookup(directories=[dirname], filesystem_checks=False)
template = lookup.get_template('template.html')
def render():
return template.render(title=TITLE, user=USER, list_items=U_ITEMS)
return template.render(title=TITLE, user=USER, list_items=ITEMS)
if verbose:
print(template.code + " " + render())
return render
Expand All @@ -94,7 +82,7 @@ def jinja2(dirname, verbose=False):
env = Environment(loader=FileSystemLoader(dirname))
template = env.get_template('template.html')
def render():
return template.render(title=TITLE, user=USER, list_items=U_ITEMS)
return template.render(title=TITLE, user=USER, list_items=ITEMS)
if verbose:
print(render())
return render
Expand All @@ -106,7 +94,7 @@ def cheetah(dirname, verbose=False):
template = Template(file=filename)
def render():
template.__dict__.update({'title': TITLE, 'user': USER,
'list_items': U_ITEMS})
'list_items': ITEMS})
return template.respond()

if verbose:
Expand Down
6 changes: 0 additions & 6 deletions examples/wsgi/run_wsgi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
#!/usr/bin/python

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import cgi, re, os, posixpath, mimetypes
from mako.lookup import TemplateLookup
from mako import exceptions
Expand Down
2 changes: 1 addition & 1 deletion mako/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# the MIT License: http://www.opensource.org/licenses/mit-license.php


__version__ = "1.1.6"
__version__ = "1.2.0"
2 changes: 1 addition & 1 deletion mako/_ast_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def iter_fields(node):
pass


class NodeVisitor(object):
class NodeVisitor:

"""
Walks the abstract syntax tree and call visitor functions for every node
Expand Down
11 changes: 5 additions & 6 deletions mako/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

import re

from mako import compat
from mako import exceptions
from mako import pyparser


class PythonCode(object):
class PythonCode:

"""represents information about a string containing Python code"""

Expand All @@ -39,7 +38,7 @@ def __init__(self, code, **exception_kwargs):
# - AST is less likely to break with version changes
# (for example, the behavior of co_names changed a little bit
# in python version 2.5)
if isinstance(code, compat.string_types):
if isinstance(code, str):
expr = pyparser.parse(code.lstrip(), "exec", **exception_kwargs)
else:
expr = code
Expand All @@ -48,7 +47,7 @@ def __init__(self, code, **exception_kwargs):
f.visit(expr)


class ArgumentList(object):
class ArgumentList:

"""parses a fragment of code as a comma-separated list of expressions"""

Expand All @@ -57,7 +56,7 @@ def __init__(self, code, **exception_kwargs):
self.args = []
self.declared_identifiers = set()
self.undeclared_identifiers = set()
if isinstance(code, compat.string_types):
if isinstance(code, str):
if re.match(r"\S", code) and not re.match(r",\s*$", code):
# if theres text and no trailing comma, insure its parsed
# as a tuple by adding a trailing comma
Expand Down Expand Up @@ -111,7 +110,7 @@ def __init__(self, code, **exception_kwargs):
super(PythonFragment, self).__init__(code, **exception_kwargs)


class FunctionDecl(object):
class FunctionDecl:

"""function declaration"""

Expand Down
Loading

0 comments on commit a1d70af

Please sign in to comment.