Coverage for src/pytribeam/_package_metadata.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2026-09-09 18:27 +0000

1""" 

2Lightweight package and environment metadata. 

3 

4This module is intentionally safe to import in documentation builds and 

5installation checks. It must not import pytribeam.constants, pytribeam.types, 

6AutoScript runtime modules, Laser.PythonControl, workflow, or GUI modules. 

7""" 

8 

9from importlib import metadata as md 

10from importlib.util import find_spec 

11from typing import Final, Optional, Tuple 

12 

13MODULE_SHORT_NAME: Final[str] = "pytribeam" 

14 

15AUTOSCRIPT_IMPORT_NAME: Final[str] = "autoscript_sdb_microscope_client" 

16AUTOSCRIPT_DIST_NAME: Final[str] = "autoscript-sdb-microscope-client" 

17 

18LASER_IMPORT_NAME: Final[str] = "Laser" 

19LASER_PYTHONCONTROL_IMPORT_NAME: Final[str] = "Laser.PythonControl" 

20LASER_DIST_NAME: Final[str] = "Laser" 

21 

22SUPPORTED_AUTOSCRIPT_VERSIONS: Final[Tuple[str, ...]] = ( 

23 "4.8.1", 

24 "4.10.0", 

25 "4.11.0", 

26 # "4.13.1", 

27) 

28 

29SUPPORTED_LASER_API_VERSIONS: Final[Tuple[str, ...]] = ("2.2.1",) 

30 

31# Replace this with your actual current schema version. 

32YML_SCHEMA_VERSION: Final[str] = "1.0" 

33 

34 

35def get_version_or_none(dist_name: str) -> Optional[str]: 

36 """ 

37 Return the installed distribution version, or None if it is not installed. 

38 

39 This reads Python package metadata and does not import the runtime package. 

40 """ 

41 try: 

42 return md.version(dist_name) 

43 except md.PackageNotFoundError: 

44 return None 

45 

46 

47def import_available(import_name: str) -> bool: 

48 """ 

49 Return True if the import package appears importable. 

50 """ 

51 try: 

52 return find_spec(import_name) is not None 

53 except (ImportError, ModuleNotFoundError, AttributeError, ValueError): 

54 return False 

55 

56 

57def get_pytribeam_version() -> Optional[str]: 

58 """ 

59 Return the pytribeam version. 

60 

61 Prefer the generated _version.py file, because it may contain release/dev 

62 information generated by vcs-versioning. Fall back to installed package 

63 metadata if needed. 

64 """ 

65 try: 

66 from pytribeam._version import version 

67 

68 return version 

69 except Exception: 

70 return get_version_or_none(MODULE_SHORT_NAME) 

71 

72 

73def get_pytribeam_commit_id() -> Optional[str]: 

74 """ 

75 Return the pytribeam git commit identifier, if available. 

76 

77 The generated _version.py may expose commit_id, __commit_id__, or 

78 __git_commit__ depending on the release/versioning path. 

79 """ 

80 try: 

81 import pytribeam._version as version_mod 

82 

83 commit = getattr(version_mod, "commit_id", None) 

84 if commit: 

85 return commit 

86 

87 commit = getattr(version_mod, "__commit_id__", None) 

88 if commit: 

89 return commit 

90 

91 commit = getattr(version_mod, "__git_commit__", None) 

92 if commit: 

93 return commit 

94 

95 return None 

96 except Exception: 

97 return None 

98 

99 

100def get_autoscript_version() -> Optional[str]: 

101 """ 

102 Return the installed AutoScript distribution version, if detected. 

103 """ 

104 return get_version_or_none(AUTOSCRIPT_DIST_NAME) 

105 

106 

107def get_laser_api_version() -> Optional[str]: 

108 """ 

109 Return the installed Laser API distribution version, if detected. 

110 """ 

111 return get_version_or_none(LASER_DIST_NAME) 

112 

113 

114def autoscript_available() -> bool: 

115 """ 

116 Return True if the AutoScript import package appears available. 

117 """ 

118 return import_available(AUTOSCRIPT_IMPORT_NAME) 

119 

120 

121def laser_api_available() -> bool: 

122 """ 

123 Return True if the Laser API top-level import package appears available. 

124 """ 

125 return import_available(LASER_IMPORT_NAME) 

126 

127 

128def laser_pythoncontrol_available() -> bool: 

129 """ 

130 Return True if Laser.PythonControl appears available. 

131 """ 

132 return import_available(LASER_PYTHONCONTROL_IMPORT_NAME)