Skip to content

Commit f662db6

Browse files
committed
Format
1 parent eeaa433 commit f662db6

File tree

6 files changed

+102
-61
lines changed

6 files changed

+102
-61
lines changed

modelica_fmi/src/modelica_fmi/_lib.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
def name(variable: ModelVariable | Item, suffix: str = "") -> str:
88
"""Convert the FMI variable name to a valid Modelica identifier"""
99
import re
10+
1011
ex = re.compile(r"^[a-zA-Z_$][\w$]*$")
1112
if ex.match(variable.name):
1213
return variable.name + suffix
@@ -51,14 +52,14 @@ def set_variables(variables: Iterable[ModelVariable], indent: int = 2) -> str:
5152
if len(variable.dimensions) == 2:
5253
line += "Matrix"
5354
line += f"(instance, valueReferences={{{variable.valueReference}}}, values="
54-
if variable.type == 'Enumeration':
55+
if variable.type == "Enumeration":
5556
line += f"Types.{variable.declaredType.name}ToInt64("
5657
if not variable.dimensions:
5758
line += "{"
5859
line += name(variable)
5960
if not variable.dimensions:
6061
line += "}"
61-
if variable.type == 'Enumeration':
62+
if variable.type == "Enumeration":
6263
line += ")"
6364
line += ");"
6465
lines.append(line)
@@ -121,9 +122,7 @@ def value2enum(value: str) -> str:
121122
return f'"{variable.start}"'
122123
elif variable.type == "Enumeration":
123124
item = next(
124-
item
125-
for item in variable.declaredType.items
126-
if item.value == variable.start
125+
item for item in variable.declaredType.items if item.value == variable.start
127126
)
128127
return f"Types.{variable.declaredType.name}.{name(item)}"
129128
else:
@@ -140,7 +139,6 @@ def modelica_type(variable):
140139

141140

142141
def modifiers(variable: ModelVariable, start: bool = False):
143-
144142
attrs = []
145143

146144
prefix = "each " if variable.dimensions else ""
@@ -150,16 +148,13 @@ def modifiers(variable: ModelVariable, start: bool = False):
150148

151149
if variable.unit is not None:
152150
attrs.append(f'{prefix}unit="{variable.unit}"')
153-
elif (
154-
variable.declaredType is not None and variable.declaredType.unit is not None
155-
):
151+
elif variable.declaredType is not None and variable.declaredType.unit is not None:
156152
attrs.append(f'{prefix}unit="{variable.declaredType.unit}"')
157153

158154
if variable.quantity is not None:
159155
attrs.append(f'{prefix}quantity="{variable.quantity}"')
160156
elif (
161-
variable.declaredType is not None
162-
and variable.declaredType.quantity is not None
157+
variable.declaredType is not None and variable.declaredType.quantity is not None
163158
):
164159
attrs.append(f'{prefix}quantity="{variable.declaredType.quantity}"')
165160

@@ -168,19 +163,20 @@ def modifiers(variable: ModelVariable, start: bool = False):
168163

169164
return ""
170165

166+
171167
def choices(variable: ModelVariable) -> str:
172168
if variable.type == "Enumeration":
173169
choices = [
174-
f'choice={item.value} "{item.name}"'
175-
for item in variable.declaredType.items
170+
f'choice={item.value} "{item.name}"' for item in variable.declaredType.items
176171
]
177172
return " annotation(choices(" + ", ".join(choices) + "))"
178173
else:
179174
return ""
180175

181176

182-
def dependencies(model_description: ModelDescription, variable: ModelVariable, type, nValues=False):
183-
177+
def dependencies(
178+
model_description: ModelDescription, variable: ModelVariable, type, nValues=False
179+
):
184180
variables = {}
185181

186182
unknown = next(
@@ -206,6 +202,7 @@ def dependencies(model_description: ModelDescription, variable: ModelVariable, t
206202

207203
return arguments
208204

205+
209206
def dependencies2(model_description: ModelDescription, variable):
210207
unknown = next(
211208
filter(
@@ -215,16 +212,18 @@ def dependencies2(model_description: ModelDescription, variable):
215212
)
216213
return unknown.dependencies
217214

215+
218216
def numel(variables: dict[int, ModelVariable], variable: ModelVariable):
219217
n = 1
220218
for dimension in variable.dimensions:
221219
n *= int(
222220
dimension.start
223-
if dimension.start
221+
if dimension.start
224222
else variables[dimension.valueReference].start
225223
)
226224
return n
227225

226+
228227
def shape(variables: dict[int, ModelVariable], variable: ModelVariable):
229228
s = []
230229
for dimension in variable.dimensions:
@@ -237,7 +236,13 @@ def shape(variables: dict[int, ModelVariable], variable: ModelVariable):
237236
)
238237
return tuple(s)
239238

240-
def dependencies3(model_description: ModelDescription, variables: dict[int, ModelVariable], variable: ModelVariable, types: list[str]):
239+
240+
def dependencies3(
241+
model_description: ModelDescription,
242+
variables: dict[int, ModelVariable],
243+
variable: ModelVariable,
244+
types: list[str],
245+
):
241246
arguments = []
242247

243248
for type in types:

modelica_fmi/src/modelica_fmi/cli.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ def main():
1919
version=f"%(prog)s version {version}",
2020
help="print the program version",
2121
)
22-
parser.add_argument("--basic", action="store_true", help="Use basic co-simulation w/o initialization")
23-
parser.add_argument("--hide-connectors", action="store_true", help="Hide the connectors in the graphical representation")
22+
parser.add_argument(
23+
"--basic",
24+
action="store_true",
25+
help="Use basic co-simulation w/o initialization",
26+
)
27+
parser.add_argument(
28+
"--hide-connectors",
29+
action="store_true",
30+
help="Hide the connectors in the graphical representation",
31+
)
2432
parser.add_argument("fmu_path", help="Path of the FMU to import")
2533
parser.add_argument("model_path", help="Path of the Modelica file to generate")
2634

modelica_fmi/src/modelica_fmi/gui/ImportDialog.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from PySide6.QtWidgets import (QDialog, QFileDialog, QMessageBox)
1+
from PySide6.QtWidgets import QDialog, QFileDialog, QMessageBox
22
from PySide6.QtCore import QSettings
33
from modelica_fmi.gui.generated.ImportDialog import Ui_ImportDialog
44
from modelica_fmi.import_fmu_to_modelica import import_fmu_to_modelica
55

66

77
class ImportDialog(QDialog):
8-
98
def __init__(self, parent=None):
109
super().__init__(parent)
1110
self.ui = Ui_ImportDialog()
@@ -15,23 +14,26 @@ def __init__(self, parent=None):
1514
self.ui.selectModelPathButton.clicked.connect(self.selectModelPath)
1615

1716
def selectFMUPath(self):
18-
filename, _ = QFileDialog.getOpenFileName(parent=self,
19-
caption="Select FMU",
20-
dir=self.ui.fmuPathLineEdit.text(),
21-
filter="FMUs (*.fmu)")
17+
filename, _ = QFileDialog.getOpenFileName(
18+
parent=self,
19+
caption="Select FMU",
20+
dir=self.ui.fmuPathLineEdit.text(),
21+
filter="FMUs (*.fmu)",
22+
)
2223
if filename:
2324
self.ui.fmuPathLineEdit.setText(filename)
2425

2526
def selectModelPath(self):
26-
filename, _ = QFileDialog.getSaveFileName(parent=self,
27-
caption="Select Modelica File",
28-
dir=self.ui.modelPathLineEdit.text(),
29-
filter="Modelica Files (*.mo)")
27+
filename, _ = QFileDialog.getSaveFileName(
28+
parent=self,
29+
caption="Select Modelica File",
30+
dir=self.ui.modelPathLineEdit.text(),
31+
filter="Modelica Files (*.mo)",
32+
)
3033
if filename:
3134
self.ui.modelPathLineEdit.setText(filename)
3235

3336
def importFMU(self):
34-
3537
settings = QSettings()
3638
settings.setValue("fmuPath", self.ui.fmuPathLineEdit.text())
3739
settings.setValue("modelPath", self.ui.modelPathLineEdit.text())

modelica_fmi/src/modelica_fmi/gui/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from modelica_fmi.import_fmu_to_modelica import import_fmu_to_modelica
2-
from PySide6.QtWidgets import (QDialog, QMessageBox)
2+
from PySide6.QtWidgets import QDialog, QMessageBox
33
from PySide6.QtCore import QSettings
44

55

66
def compile_resources():
7-
""" Compile the .ui and .qrc files if they are available and newer than the compiled .py files """
7+
"""Compile the .ui and .qrc files if they are available and newer than the compiled .py files"""
88

99
import os
1010
from pathlib import Path
@@ -14,46 +14,47 @@ def compile_resources():
1414

1515
pyside_dir = Path(PySide6.__file__).parent
1616

17-
if os.name == 'posix':
18-
pyside_dir = pyside_dir / 'Qt' / 'libexec'
17+
if os.name == "posix":
18+
pyside_dir = pyside_dir / "Qt" / "libexec"
1919

2020
gui_dir = Path(__file__).parent
21-
forms_dir = gui_dir / 'forms'
22-
generated_dir = gui_dir / 'generated'
21+
forms_dir = gui_dir / "forms"
22+
generated_dir = gui_dir / "generated"
2323

2424
# compile the forms
2525
if forms_dir.is_dir():
26-
2726
for file in os.listdir(forms_dir):
28-
29-
if not file.endswith('.ui'):
27+
if not file.endswith(".ui"):
3028
continue
3129

3230
form = os.path.basename(file)
3331
form, _ = os.path.splitext(form)
3432

35-
ui_file = forms_dir / f'{form}.ui'
36-
py_file = generated_dir / f'{form}.py'
33+
ui_file = forms_dir / f"{form}.ui"
34+
py_file = generated_dir / f"{form}.py"
3735

3836
if os.path.isfile(ui_file):
39-
if not py_file.is_file() or getmtime(ui_file) > os.path.getmtime(py_file):
37+
if not py_file.is_file() or getmtime(ui_file) > os.path.getmtime(
38+
py_file
39+
):
4040
print(f"UIC'ing {ui_file}")
41-
uic = pyside_dir / 'uic'
42-
check_call([uic, ui_file, '-o', py_file, '-g', 'python', '--from-imports'])
41+
uic = pyside_dir / "uic"
42+
check_call(
43+
[uic, ui_file, "-o", py_file, "-g", "python", "--from-imports"]
44+
)
4345

44-
icons_qrc = gui_dir / 'icons' / 'icons.qrc'
45-
icons_rc_py = generated_dir / 'icons_rc.py'
46+
icons_qrc = gui_dir / "icons" / "icons.qrc"
47+
icons_rc_py = generated_dir / "icons_rc.py"
4648

4749
# compile the resources
4850
if icons_qrc.is_file():
4951
if not icons_rc_py.is_file() or getmtime(icons_qrc) > getmtime(icons_rc_py):
5052
print(f"RCC'ing {icons_qrc}")
51-
rcc = pyside_dir / 'rcc'
52-
check_call([rcc, icons_qrc, '-o', icons_rc_py, '-g', 'python'])
53+
rcc = pyside_dir / "rcc"
54+
check_call([rcc, icons_qrc, "-o", icons_rc_py, "-g", "python"])
5355

5456

5557
def main():
56-
5758
compile_resources()
5859

5960
import sys
@@ -75,7 +76,6 @@ def main():
7576
dialog.ui.modelPathLineEdit.setText(settings.value("modelPath", defaultValue=""))
7677

7778
if dialog.exec() == QDialog.DialogCode.Accepted:
78-
7979
settings.setValue("fmuPath", dialog.ui.fmuPathLineEdit.text())
8080
settings.setValue("modelPath", dialog.ui.modelPathLineEdit.text())
8181

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
""" Entry point for the graphical user interface """
1+
"""Entry point for the graphical user interface"""
22

3-
if __name__ == '__main__':
3+
if __name__ == "__main__":
44
from modelica_fmi.gui import main
5+
56
main()

modelica_fmi/src/modelica_fmi/import_fmu_to_modelica.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
from os import PathLike
22
from typing import Literal, Iterable
33

4-
from modelica_fmi._lib import start_value, subscripts, modelica_type, fmi2_type, fmi3_type, modifiers, choices, \
5-
dependencies, dependencies2, dependencies3, numel, set_variables, name
4+
from modelica_fmi._lib import (
5+
start_value,
6+
subscripts,
7+
modelica_type,
8+
fmi2_type,
9+
fmi3_type,
10+
modifiers,
11+
choices,
12+
dependencies,
13+
dependencies2,
14+
dependencies3,
15+
numel,
16+
set_variables,
17+
name,
18+
)
619

720

821
class FMUImportError(Exception):
@@ -15,7 +28,7 @@ def import_fmu_to_modelica(
1528
interface_type: Literal["CoSimulation"] = "CoSimulation",
1629
variables: Iterable[str] | None = None,
1730
basic: bool = False,
18-
hide_connectors = False,
31+
hide_connectors=False,
1932
):
2033
from os import makedirs
2134
from pathlib import Path
@@ -88,7 +101,9 @@ def import_fmu_to_modelica(
88101

89102
fmiMajorVersion = int(model_description.fmiVersion[0])
90103

91-
template = environment.get_template(f"FMI{fmiMajorVersion}_{"Basic" if basic else ""}{IT}.mo")
104+
template = environment.get_template(
105+
f"FMI{fmiMajorVersion}_{'Basic' if basic else ''}{IT}.mo"
106+
)
92107

93108
parameters = []
94109
structural_parameters = []
@@ -165,16 +180,26 @@ def import_fmu_to_modelica(
165180

166181
template.globals.update(
167182
{
168-
"start_value": lambda *args, **kwargs: start_value(variables, *args, **kwargs),
169-
"subscripts": lambda *args, **kwargs: subscripts(variables, *args, **kwargs),
183+
"start_value": lambda *args, **kwargs: start_value(
184+
variables, *args, **kwargs
185+
),
186+
"subscripts": lambda *args, **kwargs: subscripts(
187+
variables, *args, **kwargs
188+
),
170189
"modelica_type": modelica_type,
171190
"fmi_type": fmi2_type if fmiMajorVersion == 2 else fmi3_type,
172191
"modifiers": modifiers,
173192
"choices": choices,
174193
"name": name,
175-
"dependencies": lambda *args, **kwargs: dependencies(model_description, *args, **kwargs),
176-
"dependencies2": lambda *args, **kwargs: dependencies2(model_description, *args, **kwargs),
177-
"dependencies3": lambda *args, **kwargs: dependencies3(model_description, variables, *args, **kwargs),
194+
"dependencies": lambda *args, **kwargs: dependencies(
195+
model_description, *args, **kwargs
196+
),
197+
"dependencies2": lambda *args, **kwargs: dependencies2(
198+
model_description, *args, **kwargs
199+
),
200+
"dependencies3": lambda *args, **kwargs: dependencies3(
201+
model_description, variables, *args, **kwargs
202+
),
178203
"numel": lambda *args, **kwargs: numel(variables, *args, **kwargs),
179204
"set_variables": set_variables,
180205
}

0 commit comments

Comments
 (0)