Repository: lbl-srg/BuildingsPy

Issue:
Add mechanism to exclude or leave certain Modelica code when merging IBPSA to other libraries
The heat pump model `PartialReversibleRefrigerantMachine` has code that need to be removed for `AixLib`, but left in place for `Buildings`, `BuildingSystems` and `IDEAS`.

The current mechanism with `<!-- ....` was designed for documentation and does not work for the modelica code. This issue is to add a new mechanism to provide this functionality.

@FWuellhorst : FYI

Discussion:

Solution patch:
diff --git a/buildingspy/CHANGES.txt b/buildingspy/CHANGES.txt
index 97b17b2..49f0630 100644
--- a/buildingspy/CHANGES.txt
+++ b/buildingspy/CHANGES.txt
@@ -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)
diff --git a/buildingspy/development/merger.py b/buildingspy/development/merger.py
index 8e92201..fe84d9c 100755
--- a/buildingspy/development/merger.py
+++ b/buildingspy/development/merger.py
@@ -160,13 +160,15 @@ class IBPSA(object):
             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
@@ -202,6 +204,50 @@ class IBPSA(object):
 
         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):
         """


Test patch:
diff --git a/buildingspy/tests/test_development_merger.py b/buildingspy/tests/test_development_merger.py
index f12f98c..88c35b1 100644
--- a/buildingspy/tests/test_development_merger.py
+++ b/buildingspy/tests/test_development_merger.py
@@ -88,6 +88,35 @@ class Test_development_merger_IBPSA(unittest.TestCase):
                          "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
         """