Source

genet / class_node.py

Full commit
#!/usr/bin/env python
################################################################################
# $Id$
################################################################################
# Copyright (c) 2008 Santiago Paya Miralta <santiagopm::wanadoo.es>
#
# This file is part of mine.
#
# Mine is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
################################################################################
"""
Define base class `node' with the minimal attributes as abstract 
attributes.

Every element in the network has:

* level: topological level, ISO level, layer, ...
* id: its own identifier.
* iddown: in which element is this element encapsulated into.
* idtype: what kind of element is.

Also every puntual element (node) has a topological description:

* idtopo: may be end point, one to one connector or fork connector.

"""

class node:
    """
    Simple class with basic attributes.

    The 'iddown' object should be from the 'downlevel' level,
    this is resolved at execution time from an intelligent interface.
    """
    def __init__(self, level, iddown, downlevel, idtype):
        self.id = None
        self.level = level
        self.iddown = iddown
        self.downlevel = downlevel
        self.idtype = idtype
    
    def store(self, db):
        """
        Stores data in db. 

        Param: db is the database object.
        Return: the SQL statement and node id.
        """
        if self.id:
            st = "UPDATE gn_" + str(self.level) + "_nodes SET " \
                "iddown = " + str(self.iddown) + ", " \
                "downlevel = " + str(self.downlevel) + ", " \
                "idtype = " + str(self.idtype) + " " \
                "WHERE id = " + str(self.id) + ";"
            db.query(st)
        else:
            st = "INSERT INTO gn_" + str(self.level) + "_nodes(iddown, downlevel, idtype) " \
                "VALUES(" \
                + str(self.iddown) + ", " \
                + str(self.downlevel) + ", " \
                + str(self.idtype) + ") " \
                "RETURNING id;"
            self.id = db.query(st).getresult()[0][0]
        return st, self.id