Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Run tests on Python${{ matrix.python-version }}
run: |
nix-shell --argstr pyVersion ${{ matrix.python-version }} --run \
"pip install -e . && pip install -r requirements.txt && pip install -r requirements-test.txt && python -m pytest"
"pip install -e . && pip install -r requirements.txt && pip install -r requirements-test.txt && make test"

- name: Coveralls
env:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test:

.PHONY: typecheck
typecheck:
mypy --config-file setup.cfg --package $(PROJECT_NAME)
mypy --config-file setup.cfg --strict --package $(PROJECT_NAME)

.PHONY: prepare-dist
prepare-dist:
Expand Down
10 changes: 6 additions & 4 deletions plim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import functools
from typing import Mapping, Type, Sequence, Any, Callable

from pyrsistent import v

from .lexer import compile_plim_source
from . import syntax as available_syntax


def preprocessor_factory(custom_parsers=None, syntax='mako'):
def preprocessor_factory(custom_parsers: Sequence[Any] = v(), syntax: str = 'mako') -> Callable[[str, bool], str]:
"""

:param custom_parsers: a list of 2-tuples of (parser_regex, parser_callable) or None
:type custom_parsers: list or None
:param syntax: name of the target template engine ('mako' by default)
:type syntax: str or None
:return: preprocessor instance
"""
syntax_choices = {
syntax_choices: Mapping[str, Type[available_syntax.BaseSyntax]] = {
'mako': available_syntax.Mako,
'django': available_syntax.Django,
}
selected_syntax = syntax_choices[syntax](custom_parsers)
selected_syntax = syntax_choices[syntax](custom_parsers or v())
return functools.partial(compile_plim_source, syntax=selected_syntax)


Expand Down
2 changes: 1 addition & 1 deletion plim/adapters/pyramid_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def add_plim_renderer(config, extension, mako_settings_prefix='mako.', preproces
renderer_factory = MakoRendererFactory()
config.add_renderer(extension, renderer_factory)

def register():
def register() -> None:
settings = copy.copy(config.registry.settings)
settings['{prefix}preprocessor'.format(prefix=mako_settings_prefix)] = preprocessor

Expand Down
2 changes: 1 addition & 1 deletion plim/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def plimc(args=None, stdout=None):
# Add an empty string path, so modules located at the current working dir
# are reachable and considered in the first place (see issue #32).
sys.path.insert(0, '')
preprocessor = EntryPoint.parse('x={}'.format(preprocessor_path)).load(False)
preprocessor = EntryPoint.parse('x={}'.format(preprocessor_path)).resolve()

# Render to html, if requested
# ----------------------------
Expand Down
20 changes: 7 additions & 13 deletions plim/errors.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
# -*- coding: utf-8 -*-
from .util import u



class PlimError(Exception):
def __str__(self):
return self.__unicode__().encode('utf-8')
pass


class PlimSyntaxError(PlimError):
def __init__(self, msg, line):
def __init__(self, msg: str, line: str):
super(PlimSyntaxError, self).__init__()
self.msg = msg
self.line = line

def __unicode__(self):
return u('{msg} | at line(pos) "{line}"').format(msg=self.msg, line=self.line)
def __str__(self) -> str:
return '{msg} | at line(pos) "{line}"'.format(msg=self.msg, line=self.line)


class ParserNotFound(PlimError):
def __init__(self, lineno, line):
def __init__(self, lineno: int, line: str):
super(ParserNotFound, self).__init__()
self.lineno = lineno
self.line = line

def __unicode__(self):
return u("Invalid syntax at line {lineno}: {line}").format(
def __str__(self) -> str:
return "Invalid syntax at line {lineno}: {line}".format(
lineno=self.lineno, line=self.line)
20 changes: 10 additions & 10 deletions plim/extensions.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
from typing import Mapping

from docutils.core import publish_parts
import coffeescript
from scss import Scss
from stylus import Stylus

from .util import u



def rst_to_html(source):
def rst_to_html(source: str) -> str:
# This code was taken from http://wiki.python.org/moin/ReStructuredText
# You may also be interested in http://www.tele3.cz/jbar/rest/about.html
html = publish_parts(source=source, writer_name='html')
html: Mapping[str, str] = publish_parts(source=source, writer_name='html')
return html['html_body']


def coffee_to_js(source):
return u('<script>{js}</script>').format(js=coffeescript.compile(source))
def coffee_to_js(source: str) -> str:
return '<script>{js}</script>'.format(js=coffeescript.compile(source))


def scss_to_css(source):
def scss_to_css(source: str) -> str:
css = Scss().compile(source).strip()
return u('<style>{css}</style>').format(css=css)
return '<style>{css}</style>'.format(css=css)


def stylus_to_css(source):
def stylus_to_css(source: str) -> str:
compiler = Stylus()
return u('<style>{css}</style>').format(css=compiler.compile(source).strip())
return '<style>{css}</style>'.format(css=compiler.compile(source).strip())
Loading