implemented Interfaces, InterfaceTags, Messages, NetworkMethods, NetworkTypes, NodeGroups
[plcapi.git] / PLC / NetworkMethods.py
1 #
2 # Functions for interacting with the network_methods 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 NetworkMethod(AlchemyObj):
11     """
12     Representation of a row in the network_methods table. To use,
13     instantiate with a dict of values.
14     """
15
16     tablename = 'network_methods'
17     join_tables = ['interfaces']
18     fields = {
19         'method': Parameter(str, "Network method", max = 20, primary_key=True),
20         }
21
22     def validate_method(self, name):
23         # Make sure name is not blank
24         if not len(name):
25             raise PLCInvalidArgument, "Network method must be specified"
26
27         # Make sure network method does not alredy exist
28         conflicts = NetworkMethods(self.api, name)
29         if conflicts:
30             raise PLCInvalidArgument, "Network method name already in use"
31
32         return name
33
34     def sync(self, commit = True, validate=True):
35         AlchemyObj.sync(self, commit, validate)
36         AlchemyObj.insert(self, dict(self))
37
38     def delete(self, commit=True):
39         assert method in self
40         AlchemyObj.delete(self, dict(self))
41         
42
43 class NetworkMethods(list):
44     """
45     Representation of the network_methods table in the database.
46     """
47
48     def __init__(self, api, methods = None):
49
50         if not methods:
51             network_methods = NetworkMethod().select()
52         elif isinstance(methods, StringTypes):
53             network_methods = NetworkMethod().select(filter={'method': methods})
54         else:
55             raise PLCInvalidArgument, "Wrong network method filter %r"%methods
56
57         self.extend(network_methods)
58