WIP location (to be discarded)

This commit is contained in:
GeoffreyCoulaud
2023-05-01 00:30:13 +02:00
parent 524a56ea9a
commit 0abb7d3df9
3 changed files with 71 additions and 40 deletions

View File

@@ -1,20 +1,35 @@
from dataclasses import dataclass
from functools import cached_property
from pathlib import Path
from os import PathLike
@dataclass
class Location():
"""Abstraction for a location that can be overriden by a schema key"""
"""Abstraction for a location that has multiple candidate paths"""
win = None
candidates: list[PathLike] = None
default: str = None
key: str = None
def __init__(self, *candidates):
self.candidates = list()
self.candidates.extend(candidates)
return self
def add(self, canddiate):
"""Add a candidate (last evaluated)"""
self.candidates.append(canddiate)
return self
def add_override(self, candidate):
"""Add a canddiate (first evaluated)"""
self.candidates.insert(0, candidate)
return self
@cached_property
def path(self):
override = Path(self.win.schema.get_string(self.path_override_key))
if override.exists():
return override
return self.path_default
"""Chosen path depending on availability on the disk."""
for candidate in self.candidates:
p = Path(candidate).expanduser()
if p.exists:
return p
return None