implemented Interfaces, InterfaceTags, Messages, NetworkMethods, NetworkTypes, NodeGroups
[plcapi.git] / PLC / NetworkTypes.py
1 #
2 # Functions for interacting with the network_types table in the database
3 #
4 #
5 from types import StringTypes
6 from PLC.Faults import *
7 from PLC.Parameter import Parameter
8 from PLC.Storage.AlchemyObject import AlchemyObj
9
10 class NetworkType(AlchemyObj):
11     """
12     Representation of a row in the network_types table. To use,
13     instantiate with a dict of values.
14     """
15
16     tablename = 'network_types'
17     fields = {
18         'type': Parameter(str, "Network type", max = 20, primary_key=True),
19         }
20
21     def validate_type(self, name):
22         # Make sure name is not blank
23         if not len(name):
24             raise PLCInvalidArgument, "Network type must be specified"
25
26         # Make sure network type does not alredy exist
27         conflicts = NetworkTypes(self.api, name)
28         if conflicts:
29             raise PLCInvalidArgument, "Network type name already in use"
30
31         return name
32
33 class NetworkTypes(list):
34     """
35     Representation of the network_types table in the database.
36     """
37
38     def __init__(self, api, types = None):
39         if not types:
40             network_types = NetworkType().select()
41         elif isinstance(types, StringTypes):
42             network_types = NetworkType().select({'type': types})
43         else:
44             raise PLCInvalidArgument, "Wrong network type filter %r"%types 
45
46         self.extend(network_types)