Fix version output when missing.
[plcapi.git] / PLC / NetworkMethods.py
1 #
2 # Functions for interacting with the network_methods table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id$
8 # $URL$
9 #
10
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
14
15 class NetworkMethod(Row):
16     """
17     Representation of a row in the network_methods table. To use,
18     instantiate with a dict of values.
19     """
20
21     table_name = 'network_methods'
22     primary_key = 'method'
23     join_tables = ['interfaces']
24     fields = {
25         'method': Parameter(str, "Network method", max = 20),
26         }
27
28     def validate_method(self, name):
29         # Make sure name is not blank
30         if not len(name):
31             raise PLCInvalidArgument, "Network method must be specified"
32
33         # Make sure network method does not alredy exist
34         conflicts = NetworkMethods(self.api, [name])
35         if conflicts:
36             raise PLCInvalidArgument, "Network method name already in use"
37
38         return name
39
40 class NetworkMethods(Table):
41     """
42     Representation of the network_methods table in the database.
43     """
44
45     def __init__(self, api, methods = None):
46         Table.__init__(self, api, NetworkMethod)
47
48         sql = "SELECT %s FROM network_methods" % \
49               ", ".join(NetworkMethod.fields)
50
51         if methods:
52             sql += " WHERE method IN (%s)" % ", ".join(map(api.db.quote, methods))
53
54         self.selectall(sql)