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

47 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-09-29 10:17 +0000

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

2 

3from __future__ import annotations 

4from pathlib import Path 

5from typing import Callable 

6import glob 

7from sparkle.instance import Instance_Set, InstanceSet 

8 

9 

10def resolve_object_name( 

11 name: str | Path, 

12 nickname_dict: dict = {}, 

13 target_dir: Path = Path(), 

14 class_name: Callable = None, 

15) -> Path | any: 

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

17 

18 Args: 

19 name: The (nick)name to resolve 

20 target_dir: The location where the file object should exist 

21 nickname_dict: Nicknames 

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

23 that is constructed from this Path. 

24 

25 Returns: 

26 Path to the object, None if unresolvable. 

27 """ 

28 path = None 

29 # We cannot handle None as a name 

30 if name is None: 

31 return None 

32 # First check if the name already is a path 

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

34 path = Path(name) 

35 # Second check if its a nickname registered in Sparkle 

36 elif str(name) in nickname_dict: 

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

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

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

40 path = target_dir / name 

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

42 try: 

43 if class_name is not None: 

44 if path is not None: 

45 return class_name(path) 

46 if name is not None: 

47 return class_name(name) 

48 except Exception: 

49 return None 

50 return path 

51 

52 

53def resolve_instance_name( 

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

55) -> str | InstanceSet: 

56 """Attempts to resolve an instance name. 

57 

58 Args: 

59 name: The name to resolve 

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

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

62 

63 Returns: 

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

65 """ 

66 # Check if name is a multi file instance path 

67 matches = glob.glob(name + ".*") 

68 

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

70 name_path = Path(name) 

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

72 return name 

73 # Concat multi file instance 

74 elif matches: 

75 return " ".join(str(p) for p in matches) 

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

77 elif isinstance(target, Path): 

78 instances = [] 

79 for instance_dir in target.iterdir(): 

80 if instance_dir.is_dir(): 

81 instances.append(Instance_Set(instance_dir)) 

82 target = instances 

83 

84 out_set = None 

85 for instance_set in target: 

86 instance_path = instance_set.get_path_by_name(name) 

87 if instance_path is None: 

88 continue 

89 out_set = instance_set 

90 # Handle multi file instance 

91 instance_path = ( 

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

93 ) 

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

95 break 

96 

97 return instance_path if return_path else out_set