Skip to content

Commit c5fdb74

Browse files
committed
'If'
1 parent 8141e08 commit c5fdb74

File tree

5 files changed

+139
-3
lines changed

5 files changed

+139
-3
lines changed

Test/test_commands.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ def testWait(self):
183183
self.assertEqual(str(cmd), "Wait('counts', 1000, comparison='increase by', timeout=5, errhandler='someHandler')")
184184
self.assertEqual(ET.tostring(cmd.genXML()), "<wait><device>counts</device><value>1000</value><comparison>INCREASE_BY</comparison><timeout>5.0</timeout><error_handler>someHandler</error_handler></wait>")
185185

186+
def testIf(self):
187+
cmd = If('device', '>', 3.14)
188+
print cmd
189+
self.assertEqual(str(cmd), "If('device', '>', 3.14, tolerance=0.1)")
190+
self.assertEqual(ET.tostring(cmd.genXML()), "<if><device>device</device><comparison>ABOVE</comparison><value>3.14</value><tolerance>0.1</tolerance><body /></if>")
191+
192+
cmd = If('device', '>', 3.14, [ Comment('BODY') ])
193+
print cmd
194+
self.assertEqual(str(cmd), "If('device', '>', 3.14, [ Comment('BODY') ], tolerance=0.1)")
195+
self.assertEqual(ET.tostring(cmd.genXML()), "<if><device>device</device><comparison>ABOVE</comparison><value>3.14</value><tolerance>0.1</tolerance><body><comment><text>BODY</text></comment></body></if>")
196+
197+
186198
def testLoop(self):
187199
cmd = Loop('pv1', 1, 10, 0.1)
188200
print cmd

doc/commands.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ Parallel
5454
.. autoclass:: scan.commands.parallel.Parallel
5555
:members:
5656

57+
If
58+
--
59+
60+
.. autoclass:: scan.commands.iff.If
61+
:members:
62+
5763
Include
5864
-------
5965

@@ -75,4 +81,4 @@ Command Sequence
7581
----------------
7682
.. autoclass:: scan.commands.commandsequence.CommandSequence
7783
:members:
78-
84+

scan/commands/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from scan.commands.comment import Comment
44
from scan.commands.configlog import ConfigLog
55
from scan.commands.delay import Delay
6+
from scan.commands.iff import If
67
from scan.commands.include import Include
78
from scan.commands.log import Log
89
from scan.commands.loop import Loop
910
from scan.commands.script import Script
1011
from scan.commands.set import Set
1112
from scan.commands.wait import Wait
1213
from scan.commands.parallel import Parallel
13-
from scan.commands.sequence import Sequence
14+
from scan.commands.sequence import Sequence

scan/commands/iff.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
from scan.commands.command import Command
2+
import xml.etree.ElementTree as ET
3+
4+
class If(Command):
5+
"""Conditionally execute commands.
6+
7+
:param device: Device name
8+
:param value: Desired value.
9+
:param comparison: How current value is compared to the desired value.
10+
Options: '=', '>', '>=', '<' , '<='.
11+
:param body: One or more commands
12+
:param tolerance: Tolerance when checking numeric `readback`.
13+
Defaults to 0.1
14+
:param errhandler: Error handler
15+
16+
Examples:
17+
18+
>>> cmd = If('pv1', '=', 10, [ Comment("It's 10") ])
19+
"""
20+
21+
__comparisons= {'=':'EQUALS',
22+
'>':'ABOVE',
23+
'>=':'AT_LEAST',
24+
'<':'BELOW',
25+
'<=':'AT_MOST'}
26+
27+
def __init__(self, device, comparison, value, body=None, *args, **kwargs):
28+
if not isinstance(device, str):
29+
raise Exception("Expect device name, got '%s'" % str(device))
30+
self.__device = device
31+
32+
if isinstance(body, Command):
33+
self.__body = [ body ]
34+
elif body:
35+
self.__body = list(body)
36+
else:
37+
self.__body = list()
38+
if args:
39+
self.__body += args
40+
if not comparison in If.__comparisons:
41+
raise Exception("Invalid comparison '%s'" % comparison)
42+
self.__comparison = comparison
43+
self.__desiredValue = value
44+
self.__tolerance = kwargs['tolerance'] if 'tolerance' in kwargs else 0.1
45+
self.__errHandler = kwargs['errhandler'] if 'errhandler' in kwargs else None
46+
47+
def getDevice(self):
48+
""":return: Device name"""
49+
return self.__device
50+
51+
def getBody(self):
52+
"""Obtain list of body commands.
53+
54+
The If(..) constructor creates a safe
55+
copy of the passed 'body' to prevent side effects
56+
when that body is later changed and maybe
57+
used to construct another If(..) instance.
58+
59+
If there is a desire to change the body
60+
(before it's submitted to the scan server),
61+
this method provides that list of commands.
62+
:return: Body
63+
"""
64+
return self.__body
65+
66+
def genXML(self):
67+
xml = ET.Element('if')
68+
69+
ET.SubElement(xml, 'device').text = self.__device
70+
ET.SubElement(xml, 'comparison').text = If.__comparisons[self.__comparison]
71+
ET.SubElement(xml, 'value').text = str(self.__desiredValue)
72+
ET.SubElement(xml, "tolerance").text = str(self.__tolerance)
73+
74+
body = ET.SubElement(xml,'body')
75+
for command in self.__body:
76+
body.append(command.genXML())
77+
78+
if self.__errHandler:
79+
ET.SubElement(xml,'error_handler').text = str(self.__errHandler)
80+
81+
return xml
82+
83+
def __repr__(self):
84+
result = "If('%s', '%s'" % (self.__device, self.__comparison)
85+
if isinstance(self.__desiredValue, str):
86+
result += ", '%s'" % self.__desiredValue
87+
else:
88+
result += ", %s" % str(self.__desiredValue)
89+
90+
if len(self.__body)!=0:
91+
result += ", [ "
92+
result += ", ".join([ cmd.__repr__() for cmd in self.__body ])
93+
result+= " ]"
94+
result += ', tolerance=%g' % self.__tolerance
95+
if self.__errHandler:
96+
result += ", errhandler='%s'" % self.__errHandler
97+
result += ')'
98+
return result
99+
100+
def format(self, level=0):
101+
result = self.indent(level) + "If('%s', '%s'" % (self.__device, self.__comparison)
102+
if isinstance(self.__desiredValue, str):
103+
result += ", '%s'" % self.__desiredValue
104+
else:
105+
result += ", %s" % str(self.__desiredValue)
106+
107+
if len(self.__body)!=0:
108+
result += ",\n" + self.indent(level) + "[\n"
109+
result += ",\n".join([ cmd.format(level+1) for cmd in self.__body ])
110+
result += "\n" + self.indent(level) + "]"
111+
112+
113+
if self.__tolerance > 0:
114+
result += ', tolerance=%g' % self.__tolerance
115+
result += ')'
116+
return result

scan/version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

2-
__version__ = '1.4.1'
2+
__version__ = '1.5.0'
33

44
version_history = """
5+
1.5.0 - If(..) command
56
1.4.1 - Better Set(..) representation
67
1.4.0 - Jython uses Java HTTP API, CPython uses urllib2
78
1.3.12 - Add Parallel.append(..) method

0 commit comments

Comments
 (0)