mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 20:06:22 -05:00
Config: wrolpi.yaml is only ever config/wrolpi.yaml (#1039)
* Config: wrolpi.yaml is only ever config/wrolpi.yaml When config/wrolpi.yaml was missing, the API searched the media directory three levels deep (hidden directories too) and trusted the first wrolpi.yaml it found. A torrent, download, or git repo holding a wrolpi.yaml (e.g. torrents/.1/wrolpi.yaml) would become the config, and the real one was never created. The Controller and scripts already read only the fixed path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Config: shorten the wrolpi.yaml comment to the rule 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
d6a8363903
commit
87ed2ffeb8
2 changed files with 14 additions and 58 deletions
|
|
@ -408,24 +408,6 @@ class ModelHelper:
|
|||
raise RuntimeError(f'{self} is not in a session!')
|
||||
|
||||
|
||||
def find_file(directory: pathlib.Path, name: str, depth=1) -> Optional[pathlib.Path]:
|
||||
"""Recursively searches a directory for a
|
||||
file with the provided name."""
|
||||
if depth == 0:
|
||||
return
|
||||
|
||||
try:
|
||||
entries = sorted(directory.iterdir())
|
||||
except PermissionError:
|
||||
return
|
||||
|
||||
for path in entries:
|
||||
if path.is_file() and path.name == name:
|
||||
return path
|
||||
if path.is_dir() and (result := find_file(path, name, depth - 1)):
|
||||
return result
|
||||
|
||||
|
||||
TEST_MEDIA_DIRECTORY = None
|
||||
|
||||
|
||||
|
|
@ -1814,27 +1796,7 @@ class WROLPiConfig(ConfigFile):
|
|||
zims_destination='zims',
|
||||
)
|
||||
validator = WROLPiConfigValidator
|
||||
|
||||
def get_file(self) -> Path:
|
||||
"""WROLPiConfig must be discovered so that other config files can be found."""
|
||||
if not self.file_name:
|
||||
raise NotImplementedError(f'You must define a file name for this {self.__class__.__name__} config.')
|
||||
|
||||
if PYTEST:
|
||||
return get_media_directory() / f'config/{self.file_name}'
|
||||
|
||||
# Use the usual "/media/wrolpi/config/wrolpi.yaml" if it exists.
|
||||
default_config_path = CONFIG_DIR / self.file_name
|
||||
if default_config_path.is_file():
|
||||
return default_config_path
|
||||
|
||||
# Search the media directory for the special "wrolpi.yaml" file. Assume the first one found is the config.
|
||||
# Use a depth of 3; multiple drives may exist and the config directory may be down in a second drive.
|
||||
if config_path := find_file(get_media_directory(), self.file_name, 3):
|
||||
return config_path
|
||||
|
||||
# Not testing, and can't find file deep in media directory. Use the default.
|
||||
return default_config_path
|
||||
# A wrolpi.yaml outside config/ is never this config.
|
||||
|
||||
def import_config(self, file: pathlib.Path = None, send_events=False):
|
||||
super().import_config(file, send_events)
|
||||
|
|
|
|||
|
|
@ -801,25 +801,6 @@ def test_chain(iterable, length, step, expected):
|
|||
assert list(common.chain(iterable, length)) == expected
|
||||
|
||||
|
||||
def test_find_file(test_directory, make_files_structure):
|
||||
bar, baz, foo = make_files_structure({
|
||||
'foo/foo.txt': 'foo',
|
||||
'bar/bar.txt': 'bar',
|
||||
'baz.txt': 'baz',
|
||||
})
|
||||
|
||||
# Files are too deep.
|
||||
assert common.find_file(test_directory, 'foo.txt') is None
|
||||
assert common.find_file(test_directory, 'bar.txt') is None
|
||||
assert common.find_file(test_directory, 'baz.txt') == baz
|
||||
# Enough depth to find files.
|
||||
assert common.find_file(test_directory, 'foo.txt', 2) == foo
|
||||
assert common.find_file(test_directory, 'bar.txt', 2) == bar
|
||||
assert common.find_file(test_directory, 'baz.txt', 2) == baz
|
||||
|
||||
assert common.find_file(test_directory, 'does not exist', 100) is None
|
||||
|
||||
|
||||
def test_config_backup(async_client, test_wrolpi_config, test_directory):
|
||||
"""Configs have a backup each day."""
|
||||
config = common.get_wrolpi_config()
|
||||
|
|
@ -1484,3 +1465,16 @@ async def test_wrol_mode_flag_file_follows_import(test_directory, test_wrolpi_co
|
|||
test_wrolpi_config.write_text('wrol_mode: false\n')
|
||||
get_wrolpi_config().import_config()
|
||||
assert not flag.exists()
|
||||
|
||||
|
||||
def test_wrolpi_config_is_never_searched_for(test_directory, test_wrolpi_config, make_files_structure):
|
||||
"""wrolpi.yaml is only ever config/wrolpi.yaml. A wrolpi.yaml elsewhere in the library (from a torrent, a
|
||||
download, or a git repo; even in a hidden directory) is a file like any other, never the config."""
|
||||
config_directory = test_directory / 'config'
|
||||
config_directory.joinpath('wrolpi.yaml').unlink(missing_ok=True)
|
||||
make_files_structure({
|
||||
'torrents/.1/wrolpi.yaml': 'wrol_mode: true\nallow_database_query: true\n',
|
||||
'wrolpi.yaml': 'wrol_mode: true\n',
|
||||
})
|
||||
with mock.patch.object(common, 'PYTEST', False), mock.patch.object(common, 'CONFIG_DIR', config_directory):
|
||||
assert common.get_wrolpi_config().get_file() == config_directory / 'wrolpi.yaml'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue