1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2012-05-31 20:01:30 +0000 (Thu, 31 May 2012) $ |
---|
5 | # $Author: jemian $ |
---|
6 | # $Revision: 858 $ |
---|
7 | # $URL: qtprobe-demo/traits_probe.py $ |
---|
8 | # $Id: traits_probe.py 858 2012-05-31 20:01:30Z jemian $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | |
---|
11 | ''' |
---|
12 | EPICS PvProbe built using Enthought Traits |
---|
13 | |
---|
14 | *pvProbe* is one of the simplest EPICS user GUI tools. |
---|
15 | It is used to watch the value of an EPICS Process Variable. |
---|
16 | |
---|
17 | This version is based on the Enthought Traits user interface toolkit. |
---|
18 | |
---|
19 | USAGE:: |
---|
20 | |
---|
21 | [jemian@gov]$ ./pvProbe.py -h |
---|
22 | usage: pvProbe.py [-h] [pv] |
---|
23 | |
---|
24 | pvProbe: EPICS Traits-based version of Probe |
---|
25 | |
---|
26 | positional arguments: |
---|
27 | pv EPICS PV name |
---|
28 | |
---|
29 | optional arguments: |
---|
30 | -h, --help show this help message and exit |
---|
31 | |
---|
32 | |
---|
33 | SEE ALSO: |
---|
34 | |
---|
35 | For more information about Python version of pvProbe, see |
---|
36 | this presentation from the 2012 EPICS Collaboration Meeting: |
---|
37 | https://subversion.xor.aps.anl.gov/bcdaext/qtprobe-demo/docs/build/html/index.html |
---|
38 | |
---|
39 | This code is the work entitled *traits_probe.py*. |
---|
40 | ''' |
---|
41 | |
---|
42 | __description__ = "EPICS Traits-based version of pvProbe" |
---|
43 | |
---|
44 | |
---|
45 | import argparse |
---|
46 | import epics |
---|
47 | import sys #@UnusedImport |
---|
48 | |
---|
49 | |
---|
50 | choose_Qt4 = True |
---|
51 | choose_Qt4 = False |
---|
52 | from traits.etsconfig.api import ETSConfig |
---|
53 | if choose_Qt4: |
---|
54 | ETSConfig.toolkit = 'qt4' |
---|
55 | else: |
---|
56 | ETSConfig.toolkit = 'wx' |
---|
57 | |
---|
58 | from traits.api import * #@UnusedWildImport |
---|
59 | from traitsui.api import * #@UnusedWildImport |
---|
60 | from traitsui.key_bindings \ |
---|
61 | import KeyBinding, KeyBindings |
---|
62 | |
---|
63 | |
---|
64 | key_bindings = KeyBindings( |
---|
65 | KeyBinding( binding1 = 'Enter', |
---|
66 | description = 'Connect to new PV', |
---|
67 | method_name = 'connect' ), |
---|
68 | KeyBinding( binding1 = 'Return', |
---|
69 | description = 'Connect to new PV', |
---|
70 | method_name = 'connect' ), |
---|
71 | ) |
---|
72 | |
---|
73 | |
---|
74 | # |
---|
75 | class CodeHandler ( Handler ): |
---|
76 | '''TraitsUI Handler class for bound methods |
---|
77 | ''' |
---|
78 | |
---|
79 | def connect ( self, info ): |
---|
80 | '''handle a request to connect with an EPICS PV |
---|
81 | ''' |
---|
82 | info.object.do_connect() |
---|
83 | if len(info.object.name) > 0: |
---|
84 | # FIXME: This is not working |
---|
85 | info.object.title = "pvProbe: " + str(info.object.name) |
---|
86 | |
---|
87 | |
---|
88 | class Probe( HasTraits ): |
---|
89 | '''EPICS PvProbe built using Enthought Traits |
---|
90 | ''' |
---|
91 | name = Str |
---|
92 | value = Str |
---|
93 | _chid = Instance( epics.PV, value = None ) |
---|
94 | |
---|
95 | def do_callback(self, value=None, **kwds): |
---|
96 | """simple monitor callback""" |
---|
97 | self.value = str(value) |
---|
98 | |
---|
99 | traits_view = View( |
---|
100 | Item('name'), |
---|
101 | Readonly('value'), |
---|
102 | resizable=True, |
---|
103 | width = 250, |
---|
104 | title='pvProbe', |
---|
105 | key_bindings = key_bindings, |
---|
106 | handler = CodeHandler() |
---|
107 | ) |
---|
108 | |
---|
109 | def do_connect(self): |
---|
110 | '''connect this GUI with an EPICS Process Variable |
---|
111 | and set up a Channel Access monitor to update ``value`` |
---|
112 | whenever the PV changes. |
---|
113 | ''' |
---|
114 | if len(self.name) > 0: |
---|
115 | try: |
---|
116 | if self._chid is not None: |
---|
117 | self._chid.cancel_callback() |
---|
118 | self._chid.disconnect() |
---|
119 | except: |
---|
120 | pass |
---|
121 | self._chid = epics.PV(str(self.name), |
---|
122 | callback=self.do_callback) |
---|
123 | |
---|
124 | def __init__(self, name=""): |
---|
125 | HasTraits.__init__(self) |
---|
126 | self.name = name |
---|
127 | self.do_connect() |
---|
128 | |
---|
129 | |
---|
130 | if __name__ == '__main__': |
---|
131 | parser = argparse.ArgumentParser(description=__description__) |
---|
132 | parser.add_argument('pv', |
---|
133 | action='store', |
---|
134 | nargs='?', |
---|
135 | help="EPICS PV name", |
---|
136 | default="EpicsDemo1") |
---|
137 | results = parser.parse_args() |
---|
138 | |
---|
139 | probe = Probe(name=results.pv) |
---|
140 | probe.configure_traits() |
---|