2 # -*- coding: utf-8 -*-
4 #This file is part of CanFestival, a library implementing CanOpen Stack.
6 #Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD
8 #See COPYING file for copyrights details.
10 #This library is free software; you can redistribute it and/or
11 #modify it under the terms of the GNU Lesser General Public
12 #License as published by the Free Software Foundation; either
13 #version 2.1 of the License, or (at your option) any later version.
15 #This library is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 #Lesser General Public License for more details.
20 #You should have received a copy of the GNU Lesser General Public
21 #License along with this library; if not, write to the Free Software
22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 from gnosis.xml.pickle import *
25 from gnosis.xml.pickle.util import setParanoia
29 import eds_utils, gen_cfile
36 type_model = re.compile('([\_A-Z]*)([0-9]*)')
37 range_model = re.compile('([\_A-Z]*)([0-9]*)\[([\-0-9]*)-([\-0-9]*)\]')
38 name_model = re.compile('(.*)\[(.*)\]')
40 def IsOfType(object, typedef):
41 return type(object) == typedef
43 #-------------------------------------------------------------------------------
44 # Formating Name of an Entry
45 #-------------------------------------------------------------------------------
48 Format the text given with the index and subindex defined
51 def StringFormat(text, idx, sub):
52 result = name_model.match(text)
54 format = result.groups()
55 return format[0]%eval(format[1])
59 #-------------------------------------------------------------------------------
60 # Search in a Mapping Dictionary
61 #-------------------------------------------------------------------------------
64 Return the index of the informations in the Object Dictionary in case of identical
67 def FindIndex(index, mappingdictionary):
68 if index in mappingdictionary:
71 listpluri = [idx for idx in mappingdictionary.keys() if mappingdictionary[idx]["struct"] & OD_IdenticalIndexes]
74 nb_max = mappingdictionary[idx]["nbmax"]
75 incr = mappingdictionary[idx]["incr"]
76 if idx < index < idx + incr * nb_max and (index - idx)%incr == 0:
81 Return the index of the typename given by searching in mappingdictionary
83 def FindTypeIndex(typename, mappingdictionary):
85 for index, values in mappingdictionary.iteritems():
87 testdic[values["name"]] = index
88 if typename in testdic:
89 return testdic[typename]
93 Return the name of the type by searching in mappingdictionary
95 def FindTypeName(typeindex, mappingdictionary):
96 if typeindex < 0x1000 and typeindex in mappingdictionary:
97 return mappingdictionary[typeindex]["name"]
101 Return the default value of the type by searching in mappingdictionary
103 def FindTypeDefaultValue(typeindex, mappingdictionary):
104 if typeindex < 0x1000 and typeindex in mappingdictionary:
105 return mappingdictionary[typeindex]["default"]
109 Return the list of types defined in mappingdictionary
111 def FindTypeList(mappingdictionary):
113 for index in mappingdictionary.keys():
115 list.append((index, mappingdictionary[index]["name"]))
119 Return the name of an entry by searching in mappingdictionary
121 def FindEntryName(index, mappingdictionary):
122 base_index = FindIndex(index, mappingdictionary)
124 infos = mappingdictionary[base_index]
125 if infos["struct"] & OD_IdenticalIndexes:
126 return StringFormat(infos["name"], (index - base_index) / infos["incr"] + 1, 0)
132 Return the informations of one entry by searching in mappingdictionary
134 def FindEntryInfos(index, mappingdictionary):
135 base_index = FindIndex(index, mappingdictionary)
137 copy = mappingdictionary[base_index].copy()
138 if copy["struct"] & OD_IdenticalIndexes:
139 copy["name"] = StringFormat(copy["name"], (index - base_index) / copy["incr"] + 1, 0)
145 Return the informations of one subentry of an entry by searching in mappingdictionary
147 def FindSubentryInfos(index, subIndex, mappingdictionary):
148 base_index = FindIndex(index, mappingdictionary)
150 struct = mappingdictionary[base_index]["struct"]
151 if struct & OD_Subindex:
152 if struct & OD_IdenticalSubindexes:
153 if struct & OD_IdenticalIndexes:
154 incr = mappingdictionary[base_index]["incr"]
158 return mappingdictionary[base_index]["values"][0].copy()
159 elif 0 < subIndex <= mappingdictionary[base_index]["values"][1]["nbmax"]:
160 copy = mappingdictionary[base_index]["values"][1].copy()
161 copy["name"] = StringFormat(copy["name"], (index - base_index) / incr + 1, subIndex)
163 elif struct & OD_MultipleSubindexes and 0 <= subIndex < len(mappingdictionary[base_index]["values"]):
164 return mappingdictionary[base_index]["values"][subIndex].copy()
166 return mappingdictionary[base_index]["values"][0].copy()
170 Return the list of variables that can be mapped defined in mappingdictionary
172 def FindMapVariableList(mappingdictionary, Manager):
174 for index in mappingdictionary.iterkeys():
175 if Manager.IsCurrentEntry(index):
176 for subIndex, values in enumerate(mappingdictionary[index]["values"]):
177 if mappingdictionary[index]["values"][subIndex]["pdo"]:
178 infos = Manager.GetEntryInfos(mappingdictionary[index]["values"][subIndex]["type"])
179 if mappingdictionary[index]["struct"] & OD_IdenticalSubindexes:
180 values = Manager.GetCurrentEntry(index)
181 for i in xrange(len(values) - 1):
182 list.append((index, i + 1, infos["size"], StringFormat(mappingdictionary[index]["values"][subIndex]["name"],1,i+1)))
184 list.append((index, subIndex, infos["size"], mappingdictionary[index]["values"][subIndex]["name"]))
188 Return the list of mandatory indexes defined in mappingdictionary
190 def FindMandatoryIndexes(mappingdictionary):
192 for index in mappingdictionary.iterkeys():
193 if index >= 0x1000 and mappingdictionary[index]["need"]:
199 Class implementing a buffer of changes made on the current editing Object Dictionary
205 Constructor initialising buffer
207 def __init__(self, currentstate, issaved = False):
209 self.CurrentIndex = -1
212 # if current state is defined
214 self.CurrentIndex = 0
217 # Initialising buffer with currentstate at the first place
218 for i in xrange(UndoBufferLength):
220 self.Buffer.append(currentstate)
222 self.Buffer.append(None)
223 # Initialising index of state saved
230 Add a new state in buffer
232 def Buffering(self, currentstate):
233 self.CurrentIndex = (self.CurrentIndex + 1) % UndoBufferLength
234 self.Buffer[self.CurrentIndex] = currentstate
235 # Actualising buffer limits
236 self.MaxIndex = self.CurrentIndex
237 if self.MinIndex == self.CurrentIndex:
238 # If the removed state was the state saved, there is no state saved in the buffer
239 if self.LastSave == self.MinIndex:
241 self.MinIndex = (self.MinIndex + 1) % UndoBufferLength
242 self.MinIndex = max(self.MinIndex, 0)
245 Return current state of buffer
248 return self.Buffer[self.CurrentIndex]
251 Change current state to previous in buffer and return new current state
254 if self.CurrentIndex != self.MinIndex:
255 self.CurrentIndex = (self.CurrentIndex - 1) % UndoBufferLength
256 return self.Buffer[self.CurrentIndex]
260 Change current state to next in buffer and return new current state
263 if self.CurrentIndex != self.MaxIndex:
264 self.CurrentIndex = (self.CurrentIndex + 1) % UndoBufferLength
265 return self.Buffer[self.CurrentIndex]
269 Return True if current state is the first in buffer
272 return self.CurrentIndex == self.MinIndex
275 Return True if current state is the last in buffer
278 return self.CurrentIndex == self.MaxIndex
281 Note that current state is saved
283 def CurrentSaved(self):
284 self.LastSave = self.CurrentIndex
287 Return True if current state is saved
289 def IsCurrentSaved(self):
290 return self.LastSave == self.CurrentIndex
295 Class which control the operations made on the node and answer to view requests
303 def __init__(self, cwd):
304 self.LastNewIndex = 0
308 self.CurrentNode = None
309 self.ScriptDirectory = cwd
310 self.UndoBuffers = []
312 #-------------------------------------------------------------------------------
313 # Type and Map Variable Lists
314 #-------------------------------------------------------------------------------
317 Generate the list of types defined for the current node
319 def GenerateTypeList(self):
321 self.TypeTranslation = {}
322 list = self.GetTypeList()
324 for index, name in list:
325 self.TypeList += "%s%s"%(sep,name)
326 self.TypeTranslation[name] = index
330 Generate the list of variables that can be mapped for the current node
332 def GenerateMapList(self):
333 self.MapList = "None"
334 self.NameTranslation = {"None" : "00000000"}
335 self.MapTranslation = {"00000000" : "None"}
336 list = self.GetMapVariableList()
337 for index, subIndex, size, name in list:
338 self.MapList += ",%s"%name
339 map = "%04X%02X%02X"%(index,subIndex,size)
340 self.NameTranslation[name] = map
341 self.MapTranslation[map] = name
344 Return the list of types defined for the current node
346 def GetCurrentTypeList(self):
350 Return the list of variables that can be mapped for the current node
352 def GetCurrentMapList(self):
355 #-------------------------------------------------------------------------------
356 # Create Load and Save Functions
357 #-------------------------------------------------------------------------------
360 Create a new node and add a new buffer for storing it
362 def CreateNewNode(self, name, id, type, description, profile, filepath, NMT, options):
365 # Try to load profile given
366 result = self.LoadProfile(profile, filepath, node)
368 # if success, initialising node
369 self.CurrentNode = node
370 self.CurrentNode.SetNodeName(name)
371 self.CurrentNode.SetNodeID(id)
372 self.CurrentNode.SetNodeType(type)
373 self.CurrentNode.SetNodeDescription(description)
374 AddIndexList = self.GetMandatoryIndexes()
375 if NMT == "NodeGuarding":
376 AddIndexList.extend([0x100C, 0x100D])
377 elif NMT == "Heartbeat":
378 AddIndexList.append(0x1017)
379 for option in options:
380 if option == "DS302":
381 DS302Path = os.path.join(self.ScriptDirectory, "config/DS-302.prf")
382 # Charging DS-302 profile if choosen by user
383 if os.path.isfile(DS302Path):
386 self.CurrentNode.SetDS302Profile(Mapping)
387 self.CurrentNode.ExtendSpecificMenu(AddMenuEntries)
389 return "Problem with DS-302! Syntax Error."
391 return "Couldn't find DS-302 in 'config' folder!"
392 elif option == "GenSYNC":
393 AddIndexList.extend([0x1005, 0x1006])
394 elif option == "Emergency":
395 AddIndexList.append(0x1014)
396 elif option == "SaveConfig":
397 AddIndexList.extend([0x1010, 0x1011, 0x1020])
398 elif option == "StoreEDS":
399 AddIndexList.extend([0x1021, 0x1022])
402 self.SetCurrentFilePath("")
403 # Add Mandatory indexes
404 self.ManageEntriesOfCurrent(AddIndexList, [])
406 self.GenerateTypeList()
407 self.GenerateMapList()
413 Load a profile in node
415 def LoadProfile(self, profile, filepath, node):
416 if profile != "None":
417 # Try to charge the profile given
420 node.SetProfileName(profile)
421 node.SetProfile(Mapping)
422 node.SetSpecificMenu(AddMenuEntries)
425 return "Syntax Error\nBad OD Profile file!."
428 node.SetProfileName("None")
430 node.SetSpecificMenu([])
434 Open a file and store it in a new buffer
436 def OpenFileInCurrent(self, filepath):
438 file = open(filepath, "r")
441 self.CurrentNode = node
442 # Add a new buffer and defining current state
443 self.AddNodeBuffer(self.CurrentNode.Copy(), True)
444 self.SetCurrentFilePath(filepath)
446 self.GenerateTypeList()
447 self.GenerateMapList()
451 Save current node in a file
453 def SaveCurrentInFile(self, filepath = None):
454 # if no filepath given, verify if current node has a filepath defined
456 filepath = self.GetCurrentFilePath()
460 file = open(filepath, "w")
461 dump(self.CurrentNode, file)
463 self.SetCurrentFilePath(filepath)
464 # Update saved state in buffer
465 self.UndoBuffers[self.NodeIndex].CurrentSaved()
471 def CloseCurrent(self, ignore = False):
472 # Verify if it's not forced that the current node is saved before closing it
473 if self.UndoBuffers[self.NodeIndex].IsCurrentSaved() or ignore:
474 self.RemoveNodeBuffer(self.NodeIndex)
479 Import an eds file and store it in a new buffer if no node edited
481 def ImportCurrentFromEDSFile(self, filepath):
482 # Generate node from definition in a xml file
483 result = eds_utils.GenerateNode(filepath, self, self.ScriptDirectory)
484 if isinstance(result, Node):
485 self.CurrentNode = result
486 self.GenerateTypeList()
487 self.GenerateMapList()
488 if len(self.UndoBuffers) == 0:
490 self.SetCurrentFilePath("")
491 self.BufferCurrentNode()
497 Export to an eds file and store it in a new buffer if no node edited
499 def ExportCurrentToEDSFile(self, filepath):
500 return eds_utils.GenerateEDSFile(filepath, self)
503 Build the C definition of Object Dictionary for current node
505 def ExportCurrentToCFile(self, filepath):
506 return gen_cfile.GenerateFile(filepath, self)
508 #-------------------------------------------------------------------------------
509 # Add Entries to Current Functions
510 #-------------------------------------------------------------------------------
513 Add the specified number of subentry for the given entry. Verify that total
514 number of subentry (except 0) doesn't exceed nbmax defined
516 def AddSubentriesToCurrent(self, index, number):
517 # Informations about entry
518 length = self.CurrentNode.GetEntry(index, 0)
519 infos = self.GetEntryInfos(index)
520 subentry_infos = self.GetSubentryInfos(index, 1)
521 # Get default value for subindex
522 if "default" in subentry_infos:
523 default = subentry_infos["default"]
525 default = self.GetTypeDefaultValue(subentry_infos["type"])
526 # First case entry is record
527 if infos["struct"] & OD_IdenticalSubindexes:
528 for i in xrange(1, min(number,subentry_infos["nbmax"]-length) + 1):
529 self.CurrentNode.AddEntry(index, length + i, default)
530 self.BufferCurrentNode()
531 # Second case entry is array, only possible for manufacturer specific
532 elif infos["struct"] & OD_MultipleSubindexes and 0x2000 <= index <= 0x5FFF:
533 values = {"name" : "Undefined", "type" : 5, "access" : "rw", "pdo" : True}
534 for i in xrange(1, min(number,0xFE-length) + 1):
535 self.CurrentNode.AddMappingEntry(index, length + i, values = values.copy())
536 self.CurrentNode.AddEntry(index, length + i, 0)
537 self.BufferCurrentNode()
540 Remove the specified number of subentry for the given entry. Verify that total
541 number of subentry (except 0) isn't less than 1
543 def RemoveSubentriesFromCurrent(self, index, number):
544 # Informations about entry
545 infos = self.GetEntryInfos(index)
546 length = self.CurrentNode.GetEntry(index, 0)
547 # Entry is a record, or is an array of manufacturer specific
548 if infos["struct"] & OD_IdenticalSubindexes or 0x2000 <= index <= 0x5FFF and infos["struct"] & OD_IdenticalSubindexes:
549 for i in xrange(min(number, length - 1)):
550 self.RemoveCurrentVariable(index, length - i)
551 self.BufferCurrentNode()
554 Add a SDO Server to current node
556 def AddSDOServerToCurrent(self):
557 # An SDO Server is already defined at index 0x1200
558 if self.CurrentNode.IsEntry(0x1200):
559 indexlist = [self.GetLineFromIndex(0x1201)]
560 if None not in indexlist:
561 self.ManageEntriesOfCurrent(indexlist, [])
562 # Add an SDO Server at index 0x1200
564 self.ManageEntriesOfCurrent([0x1200], [])
567 Add a SDO Server to current node
569 def AddSDOClientToCurrent(self):
570 indexlist = [self.GetLineFromIndex(0x1280)]
571 if None not in indexlist:
572 self.ManageEntriesOfCurrent(indexlist, [])
575 Add a Transmit PDO to current node
577 def AddPDOTransmitToCurrent(self):
578 indexlist = [self.GetLineFromIndex(0x1800),self.GetLineFromIndex(0x1A00)]
579 if None not in indexlist:
580 self.ManageEntriesOfCurrent(indexlist, [])
583 Add a Receive PDO to current node
585 def AddPDOReceiveToCurrent(self):
586 indexlist = [self.GetLineFromIndex(0x1400),self.GetLineFromIndex(0x1600)]
587 if None not in indexlist:
588 self.ManageEntriesOfCurrent(indexlist, [])
591 Add a list of entries defined in profile for menu item selected to current node
593 def AddSpecificEntryToCurrent(self, menuitem):
595 for menu, indexes in self.CurrentNode.GetSpecificMenu():
597 for index in indexes:
598 indexlist.append(self.GetLineFromIndex(index))
599 if None not in indexlist:
600 self.ManageEntriesOfCurrent(indexlist, [])
603 Search the first index available for a pluri entry from base_index
605 def GetLineFromIndex(self, base_index):
608 infos = self.GetEntryInfos(base_index)
609 while index < base_index + infos["incr"]*infos["nbmax"] and not found:
610 if not self.CurrentNode.IsEntry(index):
613 index += infos["incr"]
619 Add entries specified in addinglist and remove entries specified in removinglist
621 def ManageEntriesOfCurrent(self, addinglist, removinglist):
622 # Add all the entries in addinglist
623 for index in addinglist:
624 infos = self.GetEntryInfos(index)
625 if infos["struct"] & OD_MultipleSubindexes:
626 # First case entry is a record
627 if infos["struct"] & OD_IdenticalSubindexes:
628 subentry_infos = self.GetSubentryInfos(index, 1)
629 if "default" in subentry_infos:
630 default = subentry_infos["default"]
632 default = self.GetTypeDefaultValue(subentry_infos["type"])
633 self.CurrentNode.AddEntry(index, 1, default)
634 # Second case entry is a record
637 subentry_infos = self.GetSubentryInfos(index, i)
638 while subentry_infos:
639 if "default" in subentry_infos:
640 default = subentry_infos["default"]
642 default = self.GetTypeDefaultValue(subentry_infos["type"])
643 self.CurrentNode.AddEntry(index, i, default)
645 subentry_infos = self.GetSubentryInfos(index, i)
646 # Third case entry is a record
648 subentry_infos = self.GetSubentryInfos(index, 0)
649 if "default" in subentry_infos:
650 default = subentry_infos["default"]
652 default = self.GetTypeDefaultValue(subentry_infos["type"])
653 self.CurrentNode.AddEntry(index, 0, default)
654 # Remove all the entries in removinglist
655 for index in removinglist:
656 self.RemoveCurrentVariable(index)
657 self.BufferCurrentNode()
661 Remove an entry from current node. Analize the index to perform the correct
664 def RemoveCurrentVariable(self, index, subIndex = None):
665 Mappings = self.CurrentNode.GetMappings()
666 if index < 0x1000 and subIndex == None:
667 type = self.CurrentNode.GetEntry(index, 1)
668 for i in Mappings[-1]:
669 for value in Mappings[-1][i]["values"]:
670 if value["type"] == index:
672 self.CurrentNode.RemoveMappingEntry(index)
673 self.CurrentNode.RemoveEntry(index)
674 elif index == 0x1200 and subIndex == None:
675 self.CurrentNode.RemoveEntry(0x1200)
676 elif 0x1201 <= index <= 0x127F and subIndex == None:
677 self.CurrentNode.RemoveLine(index, 0x127F)
678 elif 0x1280 <= index <= 0x12FF and subIndex == None:
679 self.CurrentNode.RemoveLine(index, 0x12FF)
680 elif 0x1400 <= index <= 0x15FF or 0x1600 <= index <= 0x17FF and subIndex == None:
681 if 0x1600 <= index <= 0x17FF and subIndex == None:
683 self.CurrentNode.RemoveLine(index, 0x15FF)
684 self.CurrentNode.RemoveLine(index + 0x200, 0x17FF)
685 elif 0x1800 <= index <= 0x19FF or 0x1A00 <= index <= 0x1BFF and subIndex == None:
686 if 0x1A00 <= index <= 0x1BFF:
688 self.CurrentNode.RemoveLine(index, 0x19FF)
689 self.CurrentNode.RemoveLine(index + 0x200, 0x1BFF)
692 for menu,list in self.CurrentNode.GetSpecificMenu():
694 iinfos = self.GetEntryInfos(i)
695 indexes = [i + incr * iinfos["incr"] for incr in xrange(iinfos["nbmax"])]
700 jinfos = self.GetEntryInfos(j)
701 self.CurrentNode.RemoveLine(j + diff, j + jinfos["incr"]*jinfos["nbmax"], jinfos["incr"])
702 self.CurrentNode.RemoveMapVariable(index, subIndex)
704 infos = self.GetEntryInfos(index)
705 if not infos["need"]:
706 self.CurrentNode.RemoveEntry(index, subIndex)
707 if index in Mappings[-1]:
708 self.CurrentNode.RemoveMappingEntry(index, subIndex)
709 self.GenerateMapList()
711 def AddMapVariableToCurrent(self, index, name, struct, number):
712 if 0x2000 <= index <= 0x5FFF:
713 if not self.CurrentNode.IsEntry(index):
714 self.CurrentNode.AddMappingEntry(index, name = name, struct = struct)
716 values = {"name" : name, "type" : 5, "access" : "rw", "pdo" : True}
717 self.CurrentNode.AddMappingEntry(index, 0, values = values)
718 self.CurrentNode.AddEntry(index, 0, 0)
720 values = {"name" : "Number of Entries", "type" : 2, "access" : "ro", "pdo" : False}
721 self.CurrentNode.AddMappingEntry(index, 0, values = values)
723 values = {"name" : name + " %d[(sub)]", "type" : 5, "access" : "rw", "pdo" : True, "nbmax" : 0xFE}
724 self.CurrentNode.AddMappingEntry(index, 1, values = values)
725 for i in xrange(number):
726 self.CurrentNode.AddEntry(index, i + 1, 0)
728 for i in xrange(number):
729 values = {"name" : "Undefined", "type" : 5, "access" : "rw", "pdo" : True}
730 self.CurrentNode.AddMappingEntry(index, i + 1, values = values)
731 self.CurrentNode.AddEntry(index, i + 1, 0)
732 self.GenerateMapList()
733 self.BufferCurrentNode()
736 return "Index 0x%04X already defined!"%index
738 return "Index 0x%04X isn't a valid index for Map Variable!"%index
740 def AddUserTypeToCurrent(self, type, min, max, length):
742 while index < 0x100 and self.CurrentNode.IsEntry(index):
745 customisabletypes = self.GetCustomisableTypes()
746 name, valuetype = customisabletypes[type]
747 size = self.GetEntryInfos(type)["size"]
748 default = self.GetTypeDefaultValue(type)
750 self.CurrentNode.AddMappingEntry(index, name = "%s[%d-%d]"%(name, min, max), struct = 3, size = size, default = default)
751 self.CurrentNode.AddMappingEntry(index, 0, values = {"name" : "Number of Entries", "type" : 0x02, "access" : "ro", "pdo" : False})
752 self.CurrentNode.AddMappingEntry(index, 1, values = {"name" : "Type", "type" : 0x02, "access" : "ro", "pdo" : False})
753 self.CurrentNode.AddMappingEntry(index, 2, values = {"name" : "Minimum Value", "type" : type, "access" : "ro", "pdo" : False})
754 self.CurrentNode.AddMappingEntry(index, 3, values = {"name" : "Maximum Value", "type" : type, "access" : "ro", "pdo" : False})
755 self.CurrentNode.AddEntry(index, 1, type)
756 self.CurrentNode.AddEntry(index, 2, min)
757 self.CurrentNode.AddEntry(index, 3, max)
759 self.CurrentNode.AddMappingEntry(index, name = "%s%d"%(name, length), struct = 3, size = length * size, default = default)
760 self.CurrentNode.AddMappingEntry(index, 0, values = {"name" : "Number of Entries", "type" : 0x02, "access" : "ro", "pdo" : False})
761 self.CurrentNode.AddMappingEntry(index, 1, values = {"name" : "Type", "type" : 0x02, "access" : "ro", "pdo" : False})
762 self.CurrentNode.AddMappingEntry(index, 2, values = {"name" : "Length", "type" : 0x02, "access" : "ro", "pdo" : False})
763 self.CurrentNode.AddEntry(index, 1, type)
764 self.CurrentNode.AddEntry(index, 2, length)
765 self.GenerateTypeList()
766 self.BufferCurrentNode()
769 return "Too many User Types have already been defined!"
771 #-------------------------------------------------------------------------------
772 # Modify Entry and Mapping Functions
773 #-------------------------------------------------------------------------------
775 def SetCurrentEntryCallbacks(self, index, value):
776 if self.CurrentNode and self.CurrentNode.IsEntry(index):
777 entry_infos = self.GetEntryInfos(index)
778 if "callback" not in entry_infos:
779 self.CurrentNode.SetParamsEntry(index, None, callback = value)
780 self.BufferCurrentNode()
782 def SetCurrentEntry(self, index, subIndex, value, name, editor):
783 if self.CurrentNode and self.CurrentNode.IsEntry(index):
787 value = int(self.NameTranslation[value], 16)
788 self.CurrentNode.SetEntry(index, subIndex, value)
791 elif editor == "bool":
792 value = value == "True"
793 self.CurrentNode.SetEntry(index, subIndex, value)
794 elif editor == "time":
795 self.CurrentNode.SetEntry(index, subIndex, value)
796 elif editor == "number":
797 self.CurrentNode.SetEntry(index, subIndex, value)
798 elif editor == "domain":
800 if len(value) % 2 != 0:
802 value = value.decode('hex_codec')
803 self.CurrentNode.SetEntry(index, subIndex, value)
807 subentry_infos = self.GetSubentryInfos(index, subIndex)
808 type = subentry_infos["type"]
810 for typeindex, typevalue in CustomisableTypes:
811 dic[typeindex] = typevalue
813 type = self.CurrentNode.GetEntry(type)[1]
816 value = int(value, 16)
817 self.CurrentNode.SetEntry(index, subIndex, value)
821 self.CurrentNode.SetEntry(index, subIndex, value)
822 elif name in ["comment", "save"]:
823 if editor == "option":
824 value = value == "Yes"
826 self.CurrentNode.SetParamsEntry(index, subIndex, save = value)
827 elif name == "comment":
828 self.CurrentNode.SetParamsEntry(index, subIndex, comment = value)
831 value = self.TypeTranslation[value]
832 size = self.GetEntryInfos(value)["size"]
833 self.CurrentNode.UpdateMapVariable(index, subIndex, size)
834 elif editor in ["access","raccess"]:
836 for abbrev,access in AccessType.iteritems():
839 if editor == "raccess" and not self.CurrentNode.IsMappingEntry(index):
840 entry_infos = self.GetEntryInfos(index)
841 self.CurrentNode.AddMappingEntry(index, name = entry_infos["name"], struct = 7)
842 self.CurrentNode.AddMappingEntry(index, 0, values = self.GetSubentryInfos(index, 0, False).copy())
843 self.CurrentNode.AddMappingEntry(index, 1, values = self.GetSubentryInfos(index, 1, False).copy())
844 self.CurrentNode.SetMappingEntry(index, subIndex, values = {name : value})
845 if name == "name" or editor == "type":
846 self.GenerateMapList()
847 self.BufferCurrentNode()
849 def SetCurrentEntryName(self, index, name):
850 self.CurrentNode.SetMappingEntry(index, name=name)
851 self.BufferCurrentNode()
853 def SetCurrentUserType(self, index, type, min, max, length):
854 customisabletypes = self.GetCustomisableTypes()
855 values, valuetype = self.GetCustomisedTypeValues(index)
856 name, new_valuetype = customisabletypes[type]
857 size = self.GetEntryInfos(type)["size"]
858 default = self.GetTypeDefaultValue(type)
859 if new_valuetype == 0:
860 self.CurrentNode.SetMappingEntry(index, name = "%s[%d-%d]"%(name, min, max), struct = 3, size = size, default = default)
862 self.CurrentNode.SetMappingEntry(index, 2, values = {"name" : "Minimum Value", "type" : type, "access" : "ro", "pdo" : False})
863 self.CurrentNode.AddMappingEntry(index, 3, values = {"name" : "Maximum Value", "type" : type, "access" : "ro", "pdo" : False})
864 self.CurrentNode.SetEntry(index, 1, type)
865 self.CurrentNode.SetEntry(index, 2, min)
867 self.CurrentNode.AddEntry(index, 3, max)
869 self.CurrentNode.SetEntry(index, 3, max)
870 elif new_valuetype == 1:
871 self.CurrentNode.SetMappingEntry(index, name = "%s%d"%(name, length), struct = 3, size = size, default = default)
873 self.CurrentNode.SetMappingEntry(index, 2, values = {"name" : "Length", "type" : 0x02, "access" : "ro", "pdo" : False})
874 self.CurrentNode.RemoveMappingEntry(index, 3)
875 self.CurrentNode.SetEntry(index, 1, type)
876 self.CurrentNode.SetEntry(index, 2, length)
878 self.CurrentNode.RemoveEntry(index, 3)
879 self.BufferCurrentNode()
881 #-------------------------------------------------------------------------------
882 # Current Buffering Management Functions
883 #-------------------------------------------------------------------------------
885 def BufferCurrentNode(self):
886 self.UndoBuffers[self.NodeIndex].Buffering(self.CurrentNode.Copy())
888 def CurrentIsSaved(self):
889 return self.UndoBuffers[self.NodeIndex].IsCurrentSaved()
891 def OneFileHasChanged(self):
893 for buffer in self.UndoBuffers:
894 result |= not buffer.IsCurrentSaved()
897 def GetBufferNumber(self):
898 return len(self.UndoBuffers)
900 def LoadCurrentPrevious(self):
901 self.CurrentNode = self.UndoBuffers[self.NodeIndex].Previous().Copy()
903 def LoadCurrentNext(self):
904 self.CurrentNode = self.UndoBuffers[self.NodeIndex].Next().Copy()
906 def AddNodeBuffer(self, currentstate = None, issaved = False):
907 self.NodeIndex = len(self.UndoBuffers)
908 self.UndoBuffers.append(UndoBuffer(currentstate, issaved))
909 self.FilePaths.append("")
910 self.FileNames.append("")
912 def ChangeCurrentNode(self, index):
913 if index < len(self.UndoBuffers):
914 self.NodeIndex = index
915 self.CurrentNode = self.UndoBuffers[self.NodeIndex].Current().Copy()
916 self.GenerateTypeList()
917 self.GenerateMapList()
919 def RemoveNodeBuffer(self, index):
920 self.UndoBuffers.pop(index)
921 self.FilePaths.pop(index)
922 self.FileNames.pop(index)
923 self.NodeIndex = min(self.NodeIndex, len(self.UndoBuffers) - 1)
924 if len(self.UndoBuffers) > 0:
925 self.CurrentNode = self.UndoBuffers[self.NodeIndex].Current().Copy()
926 self.GenerateTypeList()
927 self.GenerateMapList()
929 self.CurrentNode = None
931 def GetCurrentNodeIndex(self):
932 return self.NodeIndex
934 def GetCurrentFilename(self):
935 return self.GetFilename(self.NodeIndex)
937 def GetAllFilenames(self):
939 for i in xrange(len(self.UndoBuffers)):
940 filenames.append(self.GetFilename(i))
943 def GetFilename(self, index):
944 if self.UndoBuffers[index].IsCurrentSaved():
945 return self.FileNames[index]
947 return "~%s~"%self.FileNames[index]
949 def SetCurrentFilePath(self, filepath):
950 self.FilePaths[self.NodeIndex] = filepath
952 self.LastNewIndex += 1
953 self.FileNames[self.NodeIndex] = "Unnamed%d"%self.LastNewIndex
955 self.FileNames[self.NodeIndex] = os.path.splitext(os.path.basename(filepath))[0]
957 def GetCurrentFilePath(self):
958 if len(self.FilePaths) > 0:
959 return self.FilePaths[self.NodeIndex]
963 def GetCurrentBufferState(self):
964 first = self.UndoBuffers[self.NodeIndex].IsFirst()
965 last = self.UndoBuffers[self.NodeIndex].IsLast()
966 return not first, not last
968 #-------------------------------------------------------------------------------
969 # Profiles Management Functions
970 #-------------------------------------------------------------------------------
972 def GetCurrentCommunicationLists(self):
974 for index in MappingDictionary.iterkeys():
975 if 0x1000 <= index < 0x1200:
977 return self.GetProfileLists(MappingDictionary, list)
979 def GetCurrentDS302Lists(self):
980 return self.GetSpecificProfileLists(self.CurrentNode.GetDS302Profile())
982 def GetCurrentProfileLists(self):
983 return self.GetSpecificProfileLists(self.CurrentNode.GetProfile())
985 def GetSpecificProfileLists(self, mappingdictionary):
988 for name, list in self.CurrentNode.GetSpecificMenu():
989 exclusionlist.extend(list)
990 for index in mappingdictionary.iterkeys():
991 if index not in exclusionlist:
992 validlist.append(index)
993 return self.GetProfileLists(mappingdictionary, validlist)
995 def GetProfileLists(self, mappingdictionary, list):
999 dictionary[index] = (mappingdictionary[index]["name"], mappingdictionary[index]["need"])
1000 if self.CurrentNode.IsEntry(index):
1001 current.append(index)
1002 return dictionary, current
1004 def GetCurrentNextMapIndex(self):
1005 if self.CurrentNode:
1007 while self.CurrentNode.IsEntry(index) and index < 0x5FFF:
1014 def CurrentDS302Defined(self):
1015 if self.CurrentNode:
1016 return len(self.CurrentNode.GetDS302Profile()) > 0
1019 #-------------------------------------------------------------------------------
1020 # Node State and Values Functions
1021 #-------------------------------------------------------------------------------
1023 def GetCurrentNodeInfos(self):
1024 name = self.CurrentNode.GetNodeName()
1025 id = self.CurrentNode.GetNodeID()
1026 type = self.CurrentNode.GetNodeType()
1027 description = self.CurrentNode.GetNodeDescription()
1028 return name, id, type, description
1030 def SetCurrentNodeInfos(self, name, id, type, description):
1031 self.CurrentNode.SetNodeName(name)
1032 self.CurrentNode.SetNodeID(id)
1033 self.CurrentNode.SetNodeType(type)
1034 self.CurrentNode.SetNodeDescription(description)
1035 self.BufferCurrentNode()
1037 def GetCurrentProfileName(self):
1038 if self.CurrentNode:
1039 return self.CurrentNode.GetProfileName()
1042 def IsCurrentEntry(self, index):
1043 if self.CurrentNode:
1044 return self.CurrentNode.IsEntry(index)
1047 def GetCurrentEntry(self, index, subIndex = None):
1048 if self.CurrentNode:
1049 return self.CurrentNode.GetEntry(index, subIndex)
1052 def GetCurrentParamsEntry(self, index, subIndex = None):
1053 if self.CurrentNode:
1054 return self.CurrentNode.GetParamsEntry(index, subIndex)
1057 def GetCurrentValidIndexes(self, min, max):
1059 for index in self.CurrentNode.GetIndexes():
1060 if min <= index <= max:
1061 validindexes.append((self.GetEntryName(index), index))
1064 def GetCurrentValidChoices(self, min, max):
1067 for menu, indexes in self.CurrentNode.GetSpecificMenu():
1068 exclusionlist.extend(indexes)
1070 for index in indexes:
1071 good &= min <= index <= max
1073 validchoices.append((menu, None))
1074 list = [index for index in MappingDictionary.keys() if index >= 0x1000]
1075 profiles = self.CurrentNode.GetMappings(False)
1076 for profile in profiles:
1077 list.extend(profile.keys())
1080 if min <= index <= max and not self.CurrentNode.IsEntry(index) and index not in exclusionlist:
1081 validchoices.append((self.GetEntryName(index), index))
1084 def HasCurrentEntryCallbacks(self, index):
1085 if self.CurrentNode and self.CurrentNode.IsEntry(index):
1086 entry_infos = self.GetEntryInfos(index)
1087 if "callback" in entry_infos:
1088 return entry_infos["callback"]
1089 return self.CurrentNode.HasEntryCallbacks(index)
1092 def GetCurrentEntryValues(self, index):
1093 if self.CurrentNode and self.CurrentNode.IsEntry(index):
1094 entry_infos = self.GetEntryInfos(index)
1097 values = self.CurrentNode.GetEntry(index)
1098 params = self.CurrentNode.GetParamsEntry(index)
1099 if type(values) == ListType:
1100 for i, value in enumerate(values):
1101 data.append({"value" : value})
1102 data[-1].update(params[i])
1104 data.append({"value" : values})
1105 data[-1].update(params)
1106 for i, dic in enumerate(data):
1107 infos = self.GetSubentryInfos(index, i)
1108 dic["subindex"] = "0x%02X"%i
1109 dic["name"] = infos["name"]
1110 dic["type"] = self.GetTypeName(infos["type"])
1111 dic["access"] = AccessType[infos["access"]]
1112 dic["save"] = OptionType[dic["save"]]
1113 editor = {"subindex" : None, "save" : "option", "callback" : "option", "comment" : "string"}
1114 if type(values) == ListType and i == 0:
1115 editor["name"] = None
1116 editor["type"] = None
1117 if 0x1600 <= index <= 0x17FF or 0x1A00 <= index <= 0x1C00:
1118 editor["access"] = "raccess"
1120 editor["access"] = None
1121 editor["value"] = None
1123 if infos["user_defined"]:
1124 if entry_infos["struct"] & OD_IdenticalSubindexes:
1125 editor["name"] = None
1127 editor["type"] = None
1128 editor["access"] = None
1130 editor["type"] = "type"
1131 editor["access"] = "access"
1133 if entry_infos["struct"] & OD_MultipleSubindexes:
1134 editor["name"] = "string"
1136 editor["name"] = None
1137 editor["type"] = "type"
1138 editor["access"] = "access"
1140 editor["name"] = None
1141 editor["type"] = None
1142 editor["access"] = None
1144 editor["value"] = None
1146 dic["value"] = self.GetTypeName(dic["value"])
1147 elif 0x1600 <= index <= 0x17FF or 0x1A00 <= index <= 0x1C00:
1148 editor["value"] = "map"
1149 dic["value"] = self.MapTranslation["%08X"%dic["value"]]
1151 if dic["type"].startswith("VISIBLE_STRING"):
1152 editor["value"] = "string"
1153 elif dic["type"] in ["TIME_OF_DAY","TIME_DIFFERENCE"]:
1154 editor["value"] = "time"
1155 elif dic["type"] == "DOMAIN":
1156 editor["value"] = "domain"
1157 dic["value"] = dic["value"].encode('hex_codec')
1158 elif dic["type"] == "BOOLEAN":
1159 editor["value"] = "bool"
1160 dic["value"] = BoolType[dic["value"]]
1161 result = type_model.match(dic["type"])
1163 values = result.groups()
1164 if values[0] == "UNSIGNED":
1165 format = "0x%0" + str(int(values[1])/4) + "X"
1166 dic["value"] = format%dic["value"]
1167 editor["value"] = "string"
1168 if values[0] == "INTEGER":
1169 editor["value"] = "number"
1170 elif values[0] == "REAL":
1171 editor["value"] = "float"
1172 elif values[0] == "VISIBLE_STRING":
1173 editor["length"] = values[0]
1174 result = range_model.match(dic["type"])
1176 values = result.groups()
1177 if values[0] in ["UNSIGNED", "INTEGER", "REAL"]:
1178 editor["min"] = values[2]
1179 editor["max"] = values[3]
1180 editors.append(editor)
1181 return data, editors
1185 #-------------------------------------------------------------------------------
1186 # Node Informations Functions
1187 #-------------------------------------------------------------------------------
1189 def GetCustomisedTypeValues(self, index):
1190 values = self.CurrentNode.GetEntry(index)
1191 customisabletypes = self.GetCustomisableTypes()
1192 return values, customisabletypes[values[1]][1]
1194 def GetEntryName(self, index, node = None):
1197 node = self.CurrentNode
1198 NodeMappings = node.GetMappings()
1200 while not result and i < len(NodeMappings):
1201 result = FindEntryName(index, NodeMappings[i])
1204 result = FindEntryName(index, MappingDictionary)
1207 def GetEntryInfos(self, index, node = None):
1210 node = self.CurrentNode
1211 NodeMappings = node.GetMappings()
1213 while not result and i < len(NodeMappings):
1214 result = FindEntryInfos(index, NodeMappings[i])
1217 result = FindEntryInfos(index, MappingDictionary)
1220 def GetSubentryInfos(self, index, subIndex, node = None):
1223 node = self.CurrentNode
1224 NodeMappings = node.GetMappings()
1226 while not result and i < len(NodeMappings):
1227 result = FindSubentryInfos(index, subIndex, NodeMappings[i])
1229 result["user_defined"] = i == len(NodeMappings) - 1 and index >= 0x1000
1232 result = FindSubentryInfos(index, subIndex, MappingDictionary)
1234 result["user_defined"] = False
1237 def GetTypeIndex(self, typename, node = None):
1240 node = self.CurrentNode
1241 NodeMappings = node.GetMappings()
1243 while not result and i < len(NodeMappings):
1244 result = FindTypeIndex(typename, NodeMappings[i])
1247 result = FindTypeIndex(typename, MappingDictionary)
1250 def GetTypeName(self, typeindex, node = None):
1253 node = self.CurrentNode
1254 NodeMappings = node.GetMappings()
1256 while not result and i < len(NodeMappings):
1257 result = FindTypeName(typeindex, NodeMappings[i])
1260 result = FindTypeName(typeindex, MappingDictionary)
1263 def GetTypeDefaultValue(self, typeindex, node = None):
1266 node = self.CurrentNode
1268 NodeMappings = node.GetMappings()
1270 while not result and i < len(NodeMappings):
1271 result = FindTypeDefaultValue(typeindex, NodeMappings[i])
1274 result = FindTypeDefaultValue(typeindex, MappingDictionary)
1277 def GetTypeList(self, node = None):
1278 list = FindTypeList(MappingDictionary)
1280 node = self.CurrentNode
1281 for NodeMapping in self.CurrentNode.GetMappings():
1282 list.extend(FindTypeList(NodeMapping))
1286 def GetMapVariableList(self, node = None):
1287 list = FindMapVariableList(MappingDictionary, self)
1289 node = self.CurrentNode
1290 for NodeMapping in node.GetMappings():
1291 list.extend(FindMapVariableList(NodeMapping, self))
1295 def GetMandatoryIndexes(self, node = None):
1296 list = FindMandatoryIndexes(MappingDictionary)
1298 node = self.CurrentNode
1299 for NodeMapping in node.GetMappings():
1300 list.extend(FindMandatoryIndexes(NodeMapping))
1303 def GetCustomisableTypes(self):
1305 for index, valuetype in CustomisableTypes:
1306 name = self.GetTypeName(index)
1307 dic[index] = [name, valuetype]
1310 def GetCurrentSpecificMenu(self):
1311 if self.CurrentNode:
1312 return self.CurrentNode.GetSpecificMenu()