Repository: idaholab/TEAL (language: python)

Issue:
[DEFECT] TEAL CashFlow standalone does not work
--------
Defect Description
--------

**Describe the defect**

##### What did you expect to see happen?

That the CashFlow standalone could be used. 

I cannot use TEAL as "Plugin/external model" because I need to sample/modify variables in the Cash flow input files (not accessible through RAVEN external model interface)

##### What did you see instead?
Running the command ```python ./raven/plugins/TEAL/src/CashFlow_ExtMod.py -h        ``` as indicated in the manual does not work. It crashes with an error:

```
Traceback (most recent call last):
  File "./raven/plugins/TEAL/src/CashFlow_ExtMod.py", line 27, in <module>
    import main
  File "./raven/plugins/TEAL/src/main.py", line 28, in <module>
    from ..src import CashFlows
ImportError: attempted relative import with no known parent package
```

##### Do you have a suggested fix for the development team?

a driver is needed

**Describe how to Reproduce**
Steps to reproduce the behavior:
1.  see above
2. 
3. 
4. 

**Screenshots and Input Files**
Please attach the input file(s) that generate this error.  The simpler the input, the faster we can find the issue.

**Platform (please complete the following information):**
 - OS: [e.g. iOS]
 - Version: [e.g. 22]
 - Dependencies Installation: [CONDA or PIP]

----------------
For Change Control Board: Issue Review
----------------
This review should occur before any development is performed as a response to this issue.
- [x] 1. Is it tagged with a type: defect or task?
- [x] 2. Is it tagged with a priority: critical, normal or minor?
- [x] 3. If it will impact requirements or requirements tests, is it tagged with requirements?
- [x] 4. If it is a defect, can it cause wrong results for users? If so an email needs to be sent to the users.
- [x] 5. Is a rationale provided? (Such as explaining why the improvement is needed or why current code is wrong.)

-------
For Change Control Board: Issue Closure
-------
This review should occur when the issue is imminently going to be closed.
- [ ] 1. If the issue is a defect, is the defect fixed?
- [ ] 2. If the issue is a defect, is the defect tested for in the regression test system?  (If not explain why not.)
- [ ] 3. If the issue can impact users, has an email to the users group been written (the email should specify if the defect impacts stable or master)?
- [ ] 4. If the issue is a defect, does it impact the latest release branch? If yes, is there any issue tagged with release (create if needed)?
- [ ] 5. If the issue is being closed without a pull request, has an explanation of why it is being closed been provided?



Discussion / hints:
@worseliz @dylanjm @wangcj05 

Fix patch:
diff --git a/src/CashFlow_ExtMod.py b/src/CashFlow_ExtMod.py
index e25fa4e25..d4ffe4dd4 100644
--- a/src/CashFlow_ExtMod.py
+++ b/src/CashFlow_ExtMod.py
@@ -24,7 +24,7 @@
 import warnings
 warnings.simplefilter('default', DeprecationWarning)
 
-from ..src import main
+from . import main
 
 # This plugin imports RAVEN modules. if run in stand-alone, RAVEN needs to be installed and this file
 # needs to be in the propoer plugin directory.
@@ -120,7 +120,8 @@ def run(self, container, Inputs):
 #################################
 # Run the plugin in stand alone #
 #################################
-if __name__ == "__main__":
+def TEALmain():
+  """ run TEAL in standalone """
   # emulate RAVEN container
   class FakeSelf:
     """
@@ -172,6 +173,8 @@ def __init__(self):
   myInputs = {}
   with open(inpOpt.iINP[0]) as f:
     for l in f:
+      if l.strip().startswith("#") or not len(l.strip()):
+        continue
       (key, val) = l.split(' ', 1)
       myInputs[key] = np.array([float(n) for n in val.split(",")])
   #if Myverbosity < 2:
diff --git a/src/CashFlows.py b/src/CashFlows.py
index 098e7179b..3e8d2614e 100644
--- a/src/CashFlows.py
+++ b/src/CashFlows.py
@@ -26,8 +26,8 @@
 
 import numpy as np
 
-from ..src import Amortization
-from ..src import _utils as tutils
+from . import Amortization
+from . import _utils as tutils
 
 # load RAVEN if available (e.g. pip-installed), otherwise add to env
 try:
diff --git a/src/_utils.py b/src/_utils.py
index e537c203e..4999c5cbc 100644
--- a/src/_utils.py
+++ b/src/_utils.py
@@ -15,7 +15,6 @@
 # limitations under the License.
 """
   utilities for use within TEAL
-
 """
 import xml.etree.ElementTree as ET
 from os import path
diff --git a/src/main.py b/src/main.py
index e7cd9f1d3..f83ca1665 100644
--- a/src/main.py
+++ b/src/main.py
@@ -25,7 +25,7 @@
 import numpy as np
 import numpy_financial as npf
 
-from ..src import CashFlows
+from . import CashFlows
 
 from ravenframework.utils.graphStructure import graphObject
 from ravenframework.utils import mathUtils
diff --git a/teal_standalone.py b/teal_standalone.py
new file mode 100644
index 000000000..a4677f37e
--- /dev/null
+++ b/teal_standalone.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+# Copyright 2017 Battelle Energy Alliance, LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""
+Created on Dec 01, 2023
+
+@author: alfoa (A. Alfonsi)
+
+This is a package that properly imports TEAL (CashFlow standalone driver) and runs it.
+"""
+import sys
+from src.CashFlow_ExtMod import TEALmain
+if __name__ == '__main__':
+  sys.exit(TEALmain())


Test patch:
diff --git a/tests/CashFlow_test_NPV_RunAsCode.py b/tests/CashFlow_test_NPV_RunAsCode.py
index 5df9dc070..131350cd5 100644
--- a/tests/CashFlow_test_NPV_RunAsCode.py
+++ b/tests/CashFlow_test_NPV_RunAsCode.py
@@ -2,7 +2,7 @@
 import os
 import sys
 # run the Cash Flow plugin as stand alone code
-os.system('python  ../src/CashFlow_ExtMod.py -iXML Cash_Flow_input_NPV.xml -iINP VarInp.txt -o out.out')
+os.system('python  ../teal_standalone.py -iXML Cash_Flow_input_NPV.xml -iINP VarInp.txt -o out.out')
 
 # read out.out and compare with gold
 last = None
@@ -24,9 +24,10 @@
 #    <description>
 #      This input tests the RAVEN plugin CashFlow in standalone mode.
 #    </description>
-#    <classesTested>Models.ExternalModel.CashFlow</classesTested>
+#    <classesTested>Models.ExternalModel.TEAL.CashFlow</classesTested>
 #    <revisions>
 #      <revision author="alfoa" date="2019-11-25">Added classTested node</revision>
+#      <revision author="alfoa" date="2023-12-04">Reactivated test to test fix of issue #79</revision>
 #    </revisions>
 #    <requirements>CF-EA-5</requirements>
 #  </TestInfo>
diff --git a/tests/tests b/tests/tests
index c97470450..a21563345 100644
--- a/tests/tests
+++ b/tests/tests
@@ -58,7 +58,7 @@
  [./CashFlow_NPV_RunAsCode]
   type = 'RavenPython'
   input = 'CashFlow_test_NPV_RunAsCode.py'
-  skip = 'Need to create driver to make this work'
+  #skip = 'Fixed addressing issue #79'
  [../]
 
  [./CashFlow_NPV_customTime]