isgri 0.4.0__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
isgri/config.py ADDED
@@ -0,0 +1,151 @@
1
+ import sys
2
+ from pathlib import Path
3
+ from typing import Optional
4
+ from platformdirs import user_config_dir
5
+
6
+ if sys.version_info >= (3, 11):
7
+ import tomllib
8
+ else:
9
+ import tomli as tomllib
10
+
11
+ import tomli_w
12
+
13
+
14
+ class Config:
15
+ """
16
+ Configuration manager for ISGRI.
17
+
18
+ Manages paths to archive directory and catalog. Config is stored
19
+ in platform-specific location (~/.config/isgri/config.toml on Linux).
20
+ Falls back to local isgri_config.toml if global config doesn't exist.
21
+
22
+ Parameters
23
+ ----------
24
+ path : Path, optional
25
+ Custom config file path. If not provided, uses platform default.
26
+
27
+ Attributes
28
+ ----------
29
+ path : Path
30
+ Path to config file
31
+ archive_path : Path or None
32
+ Path to INTEGRAL archive directory
33
+ catalog_path : Path or None
34
+ Path to catalog FITS file (validated on access)
35
+ """
36
+
37
+ DEFAULT_PATH = Path(user_config_dir("isgri")) / "config.toml"
38
+
39
+ def __init__(self, path: Optional[Path] = None):
40
+ self.path = path or self.DEFAULT_PATH
41
+ self._config = None
42
+
43
+ @property
44
+ def config(self) -> dict:
45
+ """
46
+ Load and return config dictionary.
47
+
48
+ Returns
49
+ -------
50
+ dict
51
+ Configuration dictionary
52
+ """
53
+ if self._config is not None:
54
+ return self._config
55
+
56
+ if self.path.exists():
57
+ path = self.path
58
+ elif self.path == self.DEFAULT_PATH and Path("isgri_config.toml").exists():
59
+ print("Config file not found at default path, using local isgri_config.toml instead.", file=sys.stderr)
60
+ path = Path("isgri_config.toml")
61
+ else:
62
+ self._config = {}
63
+ return self._config
64
+
65
+ with open(path, "rb") as f:
66
+ self._config = tomllib.load(f)
67
+
68
+ return self._config
69
+
70
+ @property
71
+ def archive_path(self) -> Optional[Path]:
72
+ """
73
+ Get archive directory path from config.
74
+
75
+ Returns
76
+ -------
77
+ Path or None
78
+ Path to archive directory. No validation performed.
79
+ """
80
+ path_str = self.config.get("archive_path")
81
+ if path_str:
82
+ return Path(path_str)
83
+ return None
84
+
85
+ @property
86
+ def catalog_path(self) -> Optional[Path]:
87
+ """
88
+ Get catalog path from config.
89
+
90
+ Returns
91
+ -------
92
+ Path or None
93
+ Path to catalog FITS file
94
+
95
+ Raises
96
+ ------
97
+ FileNotFoundError
98
+ If configured path doesn't exist
99
+ """
100
+ path_str = self.config.get("catalog_path")
101
+ if not path_str:
102
+ return None
103
+ path = Path(path_str)
104
+ if not path.exists():
105
+ raise FileNotFoundError(f"Catalog path does not exist: {path}")
106
+ return path
107
+
108
+ def save(self):
109
+ """Save current config to file."""
110
+ self.path.parent.mkdir(parents=True, exist_ok=True)
111
+ with open(self.path, "wb") as f:
112
+ tomli_w.dump(self._config or {}, f)
113
+
114
+ def create_new(self, archive_path: Optional[Path] = None, catalog_path: Optional[Path] = None):
115
+ """
116
+ Create new config file with given paths.
117
+
118
+ Parameters
119
+ ----------
120
+ archive_path : Path, optional
121
+ Path to archive directory
122
+ catalog_path : Path, optional
123
+ Path to catalog FITS file
124
+ """
125
+ self._config = {}
126
+ if archive_path:
127
+ self._config["archive_path"] = str(archive_path)
128
+ if catalog_path:
129
+ self._config["catalog_path"] = str(catalog_path)
130
+ self.save()
131
+
132
+ def set(self, archive_path: Optional[Path] = None, catalog_path: Optional[Path] = None):
133
+ """
134
+ Update config paths and save.
135
+
136
+ Parameters
137
+ ----------
138
+ archive_path : Path, optional
139
+ New archive directory path
140
+ catalog_path : Path, optional
141
+ New catalog path
142
+ """
143
+ if archive_path:
144
+ self.config["archive_path"] = str(archive_path)
145
+ if catalog_path:
146
+ self.config["catalog_path"] = str(catalog_path)
147
+
148
+ self.save()
149
+
150
+ def __repr__(self):
151
+ return f"Config(path={self.path}, archive={self.archive_path}, catalog={self.catalog_path})"