Skip to content

Commit 5368216

Browse files
authored
Omhome as argument (#238)
* [OMCSession] fix muteable argument for class * [OMCSessionZMQ] fix muteable argument for class * [OMCSessionHelper] allow definition of omhome as argument to the class * fix argument order - move omhome at the end * if arguments are used by position, new arguments CANNOT be added as first argument ...
1 parent f4cb77b commit 5368216

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

OMPython/__init__.py

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,38 @@ def kill(self):
122122
def wait(self, timeout):
123123
return self.process.wait(timeout=timeout)
124124

125-
class OMCSessionHelper():
126-
def __init__(self):
127-
# Get the path to the OMC executable, if not installed this will be None
128-
omc_env_home = os.environ.get('OPENMODELICAHOME')
129-
if omc_env_home:
130-
self.omhome = omc_env_home
131-
else:
132-
path_to_omc = shutil.which("omc")
133-
if path_to_omc is None:
125+
126+
class OMCSessionHelper:
127+
def __init__(self, omhome: str = None):
128+
self.omhome = None
129+
130+
# use the provided path
131+
if omhome is not None:
132+
self.omhome = omhome
133+
return
134+
135+
# check the environment variable
136+
omhome = os.environ.get('OPENMODELICAHOME')
137+
if omhome is not None:
138+
self.omhome = omhome
139+
return
140+
141+
# Get the path to the OMC executable, if not installed this will be None
142+
path_to_omc = shutil.which("omc")
143+
if path_to_omc is not None:
144+
self.omhome = os.path.dirname(os.path.dirname(path_to_omc))
145+
return
146+
134147
raise ValueError("Cannot find OpenModelica executable, please install from openmodelica.org")
135-
self.omhome = os.path.dirname(os.path.dirname(path_to_omc))
136-
def _get_omc_path(self):
137-
try:
138-
return os.path.join(self.omhome, 'bin', 'omc')
139-
except BaseException:
140-
logger.error("The OpenModelica compiler is missing in the System path (%s), please install it" % os.path.join(self.omhome, 'bin', 'omc'))
141-
raise
148+
149+
def _get_omc_path(self):
150+
try:
151+
return os.path.join(self.omhome, 'bin', 'omc')
152+
except BaseException:
153+
logger.error("The OpenModelica compiler is missing in the System path (%s), please install it"
154+
% os.path.join(self.omhome, 'bin', 'omc'))
155+
raise
156+
142157

143158
class OMCSessionBase(with_metaclass(abc.ABCMeta, object)):
144159

@@ -540,8 +555,13 @@ def getClassNames(self, className=None, recursive=False, qualified=False, sort=F
540555

541556
class OMCSession(OMCSessionHelper, OMCSessionBase):
542557

543-
def __init__(self, readonly=False, serverFlag='--interactive=corba', timeout = 10.0, docker = None, dockerContainer = None, dockerExtraArgs = [], dockerOpenModelicaPath = "omc", dockerNetwork = None):
544-
OMCSessionHelper.__init__(self)
558+
def __init__(self, readonly = False, serverFlag ='--interactive=corba', timeout = 10.0,
559+
docker = None, dockerContainer = None, dockerExtraArgs = None, dockerOpenModelicaPath = "omc",
560+
dockerNetwork = None, omhome: str = None):
561+
if dockerExtraArgs is None:
562+
dockerExtraArgs = []
563+
564+
OMCSessionHelper.__init__(self, omhome=omhome)
545565
OMCSessionBase.__init__(self, readonly)
546566
self._create_omc_log_file("objid")
547567
# Locating and using the IOR
@@ -682,8 +702,13 @@ def sendExpression(self, command, parsed=True):
682702

683703
class OMCSessionZMQ(OMCSessionHelper, OMCSessionBase):
684704

685-
def __init__(self, readonly=False, timeout = 10.00, docker = None, dockerContainer = None, dockerExtraArgs = [], dockerOpenModelicaPath = "omc", dockerNetwork = None, port = None):
686-
OMCSessionHelper.__init__(self)
705+
def __init__(self, readonly=False, timeout = 10.00,
706+
docker = None, dockerContainer = None, dockerExtraArgs = None, dockerOpenModelicaPath = "omc",
707+
dockerNetwork = None, port = None, omhome: str = None):
708+
if dockerExtraArgs is None:
709+
dockerExtraArgs = []
710+
711+
OMCSessionHelper.__init__(self, omhome=omhome)
687712
OMCSessionBase.__init__(self, readonly)
688713
# Locating and using the IOR
689714
if sys.platform != 'win32' or docker or dockerContainer:
@@ -793,8 +818,10 @@ class ModelicaSystemError(Exception):
793818

794819

795820
class ModelicaSystem(object):
796-
def __init__(self, fileName=None, modelName=None, lmodel=None, useCorba=False, commandLineOptions=None,
797-
variableFilter=None, customBuildDirectory=None, verbose=True, raiseerrors=False): # 1
821+
def __init__(self, fileName=None, modelName=None, lmodel=None,
822+
useCorba=False, commandLineOptions=None,
823+
variableFilter=None, customBuildDirectory=None, verbose=True, raiseerrors=False,
824+
omhome: str = None): # 1
798825
"""
799826
"constructor"
800827
It initializes to load file and build a model, generating object, exe, xml, mat, and json files. etc. It can be called :
@@ -806,9 +833,9 @@ def __init__(self, fileName=None, modelName=None, lmodel=None, useCorba=False, c
806833
"""
807834
if fileName is None and modelName is None and not lmodel: # all None
808835
if useCorba:
809-
self.getconn = OMCSession()
836+
self.getconn = OMCSession(omhome=omhome)
810837
else:
811-
self.getconn = OMCSessionZMQ()
838+
self.getconn = OMCSessionZMQ(omhome=omhome)
812839
return
813840

814841
self.tree = None
@@ -831,9 +858,9 @@ def __init__(self, fileName=None, modelName=None, lmodel=None, useCorba=False, c
831858
self._verbose = verbose
832859

833860
if useCorba:
834-
self.getconn = OMCSession()
861+
self.getconn = OMCSession(omhome=omhome)
835862
else:
836-
self.getconn = OMCSessionZMQ()
863+
self.getconn = OMCSessionZMQ(omhome=omhome)
837864

838865
## needed for properly deleting the OMCSessionZMQ
839866
self._omc_log_file = self.getconn._omc_log_file

0 commit comments

Comments
 (0)