different backends can be selected
This commit is contained in:
parent
f5f487c084
commit
3cb9439adf
2 changed files with 39 additions and 4 deletions
|
@ -36,8 +36,14 @@ class RcloneBackend:
|
||||||
"""
|
"""
|
||||||
self.remote_name = remote_name
|
self.remote_name = remote_name
|
||||||
|
|
||||||
|
def actual_path(self, destination_path: str):
|
||||||
|
return destination_path
|
||||||
|
|
||||||
def exists(self, destination_path: bytes):
|
def exists(self, destination_path: bytes):
|
||||||
destination = b"%s:%s" % (self.remote_name.encode("utf8"), self.actual_path(destination_path))
|
destination = b"%s:%s" % (
|
||||||
|
self.remote_name.encode("utf8"),
|
||||||
|
self.actual_path(destination_path),
|
||||||
|
)
|
||||||
cmd = ["rclone", "--quiet", "lsjson", destination]
|
cmd = ["rclone", "--quiet", "lsjson", destination]
|
||||||
try:
|
try:
|
||||||
output = subprocess.check_output(cmd)
|
output = subprocess.check_output(cmd)
|
||||||
|
@ -58,6 +64,12 @@ class RcloneBackend:
|
||||||
subprocess.run(cmd, check=True)
|
subprocess.run(cmd, check=True)
|
||||||
|
|
||||||
|
|
||||||
|
class ArchiveBackend(RcloneBackend):
|
||||||
|
# XXX: there should be a way to "reserve" a bucket
|
||||||
|
# with that, we could now the URL before copying
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ArkiwiBackend(RcloneBackend):
|
class ArkiwiBackend(RcloneBackend):
|
||||||
def __init__(self, remote_name: str, prefix: str):
|
def __init__(self, remote_name: str, prefix: str):
|
||||||
super().__init__(remote_name)
|
super().__init__(remote_name)
|
||||||
|
@ -77,6 +89,9 @@ class ArkiwiBackend(RcloneBackend):
|
||||||
)
|
)
|
||||||
return f"https://www.arkiwi.org/path64/{path}/redirect"
|
return f"https://www.arkiwi.org/path64/{path}/redirect"
|
||||||
|
|
||||||
|
def exists(self, destination_path: bytes):
|
||||||
|
return False
|
||||||
|
|
||||||
def copy(self, filename: Path, destination_path: bytes) -> str:
|
def copy(self, filename: Path, destination_path: bytes) -> str:
|
||||||
"""
|
"""
|
||||||
returns the URL
|
returns the URL
|
||||||
|
@ -95,6 +110,13 @@ class ArkiwiBackend(RcloneBackend):
|
||||||
return url
|
return url
|
||||||
|
|
||||||
|
|
||||||
|
BACKENDS = {
|
||||||
|
"arkiwi.org": ArkiwiBackend,
|
||||||
|
"archive.org": ArchiveBackend,
|
||||||
|
"default": ArkiwiBackend,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Carichello:
|
class Carichello:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.parser = self.get_parser()
|
self.parser = self.get_parser()
|
||||||
|
@ -127,12 +149,24 @@ class Carichello:
|
||||||
self.args = self.parser.parse_args()
|
self.args = self.parser.parse_args()
|
||||||
with self.args.config.open() as buf:
|
with self.args.config.open() as buf:
|
||||||
self.config = json.load(buf)
|
self.config = json.load(buf)
|
||||||
|
self.config.setdefault("type", "default")
|
||||||
|
BackendCls = BACKENDS[self.config["type"]]
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
dest_directory = f"/{now.year}/{now.month}"
|
dest_directory = f"/{now.year}/{now.month}"
|
||||||
dest_file = f"{dest_directory}/{self.args.file.name}".encode("utf8")
|
dest_file = f"{dest_directory}/{self.args.file.name}".encode("utf8")
|
||||||
backend = ArkiwiBackend(self.config["remote"], self.config["prefix"])
|
if (
|
||||||
|
BackendCls == ArkiwiBackend
|
||||||
|
): # XXX: reimplementa in modo che questo if non sia necessario
|
||||||
|
backend = BackendCls(self.config["remote"], self.config["prefix"])
|
||||||
|
else:
|
||||||
|
backend = BackendCls(self.config["remote"])
|
||||||
|
|
||||||
|
if hasattr(backend, "path_to_url"):
|
||||||
url = backend.path_to_url(dest_file)
|
url = backend.path_to_url(dest_file)
|
||||||
LOG.info("file %s would be uploaded to %s", str(self.args.file), url)
|
LOG.info("file %s would be uploaded to %s", str(self.args.file), url)
|
||||||
|
else:
|
||||||
|
url = None
|
||||||
|
LOG.info("file %s would be uploaded", str(self.args.file))
|
||||||
zenity = Popen(
|
zenity = Popen(
|
||||||
[
|
[
|
||||||
"zenity",
|
"zenity",
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
|
"type": "arkiwi",
|
||||||
"remote": "arkiwiOndaRossa",
|
"remote": "arkiwiOndaRossa",
|
||||||
"prefix": "Radio_Onda_Rossa"
|
"prefix": "Radio_Onda_Rossa"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue