forked from ISISComputingGroup/JSON_bourne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_utils.py
More file actions
105 lines (78 loc) · 2.98 KB
/
block_utils.py
File metadata and controls
105 lines (78 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from __future__ import unicode_literals
def shorten_title(title):
"""
Gets a PV title by shortening its address to the last segment.
If the title contains an RC value the PV title & the RC value
are returned.
Args:
title: The PV address as string.
Returns: The last segment of the input PV address as string.
"""
title_parts = title.split(':')
rc_values = ["HIGH.VAL", "LOW.VAL", "INRANGE.VAL", "ENABLE.VAL"]
if "RC" in title_parts and title_parts[-1] in rc_values:
return ':'.join(title_parts[-3:])
else:
return title_parts[-1]
def set_rc_values_for_block_from_pvs(block, pvs):
"""Search pvs for RC values for given block and return them"""
block_name = block.get_name()
items = pvs.items()
for k, v in items:
if k is None:
# not a valid key, skip this entry
continue
key_parts = k.split(':')
name = key_parts[0].strip()
suffix = key_parts[-1]
if block_name != name:
# block name does not match, skip this entry
continue
if "LOW.VAL" == suffix:
block.set_rc_low(v.get_value())
elif "HIGH.VAL" == suffix:
block.set_rc_high(v.get_value())
elif "INRANGE.VAL" == suffix:
block.set_rc_inrange(v.get_value())
elif "ENABLE.VAL" == suffix:
block.set_rc_enabled(v.get_value())
def set_rc_values_for_blocks(blocks, pvs):
"""Set all RC values for all the given blocks"""
for block in blocks:
set_rc_values_for_block_from_pvs(block, pvs)
def format_blocks(blocks):
"""
Converts a list of block objects into JSON.
Args:
blocks: A dictionary of block names to block objects.
Returns: A JSON dictionary of block names to block descriptions.
"""
blocks_formatted = {}
for name, block in blocks.items():
blocks_formatted[name] = block.get_description()
return blocks_formatted
def format_block_value(val, precision):
"""
Formats block values using the same rules as the blocks screen in the GUI.
Args:
val (str): the block value to format
precision (int): the precision to format the block to. If None then will not format.
Returns:
the formatted block value
"""
small_number_threshold = 0.001
big_number_threshold = 1000000
assert small_number_threshold < big_number_threshold
# No precision specified = do not format.
if precision is None or precision < 0:
return u"{}".format(val)
try:
float_val = float(val)
if small_number_threshold < abs(float_val) < big_number_threshold or float_val == 0:
format_str = u"{{:.{}f}}".format(precision)
else:
format_str = u"{{:.{}G}}".format(precision)
return format_str.format(float_val)
except (ValueError, TypeError):
# If number does not parse as a float, or formatting failed, just return it in string form.
return u"{}".format(val)