first draft for ilinks : typed and valued links between interfaces
[plcapi.git] / PLC / IlinkTypes.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 # $Revision: 9423 $
5 #
6 from types import StringTypes
7
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
13
14 class IlinkType (Row):
15
16     """
17     Representation of a row in the ilink_types table.
18     """
19
20     table_name = 'ilink_types'
21     primary_key = 'ilink_type_id'
22     join_tables = ['ilink']
23     fields = {
24         'ilink_type_id': Parameter(int, "ilink type identifier"),
25         'name': Parameter(str, "ilink type name", max = 100),
26         'description': Parameter(str, "ilink type description", max = 254),
27         'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
28         }
29
30     def validate_name(self, name):
31         if not len(name):
32             raise PLCInvalidArgument, "ilink type name must be set"
33
34         conflicts = IlinkTypes(self.api, [name])
35         for tag_type in conflicts:
36             if 'ilink_type_id' not in self or \
37                    self['ilink_type_id'] != tag_type['ilink_type_id']:
38                 raise PLCInvalidArgument, "ilink type name already in use"
39
40         return name
41
42     def validate_min_role_id(self, role_id):
43         roles = [row['role_id'] for row in Roles(self.api)]
44         if role_id not in roles:
45             raise PLCInvalidArgument, "Invalid role"
46
47         return role_id
48
49 class IlinkTypes(Table):
50     """
51     Representation of row(s) from the ilink_types table
52     in the database.
53     """
54
55     def __init__(self, api, ilink_type_filter = None, columns = None):
56         Table.__init__(self, api, IlinkType, columns)
57
58         sql = "SELECT %s FROM ilink_types WHERE True" % \
59               ", ".join(self.columns)
60
61         if ilink_type_filter is not None:
62             if isinstance(ilink_type_filter, (list, tuple, set)):
63                 # Separate the list into integers and strings
64                 ints = filter(lambda x: isinstance(x, (int, long)), ilink_type_filter)
65                 strs = filter(lambda x: isinstance(x, StringTypes), ilink_type_filter)
66                 ilink_type_filter = Filter(IlinkType.fields, {'ilink_type_id': ints, 'name': strs})
67                 sql += " AND (%s) %s" % ilink_type_filter.sql(api, "OR")
68             elif isinstance(ilink_type_filter, dict):
69                 ilink_type_filter = Filter(IlinkType.fields, ilink_type_filter)
70                 sql += " AND (%s) %s" % ilink_type_filter.sql(api, "AND")
71             elif isinstance (ilink_type_filter, StringTypes):
72                 ilink_type_filter = Filter(IlinkType.fields, {'name':[ilink_type_filter]})
73                 sql += " AND (%s) %s" % ilink_type_filter.sql(api, "AND")
74             else:
75                 raise PLCInvalidArgument, "Wrong ilink type filter %r"%ilink_type_filter
76
77         self.selectall(sql)