/usr/lib/python3/dist-packages/PyTango/attr_data.py is in python3-pytango 8.1.1-1build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | # ------------------------------------------------------------------------------
# This file is part of PyTango (http://www.tinyurl.com/PyTango)
#
# Copyright 2006-2012 CELLS / ALBA Synchrotron, Bellaterra, Spain
# Copyright 2013-2014 European Synchrotron Radiation Facility, Grenoble, France
#
# Distributed under the terms of the GNU Lesser General Public License,
# either version 3 of the License, or (at your option) any later version.
# See LICENSE.txt for more info.
# ------------------------------------------------------------------------------
"""
This is an internal PyTango module.
"""
from __future__ import with_statement
from __future__ import print_function
__all__ = [ "AttrData" ]
__docformat__ = "restructuredtext"
import inspect
from ._PyTango import Except, CmdArgType, AttrDataFormat, AttrWriteType
from ._PyTango import DispLevel, UserDefaultAttrProp
from ._PyTango import Attr, SpectrumAttr, ImageAttr
from .utils import is_non_str_seq, is_pure_str
class AttrData(object):
"""A helper class that contains the same information one of the items in
DeviceClass.attr_list but in object form"""
def __init__(self, name, class_name, attr_info=None):
self.class_name = class_name
self.attr_name = name
self.attr_type = CmdArgType.DevVoid
self.attr_format = AttrDataFormat.SCALAR
self.attr_write = AttrWriteType.READ
self.dim_x = 1
self.dim_y = 0
self.display_level = DispLevel.OPERATOR
self.polling_period = -1
self.memorized = False
self.hw_memorized = False
if name is None:
self.read_method_name = None
self.write_method_name = None
self.is_allowed_name = None
else:
self.read_method_name = "read_" + name
self.write_method_name = "write_" + name
self.is_allowed_name = "is_" + name + "_allowed"
self.attr_class = None
self.attr_args = []
self.att_prop = None
if attr_info is not None:
self.from_attr_info(attr_info)
@classmethod
def from_dict(cls, attr_dict):
attr_dict = dict(attr_dict)
name = attr_dict.pop('name', None)
class_name = attr_dict.pop('class_name', None)
self = cls(name, class_name)
self.build_from_dict(attr_dict)
return self
def build_from_dict(self, attr_dict):
self.attr_type = attr_dict.pop('dtype', CmdArgType.DevDouble)
self.attr_format = attr_dict.pop('dformat', AttrDataFormat.SCALAR)
self.dim_x = attr_dict.pop('max_dim_x', 1)
self.dim_y = attr_dict.pop('max_dim_y', 0)
self.display_level = attr_dict.pop('display_level', DispLevel.OPERATOR)
self.polling_period = attr_dict.pop('polling_period', -1)
self.memorized = attr_dict.pop('memorized', False)
self.hw_memorized = attr_dict.pop('hw_memorized', False)
is_access_explicit = "access" in attr_dict
if is_access_explicit:
self.attr_write = attr_dict.pop('access')
else:
# access is defined by which methods were defined
r_explicit = "fread" in attr_dict or "fget" in attr_dict
w_explicit = "fwrite" in attr_dict or "fset" in attr_dict
if r_explicit and w_explicit:
self.attr_write = AttrWriteType.READ_WRITE
elif r_explicit:
self.attr_write = AttrWriteType.READ
elif w_explicit:
self.attr_write = AttrWriteType.WRITE
else:
self.attr_write = AttrWriteType.READ
fread = attr_dict.pop('fget', attr_dict.pop('fread', None))
if fread is not None:
if is_pure_str(fread):
self.read_method_name = fread
elif inspect.isroutine(fread):
self.read_method_name = fread.__name__
fwrite = attr_dict.pop('fset', attr_dict.pop('fwrite', None))
if fwrite is not None:
if is_pure_str(fwrite):
self.write_method_name = fwrite
elif inspect.isroutine(fwrite):
self.write_method_name = fwrite.__name__
fisallowed = attr_dict.pop('fisallowed', None)
if fisallowed is not None:
if is_pure_str(fisallowed):
self.is_allowed_name = fisallowed
elif inspect.isroutine(fisallowed):
self.is_allowed_name = fisallowed.__name__
self.attr_class = attr_dict.pop("klass", self.DftAttrClassMap[self.attr_format])
self.attr_args.extend((self.attr_name, self.attr_type, self.attr_write))
if not self.attr_format == AttrDataFormat.SCALAR:
self.attr_args.append(self.dim_x)
if not self.attr_format == AttrDataFormat.SPECTRUM:
self.attr_args.append(self.dim_y)
if len(attr_dict):
self.att_prop = self.__create_user_default_attr_prop(attr_dict)
return self
def _set_name(self, name):
old_name = self.attr_name
self.attr_name = name
self.attr_args[0] = name
if old_name is None:
if self.read_method_name is None:
self.read_method_name = "read_" + name
if self.write_method_name is None:
self.write_method_name = "write_" + name
if self.is_allowed_name is None:
self.is_allowed_name = "is_" + name + "_allowed"
def __throw_exception(self, msg, meth="create_attribute()"):
Except.throw_exception("PyDs_WrongAttributeDefinition", msg, meth)
def __create_user_default_attr_prop(self, extra_info):
"""for internal usage only"""
p = UserDefaultAttrProp()
doc = extra_info.pop('doc', None)
if 'doc' is not None:
extra_info['description'] = doc
for k, v in extra_info.items():
k_lower = k.lower()
method_name = "set_%s" % k_lower.replace(' ','_')
if hasattr(p, method_name):
method = getattr(p, method_name)
method(str(v))
elif k == 'delta_time':
p.set_delta_t(str(v))
elif not k_lower in ('display level', 'polling period', 'memorized'):
msg = "Wrong definition of attribute. " \
"The object extra information '%s' " \
"is not recognized!" % (k,)
Except.throw_exception("PyDs_WrongAttributeDefinition", msg,
"create_user_default_attr_prop()")
return p
def from_attr_info(self, attr_info):
name = self.class_name
attr_name = self.attr_name
throw_ex = self.__throw_exception
# check for well defined attribute info
# check parameter
if not is_non_str_seq(attr_info):
throw_ex("Wrong data type for value for describing attribute %s in "
"class %s\nMust be a sequence with 1 or 2 elements"
% (attr_name, name))
if len(attr_info) < 1 or len(attr_info) > 2:
throw_ex("Wrong number of argument for describing attribute %s in "
"class %s\nMust be a sequence with 1 or 2 elements"
% (attr_name, name))
extra_info = {}
if len(attr_info) == 2:
# attr_info[1] must be a dictionary
# extra_info = attr_info[1], with all the keys lowercase
for k, v in attr_info[1].items():
extra_info[k.lower()] = v
attr_info = attr_info[0]
attr_info_len = len(attr_info)
# check parameter
if not is_non_str_seq(attr_info) or \
attr_info_len < 3 or attr_info_len > 5:
throw_ex("Wrong data type for describing mandatory information for "
"attribute %s in class %s\nMust be a sequence with 3, 4 "
"or 5 elements" % (attr_name, name))
# get data type
try:
self.attr_type = CmdArgType(attr_info[0])
except:
throw_ex("Wrong data type in attribute argument for attribute %s "
"in class %s\nAttribute data type (first element in first "
"sequence) must be a PyTango.CmdArgType"
% (attr_name, name))
# get format
try:
self.attr_format = AttrDataFormat(attr_info[1])
except:
throw_ex("Wrong data format in attribute argument for attribute %s "
"in class %s\nAttribute data format (second element in "
"first sequence) must be a PyTango.AttrDataFormat"
% (attr_name, name))
if self.attr_format == AttrDataFormat.SCALAR:
if attr_info_len != 3:
throw_ex("Wrong data type in attribute argument for attribute "
"%s in class %s\nSequence describing mandatory "
"attribute parameters for scalar attribute must have "
"3 elements" % (attr_name, name))
elif self.attr_format == AttrDataFormat.SPECTRUM:
if attr_info_len != 4:
throw_ex("Wrong data type in attribute argument for attribute "
"%s in class %s\nSequence describing mandatory "
"attribute parameters for spectrum attribute must "
"have 4 elements" % (attr_name, name))
try:
self.dim_x = int(attr_info[3])
except:
throw_ex("Wrong data type in attribute argument for attribute "
"%s in class %s\n4th element in sequence describing "
"mandatory dim_x attribute parameter for spectrum "
"attribute must be an integer" % (attr_name, name))
elif self.attr_format == AttrDataFormat.IMAGE:
if attr_info_len != 5:
throw_ex("Wrong data type in attribute argument for attribute "
"%s in class %s\nSequence describing mandatory "
"attribute parameters for image attribute must have "
"5 elements" % (attr_name, name))
try:
self.dim_x = int(attr_info[3])
except:
throw_ex("Wrong data type in attribute argument for attribute "
"%s in class %s\n4th element in sequence describing "
"mandatory dim_x attribute parameter for image "
"attribute must be an integer" % (attr_name, name))
try:
self.dim_y = int(attr_info[4])
except:
throw_ex("Wrong data type in attribute argument for attribute "
"%s in class %s\n5th element in sequence desribing "
"mandatory dim_y attribute parameter for image "
"attribute must be an integer" % (attr_name, name))
#get write type
try:
self.attr_write = AttrWriteType(attr_info[2])
except:
throw_ex("Wrong data write type in attribute argument for "
"attribute %s in class %s\nAttribute write type (third "
"element in first sequence) must be a "
"PyTango.AttrWriteType" % (attr_name, name))
try:
self.display_level = DispLevel(extra_info.get("display level",
DispLevel.OPERATOR))
except:
throw_ex("Wrong display level in attribute information for "
"attribute %s in class %s\nAttribute information for "
"display level is not a PyTango.DispLevel"
% (attr_name, name))
try:
self.polling_period = int(extra_info.get("polling period", -1))
except:
throw_ex("Wrong polling period in attribute information for "
"attribute %s in class %s\nAttribute information for "
"polling period is not an integer" % (attr_name, name))
memorized = extra_info.get("memorized", "false").lower()
if memorized == "true":
self.memorized = True
self.hw_memorized = True
elif memorized == "true_without_hard_applied":
self.memorized = True
else:
self.memorized = False
self.attr_class = extra_info.get("klass", self.DftAttrClassMap[self.attr_format])
self.attr_args.extend((self.attr_name, self.attr_type, self.attr_write))
if not self.attr_format == AttrDataFormat.SCALAR:
self.attr_args.append(self.dim_x)
if not self.attr_format == AttrDataFormat.SPECTRUM:
self.attr_args.append(self.dim_y)
att_prop = None
if extra_info:
att_prop = self.__create_user_default_attr_prop(extra_info)
self.att_prop = att_prop
def to_attr(self):
attr = self.attr_class(*self.attr_args)
if self.att_prop is not None:
attr.set_default_properties(self.att_prop)
attr.set_disp_level(self.display_level)
if self.memorized:
attr.set_memorized()
attr.set_memorized_init(self.hw_memorized)
if self.polling_period > 0:
attr.set_polling_period(self.polling_period)
return attr
DftAttrClassMap = { AttrDataFormat.SCALAR : Attr,
AttrDataFormat.SPECTRUM: SpectrumAttr,
AttrDataFormat.IMAGE : ImageAttr }
|