Classify static and smart crates
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from pathlib import Path
|
||||
from typing import Iterable, Tuple
|
||||
|
||||
from serato_doctor.models.crate import Crate
|
||||
from serato_doctor.models.crate import Crate, CrateKind
|
||||
from serato_doctor.models.reference import TrackReference
|
||||
|
||||
|
||||
@@ -32,6 +32,17 @@ def path_markers(reference_roots: Iterable[Path]) -> Tuple[str, ...]:
|
||||
return configured or DEFAULT_PATH_MARKERS
|
||||
|
||||
|
||||
def classify_crate(crate_path: Path) -> CrateKind:
|
||||
parent_names = {parent.name.casefold() for parent in crate_path.parents}
|
||||
if "smartcrates" in parent_names:
|
||||
return CrateKind.SMART
|
||||
if crate_path.name.casefold() == "compatible by key.crate":
|
||||
return CrateKind.SMART
|
||||
if "subcrates" in parent_names:
|
||||
return CrateKind.STATIC
|
||||
return CrateKind.UNKNOWN
|
||||
|
||||
|
||||
def load_crate(crate_path: Path, reference_roots: Iterable[Path] = ()) -> Crate:
|
||||
text = read_crate_text(crate_path)
|
||||
refs = []
|
||||
@@ -63,7 +74,11 @@ def load_crate(crate_path: Path, reference_roots: Iterable[Path] = ()) -> Crate:
|
||||
)
|
||||
)
|
||||
|
||||
return Crate(path=crate_path, references=tuple(refs))
|
||||
return Crate(
|
||||
path=crate_path,
|
||||
references=tuple(refs),
|
||||
kind=classify_crate(crate_path),
|
||||
)
|
||||
|
||||
|
||||
def parse_crate(
|
||||
@@ -82,3 +97,15 @@ def parse_crates(
|
||||
for crate in root.rglob("*.crate"):
|
||||
refs.extend(parse_crate(crate, reference_roots))
|
||||
return refs
|
||||
|
||||
|
||||
def load_library_crates(
|
||||
serato_root: Path, reference_roots: Iterable[Path] = ()
|
||||
) -> Tuple[Crate, ...]:
|
||||
reference_roots = tuple(reference_roots)
|
||||
crates = []
|
||||
for folder_name in ("Subcrates", "Smartcrates"):
|
||||
folder = serato_root / folder_name
|
||||
for crate_path in folder.rglob("*.crate"):
|
||||
crates.append(load_crate(crate_path, reference_roots))
|
||||
return tuple(sorted(crates, key=lambda crate: str(crate.path)))
|
||||
|
||||
Reference in New Issue
Block a user