<div class="section" id="how-can-i-traverse-the-definition-in-python">
<span id="traverse-defs"></span><span id="index-0"></span>
<div class="highlight-python"><div class="highlight"><pre><span class="c">#!/usr/bin/env python2.7</span>
<span class="kn">import</span> <span class="nn">ecflow</span>
<span class="k">class</span> <span class="nc">Indentor</span><span class="p">:</span>
<span class="sd">"""This class manages indentation, for use with context manager</span>
<span class="sd"> It is used to correctly indent the definition node tree hierarchy</span>
<span class="sd"> """</span>
<span class="n">_index</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">Indentor</span><span class="o">.</span><span class="n">_index</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">Indentor</span><span class="o">.</span><span class="n">_index</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">indent</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">the_file</span><span class="p">):</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">Indentor</span><span class="o">.</span><span class="n">_index</span><span class="p">):</span>
<span class="n">the_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">' '</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">DefsTraverser</span><span class="p">:</span>
<span class="sd">"""Traverse the ecflow.Defs definition and write to file.</span>
<span class="sd"> </span>
<span class="sd"> This demonstrates that all nodes in the node tree and all attributes are accessible.</span>
<span class="sd"> Additionally the state data is also accessible. This class will write state data as</span>
<span class="sd"> comments. If the definition was returned from the server, it allows access to latest </span>
<span class="sd"> snapshot of the state data held in the server. </span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">defs</span><span class="p">):</span>
<span class="k">assert</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">defs</span><span class="p">,</span> <span class="n">ecflow</span><span class="o">.</span><span class="n">Defs</span><span class="p">)),</span><span class="s">"Expected ecflow.Defs as first argument"</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__defs</span> <span class="o">=</span> <span class="n">defs</span>
<span class="k">def</span> <span class="nf">write_to_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_name</span><span class="p">):</span>
<span class="k">assert</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">file_name</span><span class="p">,</span> <span class="nb">str</span><span class="p">)),</span><span class="s">"Expected a string argument. Representing a file name"</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_name</span><span class="p">,</span> <span class="s">'w'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">extern</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__defs</span><span class="o">.</span><span class="n">externs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="s">"extern "</span> <span class="o">+</span> <span class="n">extern</span><span class="p">)</span>
<span class="k">for</span> <span class="n">suite</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__defs</span><span class="o">.</span><span class="n">suites</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__write</span><span class="p">(</span><span class="s">"suite "</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__print_node</span><span class="p">(</span><span class="n">suite</span><span class="p">)</span>
<span class="n">clock</span> <span class="o">=</span> <span class="n">suite</span><span class="o">.</span><span class="n">get_clock</span><span class="p">()</span>
<span class="k">if</span> <span class="n">clock</span><span class="p">:</span>
<span class="n">indent</span> <span class="o">=</span> <span class="n">Indentor</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">clock</span><span class="p">))</span>
<span class="k">del</span> <span class="n">indent</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__print_nc</span><span class="p">(</span><span class="n">suite</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="s">"endsuite"</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">__print_nc</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node_container</span><span class="p">):</span>
<span class="n">indent</span> <span class="o">=</span> <span class="n">Indentor</span><span class="p">()</span>
<span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">node_container</span><span class="o">.</span><span class="n">nodes</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">ecflow</span><span class="o">.</span><span class="n">Task</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__write</span><span class="p">(</span><span class="s">"task "</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__print_node</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__write</span><span class="p">(</span><span class="s">"family "</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__print_node</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__print_nc</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="s">"endfamily"</span><span class="p">)</span>
<span class="k">del</span> <span class="n">indent</span>
<span class="k">def</span> <span class="nf">__print_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">name</span><span class="p">()</span> <span class="o">+</span> <span class="s">" # state: "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">get_state</span><span class="p">())</span> <span class="o">+</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">)</span>
<span class="n">indent</span> <span class="o">=</span> <span class="n">Indentor</span><span class="p">()</span>
<span class="n">defStatus</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_defstatus</span><span class="p">()</span>
<span class="k">if</span> <span class="n">defStatus</span> <span class="o">!=</span> <span class="n">ecflow</span><span class="o">.</span><span class="n">DState</span><span class="o">.</span><span class="n">queued</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="s">"defstatus "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">defStatus</span><span class="p">))</span>
<span class="n">autocancel</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_autocancel</span><span class="p">()</span>
<span class="k">if</span> <span class="n">autocancel</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">autocancel</span><span class="p">))</span>
<span class="n">repeat</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_repeat</span><span class="p">()</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">repeat</span><span class="o">.</span><span class="n">empty</span><span class="p">():</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">repeat</span><span class="p">)</span> <span class="o">+</span> <span class="s">" # value: "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">repeat</span><span class="o">.</span><span class="n">value</span><span class="p">()))</span>
<span class="n">late</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_late</span><span class="p">()</span>
<span class="k">if</span> <span class="n">late</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">late</span><span class="p">)</span> <span class="o">+</span> <span class="s">" # is_late: "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">late</span><span class="o">.</span><span class="n">is_late</span><span class="p">()))</span>
<span class="n">complete_expr</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_complete</span><span class="p">()</span>
<span class="k">if</span> <span class="n">complete_expr</span><span class="p">:</span>
<span class="k">for</span> <span class="n">part_expr</span> <span class="ow">in</span> <span class="n">complete_expr</span><span class="o">.</span><span class="n">parts</span><span class="p">:</span>
<span class="n">trig</span> <span class="o">=</span> <span class="s">"complete "</span>
<span class="k">if</span> <span class="n">part_expr</span><span class="o">.</span><span class="n">and_expr</span><span class="p">():</span> <span class="n">trig</span> <span class="o">=</span> <span class="n">trig</span> <span class="o">+</span> <span class="s">"-a "</span>
<span class="k">if</span> <span class="n">part_expr</span><span class="o">.</span><span class="n">or_expr</span><span class="p">():</span> <span class="n">trig</span> <span class="o">=</span> <span class="n">trig</span> <span class="o">+</span> <span class="s">"-o "</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__write</span><span class="p">(</span><span class="n">trig</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span> <span class="n">part_expr</span><span class="o">.</span><span class="n">get_expression</span><span class="p">()</span> <span class="o">+</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">)</span>
<span class="n">trigger_expr</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_trigger</span><span class="p">()</span>
<span class="k">if</span> <span class="n">trigger_expr</span><span class="p">:</span>
<span class="k">for</span> <span class="n">part_expr</span> <span class="ow">in</span> <span class="n">trigger_expr</span><span class="o">.</span><span class="n">parts</span><span class="p">:</span>
<span class="n">trig</span> <span class="o">=</span> <span class="s">"trigger "</span>
<span class="k">if</span> <span class="n">part_expr</span><span class="o">.</span><span class="n">and_expr</span><span class="p">():</span> <span class="n">trig</span> <span class="o">=</span> <span class="n">trig</span> <span class="o">+</span> <span class="s">"-a "</span>
<span class="k">if</span> <span class="n">part_expr</span><span class="o">.</span><span class="n">or_expr</span><span class="p">():</span> <span class="n">trig</span> <span class="o">=</span> <span class="n">trig</span> <span class="o">+</span> <span class="s">"-o "</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__write</span><span class="p">(</span><span class="n">trig</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span> <span class="n">part_expr</span><span class="o">.</span><span class="n">get_expression</span><span class="p">()</span> <span class="o">+</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">variables</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="s">"edit "</span> <span class="o">+</span> <span class="n">var</span><span class="o">.</span><span class="n">name</span><span class="p">()</span> <span class="o">+</span> <span class="s">" '"</span> <span class="o">+</span> <span class="n">var</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">+</span> <span class="s">"'"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">meter</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">meters</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">meter</span><span class="p">)</span> <span class="o">+</span> <span class="s">" # value: "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">meter</span><span class="o">.</span><span class="n">value</span><span class="p">()))</span>
<span class="k">for</span> <span class="n">event</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">events</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">event</span><span class="p">)</span> <span class="o">+</span> <span class="s">" # value: "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">value</span><span class="p">()))</span>
<span class="k">for</span> <span class="n">label</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">labels</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">label</span><span class="p">)</span> <span class="o">+</span> <span class="s">" # value: "</span> <span class="o">+</span> <span class="n">label</span><span class="o">.</span><span class="n">value</span><span class="p">())</span>
<span class="k">for</span> <span class="n">limit</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">limits</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">limit</span><span class="p">)</span> <span class="o">+</span> <span class="s">" # value: "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">limit</span><span class="o">.</span><span class="n">value</span><span class="p">()))</span>
<span class="k">for</span> <span class="n">inlimit</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">inlimits</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">inlimit</span><span class="p">))</span>
<span class="k">for</span> <span class="n">the_time</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">times</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">the_time</span><span class="p">))</span>
<span class="k">for</span> <span class="n">today</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">todays</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">today</span><span class="p">))</span>
<span class="k">for</span> <span class="n">date</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">dates</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">date</span><span class="p">))</span>
<span class="k">for</span> <span class="n">day</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">days</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">day</span><span class="p">))</span>
<span class="k">for</span> <span class="n">cron</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">crons</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">cron</span><span class="p">))</span>
<span class="k">for</span> <span class="n">verify</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">verifies</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">verify</span><span class="p">))</span>
<span class="k">for</span> <span class="n">zombie</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">zombies</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">__writeln</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">zombie</span><span class="p">))</span>
<span class="k">del</span> <span class="n">indent</span>
<span class="k">def</span> <span class="nf">__write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">the_string</span><span class="p">):</span>
<span class="n">Indentor</span><span class="o">.</span><span class="n">indent</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__file</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">the_string</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__writeln</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">the_string</span><span class="p">):</span>
<span class="n">Indentor</span><span class="o">.</span><span class="n">indent</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__file</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">__file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">the_string</span> <span class="o">+</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="c"># Create the client. This will read the default environment variables</span>
<span class="n">ci</span> <span class="o">=</span> <span class="n">ecflow</span><span class="o">.</span><span class="n">Client</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">,</span> <span class="s">"4141"</span><span class="p">)</span>
<span class="c"># Get the node tree suite definition as stored in the server</span>
<span class="c"># The definition is retrieved and stored on the variable 'ci'</span>
<span class="n">ci</span><span class="o">.</span><span class="n">sync_local</span><span class="p">()</span>
<span class="c"># access the definition retrieved from the server</span>
<span class="n">server_defs</span> <span class="o">=</span> <span class="n">ci</span><span class="o">.</span><span class="n">get_defs</span><span class="p">()</span>
<span class="k">if</span> <span class="n">server_defs</span> <span class="o">==</span> <span class="bp">None</span> <span class="p">:</span>
<span class="k">print</span> <span class="s">"The server has no definition"</span>
<span class="nb">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="c"># Traverse server definition writing all state as comments.</span>
<span class="n">traverser</span> <span class="o">=</span> <span class="n">DefsTraverser</span><span class="p">(</span><span class="n">server_defs</span><span class="p">)</span>
<span class="n">traverser</span><span class="o">.</span><span class="n">write_to_file</span><span class="p">(</span><span class="s">"server.defs"</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">RuntimeError</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"failed: "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
</pre></div>
</div>
</div>
|