Skip to content

Commit

Permalink
Added code to exclude certain Modelica code during merge (#567)
Browse files Browse the repository at this point in the history
For #566
  • Loading branch information
mwetter authored Aug 19, 2024
1 parent 1c5779d commit c6dfed5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions buildingspy/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ BuildingsPy Changelog
Version 5.2.0, xxxx
^^^^^^^^^^^^^^^^^^^

- In buildingspy/development/merger.py, added new comment form to allow excluding
Modelica code for certain libraries.
(https://github.com/lbl-srg/BuildingsPy/issues/566)
- In buildingspy/development/regressiontest.py, add option to create reference
results in batch mode.
(https://github.com/lbl-srg/BuildingsPy/issues/560)
Expand Down
48 changes: 47 additions & 1 deletion buildingspy/development/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ def _copy_rename(self, src_library, des_library, src, des, rep):
raise e
# Remove library specific documentation.
lines = self.remove_library_specific_documentation(lines, self._new_library_name)
# Remove library specific source code.
lines = self.remove_library_specific_modelica_code(lines, self._new_library_name)
# Write the lines to the new file
with open(des, mode="w", encoding="utf-8") as f_des:
f_des.writelines(lines)

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

return lines

@staticmethod
def remove_library_specific_modelica_code(file_lines, library_name):
""" Remove library specific Modelica code.
For example, for the `Buildings` and `IDEAS` libraries, include the
section in the commented block below, but remove the Modelica code
for other libraries.
.. code-block:: html
//@modelica_select @remove_Buildings @remove_IDEAS
some code to be removed for
Buildings and IDEAS libraries.
//@modelica_select
:param file_lines: The lines of the file to be merged.
:return: The lines of the files, with code commented removed as indicated by the tag(s) in the comment line.
"""

lines = list()
pattern_start = "//@modelica_select_start"
pattern_end = "//@modelica_select_end"
library_token = "@remove_{}".format(library_name)
regular = True
for lin in file_lines:
if pattern_start in lin and library_token in lin:
# Found the start of the commented section for this library.
# Set flag
regular = False
# Keep line as is
lines.append(lin)
elif pattern_end in lin:
# Found the end of the line
regular = True
# Keep line as is
lines.append(lin)
else:
if regular:
lines.append(lin)
else:
lines.append(f"// removed: {lin}")

return lines

@staticmethod
def filter_files(file_list, pattern):
"""
Expand Down
29 changes: 29 additions & 0 deletions buildingspy/tests/test_development_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ def test_remove_library_specific_documentation(self):
"Test without removing")
return

def test_remove_library_specific_modelica_code(self):
import buildingspy.development.merger as m

lines = [
"aaa",
"bbb",
"//@modelica_select_start @remove_Buildings",
"ccc",
"//@modelica_select_end",
"ddd",
"//@modelica_select_start @remove_SomeOtherLib",
"eee",
"//@modelica_select_end",
"fff"]
result = [
"aaa",
"bbb",
"//@modelica_select_start @remove_Buildings",
"// removed: ccc",
"//@modelica_select_end",
"ddd",
"//@modelica_select_start @remove_SomeOtherLib",
"eee",
"//@modelica_select_end",
"fff"]
self.assertEqual(result,
m.IBPSA.remove_library_specific_modelica_code(lines, "Buildings"),
"Test one library")

def test_merge(self):
"""Test merging the libraries
"""
Expand Down

0 comments on commit c6dfed5

Please sign in to comment.