From 33962ca244ab64edbdff512df0d687106f9b166d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramiro=20G=C3=B3mez?= Date: Tue, 5 Nov 2024 23:13:30 +0100 Subject: [PATCH] Fix linting issues. --- logya/__init__.py | 1 - logya/content.py | 6 ++---- logya/core.py | 10 ++++------ logya/create.py | 2 -- logya/docparser.py | 3 +-- logya/encoder.py | 5 +---- logya/generate.py | 4 +--- logya/main.py | 1 - logya/server.py | 6 ++---- logya/template.py | 2 -- logya/util.py | 6 +++--- setup.py | 2 -- tests/__init__.py | 1 - tests/test_cli.py | 14 ++++++-------- tests/test_content.py | 12 ++++-------- tests/test_core.py | 4 ++-- tests/test_stdlib.py | 18 ------------------ tests/test_template.py | 3 +-- tests/test_util.py | 6 ++---- 19 files changed, 29 insertions(+), 77 deletions(-) delete mode 100644 tests/test_stdlib.py diff --git a/logya/__init__.py b/logya/__init__.py index d8e9484..b7bf8bd 100644 --- a/logya/__init__.py +++ b/logya/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- __author__ = 'Ramiro Gómez' __email__ = 'code@ramiro.org' __version__ = '5.2.0' diff --git a/logya/content.py b/logya/content.py index a34cdd6..e438e14 100644 --- a/logya/content.py +++ b/logya/content.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from datetime import datetime from operator import itemgetter from pathlib import Path @@ -8,7 +7,6 @@ from logya.template import render from logya.util import load_yaml, slugify - # Extensions of content files that will be processed. process_extensions = { '.css', @@ -48,7 +46,7 @@ def create_url(path: Path) -> str: suffix = '' if path.suffix in remove_extensions: suffix = '/' - if 'index' == path.stem: + if path.stem == 'index': path = Path(path.parent) else: path = path.parent.joinpath(path.stem) @@ -93,7 +91,7 @@ def read(path: Path, path_rel: Path, markdown_extensions: list) -> dict | None: print(f'Error reading/parsing: {path}\n{err}') return None - if 'markdown' == content_type(path): + if content_type(path) == 'markdown': doc['body'] = markdown(doc['body'], extensions=markdown_extensions) # Ensure doc has a title. diff --git a/logya/core.py b/logya/core.py index 75903d4..ad037e9 100644 --- a/logya/core.py +++ b/logya/core.py @@ -1,13 +1,11 @@ -# -*- coding: utf-8 -*- -import yaml - from collections import ChainMap from os import walk from pathlib import Path from sys import exit -from typing import Dict -from logya.content import read, process_extensions +import yaml + +from logya.content import process_extensions, read from logya.template import init_env from logya.util import load_yaml, paths, slugify @@ -29,7 +27,7 @@ def __init__(self, dir_site: str = '.', verbose: bool = False) -> None: exit('Error: The site configuration file site.yaml could not be parsed.') # Initialize index and collections so scripts can generate indexed content before build. - self.doc_index: Dict[str, dict] = {} + self.doc_index: dict[str, dict] = {} self.collections = self.settings.get('collections', {}) for coll in self.collections.values(): coll['index'] = {} diff --git a/logya/create.py b/logya/create.py index 9f4b123..89852af 100644 --- a/logya/create.py +++ b/logya/create.py @@ -1,7 +1,5 @@ -# -*- coding: utf-8 -*- import shutil import sys - from importlib import resources from logya.util import paths diff --git a/logya/docparser.py b/logya/docparser.py index b614ab4..c0e7099 100644 --- a/logya/docparser.py +++ b/logya/docparser.py @@ -1,7 +1,6 @@ -# -*- coding: utf-8 -*- import markdown - from yaml import load + try: from yaml import CLoader as Loader except ImportError: diff --git a/logya/encoder.py b/logya/encoder.py index a128f8b..c5246e1 100644 --- a/logya/encoder.py +++ b/logya/encoder.py @@ -1,13 +1,10 @@ -# -*- coding: utf-8 -*- import datetime import json class JSONEncoder(json.JSONEncoder): def default(self, obj): - if isinstance(obj, datetime.datetime): - return obj.isoformat(sep=' ', timespec='seconds') - elif isinstance(obj, datetime.date): + if isinstance(obj, datetime.date | datetime.datetime): return obj.isoformat(sep=' ', timespec='seconds') elif isinstance(obj, datetime.timedelta): return (datetime.datetime.min + obj).time().isoformat(sep=' ', timespec='seconds') diff --git a/logya/generate.py b/logya/generate.py index 8a44137..ca840d0 100644 --- a/logya/generate.py +++ b/logya/generate.py @@ -1,10 +1,8 @@ -# -*- coding: utf-8 -*- import shutil - from shutil import copytree -from logya.core import Logya from logya.content import write_collection, write_page +from logya.core import Logya def generate(dir_site: str, verbose: bool, keep: bool, **kwargs): diff --git a/logya/main.py b/logya/main.py index 15f013c..030da91 100755 --- a/logya/main.py +++ b/logya/main.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- import argparse from logya import __version__ diff --git a/logya/server.py b/logya/server.py index 73efdfd..55be54d 100644 --- a/logya/server.py +++ b/logya/server.py @@ -1,12 +1,10 @@ -# -*- coding: utf-8 -*- import http.server import socketserver - from shutil import copyfile from urllib.parse import unquote, urlparse -from logya.core import Logya from logya.content import read, write_collection, write_page +from logya.core import Logya from logya.template import env @@ -88,4 +86,4 @@ def serve(dir_site: str, verbose: bool, host: str, port: int, **kwargs) -> None: socketserver.TCPServer.allow_reuse_address = True with socketserver.TCPServer((host, port), HTTPRequestHandler) as httpd: print(f'Serving on {base_url}') - httpd.serve_forever() \ No newline at end of file + httpd.serve_forever() diff --git a/logya/template.py b/logya/template.py index 5d34a8d..70898d8 100644 --- a/logya/template.py +++ b/logya/template.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from datetime import datetime from operator import itemgetter from pathlib import Path @@ -10,7 +9,6 @@ from logya.util import cache, slugify - env = Environment( lstrip_blocks=True, trim_blocks=True diff --git a/logya/util.py b/logya/util.py index a01a348..e912495 100644 --- a/logya/util.py +++ b/logya/util.py @@ -1,13 +1,13 @@ -# -*- coding: utf-8 -*- import re - from collections import namedtuple from pathlib import Path from string import punctuation, whitespace from yaml import dump, load + try: - from yaml import CDumper as Dumper, CLoader as Loader + from yaml import CDumper as Dumper + from yaml import CLoader as Loader except ImportError: # pragma: no cover from yaml import Dumper, Loader # type: ignore diff --git a/setup.py b/setup.py index 45db3d8..a833116 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- from pathlib import Path try: @@ -9,7 +8,6 @@ from logya import __version__ - setup( name='logya', version=__version__, diff --git a/tests/__init__.py b/tests/__init__.py index 08ccb52..03ed6f3 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import unittest diff --git a/tests/test_cli.py b/tests/test_cli.py index 6b0855f..3e573de 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,34 +1,32 @@ -# -*- coding: utf-8 -*- import subprocess - from shutil import rmtree def run(command): - return subprocess.run(command, capture_output=True, shell=True, text=True) + return subprocess.run(command, capture_output=True, shell=True, text=True, check=False) def test_generate(): out = run('python logya/main.py gen --dir-site logya/sites/docs') - assert 0 == out.returncode + assert out.returncode == 0 assert 'write pages' in out.stdout.lower() def test_generate_wrong_dir(): out = run('python logya/main.py gen --dir-site .') - assert 1 == out.returncode + assert out.returncode == 1 assert 'error' in out.stderr.lower() def test_create(): out = run('python logya/main.py create test_create_site --dir-site tests/fixtures/') - assert 0 == out.returncode + assert out.returncode == 0 assert 'site created' in out.stdout.lower() # Second time must fail out2 = run('python logya/main.py create test_create_site --dir-site tests/fixtures/') - assert 1 == out2.returncode + assert out2.returncode == 1 assert 'already exists' in out2.stderr.lower() # Cleanup - rmtree('tests/fixtures/test_create_site') \ No newline at end of file + rmtree('tests/fixtures/test_create_site') diff --git a/tests/test_content.py b/tests/test_content.py index 168f1f0..606eb60 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -1,14 +1,10 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- -import pytest - -import logya.content from pathlib import Path +import logya.content from logya.util import paths - markdown_extensions = ['attr_list', 'def_list', 'fenced_code', 'toc'] site_root = 'tests/fixtures/site/' site_paths = paths(site_root) @@ -53,7 +49,7 @@ def test_filepath(): def test_parse_empty_body(): md = Path(site_root, 'content', 'empty-body.md') doc = logya.content.read(md, md.relative_to(site_paths.content), markdown_extensions) - assert '' == doc['body'] + assert doc['body'] == '' def test_parse_error(capsys): @@ -66,7 +62,7 @@ def test_parse_error(capsys): def test_read_auto_url(): md = Path(site_root, 'content', 'separator.md') doc = logya.content.read(md, md.relative_to(site_paths.content), markdown_extensions) - assert '/separator/' == doc['url'] + assert doc['url'] == '/separator/' def test_read_error(capsys): @@ -80,4 +76,4 @@ def test_read_markdown(): md = Path(site_root, 'content', 'markdown.md') doc = logya.content.read(md, md.relative_to(site_paths.content), markdown_extensions) assert 'Link' in doc['body'] - assert 'Link with attributes' in doc['body'] \ No newline at end of file + assert 'Link with attributes' in doc['body'] diff --git a/tests/test_core.py b/tests/test_core.py index ccb0fd2..f23173b 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,9 +1,9 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- import pytest import logya.core + @pytest.fixture def L(): return logya.core.Logya(dir_site='logya/sites/base/') @@ -13,4 +13,4 @@ def test_build(L): L.build() assert '/' in L.doc_index - assert '/404/' in L.doc_index \ No newline at end of file + assert '/404/' in L.doc_index diff --git a/tests/test_stdlib.py b/tests/test_stdlib.py deleted file mode 100644 index 4b7e7fa..0000000 --- a/tests/test_stdlib.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -from collections import ChainMap - - -d1 = { - '/': {'name': 'd1 /'}, - '/a/': {'name': 'd1 /a/'}, - '/b/': {'name': 'd1 /b/'}, -} - -d2 = { - '/': {'name': 'd2 /'}, - '/b/': {'name': 'd2 /b/'}, - '/c/': {'name': 'd2 /c/'}, -} - -cm = ChainMap(d1, d2) \ No newline at end of file diff --git a/tests/test_template.py b/tests/test_template.py index c7ee241..82a7c8f 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- import pytest import logya.core @@ -49,7 +48,7 @@ def test_filesource(): def test_filesource_lines(): text = env_globals['filesource']('content/rss.xml', lines=1) - assert '---' == text + assert text == '---' def test_filesource_image(): diff --git a/tests/test_util.py b/tests/test_util.py index e56f407..859a3c2 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,14 +1,12 @@ -# -*- coding: utf-8 -*- import logya.util - site_root = 'tests/fixtures/site/' site_paths = logya.util.paths(site_root) def test_encode_content(): text = logya.util.encode_content({}, '') - assert 2 == text.count('---\n') + assert text.count('---\n') == 2 def test_slugify(): @@ -27,4 +25,4 @@ def test_slugify(): def test_paths(): - assert site_paths.public.as_posix() == site_root + 'public' \ No newline at end of file + assert site_paths.public.as_posix() == site_root + 'public'