Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
setter = getattr(fmu, 'setInt32')
else:
setter = getattr(fmu, 'set' + variable.type)
# convert Boolean values
if variable.type == 'Boolean':
if isinstance(value, str):
if value.lower() not in ['true', 'false']:
raise Exception('The start value "%s" for variable "%s" could not be converted to Boolean' %
(value, variable.name))
else:
value = value.lower() == 'true'
# convert the type
if variable.shape:
if _is_string(value):
value = value.split()
value = list(map(lambda e: variable._python_type(e), value))
if len(value) != np.prod(variable.shape):
raise ArgumentError('The start value for variable "%s" must have %d elements but has %d.' %
(variable.name, np.prod(variable.shape), len(value)))
else:
value = [variable._python_type(value)]
setter([vr], value)
if len(start_values) > 0:
raise Exception("The start values for the following variables could not be set because they don't exist: " +
', '.join(start_values.keys()))
def supported_platforms(filename):
""" Get the platforms supported by the FMU without extracting it
Parameters:
filename filename of the FMU, directory with extracted FMU or file like object
Returns:
platforms a list of supported platforms supported by the FMU
"""
from .util import _is_string
# get the files within the FMU
if _is_string(filename) and os.path.isdir(filename): # extracted FMU
names = []
for dirpath, _, filenames in os.walk(filename):
for name in filenames:
abspath = os.path.join(dirpath, name)
names.append(os.path.relpath(abspath, start=filename).replace('\\', '/'))
else: # FMU as path or file like object
import zipfile
with zipfile.ZipFile(filename, 'r') as zf:
names = zf.namelist()
platforms = []
# check for the C-sources
for name in names:
head, tail = os.path.split(name)
if head == 'sources' and tail.endswith('.c'):
returns:
model_description a ModelDescription object
"""
import zipfile
from lxml import etree
import os
from .util import _is_string
# remember the original filename
_filename = filename
if _is_string(filename) and os.path.isdir(filename): # extracted FMU
filename = os.path.join(filename, 'modelDescription.xml')
tree = etree.parse(filename)
elif _is_string(filename) and os.path.isfile(filename) and filename.lower().endswith('.xml'): # XML file
tree = etree.parse(filename)
else: # FMU as path or file like object
with zipfile.ZipFile(filename, 'r') as zf:
xml = zf.open('modelDescription.xml')
tree = etree.parse(xml)
root = tree.getroot()
fmiVersion = root.get('fmiVersion')
if not fmiVersion.startswith('3.0') and fmiVersion not in ['1.0', '2.0']:
raise Exception("Unsupported FMI version: %s" % fmiVersion)
if validate:
module_dir, _ = os.path.split(__file__)
def read_build_description(filename, validate=True):
import zipfile
from lxml import etree
import os
from .util import _is_string
if _is_string(filename) and os.path.isdir(filename): # extracted FMU
filename = os.path.join(filename, 'sources/buildDescription.xml')
if not os.path.isfile(filename):
return []
tree = etree.parse(filename)
elif _is_string(filename) and os.path.isfile(filename) and filename.lower().endswith('.xml'): # XML file
if not os.path.isfile(filename):
return []
tree = etree.parse(filename)
else: # FMU as path or file like object
with zipfile.ZipFile(filename, 'r') as zf:
if 'sources/buildDescription.xml' not in zf.namelist():
return []
xml = zf.open('sources/buildDescription.xml')
tree = etree.parse(xml)
root = tree.getroot()
fmi_version = root.get('fmiVersion')
if fmi_version is None or not fmi_version.startswith('3.0'):
raise Exception("Unsupported fmiBuildDescription version: %s" % fmi_version)
def read_build_description(filename, validate=True):
import zipfile
from lxml import etree
import os
from .util import _is_string
if _is_string(filename) and os.path.isdir(filename): # extracted FMU
filename = os.path.join(filename, 'sources/buildDescription.xml')
if not os.path.isfile(filename):
return []
tree = etree.parse(filename)
elif _is_string(filename) and os.path.isfile(filename) and filename.lower().endswith('.xml'): # XML file
if not os.path.isfile(filename):
return []
tree = etree.parse(filename)
else: # FMU as path or file like object
with zipfile.ZipFile(filename, 'r') as zf:
if 'sources/buildDescription.xml' not in zf.namelist():
return []
xml = zf.open('sources/buildDescription.xml')
tree = etree.parse(xml)
root = tree.getroot()