Skip to content

Commit c6dfed5

Browse files
authored
Added code to exclude certain Modelica code during merge (#567)
For #566
1 parent 1c5779d commit c6dfed5

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

buildingspy/CHANGES.txt

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ BuildingsPy Changelog
44
Version 5.2.0, xxxx
55
^^^^^^^^^^^^^^^^^^^
66

7+
- In buildingspy/development/merger.py, added new comment form to allow excluding
8+
Modelica code for certain libraries.
9+
(https://github.com/lbl-srg/BuildingsPy/issues/566)
710
- In buildingspy/development/regressiontest.py, add option to create reference
811
results in batch mode.
912
(https://github.com/lbl-srg/BuildingsPy/issues/560)

buildingspy/development/merger.py

+47-1
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ def _copy_rename(self, src_library, des_library, src, des, rep):
160160
raise e
161161
# Remove library specific documentation.
162162
lines = self.remove_library_specific_documentation(lines, self._new_library_name)
163+
# Remove library specific source code.
164+
lines = self.remove_library_specific_modelica_code(lines, self._new_library_name)
163165
# Write the lines to the new file
164166
with open(des, mode="w", encoding="utf-8") as f_des:
165167
f_des.writelines(lines)
166168

167169
@staticmethod
168170
def remove_library_specific_documentation(file_lines, library_name):
169-
""" Remove library specific content.
171+
""" Remove library specific documentation.
170172
171173
For example, for the `Buildings` and `IDEAS` libraries, include the
172174
section in the commented block below, but keep the comment as an html-comment
@@ -202,6 +204,50 @@ def remove_library_specific_documentation(file_lines, library_name):
202204

203205
return lines
204206

207+
@staticmethod
208+
def remove_library_specific_modelica_code(file_lines, library_name):
209+
""" Remove library specific Modelica code.
210+
211+
For example, for the `Buildings` and `IDEAS` libraries, include the
212+
section in the commented block below, but remove the Modelica code
213+
for other libraries.
214+
215+
.. code-block:: html
216+
217+
//@modelica_select @remove_Buildings @remove_IDEAS
218+
some code to be removed for
219+
Buildings and IDEAS libraries.
220+
//@modelica_select
221+
222+
:param file_lines: The lines of the file to be merged.
223+
:return: The lines of the files, with code commented removed as indicated by the tag(s) in the comment line.
224+
"""
225+
226+
lines = list()
227+
pattern_start = "//@modelica_select_start"
228+
pattern_end = "//@modelica_select_end"
229+
library_token = "@remove_{}".format(library_name)
230+
regular = True
231+
for lin in file_lines:
232+
if pattern_start in lin and library_token in lin:
233+
# Found the start of the commented section for this library.
234+
# Set flag
235+
regular = False
236+
# Keep line as is
237+
lines.append(lin)
238+
elif pattern_end in lin:
239+
# Found the end of the line
240+
regular = True
241+
# Keep line as is
242+
lines.append(lin)
243+
else:
244+
if regular:
245+
lines.append(lin)
246+
else:
247+
lines.append(f"// removed: {lin}")
248+
249+
return lines
250+
205251
@staticmethod
206252
def filter_files(file_list, pattern):
207253
"""

buildingspy/tests/test_development_merger.py

+29
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,35 @@ def test_remove_library_specific_documentation(self):
8888
"Test without removing")
8989
return
9090

91+
def test_remove_library_specific_modelica_code(self):
92+
import buildingspy.development.merger as m
93+
94+
lines = [
95+
"aaa",
96+
"bbb",
97+
"//@modelica_select_start @remove_Buildings",
98+
"ccc",
99+
"//@modelica_select_end",
100+
"ddd",
101+
"//@modelica_select_start @remove_SomeOtherLib",
102+
"eee",
103+
"//@modelica_select_end",
104+
"fff"]
105+
result = [
106+
"aaa",
107+
"bbb",
108+
"//@modelica_select_start @remove_Buildings",
109+
"// removed: ccc",
110+
"//@modelica_select_end",
111+
"ddd",
112+
"//@modelica_select_start @remove_SomeOtherLib",
113+
"eee",
114+
"//@modelica_select_end",
115+
"fff"]
116+
self.assertEqual(result,
117+
m.IBPSA.remove_library_specific_modelica_code(lines, "Buildings"),
118+
"Test one library")
119+
91120
def test_merge(self):
92121
"""Test merging the libraries
93122
"""

0 commit comments

Comments
 (0)