Skip to content

Commit 1e2aa09

Browse files
authored
Refactor/csvbuilder (#98)
1 parent e9899b1 commit 1e2aa09

File tree

4 files changed

+87
-19
lines changed

4 files changed

+87
-19
lines changed

pythonfmu/builder.py

+6-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from .osutil import get_lib_extension, get_platform
1616
from ._version import __version__
1717
from .fmi2slave import FMI2_MODEL_OPTIONS, Fmi2Slave
18-
from .csvslave import create_csv_slave
1918

2019
FilePath = Union[str, Path]
2120
HERE = Path(__file__).parent
@@ -73,11 +72,8 @@ def build_FMU(
7372
script_file = Path(script_file)
7473
if not script_file.exists():
7574
raise ValueError(f"No such file {script_file!s}")
76-
if not script_file.suffix.endswith(".py") and not script_file.suffix.endswith(".csv"):
77-
raise ValueError(f"File {script_file!s} must have extension '.py' or '.csv'!")
78-
79-
if script_file.suffix.endswith(".csv"):
80-
project_files = {script_file}
75+
if not script_file.suffix.endswith(".py"):
76+
raise ValueError(f"File {script_file!s} must have extension '.py'!")
8177

8278
dest = Path(dest)
8379
if not dest.exists():
@@ -95,13 +91,7 @@ def build_FMU(
9591

9692
with tempfile.TemporaryDirectory(prefix="pythonfmu_") as tempd:
9793
temp_dir = Path(tempd)
98-
if script_file.suffix.endswith(".csv"):
99-
py_file = temp_dir / (script_file.stem + ".py")
100-
with open(py_file, "+w") as f:
101-
f.write(create_csv_slave(script_file))
102-
script_file = py_file
103-
else:
104-
shutil.copy2(script_file, temp_dir)
94+
shutil.copy2(script_file, temp_dir)
10595

10696
# Embed pythonfmu in the FMU so it does not need to be included
10797
dep_folder = temp_dir / "pythonfmu"
@@ -210,7 +200,7 @@ def has_binary() -> bool:
210200

211201
def main():
212202
parser = argparse.ArgumentParser(
213-
prog="pythonfmu-builder", description="Build an FMU from a Python script or CSV file."
203+
prog="pythonfmu-builder", description="Build an FMU from a Python script."
214204
)
215205

216206
parser.add_argument(
@@ -224,7 +214,7 @@ def main():
224214
"-f",
225215
"--file",
226216
dest="script_file",
227-
help="Path to the Python script or CSV file.",
217+
help="Path to the Python script.",
228218
required=True,
229219
)
230220

@@ -252,7 +242,7 @@ def main():
252242
"project_files",
253243
metavar="Project files",
254244
nargs="*",
255-
help="Additional project files required by the Python script. Ignored when using CSV input.",
245+
help="Additional project files required by the Python script.",
256246
default=set(),
257247
)
258248

pythonfmu/csvslave.py renamed to pythonfmu/csvbuilder.py

+78-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
import argparse
2+
import tempfile
13
from pathlib import Path
2-
from typing import Union
4+
from typing import Union, Optional
5+
from ._version import __version__
6+
from .fmi2slave import FMI2_MODEL_OPTIONS
7+
from .builder import FmuBuilder
8+
39
FilePath = Union[str, Path]
410

11+
512
def create_csv_slave(csvfile: FilePath):
613
classname = csvfile.stem
714
filename = csvfile.name
@@ -94,3 +101,73 @@ def do_step(self, current_time: float, step_size: float) -> bool:
94101
self.find_indices(current_time, step_size)
95102
return True
96103
"""
104+
105+
106+
class CsvFmuBuilder:
107+
108+
@staticmethod
109+
def build_FMU(
110+
csv_file: FilePath,
111+
dest: FilePath = ".",
112+
documentation_folder: Optional[FilePath] = None,
113+
**options,
114+
) -> Path:
115+
116+
if not csv_file.exists():
117+
raise ValueError(f"No such file {csv_file!s}")
118+
if not csv_file.suffix.endswith(".csv"):
119+
raise ValueError(f"File {csv_file!s} must have extension '.csv'!")
120+
121+
options["project_files"] = {csv_file}
122+
123+
with tempfile.TemporaryDirectory(prefix="pythonfmu_") as tempd:
124+
temp_dir = Path(tempd)
125+
script_file = temp_dir / (csv_file.stem + ".py")
126+
with open(script_file, "+w") as f:
127+
f.write(create_csv_slave(csv_file))
128+
options["script_file"] = script_file
129+
return FmuBuilder.build_FMU(**options)
130+
131+
132+
def main():
133+
parser = argparse.ArgumentParser(
134+
prog="pythonfmu-csvbuilder", description="Build an FMU from a Python script or CSV file."
135+
)
136+
137+
parser.add_argument(
138+
"-v",
139+
"--version",
140+
action="version",
141+
version=__version__
142+
)
143+
144+
parser.add_argument(
145+
"-f",
146+
"--file",
147+
dest="script_file",
148+
help="Path to the CSV file.",
149+
required=True,
150+
)
151+
152+
parser.add_argument(
153+
"-d", "--dest", dest="dest", help="Where to save the FMU.", default="."
154+
)
155+
156+
parser.add_argument(
157+
"--doc",
158+
dest="documentation_folder",
159+
help="Documentation folder to include in the FMU.",
160+
default=None,
161+
)
162+
163+
for option in FMI2_MODEL_OPTIONS:
164+
action = "store_false" if option.value else "store_true"
165+
parser.add_argument(
166+
f"--{option.cli}",
167+
dest=option.name,
168+
help=f"If given, {option.name}={action[6:]}",
169+
action=action
170+
)
171+
172+
options = vars(parser.parse_args())
173+
CsvFmuBuilder.build_FMU(**options)

pythonfmu/tests/test_csvslave.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from pathlib import Path
33

4-
from pythonfmu.builder import FmuBuilder
4+
from pythonfmu.csvbuilder import CsvFmuBuilder
55

66
EPS = 1e-7
77
DEMO = "csvdemo.csv"
@@ -13,7 +13,7 @@ def test_csvslave(tmp_path):
1313

1414
csv_file = Path(__file__).parent / DEMO
1515

16-
fmu = FmuBuilder.build_FMU(csv_file, dest=tmp_path)
16+
fmu = CsvFmuBuilder.build_FMU(csv_file, dest=tmp_path)
1717
assert fmu.exists()
1818

1919
model_description = fmpy.read_model_description(fmu)

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pythonfmu =
4242
console_scripts =
4343
pythonfmu-builder = pythonfmu.builder:main
4444
pythonfmu-deploy = pythonfmu.deploy:main
45+
pythonfmu-csvbuilder = pythonfmu.csvbuilder:main
4546

4647
[options.extras_require]
4748
tests =

0 commit comments

Comments
 (0)