Setting tag plcapi-5.4-2
[plcapi.git] / PLC / NodeTypes.py
1 #
2 # Functions for interacting with the node_types table in the database
3 #
4 #
5
6 from PLC.Faults import *
7 from PLC.Parameter import Parameter
8 from PLC.Table import Row, Table
9
10 class NodeType(Row):
11     """
12     Representation of a row in the node_types table. To use,
13     instantiate with a dict of values.
14     """
15
16     table_name = 'node_types'
17     primary_key = 'node_type'
18     join_tables = ['nodes']
19     fields = {
20         'node_type': Parameter(str, "Node type", max = 20),
21         }
22
23     def validate_node_type(self, name):
24         # Make sure name is not blank
25         if not len(name):
26             raise PLCInvalidArgument, "Node type must be specified"
27
28         # Make sure node type does not alredy exist
29         conflicts = NodeTypes(self.api, [name])
30         if conflicts:
31             raise PLCInvalidArgument, "Node type name already in use"
32
33         return name
34
35 class NodeTypes(Table):
36     """
37     Representation of the node_types table in the database.
38     """
39
40     def __init__(self, api, node_types = None):
41         Table.__init__(self, api, NodeType)
42
43         sql = "SELECT %s FROM node_types" % \
44               ", ".join(NodeType.fields)
45
46         if node_types:
47             sql += " WHERE node_type IN (%s)" % ", ".join( [ api.db.quote (t) for t in node_types ] )
48
49         self.selectall(sql)