mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-24 03:09:56 +00:00
Compare commits
3 Commits
5aa72c4c71
...
3f642ebcf1
Author | SHA1 | Date | |
---|---|---|---|
3f642ebcf1 | |||
38571eba04 | |||
9e346530f2 |
@ -95,19 +95,6 @@ class DuplicateRunError(RuntimeError):
|
||||
self, "Another application instance is run. This error can be suppressed by using --force flag.")
|
||||
|
||||
|
||||
class EncodeError(ValueError):
|
||||
"""
|
||||
exception used for bytes encoding errors
|
||||
"""
|
||||
|
||||
def __init__(self, encodings: list[str]) -> None:
|
||||
"""
|
||||
Args:
|
||||
encodings(list[str]): list of encodings tried
|
||||
"""
|
||||
ValueError.__init__(self, f"Could not encode bytes by using {encodings}")
|
||||
|
||||
|
||||
class ExitCode(RuntimeError):
|
||||
"""
|
||||
special exception which has to be thrown to return non-zero status without error message
|
||||
|
@ -24,7 +24,6 @@ from pathlib import Path
|
||||
from typing import Any, ClassVar, IO, Self
|
||||
|
||||
from ahriman.core.alpm.pkgbuild_parser import PkgbuildParser, PkgbuildToken
|
||||
from ahriman.core.exceptions import EncodeError
|
||||
from ahriman.models.pkgbuild_patch import PkgbuildPatch
|
||||
|
||||
|
||||
@ -34,13 +33,13 @@ class Pkgbuild(Mapping[str, Any]):
|
||||
model and proxy for PKGBUILD properties
|
||||
|
||||
Attributes:
|
||||
DEFAULT_ENCODINGS(list[str]): (class attribute) list of encoding to be applied on the file content
|
||||
DEFAULT_ENCODINGS(str): (class attribute) default encoding to be applied on the file content
|
||||
fields(dict[str, PkgbuildPatch]): PKGBUILD fields
|
||||
"""
|
||||
|
||||
fields: dict[str, PkgbuildPatch]
|
||||
|
||||
DEFAULT_ENCODINGS: ClassVar[list[str]] = ["utf8", "latin-1"]
|
||||
DEFAULT_ENCODINGS: ClassVar[str] = "utf8"
|
||||
|
||||
@property
|
||||
def variables(self) -> dict[str, str]:
|
||||
@ -58,13 +57,13 @@ class Pkgbuild(Mapping[str, Any]):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, path: Path, encodings: list[str] | None = None) -> Self:
|
||||
def from_file(cls, path: Path, encoding: str | None = None) -> Self:
|
||||
"""
|
||||
parse PKGBUILD from the file
|
||||
|
||||
Args:
|
||||
path(Path): path to the PKGBUILD file
|
||||
encodings(list[str] | None, optional): the encoding of the file (Default value = None)
|
||||
encoding(str | None, optional): the encoding of the file (Default value = None)
|
||||
|
||||
Returns:
|
||||
Self: constructed instance of self
|
||||
@ -77,15 +76,10 @@ class Pkgbuild(Mapping[str, Any]):
|
||||
content = input_file.read()
|
||||
|
||||
# decode bytes content based on either
|
||||
encodings = encodings or cls.DEFAULT_ENCODINGS
|
||||
for encoding in encodings:
|
||||
try:
|
||||
io = StringIO(content.decode(encoding))
|
||||
return cls.from_io(io)
|
||||
except ValueError:
|
||||
pass
|
||||
encoding = encoding or cls.DEFAULT_ENCODINGS
|
||||
io = StringIO(content.decode(encoding, errors="backslashreplace"))
|
||||
|
||||
raise EncodeError(encodings)
|
||||
return cls.from_io(io)
|
||||
|
||||
@classmethod
|
||||
def from_io(cls, stream: IO[str]) -> Self:
|
||||
|
@ -3,9 +3,7 @@ import pytest
|
||||
from io import BytesIO, StringIO
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.core.exceptions import EncodeError
|
||||
from ahriman.models.pkgbuild import Pkgbuild
|
||||
from ahriman.models.pkgbuild_patch import PkgbuildPatch
|
||||
|
||||
@ -46,18 +44,6 @@ def test_from_file_latin(pkgbuild_ahriman: Pkgbuild, mocker: MockerFixture) -> N
|
||||
load_mock.assert_called_once_with(pytest.helpers.anyvar(int))
|
||||
|
||||
|
||||
def test_from_file_unknown_encoding(pkgbuild_ahriman: Pkgbuild, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must raise exception when encoding is unknown
|
||||
"""
|
||||
open_mock = mocker.patch("pathlib.Path.open")
|
||||
io_mock = open_mock.return_value.__enter__.return_value = MagicMock()
|
||||
io_mock.read.return_value.decode.side_effect = EncodeError(pkgbuild_ahriman.DEFAULT_ENCODINGS)
|
||||
|
||||
with pytest.raises(EncodeError):
|
||||
assert Pkgbuild.from_file(Path("local"))
|
||||
|
||||
|
||||
def test_from_io(pkgbuild_ahriman: Pkgbuild, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must correctly load from io
|
||||
|
7
tox.ini
7
tox.ini
@ -1,6 +1,6 @@
|
||||
[tox]
|
||||
envlist = check, tests
|
||||
isolated_build = True
|
||||
isolated_build = true
|
||||
labels =
|
||||
release = version, docs, publish
|
||||
dependencies = -e .[journald,pacman,s3,shell,stats,validator,web]
|
||||
@ -27,7 +27,9 @@ description = Run common checks like linter, mypy, etc
|
||||
deps =
|
||||
{[tox]dependencies}
|
||||
-e .[check]
|
||||
pip_pre = true
|
||||
setenv =
|
||||
CFLAGS="-Wno-unterminated-string-initialization"
|
||||
MYPYPATH=src
|
||||
commands =
|
||||
autopep8 --exit-code --max-line-length 120 -aa -i -j 0 -r "src/{[tox]project_name}" "tests/{[tox]project_name}"
|
||||
@ -65,7 +67,7 @@ description = Generate html documentation
|
||||
deps =
|
||||
{[tox]dependencies}
|
||||
-e .[docs]
|
||||
recreate = True
|
||||
recreate = true
|
||||
commands =
|
||||
sphinx-build -b html -a -j auto -W docs {envtmpdir}{/}html
|
||||
|
||||
@ -89,6 +91,7 @@ description = Run tests
|
||||
deps =
|
||||
{[tox]dependencies}
|
||||
-e .[tests]
|
||||
pip_pre = true
|
||||
commands =
|
||||
pytest {posargs}
|
||||
|
||||
|
Reference in New Issue
Block a user