molde 0.1.2__py3-none-any.whl → 0.1.3__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.
Files changed (55) hide show
  1. molde/__init__.py +5 -5
  2. molde/__main__.py +74 -74
  3. molde/actors/__init__.py +5 -5
  4. molde/actors/common_symbols_actor.py +164 -148
  5. molde/actors/ghost_actor.py +12 -12
  6. molde/actors/lines_actor.py +31 -31
  7. molde/actors/round_points_actor.py +7 -7
  8. molde/actors/square_points_actor.py +32 -32
  9. molde/colors/__init__.py +1 -1
  10. molde/colors/color.py +120 -120
  11. molde/colors/color_names.py +124 -124
  12. molde/interactor_styles/__init__.py +2 -2
  13. molde/interactor_styles/arcball_camera_style.py +288 -288
  14. molde/interactor_styles/box_selection_style.py +70 -70
  15. molde/main_window.ui +864 -864
  16. molde/pickers/__init__.py +2 -2
  17. molde/pickers/cell_area_picker.py +61 -61
  18. molde/pickers/cell_property_area_picker.py +84 -84
  19. molde/poly_data/__init__.py +13 -19
  20. molde/poly_data/arrows.py +1 -1
  21. molde/poly_data/lines_data.py +23 -23
  22. molde/poly_data/simple_shapes.py +1 -1
  23. molde/poly_data/vertices_data.py +24 -24
  24. molde/render_widgets/__init__.py +2 -2
  25. molde/render_widgets/animated_render_widget.py +164 -164
  26. molde/render_widgets/common_render_widget.py +433 -433
  27. molde/stylesheets/__init__.py +119 -119
  28. molde/stylesheets/common.qss +16 -16
  29. molde/stylesheets/create_color_page.py +61 -61
  30. molde/stylesheets/mainwindow.ui +611 -611
  31. molde/stylesheets/qcheckbox.qss +18 -18
  32. molde/stylesheets/qinputs.qss +78 -78
  33. molde/stylesheets/qlayouts.qss +22 -22
  34. molde/stylesheets/qmenubar.qss +12 -12
  35. molde/stylesheets/qprogressbar.qss +11 -11
  36. molde/stylesheets/qpushbutton.qss +90 -90
  37. molde/stylesheets/qradiobutton.qss +30 -30
  38. molde/stylesheets/qscrollbar.qss +29 -29
  39. molde/stylesheets/qslider.qss +61 -61
  40. molde/stylesheets/qtablewidget.qss +27 -27
  41. molde/stylesheets/qtabwidget.qss +29 -29
  42. molde/stylesheets/qtoolbar.qss +62 -62
  43. molde/stylesheets/qtoolbuttons.qss +14 -14
  44. molde/stylesheets/qtreewidget.qss +25 -25
  45. molde/ui_files/messages/new_loading_window.ui +73 -73
  46. molde/utils/__init__.py +8 -8
  47. molde/utils/format_sequences.py +44 -44
  48. molde/utils/poly_data_utils.py +66 -25
  49. molde/utils/tree_info.py +52 -52
  50. molde/windows/loading_window.py +189 -189
  51. {molde-0.1.2.dist-info → molde-0.1.3.dist-info}/METADATA +2 -4
  52. molde-0.1.3.dist-info/RECORD +68 -0
  53. {molde-0.1.2.dist-info → molde-0.1.3.dist-info}/WHEEL +1 -1
  54. molde/poly_data/complex_shapes.py +0 -26
  55. molde-0.1.2.dist-info/RECORD +0 -69
@@ -1,44 +1,44 @@
1
- from math import ceil, floor
2
-
3
-
4
- def format_long_sequence(
5
- sequence, max_width=30, max_lines=3, item_separator=", ", identation="► "
6
- ):
7
- strings = [str(i) for i in sequence]
8
-
9
- initial_lines = []
10
- for _ in range(ceil(max_lines / 2)):
11
- new_line = _extract_line(strings, max_width, len(item_separator))
12
- if not new_line:
13
- break
14
- initial_lines.append(new_line)
15
- strings = strings[len(new_line) :]
16
-
17
- strings.reverse()
18
- final_lines = []
19
- for _ in range(floor(max_lines / 2)):
20
- new_line = _extract_line(strings, max_width, len(item_separator))
21
- if not new_line:
22
- break
23
- final_lines.append(new_line)
24
- strings = strings[len(new_line) :]
25
- final_lines.reverse()
26
-
27
- if strings and initial_lines:
28
- initial_lines[-1] = ["..."]
29
- lines = initial_lines + final_lines
30
-
31
- formated_lines = [item_separator.join(line) for line in lines]
32
- concatenated_lines = identation + f"\n{identation}".join(formated_lines)
33
- return concatenated_lines
34
-
35
-
36
- def _extract_line(strings, max_width, separator_size):
37
- line = []
38
- current_width = 0
39
- for string in strings:
40
- if len(string) + current_width + separator_size > max_width:
41
- break
42
- line.append(string)
43
- current_width += len(string) + separator_size
44
- return line
1
+ from math import ceil, floor
2
+
3
+
4
+ def format_long_sequence(
5
+ sequence, max_width=30, max_lines=3, item_separator=", ", identation="► "
6
+ ):
7
+ strings = [str(i) for i in sequence]
8
+
9
+ initial_lines = []
10
+ for _ in range(ceil(max_lines / 2)):
11
+ new_line = _extract_line(strings, max_width, len(item_separator))
12
+ if not new_line:
13
+ break
14
+ initial_lines.append(new_line)
15
+ strings = strings[len(new_line) :]
16
+
17
+ strings.reverse()
18
+ final_lines = []
19
+ for _ in range(floor(max_lines / 2)):
20
+ new_line = _extract_line(strings, max_width, len(item_separator))
21
+ if not new_line:
22
+ break
23
+ final_lines.append(new_line)
24
+ strings = strings[len(new_line) :]
25
+ final_lines.reverse()
26
+
27
+ if strings and initial_lines:
28
+ initial_lines[-1] = ["..."]
29
+ lines = initial_lines + final_lines
30
+
31
+ formated_lines = [item_separator.join(line) for line in lines]
32
+ concatenated_lines = identation + f"\n{identation}".join(formated_lines)
33
+ return concatenated_lines
34
+
35
+
36
+ def _extract_line(strings, max_width, separator_size):
37
+ line = []
38
+ current_width = 0
39
+ for string in strings:
40
+ if len(string) + current_width + separator_size > max_width:
41
+ break
42
+ line.append(string)
43
+ current_width += len(string) + separator_size
44
+ return line
@@ -1,25 +1,66 @@
1
- from vtkmodules.vtkCommonCore import vtkUnsignedCharArray, vtkUnsignedIntArray
2
- from vtkmodules.vtkCommonDataModel import vtkPolyData
3
-
4
-
5
- def set_polydata_colors(data: vtkPolyData, color: tuple) -> vtkUnsignedCharArray:
6
- n_cells = data.GetNumberOfCells()
7
- cell_colors = vtkUnsignedCharArray()
8
- cell_colors.SetName("colors")
9
- cell_colors.SetNumberOfComponents(3)
10
- cell_colors.SetNumberOfTuples(n_cells)
11
- cell_colors.FillComponent(0, color[0])
12
- cell_colors.FillComponent(1, color[1])
13
- cell_colors.FillComponent(2, color[2])
14
- data.GetCellData().SetScalars(cell_colors)
15
- return cell_colors
16
-
17
-
18
- def set_polydata_property(data: vtkPolyData, property_data: int, property_name: str) -> vtkUnsignedIntArray:
19
- n_cells = data.GetNumberOfCells()
20
- cell_identifier = vtkUnsignedIntArray()
21
- cell_identifier.SetName(property_name)
22
- cell_identifier.SetNumberOfTuples(n_cells)
23
- cell_identifier.Fill(property_data)
24
- data.GetCellData().AddArray(cell_identifier)
25
- return cell_identifier
1
+ from pathlib import Path
2
+
3
+ from vtkmodules.vtkCommonCore import vtkUnsignedCharArray, vtkUnsignedIntArray
4
+ from vtkmodules.vtkCommonDataModel import vtkPolyData
5
+ from vtkmodules.vtkCommonTransforms import vtkTransform
6
+ from vtkmodules.vtkFiltersGeneral import vtkTransformPolyDataFilter
7
+ from vtkmodules.vtkIOGeometry import vtkOBJReader, vtkSTLReader
8
+
9
+
10
+ def set_polydata_colors(data: vtkPolyData, color: tuple) -> vtkUnsignedCharArray:
11
+ n_cells = data.GetNumberOfCells()
12
+ cell_colors = vtkUnsignedCharArray()
13
+ cell_colors.SetName("colors")
14
+ cell_colors.SetNumberOfComponents(3)
15
+ cell_colors.SetNumberOfTuples(n_cells)
16
+ cell_colors.FillComponent(0, color[0])
17
+ cell_colors.FillComponent(1, color[1])
18
+ cell_colors.FillComponent(2, color[2])
19
+ data.GetCellData().SetScalars(cell_colors)
20
+ return cell_colors
21
+
22
+
23
+ def set_polydata_property(
24
+ data: vtkPolyData, property_data: int, property_name: str
25
+ ) -> vtkUnsignedIntArray:
26
+ n_cells = data.GetNumberOfCells()
27
+ cell_identifier = vtkUnsignedIntArray()
28
+ cell_identifier.SetName(property_name)
29
+ cell_identifier.SetNumberOfTuples(n_cells)
30
+ cell_identifier.Fill(property_data)
31
+ data.GetCellData().AddArray(cell_identifier)
32
+ return cell_identifier
33
+
34
+
35
+ def read_obj_file(path: str | Path) -> vtkPolyData:
36
+ reader = vtkOBJReader()
37
+ reader.SetFileName(str(path))
38
+ reader.Update()
39
+ return reader.GetOutput()
40
+
41
+
42
+ def read_stl_file(path: str | Path) -> vtkPolyData:
43
+ reader = vtkSTLReader()
44
+ reader.SetFileName(str(path))
45
+ reader.Update()
46
+ return reader.GetOutput()
47
+
48
+
49
+ def transform_polydata(
50
+ polydata: vtkPolyData,
51
+ position=(0, 0, 0),
52
+ rotation=(0, 0, 0),
53
+ scale=(1, 1, 1),
54
+ ) -> vtkPolyData:
55
+ transform = vtkTransform()
56
+ transform.Translate(position)
57
+ transform.Scale(scale)
58
+ transform.RotateX(rotation[0])
59
+ transform.RotateY(rotation[1])
60
+ transform.RotateZ(rotation[2])
61
+ transform.Update()
62
+ transformation = vtkTransformPolyDataFilter()
63
+ transformation.SetTransform(transform)
64
+ transformation.SetInputData(polydata)
65
+ transformation.Update()
66
+ return transformation.GetOutput()
molde/utils/tree_info.py CHANGED
@@ -1,52 +1,52 @@
1
- from collections import namedtuple
2
-
3
- TreeItem = namedtuple("TreeItem", ["name", "data", "unity"])
4
- SEPARATOR = TreeItem("", "", "")
5
-
6
-
7
- class TreeInfo:
8
- def __init__(self, name: str) -> None:
9
- self.name = name
10
- self.items = list()
11
-
12
- def add_separator(self):
13
- self.items.append(TreeItem("", "", ""))
14
- return TreeItem("", "", "")
15
-
16
- def add_item(self, name, data, unity="") -> TreeItem:
17
- item = TreeItem(name, data, unity)
18
- self.items.append(item)
19
- return item
20
-
21
- def items_without_extra_separators(self):
22
- extra_separators = 0
23
- for item in reversed(self.items):
24
- if item != SEPARATOR:
25
- break
26
- extra_separators += 1
27
- return self.items[: len(self.items) - extra_separators]
28
-
29
- def __str__(self):
30
- title = self.name.upper()
31
- longest_name = max(0, *[len(item.name) for item in self.items])
32
- longest_data = max(0, *[len(str(item.data)) for item in self.items])
33
- spaces = longest_name + longest_data + 3
34
-
35
- text = f"{title} \n"
36
- pruned_items = self.items_without_extra_separators()
37
-
38
- for item in pruned_items[:-1]:
39
- if item == SEPARATOR:
40
- text += "│\n"
41
- continue
42
- spacer = " " * (spaces - len(item.name) - len(str(item.data)))
43
- unity = f"[{item.unity}]" if item.unity else ""
44
- text += f"├─ {item.name}: {spacer}{item.data} {unity}\n"
45
-
46
- # last item
47
- item = pruned_items[-1]
48
- spacer = " " * (spaces - len(item.name) - len(str(item.data)))
49
- unity = f"[{item.unity}]" if item.unity else ""
50
- text += f"╰─ {item.name}: {spacer}{item.data} {unity}\n"
51
- text += "\n"
52
- return text
1
+ from collections import namedtuple
2
+
3
+ TreeItem = namedtuple("TreeItem", ["name", "data", "unity"])
4
+ SEPARATOR = TreeItem("", "", "")
5
+
6
+
7
+ class TreeInfo:
8
+ def __init__(self, name: str) -> None:
9
+ self.name = name
10
+ self.items = list()
11
+
12
+ def add_separator(self):
13
+ self.items.append(TreeItem("", "", ""))
14
+ return TreeItem("", "", "")
15
+
16
+ def add_item(self, name, data, unity="") -> TreeItem:
17
+ item = TreeItem(name, data, unity)
18
+ self.items.append(item)
19
+ return item
20
+
21
+ def items_without_extra_separators(self):
22
+ extra_separators = 0
23
+ for item in reversed(self.items):
24
+ if item != SEPARATOR:
25
+ break
26
+ extra_separators += 1
27
+ return self.items[: len(self.items) - extra_separators]
28
+
29
+ def __str__(self):
30
+ title = self.name.upper()
31
+ longest_name = max(0, *[len(item.name) for item in self.items])
32
+ longest_data = max(0, *[len(str(item.data)) for item in self.items])
33
+ spaces = longest_name + longest_data + 3
34
+
35
+ text = f"{title} \n"
36
+ pruned_items = self.items_without_extra_separators()
37
+
38
+ for item in pruned_items[:-1]:
39
+ if item == SEPARATOR:
40
+ text += "│\n"
41
+ continue
42
+ spacer = " " * (spaces - len(item.name) - len(str(item.data)))
43
+ unity = f"[{item.unity}]" if item.unity else ""
44
+ text += f"├─ {item.name}: {spacer}{item.data} {unity}\n"
45
+
46
+ # last item
47
+ item = pruned_items[-1]
48
+ spacer = " " * (spaces - len(item.name) - len(str(item.data)))
49
+ unity = f"[{item.unity}]" if item.unity else ""
50
+ text += f"╰─ {item.name}: {spacer}{item.data} {unity}\n"
51
+ text += "\n"
52
+ return text
@@ -1,189 +1,189 @@
1
- import logging
2
- import re
3
- from time import sleep
4
-
5
- from PyQt5.QtCore import Qt
6
- from PyQt5.QtWidgets import QApplication, QLabel, QProgressBar, QWidget
7
- from PyQt5 import uic
8
-
9
- from molde import UI_DIR
10
-
11
-
12
- # Catches every message that contains something like [n/N]
13
- PROGRESS_FRACTION_REGEX = re.compile(r"\[\d+/\d+\]")
14
-
15
- # Catches every message that contains something like n%
16
- PROGRESS_PERCENTAGE_REGEX = re.compile(r"\d+%")
17
-
18
-
19
- class LoadingWindow(QWidget):
20
- """
21
- This function is intended to be called for functions that take
22
- a long time to run and should run together with a progress bar.
23
-
24
- The indended use is explained in the following example:
25
- ```
26
- def long_function(param_a, param_b=0):
27
- ...
28
- return value_c
29
- ```
30
-
31
- To run this function with a progress bar you just need to write
32
- ```
33
- value_c = LoadingWindow(long_function, param_a, param_b=1234)
34
- ```
35
-
36
- Disclaimers:
37
- This is a monstruosity, but it works.
38
- I hope no one ever needs to modify it.
39
- I am trying my best to explain every part of this code.
40
-
41
- An easier way to do it would be using a QWorker and a QThread,
42
- but it does not work for our Application.
43
- Qt manages the windows in the main thread, therefore the function
44
- to be loaded would need to run in a secondary thread.
45
- The problem is GMSH (wow, what a surprise).
46
-
47
- GMSH, for some reason, refuses to run in secondary thread.
48
- And GMSH is an important part of our software, that of course
49
- need a progress bar when it is running.
50
- So this is an attempt to run everything (both GMSH and QT) in the
51
- main thread without conflicts.
52
-
53
- I also don't want to mix the interface code with the engine code
54
- because it can easily became a mess and make the creation of
55
- automated tests really hard.
56
- """
57
-
58
- def __init__(self, _function, application, window_icon):
59
- super().__init__()
60
-
61
- ui_path = UI_DIR / "messages/new_loading_window.ui"
62
- uic.loadUi(ui_path, self)
63
-
64
- self._function = _function
65
- self._application = application
66
- self._window_icon = window_icon
67
-
68
- self._config_window()
69
-
70
- def _config_window(self):
71
- self.setWindowIcon(self._window_icon)
72
- self.setWindowTitle("Loading")
73
- self.setWindowFlags(Qt.Window | Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowMinimizeButtonHint)
74
- self.setWindowModality(Qt.ApplicationModal)
75
- self.update_position()
76
- self.progress_bar.setValue(0)
77
-
78
- def _define_qt_variables(self):
79
- self.progress_bar: QProgressBar
80
- self.progress_label: QLabel
81
-
82
- def update_position(self):
83
- '''
84
- Place the window on the center of the screen.
85
- '''
86
- desktop_geometry = self._application.primaryScreen().geometry()
87
- pos_x = int((desktop_geometry.width() - self.width())/2)
88
- pos_y = int((desktop_geometry.height() - self.height())/2)
89
- self.setGeometry(pos_x, pos_y, self.width(), self.height())
90
-
91
- def run(self, *args, **kwargs):
92
- self.show()
93
-
94
- # Changes the cursor to wait
95
- QApplication.setOverrideCursor(Qt.WaitCursor)
96
-
97
- # Creates a handler to update progress_bar and progress_label
98
- # every time a logging containing [n/N] appears
99
- progress_handler = ProgressBarLogUpdater(logging.DEBUG, loading_window=self)
100
- logging.getLogger().addHandler(progress_handler)
101
-
102
- # Waits the loading bar to appear and uptates pyqt
103
- sleep(0.1)
104
- QApplication.processEvents()
105
-
106
- try:
107
- # Calls the actual function
108
- return_value = self._function(*args, **kwargs)
109
-
110
- finally:
111
- """
112
- This piece of code will run even if the function have errors.
113
- Because the error is not handled here it will propagate to the
114
- function that called it.
115
- The error should be threated there, here we are just mitigating
116
- things related to the loading window.
117
- """
118
-
119
- # Restores the previous cursor
120
- QApplication.restoreOverrideCursor()
121
-
122
- # Removes the ProgressBarLogUpdater
123
- logging.getLogger().removeHandler(progress_handler)
124
-
125
- self.hide()
126
-
127
- return return_value
128
-
129
- def __call__(self, *args, **kwargs):
130
- return self.run(self._function, *args, **kwargs)
131
-
132
-
133
- class ProgressBarLogUpdater(logging.Handler):
134
- """
135
- This class is an log observer. It is meant to watch logs
136
- and use it to update an instance of the loading window.
137
- """
138
-
139
- def __init__(self, level=0, *, loading_window: LoadingWindow) -> None:
140
- super().__init__(level)
141
- self.loading_window = loading_window
142
-
143
- def emit(self, record):
144
- """
145
- This function is fired when something is logged.
146
- If the log have a marker like [n/N] or "..." in its message it
147
- will update the LoadingWindow associated with this class.
148
- """
149
-
150
- # Updates QT to prevent freezing
151
- QApplication.processEvents()
152
-
153
- percent = self.get_percentage(record.msg)
154
-
155
- if percent is not None:
156
- self.loading_window.progress_label.setText(record.msg)
157
- self.loading_window.progress_bar.setValue(percent)
158
-
159
- elif "..." in record.msg:
160
- self.loading_window.progress_label.setText(record.msg)
161
-
162
- # Updates QT to show the window modifications
163
- QApplication.processEvents()
164
-
165
- def get_percentage(self, message: str):
166
- """
167
- Uses regex to check if the message have a marker like [2/10]
168
- If it does, it extracts the step (2) and the max_step (10) and
169
- calculates the percentage (20%).
170
- Otherwise it just returns None.
171
- """
172
-
173
- if not isinstance(message, str):
174
- return
175
-
176
- matches = PROGRESS_FRACTION_REGEX.findall(message)
177
- if matches:
178
- first_match: str = matches[0]
179
- step, max_step = first_match.strip("[]").split("/")
180
- percentage = 100 * int(step) // int(max_step)
181
- return percentage
182
-
183
- matches = PROGRESS_PERCENTAGE_REGEX.findall(message)
184
- if matches:
185
- first_match: str = matches[0]
186
- percentage = int(first_match.strip("%"))
187
- return percentage
188
-
189
- return None
1
+ import logging
2
+ import re
3
+ from time import sleep
4
+
5
+ from PyQt5.QtCore import Qt
6
+ from PyQt5.QtWidgets import QApplication, QLabel, QProgressBar, QWidget
7
+ from PyQt5 import uic
8
+
9
+ from molde import UI_DIR
10
+
11
+
12
+ # Catches every message that contains something like [n/N]
13
+ PROGRESS_FRACTION_REGEX = re.compile(r"\[\d+/\d+\]")
14
+
15
+ # Catches every message that contains something like n%
16
+ PROGRESS_PERCENTAGE_REGEX = re.compile(r"\d+%")
17
+
18
+
19
+ class LoadingWindow(QWidget):
20
+ """
21
+ This function is intended to be called for functions that take
22
+ a long time to run and should run together with a progress bar.
23
+
24
+ The indended use is explained in the following example:
25
+ ```
26
+ def long_function(param_a, param_b=0):
27
+ ...
28
+ return value_c
29
+ ```
30
+
31
+ To run this function with a progress bar you just need to write
32
+ ```
33
+ value_c = LoadingWindow(long_function, param_a, param_b=1234)
34
+ ```
35
+
36
+ Disclaimers:
37
+ This is a monstruosity, but it works.
38
+ I hope no one ever needs to modify it.
39
+ I am trying my best to explain every part of this code.
40
+
41
+ An easier way to do it would be using a QWorker and a QThread,
42
+ but it does not work for our Application.
43
+ Qt manages the windows in the main thread, therefore the function
44
+ to be loaded would need to run in a secondary thread.
45
+ The problem is GMSH (wow, what a surprise).
46
+
47
+ GMSH, for some reason, refuses to run in secondary thread.
48
+ And GMSH is an important part of our software, that of course
49
+ need a progress bar when it is running.
50
+ So this is an attempt to run everything (both GMSH and QT) in the
51
+ main thread without conflicts.
52
+
53
+ I also don't want to mix the interface code with the engine code
54
+ because it can easily became a mess and make the creation of
55
+ automated tests really hard.
56
+ """
57
+
58
+ def __init__(self, _function, application, window_icon):
59
+ super().__init__()
60
+
61
+ ui_path = UI_DIR / "messages/new_loading_window.ui"
62
+ uic.loadUi(ui_path, self)
63
+
64
+ self._function = _function
65
+ self._application = application
66
+ self._window_icon = window_icon
67
+
68
+ self._config_window()
69
+
70
+ def _config_window(self):
71
+ self.setWindowIcon(self._window_icon)
72
+ self.setWindowTitle("Loading")
73
+ self.setWindowFlags(Qt.Window | Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowMinimizeButtonHint)
74
+ self.setWindowModality(Qt.ApplicationModal)
75
+ self.update_position()
76
+ self.progress_bar.setValue(0)
77
+
78
+ def _define_qt_variables(self):
79
+ self.progress_bar: QProgressBar
80
+ self.progress_label: QLabel
81
+
82
+ def update_position(self):
83
+ '''
84
+ Place the window on the center of the screen.
85
+ '''
86
+ desktop_geometry = self._application.primaryScreen().geometry()
87
+ pos_x = int((desktop_geometry.width() - self.width())/2)
88
+ pos_y = int((desktop_geometry.height() - self.height())/2)
89
+ self.setGeometry(pos_x, pos_y, self.width(), self.height())
90
+
91
+ def run(self, *args, **kwargs):
92
+ self.show()
93
+
94
+ # Changes the cursor to wait
95
+ QApplication.setOverrideCursor(Qt.WaitCursor)
96
+
97
+ # Creates a handler to update progress_bar and progress_label
98
+ # every time a logging containing [n/N] appears
99
+ progress_handler = ProgressBarLogUpdater(logging.DEBUG, loading_window=self)
100
+ logging.getLogger().addHandler(progress_handler)
101
+
102
+ # Waits the loading bar to appear and uptates pyqt
103
+ sleep(0.1)
104
+ QApplication.processEvents()
105
+
106
+ try:
107
+ # Calls the actual function
108
+ return_value = self._function(*args, **kwargs)
109
+
110
+ finally:
111
+ """
112
+ This piece of code will run even if the function have errors.
113
+ Because the error is not handled here it will propagate to the
114
+ function that called it.
115
+ The error should be threated there, here we are just mitigating
116
+ things related to the loading window.
117
+ """
118
+
119
+ # Restores the previous cursor
120
+ QApplication.restoreOverrideCursor()
121
+
122
+ # Removes the ProgressBarLogUpdater
123
+ logging.getLogger().removeHandler(progress_handler)
124
+
125
+ self.hide()
126
+
127
+ return return_value
128
+
129
+ def __call__(self, *args, **kwargs):
130
+ return self.run(self._function, *args, **kwargs)
131
+
132
+
133
+ class ProgressBarLogUpdater(logging.Handler):
134
+ """
135
+ This class is an log observer. It is meant to watch logs
136
+ and use it to update an instance of the loading window.
137
+ """
138
+
139
+ def __init__(self, level=0, *, loading_window: LoadingWindow) -> None:
140
+ super().__init__(level)
141
+ self.loading_window = loading_window
142
+
143
+ def emit(self, record):
144
+ """
145
+ This function is fired when something is logged.
146
+ If the log have a marker like [n/N] or "..." in its message it
147
+ will update the LoadingWindow associated with this class.
148
+ """
149
+
150
+ # Updates QT to prevent freezing
151
+ QApplication.processEvents()
152
+
153
+ percent = self.get_percentage(record.msg)
154
+
155
+ if percent is not None:
156
+ self.loading_window.progress_label.setText(record.msg)
157
+ self.loading_window.progress_bar.setValue(percent)
158
+
159
+ elif "..." in record.msg:
160
+ self.loading_window.progress_label.setText(record.msg)
161
+
162
+ # Updates QT to show the window modifications
163
+ QApplication.processEvents()
164
+
165
+ def get_percentage(self, message: str):
166
+ """
167
+ Uses regex to check if the message have a marker like [2/10]
168
+ If it does, it extracts the step (2) and the max_step (10) and
169
+ calculates the percentage (20%).
170
+ Otherwise it just returns None.
171
+ """
172
+
173
+ if not isinstance(message, str):
174
+ return
175
+
176
+ matches = PROGRESS_FRACTION_REGEX.findall(message)
177
+ if matches:
178
+ first_match: str = matches[0]
179
+ step, max_step = first_match.strip("[]").split("/")
180
+ percentage = 100 * int(step) // int(max_step)
181
+ return percentage
182
+
183
+ matches = PROGRESS_PERCENTAGE_REGEX.findall(message)
184
+ if matches:
185
+ first_match: str = matches[0]
186
+ percentage = int(first_match.strip("%"))
187
+ return percentage
188
+
189
+ return None