Skip to content

Commit

Permalink
Merge pull request #14 from 153957/ruff
Browse files Browse the repository at this point in the history
Replace flake8 by ruff and add type annotations
  • Loading branch information
153957 committed Jun 4, 2023
2 parents bd5415a + bb74f99 commit 6707520
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ charset = utf-8
end_of_line = lf

[*.py]
max_line_length = 79
max_line_length = 119

[*.md]
trim_trailing_whitespace = false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
cache: 'pip'
cache-dependency-path: 'requirements*.txt'
- run: make devinstall
- run: make flaketest
- run: make linttest
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ install:
pip install --upgrade pip
pip install --upgrade --upgrade-strategy eager -r requirements.txt

.PHONY: flaketest
flaketest:
flake8
.PHONY: linttest
linttest:
ruff .

.PHONY: clean
clean:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '3.7'
services:

services:
pelican:
image: 'python:3.11'
restart: unless-stopped
Expand Down
6 changes: 4 additions & 2 deletions pelicanconf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Base and development configuration for Pelican"""

from pathlib import Path

PROJECT_DIR = Path(__file__).parent
Expand Down Expand Up @@ -78,8 +80,8 @@
TAGS_SAVE_AS = False
INDEX_SAVE_AS = False

# Uncomment following line if you want document-relative URLs when developing
# RELATIVE_URLS = True
# Set the following to True if you want document-relative URLs when developing
RELATIVE_URLS = False

# No feeds, no blog!
FEED_ALL_ATOM = None
Expand Down
1 change: 1 addition & 0 deletions plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Custom Pelican plugins used for this project"""
14 changes: 6 additions & 8 deletions plugins/libsass.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
"""
libsass
"""libsass
This plugin compiles sass/scss files to plain css.
"""
import sass

from pelican import signals
from pelican import Pelican, signals

SETTINGS_NAME = 'LIBSASS_PATHS'


def compile_sass(instance):
"""
Compile SASS/SCSS with libsass to CSS
"""
def compile_sass(instance: Pelican) -> None:
"""Compile SASS/SCSS with libsass to CSS"""
paths = instance.settings.get(SETTINGS_NAME, [])

for input_output_paths in paths:
sass.compile(dirname=input_output_paths)


def register():
def register() -> None:
"""Register with pelican"""
signals.finalized.connect(compile_sass)
54 changes: 31 additions & 23 deletions plugins/shortcodes.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""
shortcodes.py
=============
"""shortcodes
Copyright: AGPLv3 <Bernardas Ališauskas @ bernardas.alisauskas@pm.me>
This plugin allows to define macros called shortcodes in content pages that will be expanded as jinja2 templates.
The purpose of this plugin is to allow explicit and quick jinja2 templating in your content without compromising markdown/rst.
The purpose of this plugin is to allow explicit and quick jinja2 templating in your content without compromising
markdown/rst.
Example:
Example usage:
#pelicanconf.py
In `pelicanconf.py` add:
SHORTCODES = {
'image': "<img src=/images/{{src}}>{{desc|title}}<img>"
Expand All @@ -18,41 +18,49 @@
[% image src=foo.png desc="this is a test" %]
will become:
which will become:
<img src=/images/foo.png>This Is A Test<img>
"""
import re

from functools import partial

from jinja2 import Template

from pelican import signals
from pelican import Pelican, signals

SETTINGS_NAME = 'SHORTCODES'


def expand_shortcodes(text, shortcodes) -> str:
def replace_shortcode(group):
"""replace shortcodes with evaluated templates"""
match = group.groups()[0]
code, _, args = match.partition(' ')
args = re.split(r'(\w+)=', args)
args = [a.strip('\'" ') for a in args if a]
kwargs = {args[i]: args[i + 1] for i in range(0, len(args), 2)}
try:
return Template(shortcodes[code]).render(**kwargs)
except KeyError:
raise KeyError(f'shortcode "{code}" not found')
def replace_shortcode(group: re.Match, shortcodes: dict[str, str]) -> str:
"""Replace shortcodes with evaluated templates"""
match = group.groups()[0]
code, _, args = match.partition(' ')
args = re.split(r'(\w+)=', args)
args = [a.strip('\'" ') for a in args if a]
kwargs = {args[i]: args[i + 1] for i in range(0, len(args), 2)}
try:
return Template(shortcodes[code]).render(**kwargs)
except KeyError:
raise KeyError(f'shortcode "{code}" not found') from None


return re.sub(r'\[% ([\w\W]+?) %\]', replace_shortcode, text, flags=re.MULTILINE)
def expand_shortcodes(text: str, shortcodes: dict[str, str]) -> str:
"""Find all shortcodes and apply the replacement templates"""
shortcode_replacer = partial(replace_shortcode, shortcodes=shortcodes)
return re.sub(r'\[% ([\w\W]+?) %\]', shortcode_replacer, text, flags=re.MULTILINE)


def content_object_init(instance):
def content_object_init(instance: Pelican) -> None:
"""Apply shortcodes to content, replacing original content"""
shortcodes = instance.settings.get(SETTINGS_NAME)
if not shortcodes or not instance._content:
return
instance._content = expand_shortcodes(instance._content, shortcodes)


def register():
def register() -> None:
"""Register with pelican"""
signals.content_object_init.connect(content_object_init)
20 changes: 10 additions & 10 deletions plugins/thumbnails.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
thumbnails
"""thumbnails
This plugin creates half size versions of images in specified path.
Expand All @@ -10,28 +9,28 @@
"""
from multiprocessing import Pool
from pathlib import Path

from PIL import Image

from pelican import signals
from pelican import Pelican, signals

SETTINGS_NAME = 'THUMBNAIL_PATHS'


def create_thumbnail(image_path):
def create_thumbnail(image_path: Path) -> None:
"""Create half size (halved width and height) thumbnail"""
extension = image_path.suffix
image = Image.open(image_path)
image.thumbnail((
image.size[0] / 2,
image.size[1] / 2
image.size[1] / 2,
))
image.save(str(image_path).replace(f'@2x{extension}', extension))


def create_thumbnails(instance):
"""
Create half size versions of '@2x' images in given paths
"""
def create_thumbnails(instance: Pelican) -> None:
"""Create half size versions of '@2x' images in given paths"""
extensions = ['.png', '.jpg', '.jpeg']
paths = instance.settings.get(SETTINGS_NAME, [])

Expand All @@ -46,5 +45,6 @@ def create_thumbnails(instance):
pool.map(create_thumbnail, image_paths)


def register():
def register() -> None:
"""Register with pelican"""
signals.finalized.connect(create_thumbnails)
4 changes: 3 additions & 1 deletion publishconf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pelicanconf import * # noqa: F401, F403
"""Production configuration for Pelican"""

from pelicanconf import * # noqa: F403

SITEURL = 'https://arne.delaat.net'
RELATIVE_URLS = False
Expand Down
47 changes: 47 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[tool.black]
target-version = ['py311']
line-length = 120
skip-string-normalization = true

[tool.ruff]
line-length = 120
target-version = 'py311'
format = 'github'

select = [
# https://github.com/charliermarsh/ruff#supported-rules
'ALL',
'E111', 'E112', 'E113', 'E114', 'E115', 'E116', 'E117',
'E201', 'E202', 'E203',
'E211',
'E221', 'E222', 'E223', 'E224', 'E225', 'E226', 'E227', 'E228',
'E231',
'E251', 'E252',
'E261', 'E262', 'E265', 'E266',
'E271', 'E272', 'E273', 'E274', 'E275',
]

ignore = [
'D213', # Start multi-line docstring on first line
'D203', # Had to disable one of these (D211)
'D400', # Do not require . at end of first line in docstring
'D415', # Do not require ./!/? at end of first line in docstring
'EM102', # Allow f-string in exception message
'Q', # Currently use single quotes
'SLF001', # Allow accessing private attributes
'TRY003', # Allow long messages in exceptions
]

[tool.ruff.isort]
lines-between-types = 1
section-order = [
'future',
'standard-library',
'third-party',
'pelican',
'first-party',
'local-folder',
]

[tool.ruff.isort.sections]
pelican = ['pelican']
3 changes: 1 addition & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
flake8
flake8-isort
ruff

0 comments on commit 6707520

Please sign in to comment.