Skip to content

Commit

Permalink
Merge pull request #138 from bckohan/v2.2.0
Browse files Browse the repository at this point in the history
V2.2.0
  • Loading branch information
bckohan committed Feb 28, 2024
2 parents d91e995 + a973608 commit 456cf84
Show file tree
Hide file tree
Showing 31 changed files with 1,478 additions and 940 deletions.
27 changes: 17 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
django-version: 'Django~=3.2.0'

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -46,11 +46,12 @@ jobs:
poetry run black render_static --check
poetry run pylint render_static
poetry run mypy render_static
poetry run doc8 -q doc
poetry check
poetry run pip check
poetry export --without-hashes --format=requirements.txt | poetry run safety check --stdin
poetry run python -m readme_renderer ./README.rst -o /tmp/README.html
cd ./doc
poetry run doc8 --ignore-path build --max-line-length 100
test:
runs-on: ubuntu-latest
Expand All @@ -72,12 +73,12 @@ jobs:
django-version: '5.0.0'

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -95,8 +96,14 @@ jobs:
- name: Run Unit Tests
run: |
poetry run pytest
poetry run pip uninstall -y jinja2 pyyaml importlib-resources
poetry run pytest --cov-append
mv .coverage py${{ matrix.python-version }}-dj${{ matrix.django-version }}.coverage
# - name: Setup tmate session
# uses: mxschmitt/action-tmate@v3
# with:
# detached: true
# timeout-minutes: 60
- name: Store coverage files
uses: actions/upload-artifact@v4
with:
Expand All @@ -108,8 +115,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'

Expand Down Expand Up @@ -137,7 +144,7 @@ jobs:
- run: poetry run coverage xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ new feature PRs must provide updated documentation for the features added. To bu
.. code-block::
cd ./doc
poetry run doc8 --ignore-path build --max-line-length 100
poetry run make html
Expand All @@ -60,7 +61,6 @@ or warnings where justified is acceptable:
poetry run black render_static
poetry run mypy render_static
poetry run pylint render_static
poetry run doc8 -q doc
poetry check
poetry run pip check
poetry run python -m readme_renderer ./README.rst
Expand Down
6 changes: 6 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Change Log
==========

v2.2.0
======

* Implemented `Refactor renderstatic command using TyperCommand <https://github.com/bckohan/django-render-static/issues/137>`_
* Implemented `Allow enum class writer to_string parameter to be the name of a property to return from toString() <https://github.com/bckohan/django-render-static/issues/132>`_

v2.1.3
======

Expand Down
7 changes: 3 additions & 4 deletions doc/source/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ renderstatic
Usage
~~~~~

.. argparse::
:module: render_static.management.commands.renderstatic
:func: get_parser
:prog: manage.py
.. typer:: render_static.management.commands.renderstatic.Command:typer_app
:prog: manage.py renderstatic
:width: 90

Example
~~~~~~~
Expand Down
7 changes: 6 additions & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from datetime import datetime
import sys
import os
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent.parent))
import render_static
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'render_static.tests.settings')
django.setup()

# Configuration file for the Sphinx documentation builder.
#
Expand Down Expand Up @@ -40,8 +45,8 @@
'sphinx_rtd_theme',
'sphinx.ext.autodoc',
'sphinx.ext.viewcode',
'sphinxarg.ext',
'sphinx.ext.todo',
'sphinxcontrib.typer'
# 'sphinx_js'
]

Expand Down
24 changes: 9 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-render-static"
version = "2.1.3"
version = "2.2.0"
description = "Use Django's template engine to render static files at deployment or package time. Includes transpilers for extending Django's url reversal and enums to JavaScript."
authors = ["Brian Kohan <[email protected]>"]
license = "MIT"
Expand Down Expand Up @@ -48,6 +48,7 @@ Django = ">=3.2,<6.0"
importlib-resources = { version = ">=1.3.0", python = "<3.9" }
Jinja2 = { version = ">=2.9,<4.0", optional = true }
PyYAML = { version = ">=5.1,<7.0", optional = true }
django-typer = "^1.0.0"

[tool.poetry.group.dev.dependencies]
django-enum = "^1.1.0"
Expand All @@ -65,11 +66,9 @@ isort = "^5.13.0"
doc8 = "^1.1.0"
pytest-cov = "^4.1.0"
pylint = "^3.0.0"
sphinx-argparse = "^0.4.0"
deepdiff = "^6.7.0"
safety = "^2.3.0"
readme-renderer = ">=42"
pygount = "^1.6"
types-PyYAML = "^6.0"
coverage = "^7.3.0"
importlib-metadata = "^7.0.0"
Expand All @@ -78,6 +77,7 @@ python-dateutil = "^2.8.2"
ipdb = "^0.13.13"
black = "^23.12.0"
aiohttp = "^3.9.1"
sphinxcontrib-typer = "^0.1.11"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down Expand Up @@ -111,25 +111,17 @@ warn_no_return = true
exclude = "tests"

[tool.doc8]
ignore-path = "doc/_build"
ignore-path = "doc/build"
max-line-length = 100
sphinx = true

[tool.isort]
# isort configuration:
# https://github.com/timothycrosley/isort/wiki/isort-Settings
include_trailing_comma = true
use_parentheses = true
# See https://github.com/timothycrosley/isort#multi-line-output-modes
multi_line_output = 3
default_section = "FIRSTPARTY"
line_length = 79

profile = "black"

[tool.pylint]
output-format = "colorized"
max-line-length = 88 # PEP 8
max-args = 6
max-args = 7

[tool.pylint.'MESSAGES CONTROL']
disable = "R0903, R0801"
Expand All @@ -156,7 +148,9 @@ addopts = [

[tool.coverage.run]
omit = [
"render_static/tests/app1/static_jinja2/batch_test/**/*"
"render_static/tests/app1/static_jinja2/batch_test/**/*",
"render_static/tests/app1/static_jinja2/**/*",
"render_static/tests/app2/static_jinja2/**/*"
]

[tool.black]
Expand Down
16 changes: 12 additions & 4 deletions render_static/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
/_/ |_|\___/_/ /_/\__,_/\___/_/ /____/\__/\__,_/\__/_/\___/
"""
from datetime import datetime

from .context import resolve_context
from .resource import resource
from .transpilers.defines_to_js import DefaultDefineTranspiler
from .transpilers.enums_to_js import EnumClassWriter
from .transpilers.urls_to_js import ClassURLWriter, SimpleURLWriter

VERSION = (2, 1, 3)
VERSION = (2, 2, 0)

__title__ = "Django Render Static"
__version__ = ".".join(str(i) for i in VERSION)
__author__ = "Brian Kohan"
__license__ = "MIT"
__copyright__ = f"Copyright 2020-{datetime.now().year} Brian Kohan"
__copyright__ = "Copyright 2020-2024 Brian Kohan"


__all__ = [
"resource",
"resolve_context",
"DefaultDefineTranspiler",
"EnumClassWriter",
"ClassURLWriter",
"SimpleURLWriter",
]


class Jinja2DependencyNeeded: # pylint: disable=R0903
Expand Down
57 changes: 43 additions & 14 deletions render_static/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
"""
from os.path import normpath
from pathlib import Path
from typing import Dict, List, Tuple
from typing import Dict, Generator, List, Tuple

from django.apps import apps
from django.apps.config import AppConfig
from django.template.backends.django import (
DjangoTemplates,
TemplateDoesNotExist,
)
from django.template import Template
from django.template.backends.django import DjangoTemplates, TemplateDoesNotExist

from render_static import Jinja2DependencyNeeded
from render_static.loaders.django import SearchableLoader
from render_static.loaders.jinja2 import SearchableLoader as SearchableJinja2Loader
from render_static.loaders.jinja2 import StaticFileSystemBatchLoader
from render_static.origin import AppOrigin
from render_static.templatetags import render_static
Expand Down Expand Up @@ -94,12 +95,25 @@ def select_templates(
f"Template selector {selector} did not resolve to any template " f"names."
)

def search_templates(
self, prefix: str, first_loader: bool = False
) -> Generator[Template, None, None]:
"""
Resolves a partial template selector into a list of template names from the
loaders configured on this backend engine.
:param prefix: The template prefix to search for
:param first_loader: Search only the first loader
:return: The list of resolved template names
"""
for loader in self.engine.template_loaders[: 1 if first_loader else None]:
if isinstance(loader, SearchableLoader):
yield from loader.search(prefix)


try:
from django.template.backends.jinja2 import ( # pylint: disable=C0412
Jinja2,
Template,
)
from django.template.backends.jinja2 import Jinja2 # pylint: disable=C0412
from django.template.backends.jinja2 import Template as Jinja2Template
from jinja2 import Environment

def default_env(**options):
Expand Down Expand Up @@ -159,7 +173,7 @@ def __init__(self, params: Dict) -> None:

super().__init__(params)

def get_template(self, template_name: str) -> Template:
def get_template(self, template_name: str) -> Jinja2Template:
"""
We override the Jinja2 get_template method so we can monkey patch
in the AppConfig of the origin if this template was from an app
Expand All @@ -183,7 +197,7 @@ def get_template(self, template_name: str) -> Template:
def select_templates(
self,
selector: str,
first_loader: bool = False,
first_loader: bool = False, # pylint: disable=unused-argument
first_preference: bool = False,
) -> List[str]:
"""
Expand All @@ -210,15 +224,30 @@ def select_templates(
self.get_template(selector)
template_names.add(selector)

if first_loader and template_names:
return list(template_names)

if template_names:
return list(template_names)

raise TemplateDoesNotExist(
f"Template selector {selector} did not resolve to any "
f"template names."
)

def search_templates(
self,
prefix: str,
first_loader: bool = False, # pylint: disable=unused-argument
) -> Generator[Jinja2Template, None, None]:
"""
Resolves a partial template selector into a list of template names from the
loaders configured on this backend engine.
:param prefix: The template prefix to search for
:param first_loader: This is ignored for the Jinja2 engine because there is
only one loader
:return: The list of resolved template names
"""
if isinstance(self.env.loader, SearchableJinja2Loader):
yield from self.env.loader.search(self.env, prefix)

except ImportError:
StaticJinja2Templates = Jinja2DependencyNeeded # type: ignore
1 change: 1 addition & 0 deletions render_static/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Callable, Dict, Optional, Sequence, Tuple, Union

from django.utils.module_loading import import_string

from render_static.exceptions import InvalidContext

try:
Expand Down
Loading

0 comments on commit 456cf84

Please sign in to comment.