From 4e78a06d0246d744b6baa361fd93426930502fa4 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Mon, 1 Oct 2012 21:36:41 -0400 Subject: [PATCH] implement NodeTags, NodeTypes, PCUTypes --- PLC/NodeTags.py | 45 +++++++++++++++++--------------- PLC/NodeTypes.py | 35 ++++++++++++++----------- PLC/PCUTypes.py | 68 ++++++++++++++++++------------------------------ 3 files changed, 69 insertions(+), 79 deletions(-) diff --git a/PLC/NodeTags.py b/PLC/NodeTags.py index 7f69e32..22c7728 100644 --- a/PLC/NodeTags.py +++ b/PLC/NodeTags.py @@ -3,21 +3,19 @@ # from PLC.Faults import * from PLC.Parameter import Parameter -from PLC.Filter import Filter -from PLC.Table import Row, Table +from PLC.Storage.AlchemyObj import AlchemyObj from PLC.Nodes import Node, Nodes from PLC.TagTypes import TagType, TagTypes -class NodeTag(Row): +class NodeTag(AlchemyObj): """ Representation of a row in the node_tag. To use, instantiate with a dict of values. """ - table_name = 'node_tag' - primary_key = 'node_tag_id' + tablename = 'node_tags' fields = { - 'node_tag_id': Parameter(int, "Node tag identifier"), + 'node_tag_id': Parameter(int, "Node tag identifier", primary_key=True), 'node_id': Node.fields['node_id'], 'hostname' : Node.fields['hostname'], 'tag_type_id': TagType.fields['tag_type_id'], @@ -27,26 +25,31 @@ class NodeTag(Row): 'category': TagType.fields['category'], } -class NodeTags(Table): + def sync(self, commit=True, validate=True): + AlchemyObj.sync(self, commit=commit, validate=validate) + if 'node_tag_id' not in self: + AlchemyObj.insert(self, dict(self)) + else: + AlchemyObj.update(self, {'node_tag_id': self['node_tag_id']}, dict(self)) + + def delete(self, commit=True): + AlchemyObj.delete(self, dict(self)) + +class NodeTags(list): """ Representation of row(s) from the node_tag table in the database. """ def __init__(self, api, node_tag_filter = None, columns = None): - Table.__init__(self, api, NodeTag, columns) - - sql = "SELECT %s FROM view_node_tags WHERE True" % \ - ", ".join(self.columns) - - if node_tag_filter is not None: - if isinstance(node_tag_filter, (list, tuple, set, int, long)): - node_tag_filter = Filter(NodeTag.fields, {'node_tag_id': node_tag_filter}) - elif isinstance(node_tag_filter, dict): - node_tag_filter = Filter(NodeTag.fields, node_tag_filter) - else: - raise PLCInvalidArgument, "Wrong node tag filter %r"%node_tag_filter - sql += " AND (%s) %s" % node_tag_filter.sql(api) + if not node_tag_filter: + node_tags = NodeTag().select() + elif isinstance(node_tag_filter, (list, tuple, set, int, long)): + node_tags = NodeTag().select(filter={'node_tag_id': node_tag_filter}) + elif isinstance(node_tag_filter, dict): + node_tags = NodeTag().select(filter=node_tag_filter) + else: + raise PLCInvalidArgument, "Wrong node tag filter %r"%node_tag_filter - self.selectall(sql) + self.extend(node_tags) diff --git a/PLC/NodeTypes.py b/PLC/NodeTypes.py index 00d4bf7..15c8cac 100644 --- a/PLC/NodeTypes.py +++ b/PLC/NodeTypes.py @@ -5,19 +5,18 @@ from PLC.Faults import * from PLC.Parameter import Parameter -from PLC.Table import Row, Table +from PLC.Storage.AlchemyObj import AlchemyObj -class NodeType(Row): +class NodeType(AlchemyObj): """ Representation of a row in the node_types table. To use, instantiate with a dict of values. """ - table_name = 'node_types' - primary_key = 'node_type' + tablename = 'node_types' join_tables = ['nodes'] fields = { - 'node_type': Parameter(str, "Node type", max = 20), + 'node_type': Parameter(str, "Node type", max = 20, primary_key=True), } def validate_node_type(self, name): @@ -32,18 +31,24 @@ class NodeType(Row): return name -class NodeTypes(Table): + def sync(self, commit=True, validate=True): + AlchemyObj.sync(self, commit=commit, validate=validate) + AlchemyObj.insert(self, dict(self) + + def delete(self, commit=True): + AlcemyObj.delete(self, dict(self)) + +class NodeTypes(list): """ Representation of the node_types table in the database. """ def __init__(self, api, node_types = None): - Table.__init__(self, api, NodeType) - - sql = "SELECT %s FROM node_types" % \ - ", ".join(NodeType.fields) - - if node_types: - sql += " WHERE node_type IN (%s)" % ", ".join( [ api.db.quote (t) for t in node_types ] ) - - self.selectall(sql) + if not node_types: + result = NodeType().select() + elif isinstance(node_types, (StringTypes, list, tuple, set)): + result = NodeType().select(filter={'node_type': node_types}) + else: + raise PLCInvalidArgument, "Wrong node type filter %r" % node_types + + self.extend(result) diff --git a/PLC/PCUTypes.py b/PLC/PCUTypes.py index 78a3353..bd41936 100644 --- a/PLC/PCUTypes.py +++ b/PLC/PCUTypes.py @@ -9,24 +9,22 @@ from types import StringTypes from PLC.Faults import * from PLC.Parameter import Parameter -from PLC.Table import Row, Table -from PLC.Filter import Filter +from PLC.Storage.AlchemyObj import AlchemyObj -class PCUType(Row): +class PCUType(AlchemyObj): """ Representation of a row in the pcu_types table. To use, instantiate with a dict of values. """ - table_name = 'pcu_types' - primary_key = 'pcu_type_id' + tablename = 'pcu_types' join_tables = ['pcu_protocol_type'] fields = { - 'pcu_type_id': Parameter(int, "PCU Type Identifier"), + 'pcu_type_id': Parameter(int, "PCU Type Identifier", primary_key=True), 'model': Parameter(str, "PCU model", max = 254), 'name': Parameter(str, "PCU full name", max = 254), - 'pcu_protocol_type_ids': Parameter([int], "PCU Protocol Type Identifiers"), - 'pcu_protocol_types': Parameter([dict], "PCU Protocol Type List") + 'pcu_protocol_type_ids': Parameter([int], "PCU Protocol Type Identifiers", joined=True), + 'pcu_protocol_types': Parameter([dict], "PCU Protocol Type List", joined=True) } def validate_model(self, model): @@ -35,14 +33,14 @@ class PCUType(Row): raise PLCInvalidArgument, "Model must be specified" # Make sure boot state does not alredy exist - conflicts = PCUTypes(self.api, [model]) + conflicts = PCUTypes(self.api, model) for pcu_type in conflicts: if 'pcu_type_id' not in self or self['pcu_type_id'] != pcu_type['pcu_type_id']: raise PLCInvalidArgument, "Model already in use" return model -class PCUTypes(Table): +class PCUTypes(list): """ Representation of the pcu_types table in the database. """ @@ -51,41 +49,25 @@ class PCUTypes(Table): # Remove pcu_protocol_types from query since its not really a field # in the db. We will add it later - if columns == None: - columns = PCUType.fields.keys() - if 'pcu_protocol_types' in columns: - removed_fields = ['pcu_protocol_types'] - columns.remove('pcu_protocol_types') + if not pcu_type_filter: + pcu_types = PCUType().select() + elif isinstance(pcu_type_filter, (list, tuple, set)): + # Separate the list into integers and strings + ints = filter(lambda x: isinstance(x, (int, long)), pcu_type_filter) + strs = filter(lambda x: isinstance(x, StringTypes), pcu_type_filter) + pcu_types = PCUType().select(filter = {'pcu_type_id': ints, 'model': strs}) + elif isinstance(pcu_type_filter, dict): + pcu_types = PCUType().select(filter = pcu_type_filter) + elif isinstance (pcu_type_filter, StringTypes): + pcu_types = PCUType().select(filter = {'model': pcu_type_filter}) + elif isinstance (pcu_type_filter, int): + pcu_types = PCUType().select(filter = {'pcu_type_id': pcu_filter}) else: - removed_fields = [] - - Table.__init__(self, api, PCUType, columns) - - sql = "SELECT %s FROM view_pcu_types WHERE True" % \ - ", ".join(self.columns) - - if pcu_type_filter is not None: - if isinstance(pcu_type_filter, (list, tuple, set)): - # Separate the list into integers and strings - ints = filter(lambda x: isinstance(x, (int, long)), pcu_type_filter) - strs = filter(lambda x: isinstance(x, StringTypes), pcu_type_filter) - pcu_type_filter = Filter(PCUType.fields, {'pcu_type_id': ints, 'model': strs}) - sql += " AND (%s) %s" % pcu_type_filter.sql(api, "OR") - elif isinstance(pcu_type_filter, dict): - pcu_type_filter = Filter(PCUType.fields, pcu_type_filter) - sql += " AND (%s) %s" % pcu_type_filter.sql(api, "AND") - elif isinstance (pcu_type_filter, StringTypes): - pcu_type_filter = Filter(PCUType.fields, {'model':pcu_type_filter}) - sql += " AND (%s) %s" % pcu_type_filter.sql(api, "AND") - elif isinstance (pcu_type_filter, int): - pcu_type_filter = Filter(PCUType.fields, {'pcu_type_id':pcu_type_filter}) - sql += " AND (%s) %s" % pcu_type_filter.sql(api, "AND") - else: - raise PLCInvalidArgument, "Wrong pcu_type filter %r"%pcu_type_filter - - - self.selectall(sql) + raise PLCInvalidArgument, "Wrong pcu_type filter %r"%pcu_type_filter + + self.extend(pcu_types) + # return a list of protocol type objects for each port type if 'pcu_protocol_types' in removed_fields: from PLC.PCUProtocolTypes import PCUProtocolTypes -- 2.43.0