refactor: give plugins an Entry based read/write interface and stop printing from the library

This commit is contained in:
randogoth 2026-10-24 22:15:10 +03:00
parent 143c2b75ef
commit 93661d664a
14 changed files with 263 additions and 209 deletions

View file

@ -7,6 +7,7 @@ import pytest
import orvanitag.orvani as orvani
from orvanitag.orvani import OrvaniTag
from orvanitag.errors import UnsupportedFile
@pytest.fixture
@ -56,7 +57,7 @@ def test_php_tag_goes_inside_the_php_block(cwd):
def test_php_without_own_opening_line_is_refused(cwd):
with pytest.raises(ValueError):
with pytest.raises(UnsupportedFile):
write('p.php', '<?php echo "ok"; ?>\n', '#demo')
assert (cwd / 'p.php').read_text() == '<?php echo "ok"; ?>\n'

View file

@ -4,6 +4,7 @@ import pytest
import orvanitag.orvani as orvani
from orvanitag.orvani import OrvaniTag
from orvanitag.errors import UnsupportedFile
CONTAINER = (
'<?xml version="1.0"?><container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">'
@ -114,6 +115,6 @@ def test_cbz_foreign_comment_is_kept(cwd):
make_cbz(cwd / 'comic.cbz', b'{"ComicBookInfo/1.0": {}}')
before = (cwd / 'comic.cbz').read_bytes()
OrvaniTag().add('comic.cbz', '#manga')
with pytest.raises(ValueError):
with pytest.raises(UnsupportedFile):
OrvaniTag().write_metadata('comic.cbz', 'ebook')
assert (cwd / 'comic.cbz').read_bytes() == before

View file

@ -0,0 +1,54 @@
import sys
import pytest
from pypdf import PdfWriter
import orvanitag.orvani as orvani
from orvanitag.cmd import main
from orvanitag.entry import Entry
from orvanitag.orvani import OrvaniTag
from orvanitag.plugins import comment
@pytest.fixture
def cwd(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
return tmp_path
def test_plugins_take_and_return_entries(cwd):
(cwd / 'a.py').write_text('print(1)\n')
comment.write(cwd / 'a.py', Entry(tags=['demo'], note='a note'), {})
assert comment.read(cwd / 'a.py', {}) == Entry(tags=['demo'], note='a note')
def test_scan_returns_found_tags_and_errors_without_printing(cwd, capsys):
writer = PdfWriter()
writer.add_blank_page(72, 72)
writer.add_metadata({'/Keywords': '#lofi'})
writer.write(cwd / 'good.pdf')
(cwd / 'broken.epub').write_text('not a zip')
found, errors = OrvaniTag().scan('.', False)
assert found == {'good.pdf': Entry(tags=['lofi'])}
assert len(errors) == 1 and 'broken.epub' in errors[0]
assert capsys.readouterr().out == ''
def test_cmd_shows_plugin_refusals_as_user_errors(cwd, monkeypatch, capsys):
(cwd / 'p.php').write_text('<?php echo 1; ?>\n')
OrvaniTag().add('p.php', '#demo')
monkeypatch.setattr(sys, 'argv', ['otag', 'w', 'comment', 'p.php'])
with pytest.raises(SystemExit) as exit:
main()
out = capsys.readouterr().out
assert exit.value.code == 1
assert 'PHP files need' in out and 'An Exception occurred' not in out
def test_configured_types_narrow_reading_too(cwd):
(cwd / orvani.CONFIG_FILE).write_text('[comment]\ntypes = [".py"]\n')
(cwd / 'a.py').write_text('# otag: #python\n')
(cwd / 'a.sh').write_text('# otag: #shell\n')
found, errors = OrvaniTag().scan('.', False)
assert found == {'a.py': Entry(tags=['python'])}