etisserant@0: #!/usr/bin/env python etisserant@0: # -*- coding: utf-8 -*- etisserant@0: etisserant@0: #This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@0: # etisserant@0: #Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD etisserant@0: # etisserant@0: #See COPYING file for copyrights details. etisserant@0: # etisserant@0: #This library is free software; you can redistribute it and/or etisserant@0: #modify it under the terms of the GNU Lesser General Public etisserant@0: #License as published by the Free Software Foundation; either etisserant@0: #version 2.1 of the License, or (at your option) any later version. etisserant@0: # etisserant@0: #This library is distributed in the hope that it will be useful, etisserant@0: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@0: #Lesser General Public License for more details. etisserant@0: # etisserant@0: #You should have received a copy of the GNU Lesser General Public etisserant@0: #License along with this library; if not, write to the Free Software etisserant@0: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: etisserant@0: from gnosis.xml.pickle import * etisserant@0: from gnosis.xml.pickle.util import setParanoia etisserant@0: setParanoia(0) etisserant@0: etisserant@0: from node import * etisserant@0: import xml_in, gen_cfile etisserant@0: etisserant@0: from types import * etisserant@0: import os, re etisserant@0: etisserant@0: UndoBufferLength = 20 etisserant@0: etisserant@0: type_model = re.compile('([\_A-Z]*)([0-9]*)') etisserant@0: range_model = re.compile('([\_A-Z]*)([0-9]*)\[([\-0-9]*)-([\-0-9]*)\]') etisserant@0: name_model = re.compile('(.*)\[(.*)\]') etisserant@0: etisserant@0: def IsOfType(object, typedef): etisserant@0: return type(object) == typedef etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Formating Name of an Entry etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Format the text given with the index and subindex defined etisserant@0: """ etisserant@0: etisserant@0: def StringFormat(text, idx, sub): etisserant@0: result = name_model.match(text) etisserant@0: if result: etisserant@0: format = result.groups() etisserant@0: return format[0]%eval(format[1]) etisserant@0: else: etisserant@0: return text etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Search in a Mapping Dictionary etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Return the index of the informations in the Object Dictionary in case of identical etisserant@0: indexes etisserant@0: """ etisserant@0: def FindIndex(index, mappingdictionary): etisserant@0: if index in mappingdictionary: etisserant@0: return index etisserant@0: else: etisserant@0: listpluri = [idx for idx in mappingdictionary.keys() if mappingdictionary[idx]["struct"] & OD_IdenticalIndexes] etisserant@0: listpluri.sort() etisserant@0: for idx in listpluri: etisserant@0: nb_max = mappingdictionary[idx]["nbmax"] etisserant@0: incr = mappingdictionary[idx]["incr"] etisserant@0: if idx < index < idx + incr * nb_max and (index - idx)%incr == 0: etisserant@0: return idx etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Return the index of the typename given by searching in mappingdictionary etisserant@0: """ etisserant@0: def FindTypeIndex(typename, mappingdictionary): etisserant@0: testdic = {} etisserant@0: for index, values in mappingdictionary.iteritems(): etisserant@0: if index < 0x1000: etisserant@0: testdic[values["name"]] = index etisserant@0: if typename in testdic: etisserant@0: return testdic[typename] etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Return the name of the type by searching in mappingdictionary etisserant@0: """ etisserant@0: def FindTypeName(typeindex, mappingdictionary): etisserant@0: if typeindex < 0x1000 and typeindex in mappingdictionary: etisserant@0: return mappingdictionary[typeindex]["name"] etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Return the default value of the type by searching in mappingdictionary etisserant@0: """ etisserant@0: def FindTypeDefaultValue(typeindex, mappingdictionary): etisserant@0: if typeindex < 0x1000 and typeindex in mappingdictionary: etisserant@0: return mappingdictionary[typeindex]["default"] etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Return the list of types defined in mappingdictionary etisserant@0: """ etisserant@0: def FindTypeList(mappingdictionary): etisserant@0: list = [] etisserant@0: for index in mappingdictionary.keys(): etisserant@0: if index < 0x1000: etisserant@0: list.append((index, mappingdictionary[index]["name"])) etisserant@0: return list etisserant@0: etisserant@0: """ etisserant@0: Return the name of an entry by searching in mappingdictionary etisserant@0: """ etisserant@0: def FindEntryName(index, mappingdictionary): etisserant@0: base_index = FindIndex(index, mappingdictionary) etisserant@0: if base_index: etisserant@0: infos = mappingdictionary[base_index] etisserant@0: if infos["struct"] & OD_IdenticalIndexes: etisserant@0: return StringFormat(infos["name"], (index - base_index) / infos["incr"] + 1, 0) etisserant@0: else: etisserant@0: return infos["name"] etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Return the informations of one entry by searching in mappingdictionary etisserant@0: """ etisserant@0: def FindEntryInfos(index, mappingdictionary): etisserant@0: base_index = FindIndex(index, mappingdictionary) etisserant@0: if base_index: etisserant@0: copy = mappingdictionary[base_index].copy() etisserant@0: if copy["struct"] & OD_IdenticalIndexes: etisserant@0: copy["name"] = StringFormat(copy["name"], (index - base_index) / copy["incr"] + 1, 0) etisserant@0: copy.pop("values") etisserant@0: return copy etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Return the informations of one subentry of an entry by searching in mappingdictionary etisserant@0: """ etisserant@0: def FindSubentryInfos(index, subIndex, mappingdictionary): etisserant@0: base_index = FindIndex(index, mappingdictionary) etisserant@0: if base_index: etisserant@0: struct = mappingdictionary[base_index]["struct"] etisserant@0: if struct & OD_Subindex: etisserant@0: if struct & OD_IdenticalSubindexes: etisserant@0: if struct & OD_IdenticalIndexes: etisserant@0: incr = mappingdictionary[base_index]["incr"] etisserant@0: else: etisserant@0: incr = 1 etisserant@0: if subIndex == 0: etisserant@0: return mappingdictionary[base_index]["values"][0].copy() etisserant@0: elif 0 < subIndex <= mappingdictionary[base_index]["values"][1]["nbmax"]: etisserant@0: copy = mappingdictionary[base_index]["values"][1].copy() etisserant@0: copy["name"] = StringFormat(copy["name"], (index - base_index) / incr + 1, subIndex) etisserant@0: return copy etisserant@0: elif struct & OD_MultipleSubindexes and 0 <= subIndex < len(mappingdictionary[base_index]["values"]): etisserant@0: return mappingdictionary[base_index]["values"][subIndex].copy() etisserant@0: elif subIndex == 0: etisserant@0: return mappingdictionary[base_index]["values"][0].copy() etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Return the list of variables that can be mapped defined in mappingdictionary etisserant@0: """ etisserant@0: def FindMapVariableList(mappingdictionary, Manager): etisserant@0: list = [] etisserant@0: for index in mappingdictionary.iterkeys(): etisserant@0: if Manager.IsCurrentEntry(index): etisserant@0: for subIndex, values in enumerate(mappingdictionary[index]["values"]): etisserant@0: if mappingdictionary[index]["values"][subIndex]["pdo"]: etisserant@0: infos = Manager.GetEntryInfos(mappingdictionary[index]["values"][subIndex]["type"]) etisserant@0: if mappingdictionary[index]["struct"] & OD_IdenticalSubindexes: etisserant@0: values = Manager.GetCurrentEntry(index) lbessard@30: for i in xrange(len(values) - 1): lbessard@30: list.append((index, i + 1, infos["size"], StringFormat(mappingdictionary[index]["values"][subIndex]["name"],1,i+1))) etisserant@0: else: etisserant@0: list.append((index, subIndex, infos["size"], mappingdictionary[index]["values"][subIndex]["name"])) etisserant@0: return list etisserant@0: etisserant@0: """ etisserant@0: Return the list of mandatory indexes defined in mappingdictionary etisserant@0: """ etisserant@0: def FindMandatoryIndexes(mappingdictionary): etisserant@0: list = [] etisserant@0: for index in mappingdictionary.iterkeys(): etisserant@0: if index >= 0x1000 and mappingdictionary[index]["need"]: etisserant@0: list.append(index) etisserant@0: return list etisserant@0: etisserant@0: etisserant@0: """ etisserant@0: Class implementing a buffer of changes made on the current editing Object Dictionary etisserant@0: """ etisserant@0: etisserant@0: class UndoBuffer: etisserant@0: etisserant@0: """ etisserant@0: Constructor initialising buffer etisserant@0: """ etisserant@0: def __init__(self, currentstate, issaved = False): etisserant@0: self.Buffer = [] etisserant@0: self.CurrentIndex = -1 etisserant@0: self.MinIndex = -1 etisserant@0: self.MaxIndex = -1 etisserant@0: # if current state is defined etisserant@0: if currentstate: etisserant@0: self.CurrentIndex = 0 etisserant@0: self.MinIndex = 0 etisserant@0: self.MaxIndex = 0 etisserant@0: # Initialising buffer with currentstate at the first place etisserant@0: for i in xrange(UndoBufferLength): etisserant@0: if i == 0: etisserant@0: self.Buffer.append(currentstate) etisserant@0: else: etisserant@0: self.Buffer.append(None) etisserant@0: # Initialising index of state saved etisserant@0: if issaved: etisserant@0: self.LastSave = 0 etisserant@0: else: etisserant@0: self.LastSave = -1 etisserant@0: etisserant@0: """ etisserant@0: Add a new state in buffer etisserant@0: """ etisserant@0: def Buffering(self, currentstate): etisserant@0: self.CurrentIndex = (self.CurrentIndex + 1) % UndoBufferLength etisserant@0: self.Buffer[self.CurrentIndex] = currentstate etisserant@0: # Actualising buffer limits etisserant@0: self.MaxIndex = self.CurrentIndex etisserant@0: if self.MinIndex == self.CurrentIndex: etisserant@0: # If the removed state was the state saved, there is no state saved in the buffer etisserant@0: if self.LastSave == self.MinIndex: etisserant@0: self.LastSave = -1 etisserant@0: self.MinIndex = (self.MinIndex + 1) % UndoBufferLength etisserant@0: self.MinIndex = max(self.MinIndex, 0) etisserant@0: etisserant@0: """ etisserant@0: Return current state of buffer etisserant@0: """ etisserant@0: def Current(self): etisserant@0: return self.Buffer[self.CurrentIndex] etisserant@0: etisserant@0: """ etisserant@0: Change current state to previous in buffer and return new current state etisserant@0: """ etisserant@0: def Previous(self): etisserant@0: if self.CurrentIndex != self.MinIndex: etisserant@0: self.CurrentIndex = (self.CurrentIndex - 1) % UndoBufferLength etisserant@0: return self.Buffer[self.CurrentIndex] etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Change current state to next in buffer and return new current state etisserant@0: """ etisserant@0: def Next(self): etisserant@0: if self.CurrentIndex != self.MaxIndex: etisserant@0: self.CurrentIndex = (self.CurrentIndex + 1) % UndoBufferLength etisserant@0: return self.Buffer[self.CurrentIndex] etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Return True if current state is the first in buffer etisserant@0: """ etisserant@0: def IsFirst(self): etisserant@0: return self.CurrentIndex == self.MinIndex etisserant@0: etisserant@0: """ etisserant@0: Return True if current state is the last in buffer etisserant@0: """ etisserant@0: def IsLast(self): etisserant@0: return self.CurrentIndex == self.MaxIndex etisserant@0: etisserant@0: """ etisserant@0: Note that current state is saved etisserant@0: """ etisserant@0: def CurrentSaved(self): etisserant@0: self.LastSave = self.CurrentIndex etisserant@0: etisserant@0: """ etisserant@0: Return True if current state is saved etisserant@0: """ etisserant@0: def IsCurrentSaved(self): etisserant@0: return self.LastSave == self.CurrentIndex etisserant@0: etisserant@0: etisserant@0: etisserant@0: """ etisserant@0: Class which control the operations made on the node and answer to view requests etisserant@0: """ etisserant@0: etisserant@0: class NodeManager: etisserant@0: etisserant@0: """ etisserant@0: Constructor etisserant@0: """ etisserant@0: def __init__(self): etisserant@0: self.LastNewIndex = 0 etisserant@0: self.FilePaths = [] etisserant@0: self.FileNames = [] etisserant@0: self.NodeIndex = -1 etisserant@0: self.CurrentNode = None etisserant@0: self.UndoBuffers = [] etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Type and Map Variable Lists etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Generate the list of types defined for the current node etisserant@0: """ etisserant@0: def GenerateTypeList(self): etisserant@0: self.TypeList = "" etisserant@0: self.TypeTranslation = {} etisserant@0: list = self.GetTypeList() etisserant@0: sep = "" etisserant@0: for index, name in list: etisserant@0: self.TypeList += "%s%s"%(sep,name) etisserant@0: self.TypeTranslation[name] = index etisserant@0: sep = "," etisserant@0: etisserant@0: """ etisserant@0: Generate the list of variables that can be mapped for the current node etisserant@0: """ etisserant@0: def GenerateMapList(self): etisserant@0: self.MapList = "None" etisserant@0: self.NameTranslation = {"None" : "00000000"} etisserant@0: self.MapTranslation = {"00000000" : "None"} etisserant@0: list = self.GetMapVariableList() etisserant@0: for index, subIndex, size, name in list: etisserant@0: self.MapList += ",%s"%name etisserant@0: map = "%04X%02X%02X"%(index,subIndex,size) etisserant@0: self.NameTranslation[name] = map etisserant@0: self.MapTranslation[map] = name etisserant@0: etisserant@0: """ etisserant@0: Return the list of types defined for the current node etisserant@0: """ etisserant@0: def GetCurrentTypeList(self): etisserant@0: return self.TypeList etisserant@0: etisserant@0: """ etisserant@0: Return the list of variables that can be mapped for the current node etisserant@0: """ etisserant@0: def GetCurrentMapList(self): etisserant@0: return self.MapList etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Create Load and Save Functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Create a new node and add a new buffer for storing it etisserant@0: """ etisserant@0: def CreateNewNode(self, name, id, type, profile, filepath, NMT, options): etisserant@0: # Create a new node etisserant@0: node = Node() etisserant@0: # Try to load profile given etisserant@0: result = self.LoadProfile(profile, filepath, node) etisserant@0: if not IsOfType(result, StringType): etisserant@0: # if success, initialising node etisserant@0: self.CurrentNode = node etisserant@0: self.CurrentNode.SetNodeName(name) etisserant@0: self.CurrentNode.SetNodeID(id) etisserant@0: self.CurrentNode.SetNodeType(type) etisserant@0: AddIndexList = self.GetMandatoryIndexes() etisserant@0: if NMT == "NodeGuarding": etisserant@0: AddIndexList.extend([0x100C, 0x100D]) etisserant@0: elif NMT == "Heartbeat": etisserant@0: AddIndexList.append(0x1017) etisserant@0: for option in options: etisserant@0: if option == "DS302": etisserant@0: # Charging DS-302 profile if choosen by user etisserant@0: if os.path.isfile("config/DS-302.prf"): etisserant@0: try: etisserant@0: Mapping = {} etisserant@0: AddMenuEntries = [] etisserant@0: execfile("config/DS-302.prf") etisserant@0: self.CurrentNode.SetDS302Profile(Mapping) etisserant@0: self.CurrentNode.ExtendSpecificMenu(AddMenuEntries) etisserant@0: except: etisserant@0: return "Problem with DS-302! Syntax Error." etisserant@0: else: etisserant@0: return "Couldn't find DS-302 in 'config' folder!" etisserant@0: elif option == "GenSYNC": etisserant@0: AddIndexList.extend([0x1005, 0x1006]) etisserant@0: elif option == "Emergency": etisserant@0: AddIndexList.append(0x1014) etisserant@0: elif option == "SaveConfig": etisserant@0: AddIndexList.extend([0x1010, 0x1011, 0x1020]) etisserant@0: elif option == "StoreEDS": etisserant@0: AddIndexList.extend([0x1021, 0x1022]) etisserant@0: # Add a new buffer etisserant@0: self.AddNodeBuffer() etisserant@0: self.SetCurrentFilePath("") etisserant@0: # Add Mandatory indexes etisserant@0: self.ManageEntriesOfCurrent(AddIndexList, []) etisserant@0: # Regenerate lists etisserant@0: self.GenerateTypeList() etisserant@0: self.GenerateMapList() etisserant@0: return True etisserant@0: else: etisserant@0: return result etisserant@0: etisserant@0: """ etisserant@0: Load a profile in node etisserant@0: """ etisserant@0: def LoadProfile(self, profile, filepath, node): etisserant@0: if profile != "None": etisserant@0: # Try to charge the profile given etisserant@0: try: etisserant@0: execfile(filepath) etisserant@0: node.SetProfileName(profile) etisserant@0: node.SetProfile(Mapping) etisserant@0: node.SetSpecificMenu(AddMenuEntries) etisserant@0: return True etisserant@0: except: etisserant@0: return "Bad OD Profile file!\nSyntax Error." etisserant@0: else: etisserant@0: # Default profile etisserant@0: node.SetProfileName("None") etisserant@0: node.SetProfile({}) etisserant@0: node.SetSpecificMenu([]) etisserant@0: return True etisserant@0: etisserant@0: """ etisserant@0: Open a file and store it in a new buffer etisserant@0: """ etisserant@0: def OpenFileInCurrent(self, filepath): etisserant@0: # Open and load file etisserant@0: file = open(filepath, "r") etisserant@0: node = load(file) etisserant@0: file.close() etisserant@0: self.CurrentNode = node etisserant@0: # Add a new buffer and defining current state etisserant@0: self.AddNodeBuffer(self.CurrentNode.Copy(), True) etisserant@0: self.SetCurrentFilePath(filepath) etisserant@0: # Regenerate lists etisserant@0: self.GenerateTypeList() etisserant@0: self.GenerateMapList() etisserant@0: return True etisserant@0: etisserant@0: """ etisserant@0: Save current node in a file etisserant@0: """ etisserant@0: def SaveCurrentInFile(self, filepath = None): etisserant@0: # if no filepath given, verify if current node has a filepath defined etisserant@0: if not filepath: etisserant@0: filepath = self.GetCurrentFilePath() etisserant@0: if filepath == "": etisserant@0: return False etisserant@0: # Save node in file etisserant@0: file = open(filepath, "w") etisserant@0: dump(self.CurrentNode, file) etisserant@0: file.close() etisserant@0: self.SetCurrentFilePath(filepath) etisserant@0: # Update saved state in buffer etisserant@0: self.UndoBuffers[self.NodeIndex].CurrentSaved() etisserant@0: return True etisserant@0: etisserant@0: """ etisserant@0: Close current state etisserant@0: """ etisserant@0: def CloseCurrent(self, ignore = False): etisserant@0: # Verify if it's not forced that the current node is saved before closing it etisserant@0: if self.UndoBuffers[self.NodeIndex].IsCurrentSaved() or ignore: etisserant@0: self.RemoveNodeBuffer(self.NodeIndex) etisserant@0: return True etisserant@0: return False etisserant@0: etisserant@0: """ etisserant@0: Import a xml file and store it in a new buffer if no node edited etisserant@0: """ etisserant@0: def ImportCurrentFromFile(self, filepath): etisserant@0: # Generate node from definition in a xml file etisserant@0: node = xml_in.GenerateNode(filepath, self) etisserant@0: if node: etisserant@0: self.CurrentNode = node etisserant@0: self.GenerateTypeList() etisserant@0: self.GenerateMapList() etisserant@0: if len(self.UndoBuffers) == 0: etisserant@0: self.AddNodeBuffer() etisserant@0: self.SetCurrentFilePath("") etisserant@0: self.BufferCurrentNode() etisserant@0: return result etisserant@0: etisserant@0: """ etisserant@0: Build the C definition of Object Dictionary for current node etisserant@0: """ etisserant@0: def ExportCurrentToFile(self, filepath): etisserant@0: return gen_cfile.GenerateFile(filepath, self) etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Add Entries to Current Functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Add the specified number of subentry for the given entry. Verify that total etisserant@0: number of subentry (except 0) doesn't exceed nbmax defined etisserant@0: """ etisserant@0: def AddSubentriesToCurrent(self, index, number): etisserant@0: # Informations about entry etisserant@0: length = self.CurrentNode.GetEntry(index, 0) etisserant@0: infos = self.GetEntryInfos(index) etisserant@0: subentry_infos = self.GetSubentryInfos(index, 1) etisserant@0: # Get default value for subindex etisserant@0: if "default" in subentry_infos: etisserant@0: default = subentry_infos["default"] etisserant@0: else: etisserant@0: default = self.GetTypeDefaultValue(subentry_infos["type"]) etisserant@0: # First case entry is record etisserant@0: if infos["struct"] & OD_IdenticalSubindexes: etisserant@0: for i in xrange(1, min(number,subentry_infos["nbmax"]-length) + 1): etisserant@0: self.CurrentNode.AddEntry(index, length + i, default) etisserant@0: self.BufferCurrentNode() etisserant@0: # Second case entry is array, only possible for manufacturer specific etisserant@0: elif infos["struct"] & OD_MultipleSubindexes and 0x2000 <= index <= 0x5FFF: etisserant@0: values = {"name" : "Undefined", "type" : 5, "access" : "rw", "pdo" : True} etisserant@0: for i in xrange(1, min(number,0xFE-length) + 1): etisserant@0: self.CurrentNode.AddMappingEntry(index, length + i, values = values.copy()) etisserant@0: self.CurrentNode.AddEntry(index, length + i, 0) etisserant@0: self.BufferCurrentNode() etisserant@0: etisserant@0: """ etisserant@0: Remove the specified number of subentry for the given entry. Verify that total etisserant@0: number of subentry (except 0) isn't less than 1 etisserant@0: """ etisserant@0: def RemoveSubentriesFromCurrent(self, index, number): etisserant@0: # Informations about entry etisserant@0: infos = self.GetEntryInfos(index) etisserant@0: length = self.CurrentNode.GetEntry(index, 0) etisserant@0: # Entry is a record, or is an array of manufacturer specific etisserant@0: if infos["struct"] & OD_IdenticalSubindexes or 0x2000 <= index <= 0x5FFF and infos["struct"] & OD_IdenticalSubindexes: etisserant@0: for i in xrange(min(number, length - 1)): etisserant@0: self.RemoveCurrentVariable(index, length - i) etisserant@0: self.BufferCurrentNode() etisserant@0: etisserant@0: """ etisserant@0: Add a SDO Server to current node etisserant@0: """ etisserant@0: def AddSDOServerToCurrent(self): etisserant@0: # An SDO Server is already defined at index 0x1200 etisserant@0: if self.CurrentNode.IsEntry(0x1200): etisserant@0: indexlist = [self.GetLineFromIndex(0x1201)] etisserant@0: if None not in indexlist: etisserant@0: self.ManageEntriesOfCurrent(indexlist, []) etisserant@0: # Add an SDO Server at index 0x1200 etisserant@0: else: etisserant@0: self.ManageEntriesOfCurrent([0x1200], []) etisserant@0: etisserant@0: """ etisserant@0: Add a SDO Server to current node etisserant@0: """ etisserant@0: def AddSDOClientToCurrent(self): etisserant@0: indexlist = [self.GetLineFromIndex(0x1280)] etisserant@0: if None not in indexlist: etisserant@0: self.ManageEntriesOfCurrent(indexlist, []) etisserant@0: etisserant@0: """ etisserant@0: Add a Transmit PDO to current node etisserant@0: """ etisserant@0: def AddPDOTransmitToCurrent(self): etisserant@0: indexlist = [self.GetLineFromIndex(0x1800),self.GetLineFromIndex(0x1A00)] etisserant@0: if None not in indexlist: etisserant@0: self.ManageEntriesOfCurrent(indexlist, []) etisserant@0: etisserant@0: """ etisserant@0: Add a Receive PDO to current node etisserant@0: """ etisserant@0: def AddPDOReceiveToCurrent(self): etisserant@0: indexlist = [self.GetLineFromIndex(0x1400),self.GetLineFromIndex(0x1600)] etisserant@0: if None not in indexlist: etisserant@0: self.ManageEntriesOfCurrent(indexlist, []) etisserant@0: etisserant@0: """ etisserant@0: Add a list of entries defined in profile for menu item selected to current node etisserant@0: """ etisserant@0: def AddSpecificEntryToCurrent(self, menuitem): etisserant@0: indexlist = [] etisserant@0: for menu, indexes in self.CurrentNode.GetSpecificMenu(): etisserant@0: if menuitem == menu: etisserant@0: for index in indexes: etisserant@0: indexlist.append(self.GetLineFromIndex(index)) etisserant@0: if None not in indexlist: etisserant@0: self.ManageEntriesOfCurrent(indexlist, []) etisserant@0: etisserant@0: """ etisserant@0: Search the first index available for a pluri entry from base_index etisserant@0: """ etisserant@0: def GetLineFromIndex(self, base_index): etisserant@0: found = False etisserant@0: index = base_index etisserant@0: infos = self.GetEntryInfos(base_index) etisserant@0: while index < base_index + infos["incr"]*infos["nbmax"] and not found: etisserant@0: if not self.CurrentNode.IsEntry(index): etisserant@0: found = True etisserant@0: else: etisserant@0: index += infos["incr"] etisserant@0: if found: etisserant@0: return index etisserant@0: return None etisserant@0: etisserant@0: """ etisserant@0: Add entries specified in addinglist and remove entries specified in removinglist etisserant@0: """ etisserant@0: def ManageEntriesOfCurrent(self, addinglist, removinglist): etisserant@0: # Add all the entries in addinglist etisserant@0: for index in addinglist: etisserant@0: infos = self.GetEntryInfos(index) etisserant@0: if infos["struct"] & OD_MultipleSubindexes: etisserant@0: # First case entry is a record etisserant@0: if infos["struct"] & OD_IdenticalSubindexes: etisserant@0: subentry_infos = self.GetSubentryInfos(index, 1) etisserant@0: if "default" in subentry_infos: etisserant@0: default = subentry_infos["default"] etisserant@0: else: etisserant@0: default = self.GetTypeDefaultValue(subentry_infos["type"]) etisserant@0: self.CurrentNode.AddEntry(index, 1, default) etisserant@0: # Second case entry is a record etisserant@0: else: etisserant@0: i = 1 etisserant@0: subentry_infos = self.GetSubentryInfos(index, i) etisserant@0: while subentry_infos: etisserant@0: if "default" in subentry_infos: etisserant@0: default = subentry_infos["default"] etisserant@0: else: etisserant@0: default = self.GetTypeDefaultValue(subentry_infos["type"]) etisserant@0: self.CurrentNode.AddEntry(index, i, default) etisserant@0: i += 1 etisserant@0: subentry_infos = self.GetSubentryInfos(index, i) etisserant@0: # Third case entry is a record etisserant@0: else: etisserant@0: subentry_infos = self.GetSubentryInfos(index, 0) etisserant@0: if "default" in subentry_infos: etisserant@0: default = subentry_infos["default"] etisserant@0: else: etisserant@0: default = self.GetTypeDefaultValue(subentry_infos["type"]) etisserant@0: self.CurrentNode.AddEntry(index, 0, default) etisserant@0: # Remove all the entries in removinglist etisserant@0: for index in removinglist: etisserant@0: self.RemoveCurrentVariable(index) etisserant@0: self.BufferCurrentNode() etisserant@0: etisserant@0: """ etisserant@0: Remove an entry from current node. Analize the index to perform the correct etisserant@0: method etisserant@0: """ etisserant@0: def RemoveCurrentVariable(self, index, subIndex = None): etisserant@0: Mappings = self.CurrentNode.GetMappings() etisserant@0: if index < 0x1000 and subIndex == None: etisserant@0: type = self.CurrentNode.GetEntry(index, 1) etisserant@0: for i in Mappings[-1]: etisserant@0: for value in Mappings[-1][i]["values"]: etisserant@0: if value["type"] == index: etisserant@0: value["type"] = type etisserant@0: self.CurrentNode.RemoveMappingEntry(index) etisserant@0: self.CurrentNode.RemoveEntry(index) etisserant@0: elif index == 0x1200 and subIndex == None: etisserant@0: self.CurrentNode.RemoveEntry(0x1200) etisserant@0: elif 0x1201 <= index <= 0x127F and subIndex == None: etisserant@0: self.CurrentNode.RemoveLine(index, 0x127F) etisserant@0: elif 0x1280 <= index <= 0x12FF and subIndex == None: etisserant@0: self.CurrentNode.RemoveLine(index, 0x12FF) etisserant@0: elif 0x1400 <= index <= 0x15FF or 0x1600 <= index <= 0x17FF and subIndex == None: etisserant@0: if 0x1600 <= index <= 0x17FF and subIndex == None: etisserant@0: index -= 0x200 etisserant@0: self.CurrentNode.RemoveLine(index, 0x15FF) etisserant@0: self.CurrentNode.RemoveLine(index + 0x200, 0x17FF) etisserant@0: elif 0x1800 <= index <= 0x19FF or 0x1A00 <= index <= 0x1BFF and subIndex == None: etisserant@0: if 0x1A00 <= index <= 0x1BFF: etisserant@0: index -= 0x200 etisserant@0: self.CurrentNode.RemoveLine(index, 0x19FF) etisserant@0: self.CurrentNode.RemoveLine(index + 0x200, 0x1BFF) etisserant@0: else: etisserant@0: found = False etisserant@0: for menu,list in self.CurrentNode.GetSpecificMenu(): etisserant@0: for i in list: etisserant@0: iinfos = self.GetEntryInfos(i) etisserant@0: indexes = [i + incr * iinfos["incr"] for incr in xrange(iinfos["nbmax"])] etisserant@0: if index in indexes: etisserant@0: found = True etisserant@0: diff = index - i etisserant@0: for j in list: etisserant@0: jinfos = self.GetEntryInfos(j) etisserant@0: self.CurrentNode.RemoveLine(j + diff, j + jinfos["incr"]*jinfos["nbmax"], jinfos["incr"]) etisserant@0: self.CurrentNode.RemoveMapVariable(index, subIndex) etisserant@0: if not found: etisserant@0: infos = self.GetEntryInfos(index) etisserant@0: if not infos["need"]: etisserant@0: self.CurrentNode.RemoveEntry(index, subIndex) etisserant@0: if index in Mappings[-1]: etisserant@0: self.CurrentNode.RemoveMappingEntry(index, subIndex) etisserant@0: self.GenerateMapList() etisserant@0: etisserant@0: def AddMapVariableToCurrent(self, index, name, struct, number): etisserant@0: if 0x2000 <= index <= 0x5FFF: etisserant@0: if not self.CurrentNode.IsEntry(index): etisserant@0: self.CurrentNode.AddMappingEntry(index, name = name, struct = struct) etisserant@0: if struct == var: etisserant@0: values = {"name" : name, "type" : 5, "access" : "rw", "pdo" : True} etisserant@0: self.CurrentNode.AddMappingEntry(index, 0, values = values) etisserant@0: self.CurrentNode.AddEntry(index, 0, 0) etisserant@0: else: etisserant@0: values = {"name" : "Number of Entries", "type" : 2, "access" : "ro", "pdo" : False} etisserant@0: self.CurrentNode.AddMappingEntry(index, 0, values = values) etisserant@0: if struct == rec: etisserant@0: values = {"name" : name + " %d[(sub)]", "type" : 5, "access" : "rw", "pdo" : True, "nbmax" : 0xFE} etisserant@0: self.CurrentNode.AddMappingEntry(index, 1, values = values) etisserant@0: for i in xrange(number): etisserant@0: self.CurrentNode.AddEntry(index, i + 1, 0) etisserant@0: else: etisserant@0: for i in xrange(number): etisserant@0: values = {"name" : "Undefined", "type" : 5, "access" : "rw", "pdo" : True} etisserant@0: self.CurrentNode.AddMappingEntry(index, i + 1, values = values) etisserant@0: self.CurrentNode.AddEntry(index, i + 1, 0) etisserant@0: self.GenerateMapList() etisserant@0: self.BufferCurrentNode() etisserant@0: return None etisserant@0: else: etisserant@0: return "Index 0x%04X already defined!"%index etisserant@0: else: etisserant@0: return "Index 0x%04X isn't a valid index for Map Variable!"%index etisserant@0: etisserant@0: def AddUserTypeToCurrent(self, type, min, max, length): etisserant@0: index = 0xA0 etisserant@0: while index < 0x100 and self.CurrentNode.IsEntry(index): etisserant@0: index += 1 etisserant@0: if index < 0x100: etisserant@0: customisabletypes = self.GetCustomisableTypes() etisserant@0: name, valuetype = customisabletypes[type] etisserant@0: size = self.GetEntryInfos(type)["size"] etisserant@0: default = self.GetTypeDefaultValue(type) etisserant@0: if valuetype == 0: etisserant@0: self.CurrentNode.AddMappingEntry(index, name = "%s[%d-%d]"%(name, min, max), struct = 3, size = size, default = default) etisserant@0: self.CurrentNode.AddMappingEntry(index, 0, values = {"name" : "Number of Entries", "type" : 0x02, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.AddMappingEntry(index, 1, values = {"name" : "Type", "type" : 0x02, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.AddMappingEntry(index, 2, values = {"name" : "Minimum Value", "type" : type, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.AddMappingEntry(index, 3, values = {"name" : "Maximum Value", "type" : type, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.AddEntry(index, 1, type) etisserant@0: self.CurrentNode.AddEntry(index, 2, min) etisserant@0: self.CurrentNode.AddEntry(index, 3, max) etisserant@0: elif valuetype == 1: etisserant@0: self.CurrentNode.AddMappingEntry(index, name = "%s%d"%(name, length), struct = 3, size = length * size, default = default) etisserant@0: self.CurrentNode.AddMappingEntry(index, 0, values = {"name" : "Number of Entries", "type" : 0x02, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.AddMappingEntry(index, 1, values = {"name" : "Type", "type" : 0x02, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.AddMappingEntry(index, 2, values = {"name" : "Length", "type" : 0x02, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.AddEntry(index, 1, type) etisserant@0: self.CurrentNode.AddEntry(index, 2, length) etisserant@0: self.GenerateTypeList() etisserant@0: self.BufferCurrentNode() etisserant@0: return None etisserant@0: else: etisserant@0: return "Too many User Types have already been defined!" etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Modify Entry and Mapping Functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: def SetCurrentEntryCallbacks(self, index, value): etisserant@0: if self.CurrentNode and self.CurrentNode.IsEntry(index): etisserant@0: entry_infos = self.GetEntryInfos(index) etisserant@0: if "callback" not in entry_infos: etisserant@0: self.CurrentNode.SetParamsEntry(index, None, callback = value) etisserant@0: self.BufferCurrentNode() etisserant@0: etisserant@0: def SetCurrentEntry(self, index, subIndex, value, name, editor): etisserant@0: if self.CurrentNode and self.CurrentNode.IsEntry(index): etisserant@0: if name == "value": etisserant@0: if editor == "map": etisserant@0: value = eval("0x%s"%self.NameTranslation[value]) etisserant@0: self.CurrentNode.SetEntry(index, subIndex, value) etisserant@0: elif editor == "bool": etisserant@0: value = value == "True" etisserant@0: self.CurrentNode.SetEntry(index, subIndex, value) etisserant@0: else: etisserant@0: subentry_infos = self.GetSubentryInfos(index, subIndex) lbessard@30: type = subentry_infos["type"] etisserant@0: dic = {} etisserant@0: for typeindex, typevalue in CustomisableTypes: etisserant@0: dic[typeindex] = typevalue lbessard@30: if type not in dic: lbessard@30: type = self.CurrentNode.GetEntry(type)[1] lbessard@30: if dic[type] == 0: etisserant@0: try: etisserant@0: value = eval(value, {}) etisserant@0: self.CurrentNode.SetEntry(index, subIndex, value) etisserant@0: except: etisserant@0: pass etisserant@0: else: etisserant@0: self.CurrentNode.SetEntry(index, subIndex, value) etisserant@0: elif name in ["comment", "save"]: etisserant@0: if editor == "option": etisserant@0: value = value == "Yes" etisserant@0: if name == "save": etisserant@0: self.CurrentNode.SetParamsEntry(index, subIndex, save = value) etisserant@0: elif name == "comment": etisserant@0: self.CurrentNode.SetParamsEntry(index, subIndex, comment = value) etisserant@0: else: etisserant@0: if editor == "type": etisserant@0: value = self.TypeTranslation[value] lbessard@63: size = self.GetEntryInfos(value)["size"] lbessard@63: self.CurrentNode.UpdateMapVariable(index, subIndex, size) etisserant@0: elif editor == "access": etisserant@0: dic = {} etisserant@0: for abbrev,access in AccessType.iteritems(): etisserant@0: dic[access] = abbrev etisserant@0: value = dic[value] etisserant@0: self.CurrentNode.SetMappingEntry(index, subIndex, values = {name : value}) lbessard@63: if name == "name" or editor == "type": etisserant@0: self.GenerateMapList() etisserant@0: self.BufferCurrentNode() etisserant@0: etisserant@0: def SetCurrentEntryName(self, index, name): etisserant@0: self.CurrentNode.SetMappingEntry(index, name=name) etisserant@0: self.BufferCurrentNode() etisserant@0: etisserant@0: def SetCurrentUserType(self, index, type, min, max, length): etisserant@0: customisabletypes = self.GetCustomisableTypes() etisserant@0: values, valuetype = self.GetCustomisedTypeValues(index) etisserant@0: name, new_valuetype = customisabletypes[type] etisserant@0: size = self.GetEntryInfos(type)["size"] etisserant@0: default = self.GetTypeDefaultValue(type) etisserant@0: if new_valuetype == 0: etisserant@0: self.CurrentNode.SetMappingEntry(index, name = "%s[%d-%d]"%(name, min, max), struct = 3, size = size, default = default) etisserant@0: if valuetype == 1: etisserant@0: self.CurrentNode.SetMappingEntry(index, 2, values = {"name" : "Minimum Value", "type" : type, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.AddMappingEntry(index, 3, values = {"name" : "Maximum Value", "type" : type, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.SetEntry(index, 1, type) etisserant@0: self.CurrentNode.SetEntry(index, 2, min) etisserant@0: if valuetype == 1: etisserant@0: self.CurrentNode.AddEntry(index, 3, max) etisserant@0: else: etisserant@0: self.CurrentNode.SetEntry(index, 3, max) etisserant@0: elif new_valuetype == 1: etisserant@0: self.CurrentNode.SetMappingEntry(index, name = "%s%d"%(name, length), struct = 3, size = size, default = default) etisserant@0: if valuetype == 0: etisserant@0: self.CurrentNode.SetMappingEntry(index, 2, values = {"name" : "Length", "type" : 0x02, "access" : "ro", "pdo" : False}) etisserant@0: self.CurrentNode.RemoveMappingEntry(index, 3) etisserant@0: self.CurrentNode.SetEntry(index, 1, type) etisserant@0: self.CurrentNode.SetEntry(index, 2, length) etisserant@0: if valuetype == 0: etisserant@0: self.CurrentNode.RemoveEntry(index, 3) etisserant@0: self.BufferCurrentNode() etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Current Buffering Management Functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: def BufferCurrentNode(self): etisserant@0: self.UndoBuffers[self.NodeIndex].Buffering(self.CurrentNode.Copy()) etisserant@0: etisserant@0: def CurrentIsSaved(self): etisserant@0: return self.UndoBuffers[self.NodeIndex].IsCurrentSaved() etisserant@0: etisserant@0: def OneFileHasChanged(self): etisserant@0: result = False etisserant@0: for buffer in self.UndoBuffers: etisserant@0: result |= not buffer.IsCurrentSaved() etisserant@0: return result etisserant@0: etisserant@0: def GetBufferNumber(self): etisserant@0: return len(self.UndoBuffers) etisserant@0: etisserant@0: def LoadCurrentPrevious(self): etisserant@0: self.CurrentNode = self.UndoBuffers[self.NodeIndex].Previous().Copy() etisserant@0: etisserant@0: def LoadCurrentNext(self): etisserant@0: self.CurrentNode = self.UndoBuffers[self.NodeIndex].Next().Copy() etisserant@0: etisserant@0: def AddNodeBuffer(self, currentstate = None, issaved = False): etisserant@0: self.NodeIndex = len(self.UndoBuffers) etisserant@0: self.UndoBuffers.append(UndoBuffer(currentstate, issaved)) etisserant@0: self.FilePaths.append("") etisserant@0: self.FileNames.append("") etisserant@0: etisserant@0: def ChangeCurrentNode(self, index): etisserant@0: if index < len(self.UndoBuffers): etisserant@0: self.NodeIndex = index etisserant@0: self.CurrentNode = self.UndoBuffers[self.NodeIndex].Current().Copy() etisserant@0: self.GenerateTypeList() etisserant@0: self.GenerateMapList() etisserant@0: etisserant@0: def RemoveNodeBuffer(self, index): etisserant@0: self.UndoBuffers.pop(index) etisserant@0: self.FilePaths.pop(index) etisserant@0: self.FileNames.pop(index) etisserant@0: self.NodeIndex = min(self.NodeIndex, len(self.UndoBuffers) - 1) etisserant@0: if len(self.UndoBuffers) > 0: etisserant@0: self.CurrentNode = self.UndoBuffers[self.NodeIndex].Current().Copy() etisserant@0: self.GenerateTypeList() etisserant@0: self.GenerateMapList() etisserant@0: else: etisserant@0: self.CurrentNode = None etisserant@0: etisserant@0: def GetCurrentNodeIndex(self): etisserant@0: return self.NodeIndex etisserant@0: etisserant@0: def GetCurrentFilename(self): etisserant@0: return self.GetFilename(self.NodeIndex) etisserant@0: etisserant@0: def GetAllFilenames(self): etisserant@0: filenames = [] etisserant@0: for i in xrange(len(self.UndoBuffers)): etisserant@0: filenames.append(self.GetFilename(i)) etisserant@0: return filenames etisserant@0: etisserant@0: def GetFilename(self, index): etisserant@0: if self.UndoBuffers[index].IsCurrentSaved(): etisserant@0: return self.FileNames[index] etisserant@0: else: etisserant@0: return "~%s~"%self.FileNames[index] etisserant@0: etisserant@0: def SetCurrentFilePath(self, filepath): etisserant@0: self.FilePaths[self.NodeIndex] = filepath etisserant@0: if filepath == "": etisserant@0: self.LastNewIndex += 1 etisserant@0: self.FileNames[self.NodeIndex] = "Unnamed%d"%self.LastNewIndex etisserant@0: else: etisserant@0: self.FileNames[self.NodeIndex] = os.path.splitext(os.path.basename(filepath))[0] etisserant@0: etisserant@0: def GetCurrentFilePath(self): etisserant@0: if len(self.FilePaths) > 0: etisserant@0: return self.FilePaths[self.NodeIndex] etisserant@0: else: etisserant@0: return "" etisserant@0: etisserant@0: def GetCurrentBufferState(self): etisserant@0: first = self.UndoBuffers[self.NodeIndex].IsFirst() etisserant@0: last = self.UndoBuffers[self.NodeIndex].IsLast() etisserant@0: return not first, not last etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Profiles Management Functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: def GetCurrentCommunicationLists(self): etisserant@0: list = [] etisserant@0: for index in MappingDictionary.iterkeys(): etisserant@0: if 0x1000 <= index < 0x1200: etisserant@0: list.append(index) etisserant@0: return self.GetProfileLists(MappingDictionary, list) etisserant@0: etisserant@0: def GetCurrentDS302Lists(self): etisserant@0: return self.GetSpecificProfileLists(self.CurrentNode.GetDS302Profile()) etisserant@0: etisserant@0: def GetCurrentProfileLists(self): etisserant@0: return self.GetSpecificProfileLists(self.CurrentNode.GetProfile()) etisserant@0: etisserant@0: def GetSpecificProfileLists(self, mappingdictionary): etisserant@0: validlist = [] etisserant@0: exclusionlist = [] etisserant@0: for name, list in self.CurrentNode.GetSpecificMenu(): etisserant@0: exclusionlist.extend(list) etisserant@0: for index in mappingdictionary.iterkeys(): etisserant@0: if index not in exclusionlist: etisserant@0: validlist.append(index) etisserant@0: return self.GetProfileLists(mappingdictionary, validlist) etisserant@0: etisserant@0: def GetProfileLists(self, mappingdictionary, list): etisserant@0: dictionary = {} etisserant@0: current = [] etisserant@0: for index in list: etisserant@0: dictionary[index] = (mappingdictionary[index]["name"], mappingdictionary[index]["need"]) etisserant@0: if self.CurrentNode.IsEntry(index): etisserant@0: current.append(index) etisserant@0: return dictionary, current etisserant@0: lbessard@39: def GetCurrentNextMapIndex(self): lbessard@39: if self.CurrentNode: lbessard@39: index = 0x2000 lbessard@39: while self.CurrentNode.IsEntry(index) and index < 0x5FFF: lbessard@39: index += 1 lbessard@39: if index < 0x6000: lbessard@39: return index lbessard@39: else: lbessard@39: return None lbessard@39: etisserant@0: def CurrentDS302Defined(self): etisserant@0: if self.CurrentNode: etisserant@0: return len(self.CurrentNode.GetDS302Profile()) > 0 etisserant@0: return False etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Node State and Values Functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: def GetCurrentNodeInfos(self): etisserant@0: name = self.CurrentNode.GetNodeName() etisserant@0: id = self.CurrentNode.GetNodeID() etisserant@0: type = self.CurrentNode.GetNodeType() etisserant@0: return name, id, type etisserant@0: etisserant@0: def SetCurrentNodeInfos(self, name, id, type): etisserant@0: self.CurrentNode.SetNodeName(name) etisserant@0: self.CurrentNode.SetNodeID(id) etisserant@0: self.CurrentNode.SetNodeType(type) etisserant@0: self.BufferCurrentNode() etisserant@0: etisserant@0: def GetCurrentProfileName(self): etisserant@0: if self.CurrentNode: etisserant@0: return self.CurrentNode.GetProfileName() etisserant@0: return "" etisserant@0: etisserant@0: def IsCurrentEntry(self, index): etisserant@0: if self.CurrentNode: etisserant@0: return self.CurrentNode.IsEntry(index) etisserant@0: return False etisserant@0: etisserant@0: def GetCurrentEntry(self, index, subIndex = None): etisserant@0: if self.CurrentNode: etisserant@0: return self.CurrentNode.GetEntry(index, subIndex) etisserant@0: return None etisserant@0: etisserant@0: def GetCurrentParamsEntry(self, index, subIndex = None): etisserant@0: if self.CurrentNode: etisserant@0: return self.CurrentNode.GetParamsEntry(index, subIndex) etisserant@0: return None etisserant@0: etisserant@0: def GetCurrentValidIndexes(self, min, max): etisserant@0: validindexes = [] etisserant@0: for index in self.CurrentNode.GetIndexes(): etisserant@0: if min <= index <= max: etisserant@0: validindexes.append((self.GetEntryName(index), index)) etisserant@0: return validindexes etisserant@0: etisserant@0: def GetCurrentValidChoices(self, min, max): etisserant@0: validchoices = [] etisserant@0: exclusionlist = [] etisserant@0: for menu, indexes in self.CurrentNode.GetSpecificMenu(): etisserant@0: exclusionlist.extend(indexes) etisserant@0: good = True etisserant@0: for index in indexes: etisserant@0: good &= min <= index <= max etisserant@0: if good: etisserant@0: validchoices.append((menu, None)) etisserant@0: list = [index for index in MappingDictionary.keys() if index >= 0x1000] etisserant@0: profiles = self.CurrentNode.GetMappings(False) etisserant@0: for profile in profiles: etisserant@0: list.extend(profile.keys()) etisserant@0: list.sort() etisserant@0: for index in list: etisserant@0: if min <= index <= max and not self.CurrentNode.IsEntry(index) and index not in exclusionlist: etisserant@0: validchoices.append((self.GetEntryName(index), index)) etisserant@0: return validchoices etisserant@0: etisserant@0: def HasCurrentEntryCallbacks(self, index): etisserant@0: if self.CurrentNode and self.CurrentNode.IsEntry(index): etisserant@0: entry_infos = self.GetEntryInfos(index) etisserant@0: if "callback" in entry_infos: etisserant@0: return entry_infos["callback"] etisserant@0: return self.CurrentNode.HasEntryCallbacks(index) etisserant@0: return False etisserant@0: etisserant@0: def GetCurrentEntryValues(self, index): etisserant@0: if self.CurrentNode and self.CurrentNode.IsEntry(index): etisserant@0: entry_infos = self.GetEntryInfos(index) etisserant@0: data = [] etisserant@0: editors = [] etisserant@0: values = self.CurrentNode.GetEntry(index) etisserant@0: params = self.CurrentNode.GetParamsEntry(index) etisserant@0: if type(values) == ListType: etisserant@0: for i, value in enumerate(values): etisserant@0: data.append({"value" : value}) etisserant@0: data[-1].update(params[i]) etisserant@0: else: etisserant@0: data.append({"value" : values}) etisserant@0: data[-1].update(params) etisserant@0: for i, dic in enumerate(data): etisserant@0: infos = self.GetSubentryInfos(index, i) etisserant@0: dic["subindex"] = "0x%02X"%i etisserant@0: dic["name"] = infos["name"] etisserant@0: dic["type"] = self.GetTypeName(infos["type"]) etisserant@0: dic["access"] = AccessType[infos["access"]] etisserant@0: dic["save"] = OptionType[dic["save"]] etisserant@0: editor = {"subindex" : None, "save" : "option", "callback" : "option", "comment" : "string"} etisserant@0: if type(values) == ListType and i == 0: etisserant@0: editor["name"] = None etisserant@0: editor["type"] = None etisserant@0: editor["access"] = None etisserant@0: editor["value"] = None etisserant@0: else: etisserant@0: if infos["user_defined"]: etisserant@0: if entry_infos["struct"] & OD_IdenticalSubindexes: etisserant@0: editor["name"] = None etisserant@0: if i > 1: etisserant@0: editor["type"] = None etisserant@0: editor["access"] = None etisserant@0: else: etisserant@0: editor["type"] = "type" etisserant@0: editor["access"] = "access" etisserant@0: else: etisserant@0: if entry_infos["struct"] & OD_MultipleSubindexes: etisserant@0: editor["name"] = "string" etisserant@0: else: etisserant@0: editor["name"] = None etisserant@0: editor["type"] = "type" etisserant@0: editor["access"] = "access" etisserant@0: else: etisserant@0: editor["name"] = None etisserant@0: editor["type"] = None etisserant@0: editor["access"] = None etisserant@0: if index < 0x260: etisserant@0: editor["value"] = None etisserant@0: if i == 1: etisserant@0: dic["value"] = self.GetTypeName(dic["value"]) etisserant@0: elif 0x1600 <= index <= 0x17FF or 0x1A00 <= index <= 0x1C00: etisserant@0: editor["value"] = "map" etisserant@0: dic["value"] = self.MapTranslation["%08X"%dic["value"]] etisserant@0: else: etisserant@0: if dic["type"].startswith("VISIBLE_STRING"): etisserant@0: editor["value"] = "string" etisserant@0: elif dic["type"] == "BOOLEAN": etisserant@0: editor["value"] = "bool" etisserant@0: dic["value"] = BoolType[dic["value"]] etisserant@0: result = type_model.match(dic["type"]) etisserant@0: if result: etisserant@0: values = result.groups() lbessard@59: if values[0] in ["INTEGER", "UNSIGNED"]: etisserant@0: format = "0x%0" + str(int(values[1])/4) + "X" etisserant@0: dic["value"] = format%dic["value"] etisserant@0: editor["value"] = "string" etisserant@0: elif values[0] == "REAL": etisserant@0: editor["value"] = "float" etisserant@0: elif values[0] == "VISIBLE_STRING": etisserant@0: editor["length"] = values[0] etisserant@0: result = range_model.match(dic["type"]) etisserant@0: if result: etisserant@0: values = result.groups() etisserant@0: if values[0] in ("UNSIGNED", "REAL"): etisserant@0: editor["min"] = values[2] etisserant@0: editor["max"] = values[3] etisserant@0: editors.append(editor) etisserant@0: return data, editors etisserant@0: else: etisserant@0: return None etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Node Informations Functions etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: def GetCustomisedTypeValues(self, index): etisserant@0: values = self.CurrentNode.GetEntry(index) etisserant@0: customisabletypes = self.GetCustomisableTypes() etisserant@0: return values, customisabletypes[values[1]][1] etisserant@0: etisserant@0: def GetEntryName(self, index, node = True): etisserant@0: result = FindEntryName(index, MappingDictionary) etisserant@0: if result == None and node: etisserant@0: NodeMappings = self.CurrentNode.GetMappings() etisserant@0: i = 0 etisserant@0: while not result and i < len(NodeMappings): etisserant@0: result = FindEntryName(index, NodeMappings[i]) etisserant@0: i += 1 etisserant@0: return result etisserant@0: etisserant@0: def GetEntryInfos(self, index, node = True): etisserant@0: result = FindEntryInfos(index, MappingDictionary) etisserant@0: if result == None and node: etisserant@0: NodeMappings = self.CurrentNode.GetMappings() etisserant@0: i = 0 etisserant@0: while not result and i < len(NodeMappings): etisserant@0: result = FindEntryInfos(index, NodeMappings[i]) etisserant@0: i += 1 etisserant@0: return result etisserant@0: etisserant@0: def GetSubentryInfos(self, index, subIndex, node = True): etisserant@0: result = FindSubentryInfos(index, subIndex, MappingDictionary) etisserant@0: if result == None and node: etisserant@0: NodeMappings = self.CurrentNode.GetMappings() etisserant@0: i = 0 etisserant@0: while not result and i < len(NodeMappings): etisserant@0: result = FindSubentryInfos(index, subIndex, NodeMappings[i]) etisserant@0: if result: etisserant@0: result["user_defined"] = i == len(NodeMappings) - 1 and index >= 0x1000 etisserant@0: i += 1 etisserant@0: else : etisserant@0: result["user_defined"] = False etisserant@0: return result etisserant@0: etisserant@0: def GetTypeIndex(self, typename, node = True): etisserant@0: result = FindTypeIndex(typename, MappingDictionary) etisserant@0: if result == None and node: etisserant@0: NodeMappings = self.CurrentNode.GetMappings() etisserant@0: i = 0 etisserant@0: while not result and i < len(NodeMappings): etisserant@0: result = FindTypeIndex(typename, NodeMappings[i]) etisserant@0: i += 1 etisserant@0: return result etisserant@0: etisserant@0: def GetTypeName(self, typeindex, node = True): etisserant@0: result = FindTypeName(typeindex, MappingDictionary) etisserant@0: if result == None and node: etisserant@0: NodeMappings = self.CurrentNode.GetMappings() etisserant@0: i = 0 etisserant@0: while not result and i < len(NodeMappings): etisserant@0: result = FindTypeName(typeindex, NodeMappings[i]) etisserant@0: i += 1 etisserant@0: return result etisserant@0: etisserant@0: def GetTypeDefaultValue(self, typeindex, node = True): etisserant@0: result = FindTypeDefaultValue(typeindex, MappingDictionary) etisserant@0: if result == None and node: etisserant@0: NodeMappings = self.CurrentNode.GetMappings() etisserant@0: i = 0 etisserant@0: while not result and i < len(NodeMappings): etisserant@0: result = FindTypeDefaultValue(typeindex, NodeMappings[i]) etisserant@0: i += 1 etisserant@0: return result etisserant@0: etisserant@0: def GetTypeList(self, node = True): etisserant@0: list = FindTypeList(MappingDictionary) etisserant@0: if node: etisserant@0: for NodeMapping in self.CurrentNode.GetMappings(): etisserant@0: list.extend(FindTypeList(NodeMapping)) etisserant@0: list.sort() etisserant@0: return list etisserant@0: etisserant@0: def GetMapVariableList(self): etisserant@0: list = FindMapVariableList(MappingDictionary, self) etisserant@0: for NodeMapping in self.CurrentNode.GetMappings(): etisserant@0: list.extend(FindMapVariableList(NodeMapping, self)) etisserant@0: list.sort() etisserant@0: return list etisserant@0: etisserant@0: def GetMandatoryIndexes(self, node = True): etisserant@0: list = FindMandatoryIndexes(MappingDictionary) etisserant@0: if node: etisserant@0: for NodeMapping in self.CurrentNode.GetMappings(): etisserant@0: list.extend(FindMandatoryIndexes(NodeMapping)) etisserant@0: return list etisserant@0: etisserant@0: def GetCustomisableTypes(self): etisserant@0: dic = {} etisserant@0: for index, valuetype in CustomisableTypes: etisserant@0: name = self.GetTypeName(index) etisserant@0: dic[index] = [name, valuetype] etisserant@0: return dic etisserant@0: etisserant@0: def GetCurrentSpecificMenu(self): etisserant@0: if self.CurrentNode: etisserant@0: return self.CurrentNode.GetSpecificMenu() etisserant@0: return [] etisserant@0: etisserant@0: