Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making python-modules work #3217

Merged
merged 10 commits into from
Dec 3, 2018
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