Coverage for sparkle/CLI/help/nicknames.py: 91%
47 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-01 13:21 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-01 13:21 +0000
1"""Helper functions for CLI nicknames."""
2from __future__ import annotations
3from pathlib import Path
4from typing import Callable
5import glob
6from sparkle.instance import Instance_Set, InstanceSet
9def resolve_object_name(name: str | Path,
10 nickname_dict: dict = {},
11 target_dir: Path = Path(),
12 class_name: Callable = None) -> Path | any:
13 """Attempts to resolve a (nick) name.
15 Args:
16 name: The (nick)name to resolve
17 target_dir: The location where the file object should exist
18 class_name: If passed, will attempt to return an object
19 that is constructed from this Path.
21 Returns:
22 Path to the object, None if unresolvable.
23 """
24 path = None
25 # We cannot handle None as a name
26 if name is None:
27 return None
28 # First check if the name already is a path
29 if isinstance(name, (str, Path)) and Path(name).exists():
30 path = Path(name)
31 # Second check if its a nickname registered in Sparkle
32 elif str(name) in nickname_dict:
33 path = Path(nickname_dict[str(name)])
34 # Third check if we can create a valid path with the name
35 elif isinstance(name, (str, Path)) and (target_dir / name).exists():
36 path = (target_dir / name)
37 # Finally, attempt to construct the object from the Path
38 try:
39 if class_name is not None:
40 if path is not None:
41 return class_name(path)
42 if name is not None:
43 return class_name(name)
44 except Exception:
45 return None
46 return path
49def resolve_instance_name(name: str,
50 target: Path | list[InstanceSet],
51 return_path: bool = True) -> str | InstanceSet:
52 """Attempts to resolve an instance name.
54 Args:
55 name: The name to resolve
56 target: Path to look for instance sets or the instance sets directly
57 return_path: Whether to return the path of the instance or the instance set
59 Returns:
60 The path or the instance set of the given instance name
61 """
62 # Check if name is a multi file instance path
63 matches = glob.glob(name + ".*")
65 # Check if the name is already an instance file path
66 name_path = Path(name)
67 if name_path.exists() and name_path.is_file():
68 return name
69 # Concat multi file instance
70 elif matches:
71 return " ".join(str(p) for p in matches)
72 # Target is a path to a directory that contains instance directories
73 elif isinstance(target, Path):
74 instances = []
75 for instance_dir in target.iterdir():
76 if instance_dir.is_dir():
77 instances.append(Instance_Set(instance_dir))
78 target = instances
80 out_set = None
81 for instance_set in target:
82 instance_path = instance_set.get_path_by_name(name)
83 if instance_path is None:
84 continue
85 out_set = instance_set
86 # Handle multi file instance
87 instance_path = [instance_path] if not isinstance(
88 instance_path, list) else instance_path
89 instance_path = " ".join(str(p) for p in instance_path)
90 break
92 return instance_path if return_path else out_set