objdictgen/nodelist.py
author Edouard Tisserant
Fri, 19 Jun 2015 20:16:07 +0200
changeset 792 2b9fd5086704
parent 781 77fe158d6ccd
permissions -rw-r--r--
some fixes
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 node import *
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    25
import eds_utils
268
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
    26
import os, shutil, types
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    27
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    28
#-------------------------------------------------------------------------------
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    29
#                          Definition of NodeList Object
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    30
#-------------------------------------------------------------------------------
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
Class recording a node list for a CANOpen network.
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
class NodeList:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    37
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    38
    def __init__(self, manager, netname = ""):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    39
        self.Root = ""
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    40
        self.Manager = manager
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    41
        self.NetworkName = netname
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    42
        self.SlaveNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    43
        self.EDSNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    44
        self.CurrentSelected = None
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    45
        self.Changed = False
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    46
    
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    47
    def HasChanged(self):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    48
        return self.Changed or not self.Manager.CurrentIsSaved()
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    49
    
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    50
    def ForceChanged(self, changed):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
    51
        self.Changed = changed
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    52
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    53
    def GetNetworkName(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    54
        return self.NetworkName
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    55
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    56
    def SetNetworkName(self, name):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    57
        self.NetworkName = name
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    58
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    59
    def GetManager(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    60
        return self.Manager
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    61
    
781
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    62
    def GetEDSFolder(self, root_path=None):
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    63
        if root_path is None:
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    64
            root_path = self.Root
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    65
        return os.path.join(root_path, "eds")
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    66
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    67
    def GetRoot(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    68
        return self.Root
272
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    69
bbdbf0c17d3c Added SetRoot in networkedit to change project root on the fly
etisserant
parents: 270
diff changeset
    70
    def SetRoot(self, newrootpath):
781
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    71
        if os.path.isdir(newrootpath):
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    72
            self.Root = newrootpath
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    73
            self.Manager.SetCurrentFilePath(os.path.join(self.Root, "master.od"))
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    74
            return True
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
    75
        return False
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    76
    
748
18ed0f3b030a Fixing networkedit to deal with non-null master nodeid
Laurent Bessard
parents: 744
diff changeset
    77
    def GetMasterNodeID(self):
18ed0f3b030a Fixing networkedit to deal with non-null master nodeid
Laurent Bessard
parents: 744
diff changeset
    78
        return self.Manager.GetCurrentNodeID()
18ed0f3b030a Fixing networkedit to deal with non-null master nodeid
Laurent Bessard
parents: 744
diff changeset
    79
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    80
    def GetSlaveNumber(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    81
        return len(self.SlaveNodes)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    82
    
740
b6646a975f23 Fix bug when importing and loading not well formatted eds files
Laurent Bessard
parents: 580
diff changeset
    83
    def GetSlaveName(self, idx):
b6646a975f23 Fix bug when importing and loading not well formatted eds files
Laurent Bessard
parents: 580
diff changeset
    84
        return self.SlaveNodes[idx]["Name"]
b6646a975f23 Fix bug when importing and loading not well formatted eds files
Laurent Bessard
parents: 580
diff changeset
    85
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    86
    def GetSlaveNames(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    87
        nodes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    88
        nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    89
        return ["0x%2.2X %s"%(idx, self.SlaveNodes[idx]["Name"]) for idx in nodes]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    90
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    91
    def GetSlaveIDs(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    92
        nodes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    93
        nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    94
        return nodes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    95
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    96
    def SetCurrentSelected(self, selected):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    97
        self.CurrentSelected = selected
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    98
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
    99
    def GetCurrentSelected(self):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   100
        return self.CurrentSelected
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   101
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   102
    def LoadProject(self, root, netname = None):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   103
        self.SlaveNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   104
        self.EDSNodes = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   105
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   106
        self.Root = root
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   107
        if not os.path.exists(self.Root):
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   108
            return _("\"%s\" folder doesn't exist")%self.Root
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   109
        
781
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   110
        eds_folder = self.GetEDSFolder()
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   111
        if not os.path.exists(eds_folder):
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   112
            os.mkdir(eds_folder)
265
36cc4c87cdaa Create "eds" folder if it doesn't exist in any case
lbessard
parents: 258
diff changeset
   113
            #return "\"%s\" folder doesn't contain a \"eds\" folder"%self.Root
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   114
        
781
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   115
        files = os.listdir(eds_folder)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   116
        for file in files:
781
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   117
            filepath = os.path.join(eds_folder, file)
243
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   118
            if os.path.isfile(filepath) and os.path.splitext(filepath)[-1] == ".eds":
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   119
                result = self.LoadEDS(file)
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   120
                if result != None:
7fcc129a06ce Some bugs fixed
lbessard
parents: 242
diff changeset
   121
                    return result
270
178030c1dff8 Removed debug prints
etisserant
parents: 268
diff changeset
   122
                
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   123
        result = self.LoadMasterNode(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   124
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   125
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   126
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   127
        result = self.LoadSlaveNodes(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   128
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   129
            return result
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   130
        
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   131
        self.NetworkName = netname
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   132
    
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   133
    def SaveProject(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   134
        result = self.SaveMasterNode(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   135
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   136
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   137
            
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   138
        result = self.SaveNodeList(netname)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   139
        if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   140
            return result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   141
    
505
88956169c8b5 Adding support for replace imported EDS in nodelist
lbessard
parents: 337
diff changeset
   142
    def ImportEDSFile(self, edspath, force = False):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   143
        dir, file = os.path.split(edspath)
781
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   144
        eds_folder = self.GetEDSFolder()
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   145
        eds = os.path.join(eds_folder, file)
505
88956169c8b5 Adding support for replace imported EDS in nodelist
lbessard
parents: 337
diff changeset
   146
        if not force and os.path.isfile(eds):
740
b6646a975f23 Fix bug when importing and loading not well formatted eds files
Laurent Bessard
parents: 580
diff changeset
   147
            return _("EDS file already imported"), True
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   148
        else:
781
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   149
            shutil.copy(edspath, eds_folder)
740
b6646a975f23 Fix bug when importing and loading not well formatted eds files
Laurent Bessard
parents: 580
diff changeset
   150
            return self.LoadEDS(file), False
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   151
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   152
    def LoadEDS(self, eds):
781
77fe158d6ccd Fixed Save As... function in Beremiz
Laurent Bessard
parents: 748
diff changeset
   153
        edspath = os.path.join(self.GetEDSFolder(), eds)
258
8f7725451453 Some bugs fixed:
lbessard
parents: 253
diff changeset
   154
        node = eds_utils.GenerateNode(edspath)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   155
        if isinstance(node, Node):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   156
            self.EDSNodes[eds] = node
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   157
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   158
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   159
            return node
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   160
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   161
    def AddSlaveNode(self, nodeName, nodeID, eds):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   162
        if eds in self.EDSNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   163
            slave = {"Name" : nodeName, "EDS" : eds, "Node" : self.EDSNodes[eds]}
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   164
            self.SlaveNodes[nodeID] = slave
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   165
            self.Changed = True
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   166
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   167
        else:
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   168
            return _("\"%s\" EDS file is not available")%eds
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   169
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   170
    def RemoveSlaveNode(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   171
        if index in self.SlaveNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   172
            self.SlaveNodes.pop(index)
744
bc1bbbb5ef71 Fix bug with 'not saved' state for network when removing a slave
Laurent Bessard
parents: 740
diff changeset
   173
            self.Changed = True
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   174
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   175
        else:
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   176
            return _("Node with \"0x%2.2X\" ID doesn't exist")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   177
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   178
    def LoadMasterNode(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   179
        if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   180
            masterpath = os.path.join(self.Root, "%s_master.od"%netname)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   181
        else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   182
            masterpath = os.path.join(self.Root, "master.od")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   183
        if os.path.isfile(masterpath):
268
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   184
            result = self.Manager.OpenFileInCurrent(masterpath)
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   185
        else:
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   186
            result = self.Manager.CreateNewNode("MasterNode", 0x00, "master", "", "None", "", "heartbeat", ["DS302"])
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   187
        if not isinstance(result, types.IntType):
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   188
            return result
8a21eb5bdedc Bug on Load Project fixed
lbessard
parents: 265
diff changeset
   189
        return None
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   190
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   191
    def SaveMasterNode(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   192
        if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   193
            masterpath = os.path.join(self.Root, "%s_master.od"%netname)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   194
        else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   195
            masterpath = os.path.join(self.Root, "master.od")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   196
        if self.Manager.SaveCurrentInFile(masterpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   197
            return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   198
        else:
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   199
            return _("Fail to save Master Node")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   200
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   201
    def LoadSlaveNodes(self, netname = None):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   202
        cpjpath = os.path.join(self.Root, "nodelist.cpj")
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   203
        if os.path.isfile(cpjpath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   204
            try:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   205
                networks = eds_utils.ParseCPJFile(cpjpath)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   206
                network = None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   207
                if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   208
                    for net in networks:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   209
                        if net["Name"] == netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   210
                            network = net
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   211
                    self.NetworkName = netname
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   212
                elif len(networks) > 0:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   213
                    network = networks[0]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   214
                    self.NetworkName = network["Name"]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   215
                if network:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   216
                    for nodeid, node in network["Nodes"].items():
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   217
                        if node["Present"] == 1:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   218
                            result = self.AddSlaveNode(node["Name"], nodeid, node["DCFName"])
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   219
                            if result != None:
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   220
                                return result        
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   221
                self.Changed = False
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   222
            except SyntaxError, message:
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   223
                return _("Unable to load CPJ file\n%s")%message
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   224
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   225
    
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   226
    def SaveNodeList(self, netname = None):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   227
        try:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   228
            cpjpath = os.path.join(self.Root, "nodelist.cpj")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   229
            content = eds_utils.GenerateCPJContent(self)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   230
            if netname:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   231
                file = open(cpjpath, "a")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   232
            else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   233
                file = open(cpjpath, "w")
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   234
            file.write(content)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   235
            file.close()
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   236
            self.Changed = False
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   237
            return None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   238
        except:
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   239
            return _("Fail to save node list")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   240
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   241
    def GetSlaveNodeEntry(self, nodeid, index, subindex = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   242
        if nodeid in self.SlaveNodes.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   243
            self.SlaveNodes[nodeid]["Node"].SetNodeID(nodeid)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   244
            return self.SlaveNodes[nodeid]["Node"].GetEntry(index, subindex)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   245
        else:
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   246
            return _("Node 0x%2.2X doesn't exist")%nodeid
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   247
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   248
    def GetMasterNodeEntry(self, index, subindex = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   249
        return self.Manager.GetCurrentEntry(index, subindex)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   250
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   251
    def SetMasterNodeEntry(self, index, subindex = None, value = None):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   252
        self.Manager.SetCurrentEntry(index, subindex, value)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   253
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   254
    def GetOrderNumber(self, nodeid):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   255
        nodeindexes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   256
        nodeindexes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   257
        return nodeindexes.index(nodeid) + 1
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   258
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   259
    def GetNodeByOrder(self, order):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   260
        if order > 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   261
            nodeindexes = self.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   262
            nodeindexes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   263
            if order <= len(nodeindexes):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   264
                return self.SlaveNodes[nodeindexes[order - 1]]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   265
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   266
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   267
    def IsCurrentEntry(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   268
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   269
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   270
                return self.Manager.IsCurrentEntry(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   271
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   272
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   273
                if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   274
                    node.SetNodeID(self.CurrentSelected)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   275
                    return node.IsEntry(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   276
        return False
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   277
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   278
    def GetEntryInfos(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   279
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   280
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   281
                return self.Manager.GetEntryInfos(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   282
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   283
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   284
                if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   285
                    node.SetNodeID(self.CurrentSelected)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   286
                    return node.GetEntryInfos(index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   287
        return None
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   288
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   289
    def GetSubentryInfos(self, index, subindex):
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   290
        if self.CurrentSelected != None:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   291
            if self.CurrentSelected == 0:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   292
                return self.Manager.GetSubentryInfos(index, subindex)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   293
            else:
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   294
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   295
                if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   296
                    node.SetNodeID(self.CurrentSelected)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   297
                    return node.GetSubentryInfos(index, subindex)
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   298
        return None
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   299
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   300
    def GetCurrentValidIndexes(self, min, max):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   301
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   302
            if self.CurrentSelected == 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   303
                return self.Manager.GetCurrentValidIndexes(min, max)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   304
            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   305
                node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   306
                if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   307
                    node.SetNodeID(self.CurrentSelected)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   308
                    validindexes = []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   309
                    for index in node.GetIndexes():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   310
                        if min <= index <= max:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   311
                            validindexes.append((node.GetEntryName(index), index))
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   312
                    return validindexes
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   313
                else:
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   314
                    print _("Can't find node")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   315
        return []
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   316
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   317
    def GetCurrentEntryValues(self, index):
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   318
        if self.CurrentSelected != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   319
            node = self.SlaveNodes[self.CurrentSelected]["Node"]
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   320
            if node:
273
58b39f97e352 Some bugs fixed
lbessard
parents: 272
diff changeset
   321
                node.SetNodeID(self.CurrentSelected)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   322
                return self.Manager.GetNodeEntryValues(node, index)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   323
            else:
580
2ae92a99ac10 Adding support for internationalization
laurent
parents: 505
diff changeset
   324
                print _("Can't find node")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   325
        return [], []
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   326
    
327
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   327
    def AddToMasterDCF(self, node_id, index, subindex, size, value):
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   328
        # Adding DCF entry into Master node
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   329
        if not self.Manager.IsCurrentEntry(0x1F22):
337
9aab477d8a8a Bug on DCF edition fixed
lbessard
parents: 327
diff changeset
   330
            self.Manager.ManageEntriesOfCurrent([0x1F22], [])
327
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   331
        self.Manager.AddSubentriesToCurrent(0x1F22, 127)
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   332
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   333
        self.Manager.AddToDCF(node_id, index, subindex, size, value)
b904d9a99e28 Adding DCFEditor Dialog to networkedit
lbessard
parents: 293
diff changeset
   334
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   335
if __name__ == "__main__":
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   336
    from nodemanager import *
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   337
    import os, sys, shutil
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   338
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   339
    manager = NodeManager(sys.path[0])
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   340
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   341
    nodelist = NodeList(manager)
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   342
    
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   343
    result = nodelist.LoadProject("/home/laurent/test_nodelist")
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   344
    if result != None:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   345
        print result
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   346
    else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   347
        print "MasterNode :"
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   348
        manager.CurrentNode.Print()
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   349
        print 
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   350
        for nodeid, node in nodelist.SlaveNodes.items():
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   351
            print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid)
dac0f9b4e3f8 Adding program for network management
lbessard
parents:
diff changeset
   352
            node["Node"].Print()
242
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   353
            print
4864f7f01e1d Changes in networkedit for integration with beremiz
lbessard
parents: 239
diff changeset
   354