Skip to content

Commit

Permalink
Merge pull request #3217 from KratosMultiphysics/feature-python-modules
Browse files Browse the repository at this point in the history
making python-modules work
  • Loading branch information
philbucher authored Dec 3, 2018
2 parents 6133c93 + cfffd70 commit fcc349c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion applications/StructuralMechanicsApplication/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,4 @@ install(TARGETS KratosStructuralMechanicsCore DESTINATION libs )
install(TARGETS KratosStructuralMechanicsApplication DESTINATION libs )

# Add to the KratosMultiphisics Python module
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/StructuralMechanicsApplication.py" DESTINATION KratosMultiphysics )
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/python_scripts/__init__.py" DESTINATION "KratosMultiphysics/StructuralMechanicsApplication")
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
application_folder = "StructuralMechanicsApplication"

# The following lines are common for all applications
from . import application_importer
from .. import application_importer
import inspect
caller = inspect.stack()[1] # Information about the file that imported this, to check for unexpected imports
application_importer.ImportApplication(application,application_name,application_folder,caller)
application_importer.ImportApplication(application,application_name,application_folder,caller, __path__)
8 changes: 5 additions & 3 deletions kratos/python_interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# Initialize Kernel so that core variables have an assigned Key even if we are not importing applications
KratosGlobals.Kernel.Initialize()

# adding the scripts in "kratos/python_scripts" such that they are treated as a regular python-module
__path__.append(KratosLoader.kratos_scripts)

def CheckForPreviousImport():

first_caller = KratosGlobals.AuthorizedCaller[
Expand Down Expand Up @@ -48,6 +51,5 @@ def CheckForPreviousImport():
def CheckRegisteredApplications(*applications):
for app in applications:
if not KratosGlobals.Kernel.IsImported(app):
import __main__
raise Exception("Application "+ app + " was not imported in the main script ("+__main__.__file__+")")

import __main__
raise Exception("Application "+ app + " was not imported in the main script ("+__main__.__file__+")")
7 changes: 6 additions & 1 deletion kratos/python_interface/application_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from KratosMultiphysics import Logger


def ImportApplication(application, application_name, application_folder, caller):
def ImportApplication(application, application_name, application_folder, caller, mod_path=None):
Globals = KratosMultiphysics.KratosGlobals
Kernel = Globals.Kernel
main_caller = Globals.AuthorizedCaller
Expand All @@ -30,6 +30,11 @@ def ImportApplication(application, application_name, application_folder, caller)
# Add constitutive laws python scrips folder to path
constitutive_laws_path = os.path.join(python_path, 'constitutive_laws')
sys.path.append(constitutive_laws_path)

# adding the scripts in "APP_NAME/python_scripts" such that they are treated as a regular python-module
if mod_path is not None: # optional for backwards compatibility
mod_path.append(python_path)

# Add application to kernel
Kernel.ImportApplication(application)
# Dynamic renumbering of variables to ensure consistency
Expand Down

0 comments on commit fcc349c

Please sign in to comment.