mlgidGUI 0.4.1__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.
- mlgidGUI/__init__.py +52 -0
- mlgidGUI/__main__.py +7 -0
- mlgidGUI/__version.py +1 -0
- mlgidGUI/app/__init__.py +8 -0
- mlgidGUI/app/actions_stack.py +1 -0
- mlgidGUI/app/app.py +67 -0
- mlgidGUI/app/cif_rois/__init__.py +0 -0
- mlgidGUI/app/cif_rois/cif_peaks_calculation.py +57 -0
- mlgidGUI/app/cif_rois/cif_simulation/__init__.py +0 -0
- mlgidGUI/app/cif_rois/cif_simulation/data/__init__.py +0 -0
- mlgidGUI/app/cif_rois/cif_simulation/data/atoms.py +15 -0
- mlgidGUI/app/cif_rois/cif_simulation/data/export_database.py +28 -0
- mlgidGUI/app/cif_rois/cif_simulation/experiment.py +57 -0
- mlgidGUI/app/cif_rois/cif_simulation/giwaxs_sim.py +286 -0
- mlgidGUI/app/cif_rois/cif_simulation/int_sim.py +105 -0
- mlgidGUI/app/cif_rois/cif_simulation/possible_directions.py +20 -0
- mlgidGUI/app/cif_rois/cif_simulation/q_sim.py +159 -0
- mlgidGUI/app/data_manager/__init__.py +4 -0
- mlgidGUI/app/data_manager/data_manager.py +164 -0
- mlgidGUI/app/data_manager/import_from_h5.py +68 -0
- mlgidGUI/app/data_manager/load_data.py +78 -0
- mlgidGUI/app/data_manager/pascal/__init__.py +2 -0
- mlgidGUI/app/data_manager/pascal/annotation_fabric.py +59 -0
- mlgidGUI/app/data_manager/pascal/draw_objects.py +119 -0
- mlgidGUI/app/data_manager/pascal/exceptions.py +6 -0
- mlgidGUI/app/data_manager/pascal/format_convertor.py +127 -0
- mlgidGUI/app/data_manager/pascal/pascal_annotation.py +85 -0
- mlgidGUI/app/data_manager/pascal/protocol_implementations.py +28 -0
- mlgidGUI/app/data_manager/pascal/protocols.py +47 -0
- mlgidGUI/app/data_manager/pascal/utils.py +64 -0
- mlgidGUI/app/data_manager/save_dataset.py +57 -0
- mlgidGUI/app/data_manager/save_h5.py +178 -0
- mlgidGUI/app/data_manager/saving_parameters.py +88 -0
- mlgidGUI/app/debug_tracker.py +129 -0
- mlgidGUI/app/edf_reader.py +135 -0
- mlgidGUI/app/file_manager/__init__.py +201 -0
- mlgidGUI/app/file_manager/config_manager.py +76 -0
- mlgidGUI/app/file_manager/keys.py +542 -0
- mlgidGUI/app/file_manager/npy_file_manager.py +17 -0
- mlgidGUI/app/file_manager/object_file_manager.py +77 -0
- mlgidGUI/app/file_manager/project_structure.py +169 -0
- mlgidGUI/app/file_manager/read_fits.py +76 -0
- mlgidGUI/app/file_manager/read_geometry.py +54 -0
- mlgidGUI/app/file_manager/read_images.py +31 -0
- mlgidGUI/app/file_manager/read_meta_roi.py +40 -0
- mlgidGUI/app/file_manager/read_polar_images.py +22 -0
- mlgidGUI/app/file_manager/read_radial_profile.py +6 -0
- mlgidGUI/app/file_manager/read_roi_data.py +39 -0
- mlgidGUI/app/fitting/__init__.py +5 -0
- mlgidGUI/app/fitting/background.py +92 -0
- mlgidGUI/app/fitting/fit.py +128 -0
- mlgidGUI/app/fitting/fit_holder.py +273 -0
- mlgidGUI/app/fitting/fit_object.py +247 -0
- mlgidGUI/app/fitting/functions.py +107 -0
- mlgidGUI/app/fitting/range_strategy.py +14 -0
- mlgidGUI/app/fitting/utils.py +17 -0
- mlgidGUI/app/geometry.py +236 -0
- mlgidGUI/app/geometry_holder.py +131 -0
- mlgidGUI/app/image_holder.py +200 -0
- mlgidGUI/app/image_processing.py +23 -0
- mlgidGUI/app/polar_image.py +96 -0
- mlgidGUI/app/profiles/__init__.py +5 -0
- mlgidGUI/app/profiles/angular_profile.py +20 -0
- mlgidGUI/app/profiles/basic_profile.py +169 -0
- mlgidGUI/app/profiles/radial_profile.py +38 -0
- mlgidGUI/app/read_image.py +21 -0
- mlgidGUI/app/rois/__init__.py +4 -0
- mlgidGUI/app/rois/new_roi_dict.py +670 -0
- mlgidGUI/app/rois/roi.py +194 -0
- mlgidGUI/app/rois/roi_colors.py +76 -0
- mlgidGUI/app/rois/roi_data.py +177 -0
- mlgidGUI/app/rois/roi_dict.py +466 -0
- mlgidGUI/app/rois/roi_meta_data.py +149 -0
- mlgidGUI/app/structures/__init__.py +6 -0
- mlgidGUI/app/structures/crystal_ring.py +19 -0
- mlgidGUI/app/structures/crystals_holders.py +144 -0
- mlgidGUI/app/structures/custom_crystal.py +45 -0
- mlgidGUI/app/structures/get_ring_list.py +89 -0
- mlgidGUI/app/transformations.py +82 -0
- mlgidGUI/app/update/__init__.py +3 -0
- mlgidGUI/app/update/check_version.py +51 -0
- mlgidGUI/app/update/launch_program.py +15 -0
- mlgidGUI/app/update/script_update.py +64 -0
- mlgidGUI/app/utils.py +124 -0
- mlgidGUI/gui/__init__.py +3 -0
- mlgidGUI/gui/background_tasks.py +41 -0
- mlgidGUI/gui/background_update.py +87 -0
- mlgidGUI/gui/basic_widgets/__init__.py +12 -0
- mlgidGUI/gui/basic_widgets/buttons.py +144 -0
- mlgidGUI/gui/basic_widgets/labels.py +51 -0
- mlgidGUI/gui/basic_widgets/mouse_events.py +119 -0
- mlgidGUI/gui/basic_widgets/path_line.py +162 -0
- mlgidGUI/gui/basic_widgets/plots_1d.py +365 -0
- mlgidGUI/gui/basic_widgets/plots_2d.py +224 -0
- mlgidGUI/gui/basic_widgets/popups.py +164 -0
- mlgidGUI/gui/basic_widgets/progress_bar.py +127 -0
- mlgidGUI/gui/basic_widgets/setup_widgets.py +138 -0
- mlgidGUI/gui/basic_widgets/sliders.py +616 -0
- mlgidGUI/gui/basic_widgets/spinner.py +65 -0
- mlgidGUI/gui/basic_widgets/toolbars.py +19 -0
- mlgidGUI/gui/debug_widgets.py +388 -0
- mlgidGUI/gui/dock_area.py +115 -0
- mlgidGUI/gui/exception_message.py +42 -0
- mlgidGUI/gui/fitting/__init__.py +2 -0
- mlgidGUI/gui/fitting/fit_widget.py +966 -0
- mlgidGUI/gui/fitting/inplace_fit.py +536 -0
- mlgidGUI/gui/fitting/selected_fit_widget.py +286 -0
- mlgidGUI/gui/gui_main.py +349 -0
- mlgidGUI/gui/image_viewer.py +323 -0
- mlgidGUI/gui/init_window.py +170 -0
- mlgidGUI/gui/load_window/__init__.py +2 -0
- mlgidGUI/gui/load_window/load_folder.py +61 -0
- mlgidGUI/gui/load_window/load_h5.py +73 -0
- mlgidGUI/gui/notifications.py +68 -0
- mlgidGUI/gui/polar_image_viewer.py +153 -0
- mlgidGUI/gui/profiles/__init__.py +1 -0
- mlgidGUI/gui/profiles/angular_profile_widget.py +62 -0
- mlgidGUI/gui/profiles/radial_profile_widget.py +164 -0
- mlgidGUI/gui/project_manager/__init__.py +17 -0
- mlgidGUI/gui/project_manager/cif_files_widget.py +196 -0
- mlgidGUI/gui/project_manager/cif_results.py +146 -0
- mlgidGUI/gui/project_manager/metadata_widget.py +15 -0
- mlgidGUI/gui/project_manager/project_viewer.py +151 -0
- mlgidGUI/gui/project_manager/roi_widget.py +171 -0
- mlgidGUI/gui/roi_widgets/__init__.py +6 -0
- mlgidGUI/gui/roi_widgets/abstract_roi_holder.py +189 -0
- mlgidGUI/gui/roi_widgets/abstract_roi_widget.py +111 -0
- mlgidGUI/gui/roi_widgets/cif_file_widget.py +16 -0
- mlgidGUI/gui/roi_widgets/cif_peak_widget.py +43 -0
- mlgidGUI/gui/roi_widgets/confidence_level_widget.py +105 -0
- mlgidGUI/gui/roi_widgets/roi_1d_widget.py +136 -0
- mlgidGUI/gui/roi_widgets/roi_2d_rect_widget.py +98 -0
- mlgidGUI/gui/roi_widgets/roi_2d_ring_widget.py +203 -0
- mlgidGUI/gui/roi_widgets/rois_context_menu.py +150 -0
- mlgidGUI/gui/roi_widgets/select_roi_color_widget.py +58 -0
- mlgidGUI/gui/save_window/__init__.py +152 -0
- mlgidGUI/gui/save_window/format_box.py +33 -0
- mlgidGUI/gui/save_window/num_selected_widget.py +45 -0
- mlgidGUI/gui/save_window/options_widgets.py +105 -0
- mlgidGUI/gui/save_window/save_mode_box.py +30 -0
- mlgidGUI/gui/save_window/select_images.py +198 -0
- mlgidGUI/gui/tools.py +179 -0
- mlgidGUI/static/__init__.py +0 -0
- mlgidGUI/static/icons/__init__.py +0 -0
- mlgidGUI/static/icons/add.png +0 -0
- mlgidGUI/static/icons/add_yellow.png +0 -0
- mlgidGUI/static/icons/angular_profile.png +0 -0
- mlgidGUI/static/icons/arrow.png +0 -0
- mlgidGUI/static/icons/baseline.png +0 -0
- mlgidGUI/static/icons/center.png +0 -0
- mlgidGUI/static/icons/confirm.png +0 -0
- mlgidGUI/static/icons/control_widget.png +0 -0
- mlgidGUI/static/icons/data.png +0 -0
- mlgidGUI/static/icons/delete.png +0 -0
- mlgidGUI/static/icons/delete_yellow.png +0 -0
- mlgidGUI/static/icons/error.png +0 -0
- mlgidGUI/static/icons/find.png +0 -0
- mlgidGUI/static/icons/fit.png +0 -0
- mlgidGUI/static/icons/fix_all.png +0 -0
- mlgidGUI/static/icons/flip_horizontal.png +0 -0
- mlgidGUI/static/icons/flip_vertical.png +0 -0
- mlgidGUI/static/icons/folder.png +0 -0
- mlgidGUI/static/icons/fromfullscreen.png +0 -0
- mlgidGUI/static/icons/h5_folder.png +0 -0
- mlgidGUI/static/icons/h5_group_folder.png +0 -0
- mlgidGUI/static/icons/info.png +0 -0
- mlgidGUI/static/icons/interpolate.png +0 -0
- mlgidGUI/static/icons/item.png +0 -0
- mlgidGUI/static/icons/properties.png +0 -0
- mlgidGUI/static/icons/radial_profile.png +0 -0
- mlgidGUI/static/icons/roi_item.png +0 -0
- mlgidGUI/static/icons/rotate.png +0 -0
- mlgidGUI/static/icons/rotate_image.png +0 -0
- mlgidGUI/static/icons/rotate_right.png +0 -0
- mlgidGUI/static/icons/setup.png +0 -0
- mlgidGUI/static/icons/setup_white.png +0 -0
- mlgidGUI/static/icons/tofullscreen.png +0 -0
- mlgidGUI/static/icons/unfix_all.png +0 -0
- mlgidGUI/static/icons/update.png +0 -0
- mlgidGUI/static/icons/update_suggested.png +0 -0
- mlgidGUI/static/icons/window_icon.png +0 -0
- mlgidGUI/static/icons/window_icon_256.png +0 -0
- mlgidGUI/utils/__init__.py +1 -0
- mlgidGUI/utils/project2h5.py +11 -0
- mlgidgui-0.4.1.dist-info/METADATA +119 -0
- mlgidgui-0.4.1.dist-info/RECORD +202 -0
- mlgidgui-0.4.1.dist-info/WHEEL +5 -0
- mlgidgui-0.4.1.dist-info/entry_points.txt +5 -0
- mlgidgui-0.4.1.dist-info/licenses/LICENSE +674 -0
- mlgidgui-0.4.1.dist-info/top_level.txt +4 -0
- tests/__init__.py +0 -0
- tests/conftest.py +1 -0
- tests/fixures/__init__.py +2 -0
- tests/fixures/projects.py +102 -0
- tests/func/__init__.py +1 -0
- tests/func/test_loading_state.py +21 -0
- tests/func/test_parse_project_data.py +23 -0
- tests/func/test_save.py +62 -0
- tests/unit/__init__.py +0 -0
- tests/unit/rois/__init__.py +0 -0
- tests/unit/rois/test_basic_operations.py +18 -0
- tests/unit/test_add_data.py +33 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
from h5py import Group
|
|
6
|
+
|
|
7
|
+
from PyQt5.QtCore import pyqtSignal, QObject
|
|
8
|
+
|
|
9
|
+
from .keys import (FolderKey, FolderH5Key, FolderPathKey, RemoveWeakrefs,
|
|
10
|
+
ImageKey, ImageH5Key, ImagePathKey, InvalidKey, CIFFileKey,
|
|
11
|
+
PROJECT_KEY, IMAGE_PROJECT_KEY, GLOB_IMAGE_FORMATS)
|
|
12
|
+
|
|
13
|
+
from .project_structure import ProjectStructure, ProjectRootKey
|
|
14
|
+
from .read_images import _ReadImage, _ReadNpy
|
|
15
|
+
from .read_polar_images import _ReadPolarImage
|
|
16
|
+
from .read_geometry import _ReadGeometry
|
|
17
|
+
from .read_roi_data import _ReadRoiData
|
|
18
|
+
from .read_meta_roi import _ReadMetaData
|
|
19
|
+
from .read_radial_profile import _ReadRadialProfile
|
|
20
|
+
from .config_manager import _GlobalConfigManager
|
|
21
|
+
from .read_fits import _ReadFits
|
|
22
|
+
from appdirs import user_data_dir
|
|
23
|
+
from importlib import metadata
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class FileManager(QObject):
|
|
27
|
+
sigActiveImageChanged = pyqtSignal(object)
|
|
28
|
+
sigActiveFolderChanged = pyqtSignal(object)
|
|
29
|
+
sigProjectClosed = pyqtSignal()
|
|
30
|
+
sigProjectIsClosing = pyqtSignal()
|
|
31
|
+
sigProjectOpened = pyqtSignal()
|
|
32
|
+
sigNewFolder = pyqtSignal(object)
|
|
33
|
+
sigNewFile = pyqtSignal(object)
|
|
34
|
+
sigNewCIFFile = pyqtSignal()
|
|
35
|
+
sigRedrawCIFPeaks = pyqtSignal(object)
|
|
36
|
+
|
|
37
|
+
log = logging.getLogger(__name__)
|
|
38
|
+
|
|
39
|
+
def __init__(self):
|
|
40
|
+
config_path: Path = Path(user_data_dir('mlgidGUI', metadata.metadata('mlgidGUI')['Author']))
|
|
41
|
+
QObject.__init__(self)
|
|
42
|
+
self.config: _GlobalConfigManager = _GlobalConfigManager(config_path)
|
|
43
|
+
self.recent_projects: List[Path] = self.config.get_project_paths()
|
|
44
|
+
self._project_folder: Path or None = None
|
|
45
|
+
self._project_structure: ProjectStructure = ProjectStructure()
|
|
46
|
+
self.project_name: str = None
|
|
47
|
+
self._current_key: ImageKey or None = None
|
|
48
|
+
self.recent_projects = [p for p in self.recent_projects if p.is_dir()]
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def current_key(self) -> ImageKey or None:
|
|
52
|
+
return self._current_key
|
|
53
|
+
|
|
54
|
+
def open_latest_available_project(self):
|
|
55
|
+
self.close_project()
|
|
56
|
+
while self.recent_projects:
|
|
57
|
+
path = self.recent_projects.pop(-1)
|
|
58
|
+
if path.is_dir():
|
|
59
|
+
try:
|
|
60
|
+
self.open_project(path)
|
|
61
|
+
return
|
|
62
|
+
except Exception as err:
|
|
63
|
+
self.log.exception(err)
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def project_opened(self) -> bool:
|
|
67
|
+
return self._project_structure.project_opened
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def root(self) -> ProjectRootKey or None:
|
|
71
|
+
return self._project_structure.root
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def project_path(self) -> Path or None:
|
|
75
|
+
return self._project_folder
|
|
76
|
+
|
|
77
|
+
def change_image(self, key: ImageKey) -> None:
|
|
78
|
+
if key != self._current_key:
|
|
79
|
+
if not key or not self._current_key or key.parent != self._current_key.parent:
|
|
80
|
+
new_parent = key.parent if key else None
|
|
81
|
+
self.sigActiveFolderChanged.emit(new_parent)
|
|
82
|
+
|
|
83
|
+
self._current_key = key
|
|
84
|
+
self.sigActiveImageChanged.emit(key)
|
|
85
|
+
|
|
86
|
+
def open_project(self, path: Path) -> None:
|
|
87
|
+
self.close_project()
|
|
88
|
+
self._init_project(path)
|
|
89
|
+
self.sigProjectOpened.emit()
|
|
90
|
+
|
|
91
|
+
def add_root_path_to_project(self, path: Path) -> None:
|
|
92
|
+
if not self.project_opened:
|
|
93
|
+
return
|
|
94
|
+
key, isnew = self._project_structure.root.add_path(path)
|
|
95
|
+
if isnew:
|
|
96
|
+
if isinstance(key, ImageKey):
|
|
97
|
+
self.sigNewFile.emit(key)
|
|
98
|
+
elif isinstance(key, FolderKey):
|
|
99
|
+
self.sigNewFolder.emit(key)
|
|
100
|
+
if isinstance(key, CIFFileKey):
|
|
101
|
+
self.sigNewCIFFile.emit()
|
|
102
|
+
return key
|
|
103
|
+
|
|
104
|
+
def remove_key(self, key) -> None:
|
|
105
|
+
self._project_structure.root.remove_key(key)
|
|
106
|
+
if isinstance(key, ImageKey):
|
|
107
|
+
self._delete_image_data(key)
|
|
108
|
+
if key.parent:
|
|
109
|
+
key.parent.remove_image(key)
|
|
110
|
+
else:
|
|
111
|
+
self._delete_folder(key)
|
|
112
|
+
if self._current_key in key:
|
|
113
|
+
self.change_image(None)
|
|
114
|
+
|
|
115
|
+
def _delete_image_data(self, key: ImageKey):
|
|
116
|
+
del self.geometries[key]
|
|
117
|
+
del self.rois_data[key]
|
|
118
|
+
del self.images[key]
|
|
119
|
+
del self.polar_images[key]
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _delete_folder(self, key: FolderKey):
|
|
123
|
+
for folder in key.folder_children:
|
|
124
|
+
self._delete_folder(folder)
|
|
125
|
+
for image in key.image_children:
|
|
126
|
+
self._delete_image_data(image)
|
|
127
|
+
|
|
128
|
+
def close_project(self):
|
|
129
|
+
if self.project_opened:
|
|
130
|
+
self.sigProjectIsClosing.emit()
|
|
131
|
+
self._project_structure.save_and_close()
|
|
132
|
+
if self._project_folder not in self.recent_projects:
|
|
133
|
+
self.recent_projects.append(self._project_folder)
|
|
134
|
+
self._project_folder = None
|
|
135
|
+
self.project_name = None
|
|
136
|
+
self.sigProjectClosed.emit()
|
|
137
|
+
|
|
138
|
+
def delete_project(self, project_path: Path):
|
|
139
|
+
if self._project_folder == project_path:
|
|
140
|
+
raise ValueError('Cannot delete opened project.')
|
|
141
|
+
|
|
142
|
+
if project_path in self.recent_projects:
|
|
143
|
+
self.recent_projects.remove(project_path)
|
|
144
|
+
|
|
145
|
+
_delete_project(project_path)
|
|
146
|
+
|
|
147
|
+
def init_file_viewer(self):
|
|
148
|
+
|
|
149
|
+
for key in self._project_structure.root.folder_children:
|
|
150
|
+
self.sigNewFolder.emit(key)
|
|
151
|
+
current_image_key = None
|
|
152
|
+
for key in self._project_structure.root.image_children:
|
|
153
|
+
if not key.path.exists():
|
|
154
|
+
continue
|
|
155
|
+
self.sigNewFile.emit(key)
|
|
156
|
+
current_image_key = key
|
|
157
|
+
if current_image_key is not None:
|
|
158
|
+
self.change_image(current_image_key)
|
|
159
|
+
for key in self._project_structure.root.cif_files:
|
|
160
|
+
if not key.path.exists():
|
|
161
|
+
self._project_structure.root.remove_key(key)
|
|
162
|
+
if self._project_structure.root.cif_files:
|
|
163
|
+
self.sigNewCIFFile.emit()
|
|
164
|
+
|
|
165
|
+
def _init_project(self, path: Path):
|
|
166
|
+
self._project_folder = path
|
|
167
|
+
self._project_folder.mkdir(parents=False, exist_ok=True)
|
|
168
|
+
self._project_structure.open_project(path)
|
|
169
|
+
self.images: _ReadImage = _ReadImage(self._project_structure)
|
|
170
|
+
self.geometries: _ReadGeometry = _ReadGeometry(self._project_structure)
|
|
171
|
+
self.polar_images: _ReadPolarImage = _ReadPolarImage(self._project_structure)
|
|
172
|
+
self.rois_data: _ReadRoiData = _ReadRoiData(self._project_structure)
|
|
173
|
+
self.rois_meta_data: _ReadMetaData = _ReadMetaData(self._project_structure)
|
|
174
|
+
self.fits: _ReadFits = _ReadFits(self._project_structure)
|
|
175
|
+
#self.fits = None
|
|
176
|
+
#self.profiles = None
|
|
177
|
+
self.profiles: _ReadRadialProfile = _ReadRadialProfile(self._project_structure)
|
|
178
|
+
|
|
179
|
+
self.project_name = self._project_folder.name
|
|
180
|
+
self.init_file_viewer()
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def __len__(self):
|
|
184
|
+
return len(self.paths)
|
|
185
|
+
|
|
186
|
+
def close(self):
|
|
187
|
+
self.close_project()
|
|
188
|
+
|
|
189
|
+
def _give_h5_name(h5group: Group, name):
|
|
190
|
+
if name not in h5group.keys():
|
|
191
|
+
return name
|
|
192
|
+
num = 0
|
|
193
|
+
while True:
|
|
194
|
+
new_name = '_'.join((name, str(num)))
|
|
195
|
+
if new_name not in h5group.keys():
|
|
196
|
+
return new_name
|
|
197
|
+
num += 1
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _delete_project(project_path: Path):
|
|
201
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import pickle
|
|
2
|
+
import logging
|
|
3
|
+
from typing import List
|
|
4
|
+
import shutil
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class _GlobalConfigManager(object):
|
|
9
|
+
config_folder: Path
|
|
10
|
+
log = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
def __init__(self, config_path: Path = None):
|
|
13
|
+
if config_path:
|
|
14
|
+
self.config_folder = config_path
|
|
15
|
+
if not self.config_folder.is_dir():
|
|
16
|
+
self.config_folder.mkdir(parents=True)
|
|
17
|
+
self._saved_projects_path = str((Path.home() /'GIWAXSProjects').resolve())
|
|
18
|
+
self._copy_to_new_dir()
|
|
19
|
+
|
|
20
|
+
def get_project_paths(self) -> List[Path]:
|
|
21
|
+
project_path = (Path.home() / 'GIWAXSProjects').resolve()
|
|
22
|
+
try:
|
|
23
|
+
return [f for f in project_path.iterdir() if f.is_dir()]
|
|
24
|
+
except:
|
|
25
|
+
return []
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def add_project_path(self, path: Path):
|
|
29
|
+
paths: List[Path] = self.get_project_paths()
|
|
30
|
+
paths.append(path)
|
|
31
|
+
with open(self._saved_projects_path, 'wb') as f:
|
|
32
|
+
pickle.dump(paths, f)
|
|
33
|
+
|
|
34
|
+
def __getitem__(self, item):
|
|
35
|
+
try:
|
|
36
|
+
with open(str((self.config_folder / item).resolve()), 'rb') as f:
|
|
37
|
+
return pickle.load(f)
|
|
38
|
+
except FileNotFoundError:
|
|
39
|
+
return
|
|
40
|
+
except Exception as err:
|
|
41
|
+
self.log.exception(err)
|
|
42
|
+
return
|
|
43
|
+
|
|
44
|
+
def __setitem__(self, key, value):
|
|
45
|
+
with open(str((self.config_folder / key).resolve()), 'wb') as f:
|
|
46
|
+
pickle.dump(value, f)
|
|
47
|
+
|
|
48
|
+
def __delitem__(self, key):
|
|
49
|
+
path = self.config_folder / key
|
|
50
|
+
if path.is_file():
|
|
51
|
+
path.unlink()
|
|
52
|
+
|
|
53
|
+
def _copy_to_new_dir(self):
|
|
54
|
+
_OLD_CONFIG_PATH: Path = Path(__file__).parents[2] / 'config'
|
|
55
|
+
_OLD_CONFIG_STR: str = str(_OLD_CONFIG_PATH.resolve())
|
|
56
|
+
_OLD_SAVED_PROJECTS: str = str((_OLD_CONFIG_PATH / 'recent_projects').resolve())
|
|
57
|
+
|
|
58
|
+
if not _OLD_CONFIG_PATH.is_dir():
|
|
59
|
+
return
|
|
60
|
+
|
|
61
|
+
new_config_str_path: str = str(self.config_folder.resolve())
|
|
62
|
+
|
|
63
|
+
for p in _OLD_CONFIG_PATH.iterdir():
|
|
64
|
+
if p.name != 'recent_projects':
|
|
65
|
+
shutil.copy(p, new_config_str_path)
|
|
66
|
+
p.unlink()
|
|
67
|
+
|
|
68
|
+
try:
|
|
69
|
+
with open(_OLD_SAVED_PROJECTS, 'rb') as f:
|
|
70
|
+
old_project_paths = pickle.load(f)
|
|
71
|
+
self.update_project_paths(old_project_paths + self.get_project_paths())
|
|
72
|
+
Path(_OLD_SAVED_PROJECTS).unlink()
|
|
73
|
+
except FileNotFoundError:
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
_OLD_CONFIG_PATH.rmdir()
|