source: pvMail/doc/build/html/_modules/PvMail/pvMail.html @ 932

Last change on this file since 932 was 932, checked in by jemian, 10 years ago

moving to new address: xor --> xray

  • Property svn:mime-type set to text/html
File size: 65.4 KB
Line 
1
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6
7<html xmlns="http://www.w3.org/1999/xhtml">
8  <head>
9    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10   
11    <title>PvMail.pvMail &mdash; PvMail 3 (3.0-r801) documentation</title>
12   
13    <link rel="stylesheet" href="../../_static/sphinxdoc.css" type="text/css" />
14    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
15   
16    <script type="text/javascript">
17      var DOCUMENTATION_OPTIONS = {
18        URL_ROOT:    '../../',
19        VERSION:     '3 (3.0-r801)',
20        COLLAPSE_INDEX: false,
21        FILE_SUFFIX: '.html',
22        HAS_SOURCE:  true
23      };
24    </script>
25    <script type="text/javascript" src="../../_static/jquery.js"></script>
26    <script type="text/javascript" src="../../_static/underscore.js"></script>
27    <script type="text/javascript" src="../../_static/doctools.js"></script>
28    <link rel="top" title="PvMail 3 (3.0-r801) documentation" href="../../index.html" />
29    <link rel="up" title="Module code" href="../index.html" /> 
30  </head>
31  <body>
32    <div class="related">
33      <h3>Navigation</h3>
34      <ul>
35        <li class="right" style="margin-right: 10px">
36          <a href="../../genindex.html" title="General Index"
37             accesskey="I">index</a></li>
38        <li class="right" >
39          <a href="../../py-modindex.html" title="Python Module Index"
40             >modules</a> |</li>
41        <li><a href="../../index.html">PvMail 3 (3.0-r801) documentation</a> &raquo;</li>
42          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
43      </ul>
44    </div>
45      <div class="sphinxsidebar">
46        <div class="sphinxsidebarwrapper">
47<div id="searchbox" style="display: none">
48  <h3>Quick search</h3>
49    <form class="search" action="../../search.html" method="get">
50      <input type="text" name="q" />
51      <input type="submit" value="Go" />
52      <input type="hidden" name="check_keywords" value="yes" />
53      <input type="hidden" name="area" value="default" />
54    </form>
55    <p class="searchtip" style="font-size: 90%">
56    Enter search terms or a module, class or function name.
57    </p>
58</div>
59<script type="text/javascript">$('#searchbox').show(0);</script>
60        </div>
61      </div>
62
63    <div class="document">
64      <div class="documentwrapper">
65        <div class="bodywrapper">
66          <div class="body">
67           
68  <h1>Source code for PvMail.pvMail</h1><div class="highlight"><pre>
69<span class="c">#!/usr/bin/env python</span>
70
71<span class="c">########### SVN repository information ###################</span>
72<span class="c"># $Date: 2012-04-12 17:38:59 -0500 (Thu, 12 Apr 2012) $</span>
73<span class="c"># $Author: jemian $</span>
74<span class="c"># $Revision: 801 $</span>
75<span class="c"># $URL: https://subversion.xray.aps.anl.gov/bcdaext/pvMail/src/PvMail/pvMail.py $</span>
76<span class="c"># $Id: pvMail.py 801 2012-04-12 22:38:59Z jemian $</span>
77<span class="c">########### SVN repository information ###################</span>
78
79<span class="sd">&#39;&#39;&#39;</span>
80<span class="sd">===================================</span>
81<span class="sd">pvMail: combined CLI and GUI</span>
82<span class="sd">===================================</span>
83
84<span class="sd">Functionally based on pvMail UNIX shell script written in 1999.</span>
85
86<span class="sd">Summary</span>
87<span class="sd">--------</span>
88
89<span class="sd">Watches an EPICS PV and sends email when it changes from 0 to 1.</span>
90<span class="sd">PV value can be either integer or float.</span>
91
92<span class="sd">.. note::</span>
93<span class="sd">   When &quot;running&quot;, wait for trigger PV to go from 0 to 1.  When that</span>
94<span class="sd">   happens, fetch mail message from message PV.  Then, send that</span>
95<span class="sd">   message out to each of the email addresses.  The message </span>
96<span class="sd">   content is prioritized for view on a small-screen device such </span>
97<span class="sd">   as a pager or a PDA or smartphone.</span>
98
99<span class="sd">:author: Kurt Goetze (original version)</span>
100<span class="sd">:author: Pete Jemian (this version)</span>
101<span class="sd">:organization: AES/BCDA, Advanced Photon Source, Argonne National Laboratory</span>
102
103<span class="sd">:license: Copyright (c) 2011, UChicago Argonne, LLC</span>
104<span class="sd">:license: Copyright (c) 2009, The University of Chicago, as Operator of Argonne</span>
105<span class="sd">     National Laboratory.</span>
106<span class="sd">:license: Copyright (c) 2009 The Regents of the University of California, as</span>
107<span class="sd">     Operator of Los Alamos National Laboratory.</span>
108<span class="sd">:license: This file is distributed subject to a Software License Agreement found</span>
109<span class="sd">     in the file LICENSE that is included with this distribution. </span>
110
111<span class="sd">:note: Version History:</span>
112<span class="sd">:note: 05.09.07  kag  Initial alpha version.  Needs testing.</span>
113<span class="sd">:note: 2009-12-02 prj: converted to use wxPython (no Tkinter or Pmw)</span>
114<span class="sd">:note: 2011-11-23 prj: complete rewrite using PyEpics </span>
115<span class="sd">    and combined GUI (Traits) and CLI</span>
116<span class="sd">    </span>
117<span class="sd">:requires: EPICS system (http://www.aps.anl.gov/epics) </span>
118<span class="sd">    with at least two process variables (PVs)</span>
119<span class="sd">    where the &quot;Trigger PV&quot; toggles between values of 0 and 1</span>
120<span class="sd">    and the &quot;SendMessage PV&quot; contains a string to send as part of </span>
121<span class="sd">    the email message.</span>
122<span class="sd">:requires: PyEpics (http://cars9.uchicago.edu/software/python/pyepics3/)</span>
123<span class="sd">:requires: Traits (http://code.enthought.com/projects/traits/)</span>
124<span class="sd">&#39;&#39;&#39;</span>
125
126
127<span class="kn">import</span> <span class="nn">argparse</span>
128<span class="kn">import</span> <span class="nn">datetime</span>
129<span class="kn">import</span> <span class="nn">epics</span>
130<span class="kn">import</span> <span class="nn">logging</span>
131<span class="kn">import</span> <span class="nn">os</span>
132<span class="kn">import</span> <span class="nn">socket</span>
133<span class="kn">import</span> <span class="nn">sys</span>
134<span class="kn">import</span> <span class="nn">threading</span>
135<span class="kn">import</span> <span class="nn">time</span>
136<span class="kn">import</span> <span class="nn">traceback</span>
137
138
139<span class="n">__project_name__</span> <span class="o">=</span> <span class="s">&quot;PvMail&quot;</span>
140<span class="n">__description__</span> <span class="o">=</span> <span class="s">&quot;Watch an EPICS PV. Send email when it changes from 0 to 1.&quot;</span>
141<span class="n">__svnid__</span> <span class="o">=</span> <span class="s">&quot;$Id: pvMail.py 801 2012-04-12 22:38:59Z jemian $&quot;</span>
142<span class="n">__version__</span> <span class="o">=</span> <span class="s">&quot;3&quot;</span>
143<span class="n">__minor_version__</span> <span class="o">=</span> <span class="s">&quot;0&quot;</span>
144<span class="n">__revision__</span> <span class="o">=</span> <span class="n">__svnid__</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span>
145<span class="n">__full_version__</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">-r</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">__version__</span><span class="p">,</span> <span class="n">__minor_version__</span><span class="p">,</span> <span class="n">__revision__</span><span class="p">)</span> 
146<span class="n">__author__</span> <span class="o">=</span> <span class="s">&quot;Pete Jemian&quot;</span>
147<span class="n">__institution__</span> <span class="o">=</span> <span class="s">&quot;Advanced Photon Source, Argonne National Laboratory&quot;</span>
148<span class="n">__author_email__</span><span class="o">=</span> <span class="s">&quot;jemian@anl.gov&quot;</span>
149<span class="n">__url__</span> <span class="o">=</span> <span class="s">&quot;https://subversion.xray.aps.anl.gov/trac/bcdaext/wiki/pvMail&quot;</span>
150<span class="n">__license__</span> <span class="o">=</span> <span class="s">&quot;(c) 2009-2012, UChicago Argonne, LLC&quot;</span>
151<span class="n">__license__</span> <span class="o">+=</span> <span class="s">&quot; (see LICENSE file for details)&quot;</span>
152
153<span class="n">LOG_FILE</span> <span class="o">=</span> <span class="s">&quot;pvMail-</span><span class="si">%d</span><span class="s">.log&quot;</span> <span class="o">%</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()</span>
154<span class="n">RETRY_INTERVAL_S</span> <span class="o">=</span> <span class="mf">0.2</span>
155<span class="n">CHECKPOINT_INTERVAL_S</span> <span class="o">=</span> <span class="mi">5</span> <span class="o">*</span> <span class="mf">60.0</span>
156
157<span class="n">gui_object</span> <span class="o">=</span> <span class="bp">None</span>
158
159
160<div class="viewcode-block" id="PvMail"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail">[docs]</a><span class="k">class</span> <span class="nc">PvMail</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
161    <span class="sd">&#39;&#39;&#39;</span>
162<span class="sd">    Watch an EPICS PV (using PyEpics interface) and send an email</span>
163<span class="sd">    when the PV changes from 0 to 1.</span>
164<span class="sd">    &#39;&#39;&#39;</span>
165   
166    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
167        <span class="bp">self</span><span class="o">.</span><span class="n">trigger</span> <span class="o">=</span> <span class="bp">False</span>
168        <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="s">&quot;default message&quot;</span>
169        <span class="bp">self</span><span class="o">.</span><span class="n">subject</span> <span class="o">=</span> <span class="s">&quot;pvMail.py&quot;</span>
170        <span class="bp">self</span><span class="o">.</span><span class="n">triggerPV</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
171        <span class="bp">self</span><span class="o">.</span><span class="n">messagePV</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
172        <span class="bp">self</span><span class="o">.</span><span class="n">recipients</span> <span class="o">=</span> <span class="p">[]</span>
173        <span class="bp">self</span><span class="o">.</span><span class="n">old_value</span> <span class="o">=</span> <span class="bp">None</span>
174        <span class="bp">self</span><span class="o">.</span><span class="n">monitoredPVs</span> <span class="o">=</span> <span class="p">[]</span>
175   
176<div class="viewcode-block" id="PvMail.basicChecks"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail.basicChecks">[docs]</a>    <span class="k">def</span> <span class="nf">basicChecks</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
177        <span class="sd">&#39;&#39;&#39;</span>
178<span class="sd">        check for valid inputs, </span>
179<span class="sd">        raise exceptions as discovered, </span>
180<span class="sd">        otherwise no return result</span>
181<span class="sd">        &#39;&#39;&#39;</span>
182        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">recipients</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
183            <span class="n">msg</span> <span class="o">=</span> <span class="s">&quot;need at least one email address for list of recipients&quot;</span>
184            <span class="k">raise</span> <span class="ne">RuntimeWarning</span><span class="p">,</span> <span class="n">msg</span>
185        <span class="n">fmt</span> <span class="o">=</span> <span class="s">&quot;could not connect to </span><span class="si">%s</span><span class="s"> PV: </span><span class="si">%s</span><span class="s">&quot;</span>
186        <span class="n">parts</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;message&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">messagePV</span><span class="p">,</span> 
187                 <span class="s">&#39;trigger&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">triggerPV</span><span class="p">}</span>
188        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">pv</span> <span class="ow">in</span> <span class="n">parts</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
189            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">pv</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
190                <span class="k">raise</span> <span class="ne">RuntimeWarning</span><span class="p">,</span> <span class="s">&quot;no name for the </span><span class="si">%s</span><span class="s"> PV&quot;</span> <span class="o">%</span> <span class="n">name</span>
191            <span class="k">if</span> <span class="n">pv</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitoredPVs</span><span class="p">:</span>
192                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">testConnect</span><span class="p">(</span><span class="n">pv</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span> <span class="ow">is</span> <span class="bp">False</span><span class="p">:</span>
193                    <span class="k">raise</span> <span class="ne">RuntimeWarning</span><span class="p">,</span> <span class="n">fmt</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">pv</span><span class="p">)</span>
194        </div>
195<div class="viewcode-block" id="PvMail.testConnect"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail.testConnect">[docs]</a>    <span class="k">def</span> <span class="nf">testConnect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pvname</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">5.0</span><span class="p">):</span>
196        <span class="sd">&#39;&#39;&#39;</span>
197<span class="sd">        create PV, </span>
198<span class="sd">        wait for connection, </span>
199<span class="sd">        return connection state (True | False)</span>
200<span class="sd">        </span>
201<span class="sd">        adapted from PyEpics __createPV() method</span>
202<span class="sd">        &#39;&#39;&#39;</span>
203        <span class="n">logger</span><span class="p">(</span><span class="s">&quot;test connect with </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">pvname</span><span class="p">)</span>
204        <span class="n">retry_interval_s</span> <span class="o">=</span> <span class="mf">0.0001</span>
205        <span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
206        <span class="n">thispv</span> <span class="o">=</span> <span class="n">epics</span><span class="o">.</span><span class="n">PV</span><span class="p">(</span><span class="n">pvname</span><span class="p">)</span>
207        <span class="n">thispv</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
208        <span class="k">while</span> <span class="ow">not</span> <span class="n">thispv</span><span class="o">.</span><span class="n">connected</span><span class="p">:</span>
209            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">retry_interval_s</span><span class="p">)</span>
210            <span class="n">epics</span><span class="o">.</span><span class="n">ca</span><span class="o">.</span><span class="n">poll</span><span class="p">()</span>
211            <span class="k">if</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span> <span class="o">&gt;</span> <span class="n">timeout</span><span class="p">:</span>
212                <span class="k">break</span>
213        <span class="k">return</span> <span class="n">thispv</span><span class="o">.</span><span class="n">connected</span>
214    </div>
215<div class="viewcode-block" id="PvMail.do_start"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail.do_start">[docs]</a>    <span class="k">def</span> <span class="nf">do_start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
216        <span class="sd">&#39;&#39;&#39;start watching for triggers&#39;&#39;&#39;</span>
217        <span class="n">logger</span><span class="p">(</span><span class="s">&quot;do_start&quot;</span><span class="p">)</span>
218        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">monitoredPVs</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
219            <span class="bp">self</span><span class="o">.</span><span class="n">basicChecks</span><span class="p">()</span>
220            <span class="n">logger</span><span class="p">(</span><span class="s">&quot;passed basicChecks(), starting monitors&quot;</span><span class="p">)</span>
221            <span class="n">epics</span><span class="o">.</span><span class="n">camonitor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">messagePV</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">receiveMessageMonitor</span><span class="p">)</span>
222            <span class="n">epics</span><span class="o">.</span><span class="n">camonitor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">triggerPV</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">receiveTriggerMonitor</span><span class="p">)</span>
223            <span class="bp">self</span><span class="o">.</span><span class="n">old_value</span> <span class="o">=</span> <span class="n">epics</span><span class="o">.</span><span class="n">caget</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">triggerPV</span><span class="p">)</span>
224            <span class="c"># What happens if either self.messagePV or self.triggerPV</span>
225            <span class="c"># are changed after self.do_start() is called?</span>
226            <span class="c"># Keep a local list of what was initiated.</span>
227            <span class="bp">self</span><span class="o">.</span><span class="n">monitoredPVs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">messagePV</span><span class="p">)</span>
228            <span class="bp">self</span><span class="o">.</span><span class="n">monitoredPVs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">triggerPV</span><span class="p">)</span>
229    </div>
230<div class="viewcode-block" id="PvMail.do_stop"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail.do_stop">[docs]</a>    <span class="k">def</span> <span class="nf">do_stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
231        <span class="sd">&#39;&#39;&#39;stop watching for triggers&#39;&#39;&#39;</span>
232        <span class="n">logger</span><span class="p">(</span><span class="s">&quot;do_stop&quot;</span><span class="p">)</span>
233        <span class="k">for</span> <span class="n">pv</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">monitoredPVs</span><span class="p">:</span>
234            <span class="n">epics</span><span class="o">.</span><span class="n">camonitor_clear</span><span class="p">(</span><span class="n">pv</span><span class="p">)</span>   <span class="c"># no problem if pv was not monitored</span>
235        <span class="bp">self</span><span class="o">.</span><span class="n">monitoredPVs</span> <span class="o">=</span> <span class="p">[]</span>
236    </div>
237<div class="viewcode-block" id="PvMail.do_restart"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail.do_restart">[docs]</a>    <span class="k">def</span> <span class="nf">do_restart</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
238        <span class="sd">&#39;&#39;&#39;restart watching for triggers&#39;&#39;&#39;</span>
239        <span class="bp">self</span><span class="o">.</span><span class="n">do_stop</span><span class="p">()</span>
240        <span class="bp">self</span><span class="o">.</span><span class="n">do_start</span><span class="p">()</span>
241    </div>
242<div class="viewcode-block" id="PvMail.receiveMessageMonitor"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail.receiveMessageMonitor">[docs]</a>    <span class="k">def</span> <span class="nf">receiveMessageMonitor</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
243        <span class="sd">&#39;&#39;&#39;respond to EPICS CA monitors on message PV&#39;&#39;&#39;</span>
244        <span class="n">logger</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">messagePV</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
245        <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">value</span>
246    </div>
247<div class="viewcode-block" id="PvMail.receiveTriggerMonitor"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail.receiveTriggerMonitor">[docs]</a>    <span class="k">def</span> <span class="nf">receiveTriggerMonitor</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
248        <span class="sd">&#39;&#39;&#39;respond to EPICS CA monitors on trigger PV&#39;&#39;&#39;</span>
249        <span class="n">logger</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">triggerPV</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
250        <span class="c"># print self.old_value, type(self.old_value), value, type(value)</span>
251        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_value</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
252            <span class="k">if</span> <span class="n">value</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
253                <span class="c"># Cannot use this definition:</span>
254                <span class="c">#     self.trigger = (value == 1)</span>
255                <span class="c"># since the trigger PV just may transition back</span>
256                <span class="c"># to zero before SendMessage() runs.</span>
257                <span class="bp">self</span><span class="o">.</span><span class="n">trigger</span> <span class="o">=</span> <span class="bp">True</span>
258                <span class="n">SendMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
259        <span class="bp">self</span><span class="o">.</span><span class="n">old_value</span> <span class="o">=</span> <span class="n">value</span>
260    </div>
261<div class="viewcode-block" id="PvMail.send_test_message"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.PvMail.send_test_message">[docs]</a>    <span class="k">def</span> <span class="nf">send_test_message</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
262        <span class="sd">&#39;&#39;&#39;</span>
263<span class="sd">        sends a test message, used for development only</span>
264<span class="sd">        &#39;&#39;&#39;</span>
265        <span class="n">logger</span><span class="p">(</span><span class="s">&quot;send_test_message&quot;</span><span class="p">)</span>
266        <span class="bp">self</span><span class="o">.</span><span class="n">recipients</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;jemian&quot;</span><span class="p">,</span> <span class="s">&quot;prjemian&quot;</span><span class="p">]</span>
267        <span class="n">message</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
268        <span class="n">message</span> <span class="o">+=</span> <span class="s">&#39;host: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">socket</span><span class="o">.</span><span class="n">gethostname</span><span class="p">()</span>
269        <span class="n">message</span> <span class="o">+=</span> <span class="s">&#39;date: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
270        <span class="n">message</span> <span class="o">+=</span> <span class="s">&#39;program: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
271        <span class="n">message</span> <span class="o">+=</span> <span class="s">&#39;trigger PV: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">triggerPV</span>
272        <span class="n">message</span> <span class="o">+=</span> <span class="s">&#39;message PV: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">messagePV</span>
273        <span class="n">message</span> <span class="o">+=</span> <span class="s">&#39;recipients: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">recipients</span><span class="p">)</span>
274        <span class="bp">self</span><span class="o">.</span><span class="n">subject</span> <span class="o">=</span> <span class="s">&quot;pvMail development test&quot;</span>
275        <span class="n">sendMail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">subject</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">recipients</span><span class="p">)</span>
276
277</div></div>
278<div class="viewcode-block" id="SendMessage"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.SendMessage">[docs]</a><span class="k">class</span> <span class="nc">SendMessage</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
279    <span class="sd">&#39;&#39;&#39;</span>
280<span class="sd">    initiate sending the message in a separate thread</span>
281<span class="sd">    </span>
282<span class="sd">    :param obj pvm: instance of PvMail object on which to report</span>
283<span class="sd">    &#39;&#39;&#39;</span>
284
285    <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">pvm</span><span class="p">):</span>
286        <span class="n">logger</span><span class="p">(</span><span class="s">&quot;SendMessage&quot;</span><span class="p">)</span>
287        <span class="n">pvm</span><span class="o">.</span><span class="n">trigger</span> <span class="o">=</span> <span class="bp">False</span>        <span class="c"># triggered event received</span>
288
289        <span class="k">try</span><span class="p">:</span>
290            <span class="n">pvm</span><span class="o">.</span><span class="n">basicChecks</span><span class="p">()</span>
291           
292            <span class="n">pvm</span><span class="o">.</span><span class="n">subject</span> <span class="o">=</span> <span class="s">&quot;pvMail.py: &quot;</span> <span class="o">+</span> <span class="n">pvm</span><span class="o">.</span><span class="n">triggerPV</span>
293           
294            <span class="n">msg</span> <span class="o">=</span> <span class="n">pvm</span><span class="o">.</span><span class="n">message</span>
295            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&quot;</span><span class="se">\n\n</span><span class="s">&quot;</span>
296            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&#39;user: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&#39;LOGNAME&#39;</span><span class="p">]</span>
297            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&#39;host: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">socket</span><span class="o">.</span><span class="n">gethostname</span><span class="p">()</span>
298            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&#39;date: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
299            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&#39;program: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
300            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&#39;PID: </span><span class="si">%d</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()</span>
301            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&#39;trigger PV: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">pvm</span><span class="o">.</span><span class="n">triggerPV</span>
302            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&#39;message PV: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">pvm</span><span class="o">.</span><span class="n">messagePV</span>
303            <span class="n">msg</span> <span class="o">+=</span> <span class="s">&#39;recipients: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">pvm</span><span class="o">.</span><span class="n">recipients</span><span class="p">)</span>
304            <span class="n">pvm</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">msg</span>
305
306            <span class="n">sendMail</span><span class="p">(</span><span class="n">pvm</span><span class="o">.</span><span class="n">subject</span><span class="p">,</span> <span class="n">pvm</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">pvm</span><span class="o">.</span><span class="n">recipients</span><span class="p">)</span>
307            <span class="n">logger</span><span class="p">(</span><span class="s">&quot;message(s) sent&quot;</span><span class="p">)</span>
308        <span class="k">except</span><span class="p">:</span>
309            <span class="n">err_msg</span> <span class="o">=</span> <span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">()</span>
310            <span class="n">final_msg</span> <span class="o">=</span> <span class="s">&quot;pvm.subject = </span><span class="si">%s</span><span class="se">\n</span><span class="s">msg = </span><span class="si">%s</span><span class="se">\n</span><span class="s">traceback: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pvm</span><span class="o">.</span><span class="n">subject</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">msg</span><span class="p">),</span> <span class="n">err_msg</span><span class="p">)</span>
311            <span class="n">logger</span><span class="p">(</span><span class="n">final_msg</span><span class="p">)</span>
312</div>
313<div class="viewcode-block" id="sendMail"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.sendMail">[docs]</a><span class="k">def</span> <span class="nf">sendMail</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">recipients</span><span class="p">):</span>
314    <span class="sd">&#39;&#39;&#39;</span>
315<span class="sd">    send an email message using sendmail</span>
316<span class="sd">    </span>
317<span class="sd">    :param str subject: short text for email subject</span>
318<span class="sd">    :param str message: full text of email body</span>
319<span class="sd">    :param [str] recipients: list of email addresses to receive the message</span>
320<span class="sd">    &#39;&#39;&#39;</span>
321    <span class="k">global</span> <span class="n">gui_object</span>
322   
323    <span class="n">from_addr</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
324    <span class="n">to_addr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">recipients</span><span class="p">))</span>
325    <span class="n">mailprogram</span> <span class="o">=</span> <span class="s">&quot;/usr/lib/sendmail -F </span><span class="si">%s</span><span class="s"> -t </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">from_addr</span><span class="p">,</span> <span class="n">to_addr</span><span class="p">)</span>
326    <span class="n">mail_command</span> <span class="o">=</span> <span class="p">[</span><span class="n">mailprogram</span><span class="p">,</span> <span class="s">&quot;Subject: &quot;</span><span class="o">+</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">]</span>
327    <span class="n">cmd</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;cat &lt;&lt; +++ | </span><span class="si">%s</span><span class="se">\n</span><span class="s">+++&#39;&#39;&#39;</span> <span class="o">%</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">mail_command</span><span class="p">)</span>
328
329    <span class="n">msg</span> <span class="o">=</span> <span class="s">&quot;sending email to: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">to_addr</span>
330    <span class="n">logger</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
331    <span class="k">if</span> <span class="n">gui_object</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
332        <span class="n">gui_object</span><span class="o">.</span><span class="n">SetStatus</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>   <span class="c"># FIXME: get the GUI status line to update</span>
333
334    <span class="k">try</span><span class="p">:</span>
335        <span class="n">logger</span><span class="p">(</span> <span class="s">&quot;email command:</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">cmd</span> <span class="p">)</span>
336        <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>    <span class="c"># send the message</span>
337    <span class="k">except</span><span class="p">:</span>
338        <span class="n">err_msg</span> <span class="o">=</span> <span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">()</span>
339        <span class="n">final_msg</span> <span class="o">=</span> <span class="s">&quot;cmd = </span><span class="si">%s</span><span class="se">\n</span><span class="s">traceback: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">err_msg</span><span class="p">)</span>
340        <span class="n">logger</span><span class="p">(</span><span class="n">final_msg</span><span class="p">)</span>
341
342</div>
343<div class="viewcode-block" id="logger"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.logger">[docs]</a><span class="k">def</span> <span class="nf">logger</span><span class="p">(</span><span class="n">message</span><span class="p">):</span>
344    <span class="sd">&#39;&#39;&#39;</span>
345<span class="sd">    log a report from this class.</span>
346
347<span class="sd">    :param str message: words to be logged</span>
348<span class="sd">    &#39;&#39;&#39;</span>
349    <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
350    <span class="n">name</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
351    <span class="n">name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
352    <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;(</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">) </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">now</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
353    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
354
355</div>
356<div class="viewcode-block" id="basicStartTest"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.basicStartTest">[docs]</a><span class="k">def</span> <span class="nf">basicStartTest</span><span class="p">():</span>
357    <span class="sd">&#39;&#39;&#39;simple test of the PvMail class&#39;&#39;&#39;</span>
358    <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="n">LOG_FILE</span><span class="p">,</span><span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
359    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;startup&quot;</span><span class="p">)</span>
360    <span class="n">pvm</span> <span class="o">=</span> <span class="n">PvMail</span><span class="p">()</span>
361    <span class="n">pvm</span><span class="o">.</span><span class="n">recipients</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;prjemian@gmail.com&#39;</span><span class="p">]</span>
362    <span class="n">pvm</span><span class="o">.</span><span class="n">triggerPV</span> <span class="o">=</span> <span class="s">&quot;pvMail:trigger&quot;</span>
363    <span class="n">pvm</span><span class="o">.</span><span class="n">messagePV</span> <span class="o">=</span> <span class="s">&quot;pvMail:message&quot;</span>
364    <span class="n">retry_interval_s</span> <span class="o">=</span> <span class="mf">0.05</span>
365    <span class="n">end_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="mi">60</span>
366    <span class="n">report_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="mf">5.0</span>
367    <span class="n">pvm</span><span class="o">.</span><span class="n">do_start</span><span class="p">()</span>
368    <span class="k">while</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">end_time</span><span class="p">:</span>
369        <span class="k">if</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">report_time</span><span class="p">:</span>
370            <span class="n">report_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="mf">5.0</span>
371            <span class="n">logger</span><span class="p">(</span><span class="s">&quot;time remaining: </span><span class="si">%.1f</span><span class="s"> seconds ...&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">end_time</span> <span class="o">-</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()))</span>
372        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">retry_interval_s</span><span class="p">)</span>
373    <span class="n">pvm</span><span class="o">.</span><span class="n">do_stop</span><span class="p">()</span>
374
375</div>
376<div class="viewcode-block" id="basicMailTest"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.basicMailTest">[docs]</a><span class="k">def</span> <span class="nf">basicMailTest</span><span class="p">():</span>
377    <span class="sd">&#39;&#39;&#39;simple test sending mail using the PvMail class&#39;&#39;&#39;</span>
378    <span class="n">pvm</span> <span class="o">=</span> <span class="n">PvMail</span><span class="p">()</span>
379    <span class="n">pvm</span><span class="o">.</span><span class="n">send_test_message</span><span class="p">()</span>
380
381</div>
382<div class="viewcode-block" id="cli"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.cli">[docs]</a><span class="k">def</span> <span class="nf">cli</span><span class="p">(</span><span class="n">results</span><span class="p">):</span>
383    <span class="sd">&#39;&#39;&#39;</span>
384<span class="sd">    command-line interface to the PvMail class</span>
385<span class="sd">    </span>
386<span class="sd">    :param obj results: default parameters from argparse, see main()</span>
387<span class="sd">    &#39;&#39;&#39;</span>
388    <span class="n">logging_interval</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mi">60</span><span class="o">*</span><span class="mi">60</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mf">5.0</span><span class="p">,</span> <span class="n">results</span><span class="o">.</span><span class="n">logging_interval</span><span class="p">))</span>
389    <span class="n">sleep_duration</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mf">5.0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mf">0.0001</span><span class="p">,</span> <span class="n">results</span><span class="o">.</span><span class="n">sleep_duration</span><span class="p">))</span>
390
391    <span class="n">pvm</span> <span class="o">=</span> <span class="n">PvMail</span><span class="p">()</span>
392    <span class="n">pvm</span><span class="o">.</span><span class="n">triggerPV</span> <span class="o">=</span> <span class="n">results</span><span class="o">.</span><span class="n">trigger_PV</span>
393    <span class="n">pvm</span><span class="o">.</span><span class="n">messagePV</span> <span class="o">=</span> <span class="n">results</span><span class="o">.</span><span class="n">message_PV</span>
394    <span class="n">pvm</span><span class="o">.</span><span class="n">recipients</span> <span class="o">=</span> <span class="n">results</span><span class="o">.</span><span class="n">email_addresses</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">)</span>
395    <span class="n">checkpoint_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
396    <span class="n">pvm</span><span class="o">.</span><span class="n">do_start</span><span class="p">()</span>
397    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>     <span class="c"># endless loop, kill with ^C or equal</span>
398        <span class="k">if</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">checkpoint_time</span><span class="p">:</span>
399            <span class="n">checkpoint_time</span> <span class="o">+=</span> <span class="n">logging_interval</span>
400            <span class="n">logger</span><span class="p">(</span><span class="s">&quot;checkpoint&quot;</span><span class="p">)</span>
401        <span class="k">if</span> <span class="n">pvm</span><span class="o">.</span><span class="n">trigger</span><span class="p">:</span>
402            <span class="n">SendMessage</span><span class="p">(</span><span class="n">pvm</span><span class="p">)</span>
403        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">sleep_duration</span><span class="p">)</span>
404    <span class="n">pvm</span><span class="o">.</span><span class="n">do_stop</span><span class="p">()</span>        <span class="c"># this will never be called</span>
405
406</div>
407<div class="viewcode-block" id="gui"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.gui">[docs]</a><span class="k">def</span> <span class="nf">gui</span><span class="p">(</span><span class="n">results</span><span class="p">):</span>
408    <span class="sd">&#39;&#39;&#39;</span>
409<span class="sd">    graphical user interface to the PvMail class</span>
410<span class="sd">    </span>
411<span class="sd">    :param obj results: default parameters from argparse, see main()</span>
412<span class="sd">    &#39;&#39;&#39;</span>
413   
414    <span class="kn">import</span> <span class="nn">traits_gui</span>
415    <span class="k">global</span> <span class="n">gui_object</span>
416   
417    <span class="n">gui_object</span> <span class="o">=</span> <span class="n">traits_gui</span><span class="o">.</span><span class="n">PvMail_GUI</span><span class="p">(</span><span class="n">results</span><span class="o">.</span><span class="n">trigger_PV</span><span class="p">,</span> 
418                                <span class="n">results</span><span class="o">.</span><span class="n">message_PV</span><span class="p">,</span> 
419                                <span class="n">results</span><span class="o">.</span><span class="n">email_addresses</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">),</span>
420                                <span class="n">results</span><span class="o">.</span><span class="n">log_file</span><span class="p">,</span>
421                                <span class="p">)</span>
422    <span class="n">gui_object</span><span class="o">.</span><span class="n">configure_traits</span><span class="p">()</span>
423
424</div>
425<div class="viewcode-block" id="main"><a class="viewcode-back" href="../../pvMail/pvMail.html#PvMail.pvMail.main">[docs]</a><span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
426    <span class="sd">&#39;&#39;&#39;parse command-line arguments and choose which interface to use&#39;&#39;&#39;</span>
427    <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="n">__description__</span><span class="p">)</span>
428
429    <span class="c"># positional arguments</span>
430    <span class="c"># not required if GUI option is selected</span>
431    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;trigger_PV&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">&#39;?&#39;</span><span class="p">,</span>
432                        <span class="n">help</span><span class="o">=</span><span class="s">&quot;EPICS trigger PV name&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">)</span>
433
434    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;message_PV&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">&#39;?&#39;</span><span class="p">,</span>
435                        <span class="n">help</span><span class="o">=</span><span class="s">&quot;EPICS message PV name&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">)</span>
436
437    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;email_addresses&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s">&#39;?&#39;</span><span class="p">,</span>
438                        <span class="n">help</span><span class="o">=</span><span class="s">&quot;email address(es), comma-separated if more than one&quot;</span><span class="p">,</span> 
439                        <span class="n">default</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">)</span>
440
441    <span class="c"># optional arguments</span>
442    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;-l&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;log_file&#39;</span><span class="p">,</span>
443                        <span class="n">help</span><span class="o">=</span><span class="s">&quot;for logging program progress and comments&quot;</span><span class="p">,</span> 
444                        <span class="n">default</span><span class="o">=</span><span class="n">LOG_FILE</span><span class="p">)</span>
445
446    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;-i&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;logging_interval&#39;</span><span class="p">,</span> 
447                        <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span>
448                        <span class="n">help</span><span class="o">=</span><span class="s">&quot;checkpoint reporting interval (s) in log file&quot;</span><span class="p">,</span> 
449                        <span class="n">default</span><span class="o">=</span><span class="n">CHECKPOINT_INTERVAL_S</span><span class="p">)</span>
450
451    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;-r&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;sleep_duration&#39;</span><span class="p">,</span> 
452                        <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span>
453                        <span class="n">help</span><span class="o">=</span><span class="s">&quot;sleep duration (s) in main event loop&quot;</span><span class="p">,</span> 
454                        <span class="n">default</span><span class="o">=</span><span class="n">RETRY_INTERVAL_S</span><span class="p">)</span>
455
456    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;-g&#39;</span><span class="p">,</span> <span class="s">&#39;--gui&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
457                        <span class="n">dest</span><span class="o">=</span><span class="s">&#39;interface&#39;</span><span class="p">,</span>
458                        <span class="n">help</span><span class="o">=</span><span class="s">&#39;Use the graphical rather than command-line interface&#39;</span><span class="p">)</span>
459
460    <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;-v&#39;</span><span class="p">,</span> <span class="s">&#39;--version&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;version&#39;</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="n">__full_version__</span><span class="p">)</span>
461
462    <span class="n">results</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
463
464    <span class="n">addresses</span> <span class="o">=</span> <span class="n">results</span><span class="o">.</span><span class="n">email_addresses</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">)</span>
465    <span class="n">interface</span> <span class="o">=</span> <span class="p">{</span><span class="bp">False</span><span class="p">:</span> <span class="s">&#39;command-line&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">:</span> <span class="s">&#39;GUI&#39;</span><span class="p">}[</span><span class="n">results</span><span class="o">.</span><span class="n">interface</span><span class="p">]</span>
466
467    <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="n">results</span><span class="o">.</span><span class="n">log_file</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
468    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;#&quot;</span><span class="o">*</span><span class="mi">60</span><span class="p">)</span>
469    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;startup&quot;</span><span class="p">)</span>
470    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;trigger PV       = &quot;</span> <span class="o">+</span> <span class="n">results</span><span class="o">.</span><span class="n">trigger_PV</span><span class="p">)</span>
471    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;message PV       = &quot;</span> <span class="o">+</span> <span class="n">results</span><span class="o">.</span><span class="n">message_PV</span><span class="p">)</span>
472    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;email list       = &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">addresses</span><span class="p">)</span> <span class="p">)</span>
473    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;log file         = &quot;</span> <span class="o">+</span> <span class="n">results</span><span class="o">.</span><span class="n">log_file</span><span class="p">)</span>
474    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;logging interval = &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span> <span class="n">results</span><span class="o">.</span><span class="n">logging_interval</span> <span class="p">)</span> <span class="p">)</span>
475    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;sleep duration   = &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span> <span class="n">results</span><span class="o">.</span><span class="n">sleep_duration</span> <span class="p">)</span> <span class="p">)</span>
476    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;interface        = &quot;</span> <span class="o">+</span> <span class="n">interface</span><span class="p">)</span>
477    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;user             = &quot;</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&#39;LOGNAME&#39;</span><span class="p">])</span>
478    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;host             = &quot;</span> <span class="o">+</span> <span class="n">socket</span><span class="o">.</span><span class="n">gethostname</span><span class="p">()</span> <span class="p">)</span>
479    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;program          = &quot;</span> <span class="o">+</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">)</span>
480    <span class="n">logger</span><span class="p">(</span><span class="s">&quot;PID              = &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">())</span> <span class="p">)</span>
481   
482    <span class="k">if</span> <span class="n">results</span><span class="o">.</span><span class="n">interface</span> <span class="ow">is</span> <span class="bp">False</span><span class="p">:</span>
483        <span class="c"># When the GUI is not selected, </span>
484        <span class="c"># ensure the positional arguments are given</span>
485        <span class="n">tests</span> <span class="o">=</span> <span class="p">[</span>
486                     <span class="nb">len</span><span class="p">(</span><span class="n">results</span><span class="o">.</span><span class="n">trigger_PV</span><span class="p">),</span>
487                     <span class="nb">len</span><span class="p">(</span><span class="n">results</span><span class="o">.</span><span class="n">message_PV</span><span class="p">),</span>
488                     <span class="nb">len</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">addresses</span><span class="p">)),</span>
489                <span class="p">]</span>
490        <span class="k">if</span> <span class="mi">0</span> <span class="ow">in</span> <span class="n">tests</span><span class="p">:</span>
491            <span class="n">parser</span><span class="o">.</span><span class="n">print_usage</span><span class="p">()</span>
492            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
493   
494    <span class="c"># call the interface</span>
495    <span class="p">{</span><span class="bp">False</span><span class="p">:</span> <span class="n">cli</span><span class="p">,</span> <span class="bp">True</span><span class="p">:</span> <span class="n">gui</span><span class="p">}[</span><span class="n">results</span><span class="o">.</span><span class="n">interface</span><span class="p">](</span><span class="n">results</span><span class="p">)</span>     
496
497</div>
498<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
499    <span class="c">#  ./pvMail.py  pvMail:trigger pvMail:message jemian</span>
500    <span class="k">if</span> <span class="bp">False</span><span class="p">:</span>  <span class="c"># code development only</span>
501        <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;pvMail:trigger&#39;</span><span class="p">)</span>
502        <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;pvMail:message&#39;</span><span class="p">)</span>
503        <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;jemian&#39;</span><span class="p">)</span>
504        <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;-l mylog.log&#39;</span><span class="p">)</span>
505    <span class="n">main</span><span class="p">()</span>
506</pre></div>
507
508          </div>
509        </div>
510      </div>
511      <div class="clearer"></div>
512    </div>
513    <div class="related">
514      <h3>Navigation</h3>
515      <ul>
516        <li class="right" style="margin-right: 10px">
517          <a href="../../genindex.html" title="General Index"
518             >index</a></li>
519        <li class="right" >
520          <a href="../../py-modindex.html" title="Python Module Index"
521             >modules</a> |</li>
522        <li><a href="../../index.html">PvMail 3 (3.0-r801) documentation</a> &raquo;</li>
523          <li><a href="../index.html" >Module code</a> &raquo;</li> 
524      </ul>
525    </div>
526    <div class="footer">
527        &copy; Copyright (c) 2009-2012, UChicago Argonne, LLC (see LICENSE file for details).
528      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
529    </div>
530  </body>
531</html>
Note: See TracBrowser for help on using the repository browser.