cln-plugins/backup/backends.py

40 lines
1.2 KiB
Python
Raw Permalink Normal View History

2024-07-01 16:34:29 +02:00
"""Create a backend instance based on URI scheme dispatch."""
2025-12-11 15:47:04 +01:00
from typing import Mapping, Type
2020-12-30 21:58:39 +01:00
from urllib.parse import urlparse
from backend import Backend
from socketbackend import SocketBackend
from filebackend import FileBackend
def resolve_backend_class(backend_url):
backend_map: Mapping[str, Type[Backend]] = {
2024-07-01 16:34:29 +02:00
"file": FileBackend,
"socket": SocketBackend,
2020-12-30 21:58:39 +01:00
}
p = urlparse(backend_url)
backend_cl = backend_map.get(p.scheme, None)
return backend_cl
def get_backend(destination, create=False, require_init=False):
backend_cl = resolve_backend_class(destination)
if backend_cl is None:
2024-07-01 16:34:29 +02:00
raise ValueError(
"No backend implementation found for {destination}".format(
destination=destination,
)
)
2020-12-30 21:58:39 +01:00
backend = backend_cl(destination, create=create)
initialized = backend.initialize()
if require_init and not initialized:
2024-07-01 16:34:29 +02:00
kill(
"Could not initialize the backup {}, please use 'backup-cli' to initialize the backup first.".format(
destination
)
)
assert backend.version is not None
assert backend.prev_version is not None
return backend