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