Skip to content

Commit

Permalink
Update warnings and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed Jun 5, 2015
1 parent 0eff9b9 commit 2dcc3bb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
24 changes: 17 additions & 7 deletions usersguide-sphinx/source/introduction.rst
Expand Up @@ -333,10 +333,8 @@ We also load a file containing the dcmotor model:
It is simulated:

.. omc-mos::
:noerror:

simulate(dcmotor, startTime=0.0, stopTime=10.0)
getErrorString()

We list the source code of the model:

Expand Down Expand Up @@ -389,8 +387,14 @@ that contains these commands:

.. omc-mos ::
:clear:
writeFile("sim_BouncingBall.mos", "loadFile(getInstallationDirectoryPath() + \"/share/doc/omc/testmodels/BouncingBall.mo\"); simulate(BouncingBall, stopTime=3.0); /* plot({h,flying}); */")
:combine-lines: 5,6
:erroratend:
writeFile("sim_BouncingBall.mos", "
loadFile(getInstallationDirectoryPath() + \"/share/doc/omc/testmodels/BouncingBall.mo\");
simulate(BouncingBall, stopTime=3.0);
/* plot({h,flying}); */
")
runScript("sim_BouncingBall.mos")
Expand Down Expand Up @@ -600,7 +604,7 @@ Type in a function:
output Real y;
algorithm
y:=x*x;
end MySqr;
end mySqr;
Call the function:

Expand Down Expand Up @@ -741,9 +745,13 @@ automatically.
package Modelica
annotation(uses(Complex(version="1.0"),
ModelicaServices(version="1.1")))
ModelicaServices(version="1.1")));
end Modelica;
.. omc-mos ::
clear()
Packages will also be loaded if a model has a uses-annotation:

.. omc-loadstring ::
Expand All @@ -753,15 +761,16 @@ Packages will also be loaded if a model has a uses-annotation:
end M;
.. omc-mos ::
:parsed:
instantiateModel(M)
Packages will also be loaded by looking at the first identifier in the path:

.. omc-mos ::
:parsed:
:clear:
clear();
instantiateModel(Modelica.Electrical.Analog.Basic.Ground)
Calling the Model Query and Manipulation API
Expand Down Expand Up @@ -795,6 +804,7 @@ structure:
Different kinds of calls with returned results:

.. omc-mos ::
:erroratend:
getClassRestriction(BouncingBall)
getClassInformation(BouncingBall)
Expand Down
33 changes: 28 additions & 5 deletions usersguide-sphinx/source/sphinxcontribopenmodelica.py
Expand Up @@ -40,6 +40,25 @@ def run(self):
def fixPaths(s):
return str(s).replace(omhome, u"«OPENMODELICAHOME»").replace(dochome, u"«DOCHOME»").strip()

def onlyNotifications():
(nm,ne,nw) = omc.sendExpression("countMessages()")
return ne+nw == 0

def getErrorString(state):
(nm,ne,nw) = omc.sendExpression("countMessages()")
s = fixPaths(omc.sendExpression("getErrorString()"))
if nm==0:
return []
node = nodes.paragraph()
for x in s.split("\n"):
node += nodes.paragraph(text = x)
if ne>0:
return [nodes.error(None, node)]
elif nw>0:
return [nodes.warning(None, node)]
else:
return [nodes.note(None, node)]

class ExecMosDirective(directives.CodeBlock):
"""Execute the specified Modelica code and insert the output into the document using syntax highlighting"""
has_content = True
Expand All @@ -54,11 +73,13 @@ class ExecMosDirective(directives.CodeBlock):
'noerror': rstdirectives.flag,
'clear': rstdirectives.flag,
'parsed': rstdirectives.flag,
'combine-lines': rstdirectives.positive_int_list
'combine-lines': rstdirectives.positive_int_list,
'erroratend': rstdirectives.flag,
}

def run(self):
#oldStdout, sys.stdout = sys.stdout, StringIO()
erroratend = 'erroratend' in self.options or (not 'noerror' in self.options and len(self.content)==1)
try:
if 'clear' in self.options:
assert(omc.ask('clear()'))
Expand All @@ -80,19 +101,21 @@ def run(self):
res.append(fixPaths(omc.sendExpression(str(s))))
else:
res.append(fixPaths(omc.ask(str(s), parsed=False)))
if not ('noerror' in self.options):
if not ('noerror' in self.options or erroratend):
errs = fixPaths(omc.ask('getErrorString()', parsed=False))
if errs<>'""':
res.append(errs)
# res += sys.stdout.readlines()
self.content = res
self.arguments.append('modelica')
return super(ExecMosDirective, self).run()
return super(ExecMosDirective, self).run() + (getErrorString(self.state) if erroratend else [])
except Exception, e:
return [nodes.error(None, nodes.paragraph(text = "Unable to execute Modelica code"), nodes.paragraph(text = str(e) + "\n" + traceback.format_exc()))]
finally:
pass # sys.stdout = oldStdout

def escapeString(s):
return '"' + s.replace('"', '\\"') + '"'

class OMCLoadStringDirective(Directive):
"""Loads the code into OMC and returns the highlighted version of it"""
Expand All @@ -106,9 +129,9 @@ def run(self):
for n in self.content:
vl.append(" " + str(n), "<OMC loadString>")
node = docutils.nodes.paragraph()
omc.ask('\n'.join([str(n) for n in self.content]), parsed=False)
omc.sendExpression("loadString(%s)" % escapeString('\n'.join([str(n) for n in self.content])))
self.state.nested_parse(vl, 0, node)
return node.children
return node.children + getErrorString(self.state)

class OMCGnuplotDirective(Directive):
"""Execute the specified python code and insert the output into the document"""
Expand Down

0 comments on commit 2dcc3bb

Please sign in to comment.