mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 09:56:17 -05:00
A malformed config entry is skipped and counted instead of failing the import (#875)
* A malformed config entry is skipped and counted instead of failing the import An entry that is not a mapping (or whose key is not text, or a list that is not a list) made the whole file fail to import in playlists.yaml, repos.yaml, download_manager.yaml and tags.yaml (tag_files/tag_zims), and a non-text name did in domains.yaml. Each is now skipped and counted in import_skipped, so the config is not saved over it, and the rest of the file imports. A Channel whose `downloads` is not a list keeps its Downloads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * An unreadable entry keeps no row and does not make a section present The key of an entry too malformed to name a row is now `Unreadable(position)`. It is still counted in import_skipped, but `present` ignores it, so a section whose entries are all unreadable removes nothing (as an empty section), instead of deleting every row. A playlist whose `items` is not a list keeps its items, and a Channel Download whose url is not text is counted (its Channel keeps its Downloads) instead of failing the import. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0dd61b8e64
commit
79526afebe
10 changed files with 279 additions and 35 deletions
|
|
@ -25,7 +25,7 @@ from wrolpi.cmd import GIT_BIN, run_command
|
|||
from wrolpi.collections.models import Collection
|
||||
from wrolpi.common import ConfigFile, get_media_directory, get_relative_to_media_directory, logger, \
|
||||
escape_file_name, background_task, get_wrolpi_config
|
||||
from wrolpi.config_import import ConfigSection, media_relative
|
||||
from wrolpi.config_import import ConfigSection, Unreadable, media_relative
|
||||
from wrolpi.config_version import register_ownership
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.downloader import Download, DownloadFrequency, download_manager, save_downloads_config
|
||||
|
|
@ -1397,11 +1397,11 @@ class ReposSection(ConfigSection):
|
|||
media_directory = get_media_directory()
|
||||
rows, invalid = dict(), set()
|
||||
for position, data in enumerate(config.get('repos') or []):
|
||||
url = data.get('url')
|
||||
if not url:
|
||||
logger.error(f'Repo config entry has no url, skipping: {data}')
|
||||
# Counted as skipped; never a Repo's key.
|
||||
invalid.add(f'\0{position}')
|
||||
url = data.get('url') if isinstance(data, dict) else None
|
||||
if not url or not isinstance(url, str):
|
||||
logger.error(f'Repo config entry has no url, skipping: {data!r}')
|
||||
# Counted as skipped (see `Unreadable`).
|
||||
invalid.add(Unreadable(position))
|
||||
continue
|
||||
try:
|
||||
parsed = parse_repo_url(url)
|
||||
|
|
|
|||
|
|
@ -102,6 +102,34 @@ async def test_invalid_entry_is_kept(test_session, saved_repos):
|
|||
assert (found[TWO].mode, found[ONE].collection.description) == ('snapshot', 'Changed')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_malformed_entry_is_skipped(test_session, saved_repos):
|
||||
"""An entry that is not a mapping is skipped and counted; the rest of the file still imports."""
|
||||
edit_repo(saved_repos, ONE, description='Changed')
|
||||
edit_config(saved_repos, lambda data: data['repos'].insert(0, 'not a mapping'))
|
||||
|
||||
config = lib.get_repos_config()
|
||||
config.import_config()
|
||||
|
||||
assert config.successful_import is True
|
||||
assert config.import_skipped == 1
|
||||
found = repos(test_session)
|
||||
assert set(found) == {ONE, TWO}
|
||||
assert found[ONE].collection.description == 'Changed'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_only_malformed_entries_delete_nothing(test_session, saved_repos):
|
||||
"""A file whose entries are all unreadable deletes no Repo (like an empty list)."""
|
||||
edit_config(saved_repos, lambda data: data.update(repos=['not a mapping', dict(url=['a', 'list'])]))
|
||||
|
||||
config = lib.get_repos_config()
|
||||
config.import_config()
|
||||
|
||||
assert config.import_skipped == 2
|
||||
assert set(repos(test_session)) == {ONE, TWO}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_failed_import_creates_nothing(test_session, saved_repos, monkeypatch):
|
||||
"""A new Repo is created in the import's one write: a failure later in the write leaves nothing (each new Repo
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue