2 # Thierry Parmentelat - INRIA
6 from types import StringTypes
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Filter import Filter
11 from PLC.Table import Row, Table
12 from PLC.Roles import Role, Roles
14 class NodeTagType (Row):
17 Representation of a row in the node_tag_types table.
20 table_name = 'node_tag_types'
21 primary_key = 'node_tag_type_id'
22 join_tables = ['node_tag']
24 'node_tag_type_id': Parameter(int, "Node tag type identifier"),
25 'tagname': Parameter(str, "Node tag type name", max = 100),
26 'description': Parameter(str, "Node tag type description", max = 254),
27 'category' : Parameter (str, "Node tag category", max=64, optional=True),
28 'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
31 def validate_name(self, name):
33 raise PLCInvalidArgument, "node tag type name must be set"
35 conflicts = NodeTagTypes(self.api, [name])
36 for tag_type in conflicts:
37 if 'node_tag_type_id' not in self or \
38 self['node_tag_type_id'] != tag_type['node_tag_type_id']:
39 raise PLCInvalidArgument, "node tag type name already in use"
43 def validate_min_role_id(self, role_id):
44 roles = [row['role_id'] for row in Roles(self.api)]
45 if role_id not in roles:
46 raise PLCInvalidArgument, "Invalid role"
50 class NodeTagTypes(Table):
52 Representation of row(s) from the node_tag_types table
56 def __init__(self, api, node_tag_type_filter = None, columns = None):
57 Table.__init__(self, api, NodeTagType, columns)
59 sql = "SELECT %s FROM node_tag_types WHERE True" % \
60 ", ".join(self.columns)
62 if node_tag_type_filter is not None:
63 if isinstance(node_tag_type_filter, (list, tuple, set)):
64 # Separate the list into integers and strings
65 ints = filter(lambda x: isinstance(x, (int, long)), node_tag_type_filter)
66 strs = filter(lambda x: isinstance(x, StringTypes), node_tag_type_filter)
67 node_tag_type_filter = Filter(NodeTagType.fields, {'node_tag_type_id': ints, 'tagname': strs})
68 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "OR")
69 elif isinstance(node_tag_type_filter, dict):
70 node_tag_type_filter = Filter(NodeTagType.fields, node_tag_type_filter)
71 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "AND")
72 elif isinstance (node_tag_type_filter, StringTypes):
73 node_tag_type_filter = Filter(NodeTagType.fields, {'tagname':[node_tag_type_filter]})
74 sql += " AND (%s) %s" % node_tag_type_filter.sql(api, "AND")
76 raise PLCInvalidArgument, "Wrong node tag type filter %r"%node_tag_type_filter