Skip to content

Commit 4974416

Browse files
committed
Linting and cleanup
1 parent 33962ca commit 4974416

File tree

13 files changed

+30
-45
lines changed

13 files changed

+30
-45
lines changed

logya/content.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ def create_url(path: Path) -> str:
4646
suffix = ''
4747
if path.suffix in remove_extensions:
4848
suffix = '/'
49-
if path.stem == 'index':
50-
path = Path(path.parent)
51-
else:
52-
path = path.parent.joinpath(path.stem)
49+
path = Path(path.parent) if path.stem == 'index' else path.parent.joinpath(path.stem)
5350

5451
if not path.parts:
5552
return '/'

logya/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def update_collections(self, doc: dict):
101101

102102
# Add attribute for creating collection links in templates.
103103
links = attr + '_links'
104-
doc[links] = doc.get(links, []) + [(url, value)]
104+
doc[links] = [*doc.get(links, []), (url, value)]
105105

106106
# Update or create collection index value.
107107
if coll_data := coll['index'].get(url):

logya/docparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def parse(content, content_type=None):
1717
header_end = lines[header_start:].index('---') + 1
1818
header = '\n'.join(lines[header_start:header_end])
1919
body = '\n'.join(lines[header_end + 1:]).strip()
20-
parsed = load(header, Loader=Loader)
20+
parsed = load(header, Loader=Loader) # noqa: S506
2121

2222
# Parse body if not HTML/XML.
2323
if body and content_type == 'markdown':

logya/encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class JSONEncoder(json.JSONEncoder):
66
def default(self, obj):
77
if isinstance(obj, datetime.date | datetime.datetime):
88
return obj.isoformat(sep=' ', timespec='seconds')
9-
elif isinstance(obj, datetime.timedelta):
9+
if isinstance(obj, datetime.timedelta):
1010
return (datetime.datetime.min + obj).time().isoformat(sep=' ', timespec='seconds')
1111

1212
return json.JSONEncoder.default(self, obj)

logya/server.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
1414
L: Logya
1515

1616
def __init__(self, *args):
17-
super(HTTPRequestHandler, self).__init__(*args, directory=self.L.paths.public.as_posix())
17+
super().__init__(*args, directory=self.L.paths.public.as_posix())
1818

19-
def do_GET(self):
19+
def do_GET(self): # noqa: N802
2020
update_resource(self.path, self.L)
21-
super(HTTPRequestHandler, self).do_GET()
21+
super().do_GET()
2222

2323

24-
def update_page(url: str, L: Logya):
24+
def update_page(url: str, L: Logya) -> bool:
2525
"""Update content or collection page."""
2626

2727
if content := L.doc_index.get(url):
@@ -39,6 +39,8 @@ def update_page(url: str, L: Logya):
3939
L.info(f'Refreshed collection: {url}')
4040
return True
4141

42+
return False
43+
4244

4345
def update_resource(path: str, L: Logya) -> None:
4446
"""Update resource corresponding to given url.

logya/template.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from logya.util import cache, slugify
1111

1212
env = Environment(
13+
autoescape=True,
1314
lstrip_blocks=True,
1415
trim_blocks=True
1516
)
@@ -31,9 +32,9 @@ def _alpha_index(
3132
key = value.lower()[0]
3233
if key not in ascii_lowercase:
3334
key = non_ascii_key
34-
index[key] = index.get(key, []) + [item]
35+
index[key] = [*index.get(key, []), item]
3536

36-
reverse = False if sort_order == 'ascending' else True
37+
reverse = (sort_order == 'ascending')
3738
keys = sorted(index.keys(), reverse=reverse)
3839
return {key: sorted(index[key], key=itemgetter(sort_attr)) for key in keys}
3940

@@ -78,7 +79,7 @@ def _get_docs(L, url: str, sort_attr: str = 'created', sort_order: str = 'descen
7879
if doc_url.startswith(url):
7980
docs.append(content['doc'])
8081

81-
reverse = True if sort_order == 'descending' else False
82+
reverse = (sort_order == 'descending')
8283
return sorted((d for d in docs if sort_attr in d), key=lambda item: _sort_docs(item, sort_attr), reverse=reverse)
8384

8485

logya/util.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
2-
from collections import namedtuple
32
from pathlib import Path
43
from string import punctuation, whitespace
4+
from typing import NamedTuple
55

66
from yaml import dump, load
77

@@ -16,7 +16,13 @@
1616
forbidden = (set(punctuation) - set('+-_.,@')) | set(whitespace)
1717
re_forbidden = re.compile(f'[{re.escape("".join(forbidden))}]+')
1818

19-
Paths = namedtuple('Paths', ['root', 'content', 'public', 'static', 'templates'])
19+
# For accessing site paths.
20+
class Paths(NamedTuple):
21+
root: str
22+
content: str
23+
public: str
24+
static: str
25+
templates: str
2026

2127

2228
def cache(func):
@@ -41,7 +47,7 @@ def encode_content(headers: dict, body: str) -> str:
4147
def load_yaml(text: str) -> dict:
4248
"""Wrapper for yaml.load so yaml import is done only once."""
4349

44-
return load(text, Loader=Loader)
50+
return load(text, Loader=Loader) # noqa: S506
4551

4652

4753
def paths(dir_site: str) -> Paths:

pyproject.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ dependencies = [
4343
logya = "logya.main:main"
4444

4545
[project.urls]
46-
Documentation = "https://github.com/yaph/logya#readme"
46+
Documentation = "https://ramiro.org/logya/docs/"
4747
Issues = "https://github.com/yaph/logya/issues"
4848
Source = "https://github.com/yaph/logya"
4949

@@ -94,8 +94,11 @@ exclude_lines = [
9494
]
9595

9696
[tool.ruff.lint]
97-
ignore = ["T201", "DTZ005", "FA100"]
97+
ignore = ["DTZ006", "FA100", "FBT001", "FBT002", "N803", "N806", "T201"]
9898

9999
[tool.ruff.format]
100100
quote-style = "preserve"
101-
skip-magic-trailing-comma = true
101+
skip-magic-trailing-comma = true
102+
103+
[tool.ruff.lint.per-file-ignores]
104+
"tests/*" = ["INP", "N", "S"]

tests/__init__.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/test_content.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
from pathlib import Path
42

53
import logya.content

tests/test_core.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
import pytest
32

43
import logya.core

tests/test_template.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
import pytest
32

43
import logya.core

tests/test_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def test_encode_content():
88
text = logya.util.encode_content({}, '')
9-
assert text.count('---\n') == 2
9+
assert text.count('---\n') == 2 # noqa: PLR2004
1010

1111

1212
def test_slugify():

0 commit comments

Comments
 (0)