Coverage for src/sparkle/CLI/help/nicknames.py: 85%

46 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-15 14:11 +0000

1"""Helper functions for CLI nicknames.""" 

2 

3from __future__ import annotations 

4from pathlib import Path 

5from typing import Callable 

6from sparkle.instance import Instance_Set, InstanceSet 

7 

8 

9def resolve_object_name( 

10 name: str | Path, 

11 nickname_dict: dict = {}, 

12 target_dir: Path = Path(), 

13 class_name: Callable = None, 

14) -> Path | any: 

15 """Attempts to resolve a (nick) name. 

16 

17 Args: 

18 name: The (nick)name to resolve 

19 target_dir: The location where the file object should exist 

20 nickname_dict: Nicknames 

21 class_name: If passed, will attempt to return an object 

22 that is constructed from this Path. 

23 

24 Returns: 

25 Path to the object, None if unresolvable. 

26 """ 

27 path = None 

28 # We cannot handle None as a name 

29 if name is None: 

30 return None 

31 # First check if the name already is a path 

32 if isinstance(name, (str, Path)) and Path(name).exists(): 

33 path = Path(name) 

34 # Second check if its a nickname registered in Sparkle 

35 elif str(name) in nickname_dict: 

36 path = Path(nickname_dict[str(name)]) 

37 # Third check if we can create a valid path with the name 

38 elif isinstance(name, (str, Path)) and (target_dir / name).exists(): 

39 path = target_dir / name 

40 # Finally, attempt to construct the object from the Path 

41 try: 

42 if class_name is not None: 

43 if path is not None: 

44 return class_name(path) 

45 if name is not None: 

46 return class_name(name) 

47 except Exception: 

48 return None 

49 return path 

50 

51 

52def resolve_instance_name( 

53 name: str, target: Path | list[InstanceSet], return_path: bool = True 

54) -> str | InstanceSet: 

55 """Attempts to resolve an instance name. 

56 

57 Args: 

58 name: The name to resolve 

59 target: Path to look for instance sets or the instance sets directly 

60 return_path: Whether to return the path of the instance or the instance set 

61 

62 Returns: 

63 The path or the instance set of the given instance name 

64 """ 

65 # Check if name is a multi file instance path 

66 

67 # Check if the name is already an instance file path 

68 name_path = Path(name) 

69 if name_path.exists() and name_path.is_file(): 

70 return name 

71 # Attempt to find files 

72 matches = [p for p in name_path.parent.glob(name_path.name + ".*")] 

73 if matches: 

74 return " ".join(str(p) for p in matches) # Concat for multi file instance 

75 # Target is a path to a directory that contains instance directories 

76 elif isinstance(target, Path): 

77 instances = [] 

78 for instance_dir in target.iterdir(): 

79 if instance_dir.is_dir(): 

80 instances.append(Instance_Set(instance_dir)) 

81 target = instances 

82 

83 out_set = None 

84 for instance_set in target: 

85 instance_path = instance_set.get_path_by_name(name) 

86 if instance_path is None: 

87 continue 

88 out_set = instance_set 

89 # Handle multi file instance 

90 instance_path = ( 

91 [instance_path] if not isinstance(instance_path, list) else instance_path 

92 ) 

93 instance_path = " ".join(str(p) for p in instance_path) 

94 break 

95 

96 return instance_path if return_path else out_set