-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_utils.py
104 lines (80 loc) · 3.33 KB
/
block_utils.py
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
from __future__ import unicode_literals
import logging
from collections import OrderedDict
logger = logging.getLogger("JSON_bourne")
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:])
elif "DASHBOARD" in title_parts:
return ":".join(title_parts[-3:])
else:
return title_parts[-1]
def set_rc_values_for_blocks(blocks, run_control_pvs):
"""
Set all RC values for all the given blocks. Blocks contains the blocks and their run control settings
Args:
blocks: dictionary of {pv_names : block_objects} containing info blocks
run_control_pvs: dictionary of {pv_names : block_objects} containing run control settings
"""
for pv, block_object in run_control_pvs.items():
pv_parts = pv.split(":")
name = pv_parts[0].strip()
suffix = pv_parts[-1]
try:
block = blocks[name]
if "LOW.VAL" == suffix:
block.set_rc_low(block_object.get_value())
elif "HIGH.VAL" == suffix:
block.set_rc_high(block_object.get_value())
elif "INRANGE.VAL" == suffix:
block.set_rc_inrange(block_object.get_value())
elif "ENABLE.VAL" == suffix:
block.set_rc_enabled(block_object.get_value())
except KeyError:
logging.info("Could not find block but it has runcontrol pvs {}".format(name))
def format_blocks(blocks):
"""
Converts a dictionary of blocks in the form of {name: Block} to a dictionary of {name: dict_describing_block}.
Args:
blocks: A dictionary of block names to block objects.
Returns: A dictionary of block names to block descriptions.
"""
blocks_formatted = OrderedDict()
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 not isinstance(precision, int) or precision < 0:
return "{}".format(val)
try:
float_val = float(val)
if small_number_threshold < abs(float_val) < big_number_threshold or float_val == 0:
format_str = "{{:.{}f}}".format(precision)
else:
format_str = "{{:.{}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 "{}".format(val)