objdictgen/nodelist.py
author nico
Mon, 04 Jun 2007 17:59:50 +0200
changeset 207 b6572d0336c3
parent 205 dac0f9b4e3f8
child 239 f45fd4cd3832
permissions -rw-r--r--
First doxygen implementation
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     1
#!/usr/bin/env python
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     3
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     4
#This file is part of CanFestival, a library implementing CanOpen Stack. 
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     5
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     6
#Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     7
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     8
#See COPYING file for copyrights details.
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
     9
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    10
#This library is free software; you can redistribute it and/or
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    11
#modify it under the terms of the GNU Lesser General Public
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    12
#License as published by the Free Software Foundation; either
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    13
#version 2.1 of the License, or (at your option) any later version.
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    14
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    15
#This library is distributed in the hope that it will be useful,
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    16
#but WITHOUT ANY WARRANTY; without even the implied warranty of
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    17
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    18
#Lesser General Public License for more details.
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    19
#
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    20
#You should have received a copy of the GNU Lesser General Public
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    21
#License along with this library; if not, write to the Free Software
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    22
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    23
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    24
from gnosis.xml.pickle import *
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    25
from gnosis.xml.pickle.util import setParanoia
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    26
setParanoia(0)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    27
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    28
from node import *
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    29
import eds_utils
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    30
import os, shutil
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    31
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    32
#-------------------------------------------------------------------------------
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    33
#                          Definition of NodeList Object
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    34
#-------------------------------------------------------------------------------
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    35
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    36
"""
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    37
Class recording a node list for a CANOpen network.
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    38
"""
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    39
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    40
class NodeList:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    41
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    42
    def __init__(self, manager):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    43
        self.Root = ""
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    44
        self.Manager = manager
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    45
        self.NetworkName = ""
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    46
        self.SlaveNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    47
        self.EDSNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    48
        self.CurrentSelected = None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    49
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    50
    def GetNetworkName(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    51
        return self.NetworkName
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    52
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    53
    def SetNetworkName(self, name):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    54
        self.NetworkName = name
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    55
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    56
    def GetManager(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    57
        return self.Manager
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    58
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    59
    def GetRoot(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    60
        return self.Root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    61
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    62
    def GetSlaveNumber(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    63
        return len(self.SlaveNodes)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    64
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    65
    def GetSlaveNames(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    66
        nodes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    67
        nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    68
        return ["0x%2.2X %s"%(idx, self.SlaveNodes[idx]["Name"]) for idx in nodes]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    69
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    70
    def GetSlaveIDs(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    71
        nodes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    72
        nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    73
        return nodes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    74
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    75
    def SetCurrentSelected(self, selected):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    76
        self.CurrentSelected = selected
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    77
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    78
    def GetCurrentSelected(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    79
        return self.CurrentSelected
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    80
            
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    81
    def LoadProject(self, root):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    82
        self.SlaveNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    83
        self.EDSNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    84
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    85
        self.Root = root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    86
        if not os.path.exists(self.Root):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    87
            return "\"%s\" folder doesn't exist"%self.Root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    88
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    89
        self.EDSFolder = os.path.join(self.Root, "eds")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    90
        if not os.path.exists(self.EDSFolder):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    91
            return "\"%s\" folder doesn't contain a \"eds\" folder"%self.Root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    92
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    93
        files = os.listdir(self.EDSFolder)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    94
        for file in files:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    95
            result = self.LoadEDS(file)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    96
            if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    97
                return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    98
                
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    99
        result = self.LoadMasterNode()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   100
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   101
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   102
            
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   103
        result = self.LoadSlaveNodes()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   104
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   105
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   106
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   107
    def SaveProject(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   108
        result = self.SaveMasterNode()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   109
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   110
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   111
            
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   112
        result = self.SaveNodeList()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   113
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   114
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   115
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   116
    def ImportEDSFile(self, edspath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   117
        dir, file = os.path.split(edspath)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   118
        eds = os.path.join(self.EDSFolder, file)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   119
        if os.path.isfile(eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   120
            return "EDS file already imported"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   121
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   122
            shutil.copy(edspath, self.EDSFolder)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   123
            return self.LoadEDS(file)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   124
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   125
    def LoadEDS(self, eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   126
        edspath = os.path.join(self.EDSFolder, eds)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   127
        node = eds_utils.GenerateNode(edspath, self.Manager.ScriptDirectory)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   128
        if isinstance(node, Node):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   129
            self.EDSNodes[eds] = node
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   130
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   131
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   132
            return node
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   133
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   134
    def AddSlaveNode(self, nodeName, nodeID, eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   135
        if eds in self.EDSNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   136
            slave = {"Name" : nodeName, "EDS" : eds, "Node" : self.EDSNodes[eds]}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   137
            self.SlaveNodes[nodeID] = slave
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   138
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   139
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   140
            return "\"%s\" EDS file is not available"%eds
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   141
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   142
    def RemoveSlaveNode(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   143
        if index in self.SlaveNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   144
            self.SlaveNodes.pop(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   145
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   146
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   147
            return "Node with \"0x%2.2X\" ID doesn't exist"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   148
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   149
    def LoadMasterNode(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   150
        masterpath = os.path.join(self.Root, "master.od")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   151
        if os.path.isfile(masterpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   152
            self.Manager.OpenFileInCurrent(masterpath)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   153
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   154
            self.Manager.CreateNewNode("MasterNode", 0x00, "master", "", "None", "", "heartbeat", ["DS302"])
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   155
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   156
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   157
    def SaveMasterNode(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   158
        masterpath = os.path.join(self.Root, "master.od")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   159
        if self.Manager.SaveCurrentInFile(masterpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   160
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   161
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   162
            return "Fail to save Master Node"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   163
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   164
    def LoadSlaveNodes(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   165
        cpjpath = os.path.join(self.Root, "nodelist.cpj")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   166
        if os.path.isfile(cpjpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   167
            try:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   168
                networks = eds_utils.ParseCPJFile(cpjpath)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   169
                if len(networks) > 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   170
                    self.NetworkName = networks[0]["Name"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   171
                    for nodeid, node in networks[0]["Nodes"].items():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   172
                        if node["Present"] == 1:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   173
                            result = self.AddSlaveNode(node["Name"], nodeid, node["DCFName"])
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   174
                            if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   175
                                return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   176
            except SyntaxError, message:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   177
                return "Unable to load CPJ file\n%s"%message
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   178
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   179
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   180
    def SaveNodeList(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   181
        cpjpath = os.path.join(self.Root, "nodelist.cpj")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   182
        content = eds_utils.GenerateCPJContent(self)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   183
        file = open(cpjpath, "w")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   184
        file.write(content)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   185
        file.close()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   186
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   187
    def GetSlaveNodeEntry(self, nodeid, index, subindex = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   188
        if nodeid in self.SlaveNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   189
            self.SlaveNodes[nodeid]["Node"].SetNodeID(nodeid)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   190
            return self.SlaveNodes[nodeid]["Node"].GetEntry(index, subindex)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   191
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   192
            return "Node 0x%2.2X doesn't exist"%nodeid
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   193
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   194
    def GetMasterNodeEntry(self, index, subindex = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   195
        return self.Manager.GetCurrentEntry(index, subindex)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   196
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   197
    def SetMasterNodeEntry(self, index, subindex = None, value = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   198
        self.Manager.SetCurrentEntry(index, subindex, value)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   199
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   200
    def GetOrderNumber(self, nodeid):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   201
        nodeindexes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   202
        nodeindexes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   203
        return nodeindexes.index(nodeid) + 1
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   204
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   205
    def GetNodeByOrder(self, order):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   206
        if order > 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   207
            nodeindexes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   208
            nodeindexes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   209
            print nodeindexes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   210
            if order <= len(nodeindexes):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   211
                return self.SlaveNodes[nodeindexes[order - 1]]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   212
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   213
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   214
    def IsCurrentEntry(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   215
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   216
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   217
                return self.Manager.IsCurrentEntry(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   218
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   219
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   220
                if node:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   221
                    return node.IsEntry(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   222
        return False
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   223
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   224
    def GetEntryInfos(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   225
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   226
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   227
                return self.Manager.GetEntryInfos(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   228
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   229
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   230
                if node:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   231
                    return node.GetEntryInfos(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   232
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   233
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   234
    def GetCurrentValidIndexes(self, min, max):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   235
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   236
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   237
                return self.Manager.GetCurrentValidIndexes(min, max)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   238
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   239
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   240
                if node:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   241
                    validindexes = []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   242
                    for index in node.GetIndexes():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   243
                        if min <= index <= max:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   244
                            validindexes.append((node.GetEntryName(index), index))
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   245
                    return validindexes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   246
                else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   247
                    print "Can't find node"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   248
        return []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   249
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   250
    def GetCurrentEntryValues(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   251
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   252
            node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   253
            if node:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   254
                return self.Manager.GetNodeEntryValues(node, index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   255
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   256
                print "Can't find node"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   257
        return [], []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   258
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   259
if __name__ == "__main__":
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   260
    from nodemanager import *
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   261
    import os, sys, shutil
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   262
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   263
    manager = NodeManager(sys.path[0])
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   264
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   265
    nodelist = NodeList(manager)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   266
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   267
    result = nodelist.LoadProject("/home/laurent/test_nodelist")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   268
    if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   269
        print result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   270
    else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   271
        print "MasterNode :"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   272
        manager.CurrentNode.Print()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   273
        print 
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   274
        for nodeid, node in nodelist.SlaveNodes.items():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   275
            print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   276
            node["Node"].Print()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   277
            print
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   278