2 # Functions for interacting with the network_methods table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
7 # $Id: NetworkMethods.py,v 1.2 2006/10/06 18:19:41 mlhuang Exp $
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
14 class NetworkMethod(Row):
16 Representation of a row in the network_methods table. To use,
17 instantiate with a dict of values.
20 table_name = 'network_methods'
21 primary_key = 'method'
23 'method': Parameter(str, "Network method", max = 20),
26 def __init__(self, api, fields = {}):
27 Row.__init__(self, fields)
30 def validate_method(self, name):
31 # Remove leading and trailing spaces
34 # Make sure name is not blank after we removed the spaces
36 raise PLCInvalidArgument, "Network method must be specified"
38 # Make sure network method does not alredy exist
39 conflicts = NetworkMethods(self.api, [name])
41 raise PLCInvalidArgument, "Network method name already in use"
45 def delete(self, commit = True):
46 assert 'method' in self
48 # Clean up miscellaneous join tables
49 for table in ['nodenetworks', 'network_methods']:
50 self.api.db.do("DELETE FROM " + table + \
51 " WHERE method = %(method)s",
57 class NetworkMethods(Table):
59 Representation of the network_methods table in the database.
62 def __init__(self, api, names = None):
63 sql = "SELECT %s FROM network_methods" % \
64 ", ".join(NetworkMethod.fields)
67 # Separate the list into integers and strings
68 sql += " WHERE method IN (%s)" % ", ".join(api.db.quote(names))
70 rows = api.db.selectall(sql)
73 self[row['method']] = NetworkMethod(api, row)