objdictgen/nodemanager.py
changeset 182 988f2b302aa6
parent 176 5d57fe1e3a5b
child 188 00245bc2e6fe
equal deleted inserted replaced
181:44d812a41d49 182:988f2b302aa6
    24 from gnosis.xml.pickle import *
    24 from gnosis.xml.pickle import *
    25 from gnosis.xml.pickle.util import setParanoia
    25 from gnosis.xml.pickle.util import setParanoia
    26 setParanoia(0)
    26 setParanoia(0)
    27 
    27 
    28 from node import *
    28 from node import *
    29 import eds_in, gen_cfile
    29 import eds_utils, gen_cfile
    30 
    30 
    31 from types import *
    31 from types import *
    32 import os, re
    32 import os, re
    33 
    33 
    34 UndoBufferLength = 20
    34 UndoBufferLength = 20
   298 class NodeManager:
   298 class NodeManager:
   299 
   299 
   300     """
   300     """
   301     Constructor
   301     Constructor
   302     """
   302     """
   303     def __init__(self):
   303     def __init__(self, cwd):
   304         self.LastNewIndex = 0
   304         self.LastNewIndex = 0
   305         self.FilePaths = []
   305         self.FilePaths = []
   306         self.FileNames = []
   306         self.FileNames = []
   307         self.NodeIndex = -1
   307         self.NodeIndex = -1
   308         self.CurrentNode = None
   308         self.CurrentNode = None
       
   309         self.ScriptDirectory = cwd
   309         self.UndoBuffers = []
   310         self.UndoBuffers = []
   310 
   311 
   311 #-------------------------------------------------------------------------------
   312 #-------------------------------------------------------------------------------
   312 #                         Type and Map Variable Lists
   313 #                         Type and Map Variable Lists
   313 #-------------------------------------------------------------------------------
   314 #-------------------------------------------------------------------------------
   356 #-------------------------------------------------------------------------------
   357 #-------------------------------------------------------------------------------
   357 
   358 
   358     """
   359     """
   359     Create a new node and add a new buffer for storing it
   360     Create a new node and add a new buffer for storing it
   360     """
   361     """
   361     def CreateNewNode(self, name, id, type, profile, filepath, NMT, options):
   362     def CreateNewNode(self, name, id, type, description, profile, filepath, NMT, options):
   362         # Create a new node
   363         # Create a new node
   363         node = Node()
   364         node = Node()
   364         # Try to load profile given
   365         # Try to load profile given
   365         result = self.LoadProfile(profile, filepath, node)
   366         result = self.LoadProfile(profile, filepath, node)
   366         if not IsOfType(result, StringType):
   367         if not result:
   367             # if success, initialising node
   368             # if success, initialising node
   368             self.CurrentNode = node
   369             self.CurrentNode = node
   369             self.CurrentNode.SetNodeName(name)
   370             self.CurrentNode.SetNodeName(name)
   370             self.CurrentNode.SetNodeID(id)
   371             self.CurrentNode.SetNodeID(id)
   371             self.CurrentNode.SetNodeType(type)
   372             self.CurrentNode.SetNodeType(type)
       
   373             self.CurrentNode.SetNodeDescription(description)
   372             AddIndexList = self.GetMandatoryIndexes()
   374             AddIndexList = self.GetMandatoryIndexes()
   373             if NMT == "NodeGuarding":
   375             if NMT == "NodeGuarding":
   374                 AddIndexList.extend([0x100C, 0x100D])
   376                 AddIndexList.extend([0x100C, 0x100D])
   375             elif NMT == "Heartbeat":
   377             elif NMT == "Heartbeat":
   376                 AddIndexList.append(0x1017)
   378                 AddIndexList.append(0x1017)
   377             for option in options:
   379             for option in options:
   378                 if option == "DS302":
   380                 if option == "DS302":
       
   381                     DS302Path = os.path.join(self.ScriptDirectory, "config/DS-302.prf")
   379                     # Charging DS-302 profile if choosen by user
   382                     # Charging DS-302 profile if choosen by user
   380                     if os.path.isfile("config/DS-302.prf"):
   383                     if os.path.isfile(DS302Path):
   381                         try:
   384                         try:
   382                         	execfile("config/DS-302.prf")
   385                             execfile(DS302Path)
   383                         	self.CurrentNode.SetDS302Profile(Mapping)
   386                             self.CurrentNode.SetDS302Profile(Mapping)
   384                         	self.CurrentNode.ExtendSpecificMenu(AddMenuEntries)
   387                             self.CurrentNode.ExtendSpecificMenu(AddMenuEntries)
   385                         except:
   388                         except:
   386                             return "Problem with DS-302! Syntax Error."
   389                             return "Problem with DS-302! Syntax Error."
   387                     else:
   390                     else:
   388                         return "Couldn't find DS-302 in 'config' folder!"
   391                         return "Couldn't find DS-302 in 'config' folder!"
   389                 elif option == "GenSYNC":
   392                 elif option == "GenSYNC":
   415             try:
   418             try:
   416                 execfile(filepath)
   419                 execfile(filepath)
   417                 node.SetProfileName(profile)
   420                 node.SetProfileName(profile)
   418                 node.SetProfile(Mapping)
   421                 node.SetProfile(Mapping)
   419                 node.SetSpecificMenu(AddMenuEntries)
   422                 node.SetSpecificMenu(AddMenuEntries)
   420                 return True
   423                 return None
   421             except:
   424             except:
   422                 return "Bad OD Profile file!\nSyntax Error."
   425                 return "Syntax Error\nBad OD Profile file!."
   423         else:
   426         else:
   424             # Default profile
   427             # Default profile
   425             node.SetProfileName("None")
   428             node.SetProfileName("None")
   426             node.SetProfile({})
   429             node.SetProfile({})
   427             node.SetSpecificMenu([])
   430             node.SetSpecificMenu([])
   428             return True
   431             return None
   429 
   432 
   430     """
   433     """
   431     Open a file and store it in a new buffer
   434     Open a file and store it in a new buffer
   432     """
   435     """
   433     def OpenFileInCurrent(self, filepath):
   436     def OpenFileInCurrent(self, filepath):
   471             self.RemoveNodeBuffer(self.NodeIndex)
   474             self.RemoveNodeBuffer(self.NodeIndex)
   472             return True
   475             return True
   473         return False
   476         return False
   474 
   477 
   475     """
   478     """
   476     Import a xml file and store it in a new buffer if no node edited
   479     Import an eds file and store it in a new buffer if no node edited
   477     """
   480     """
   478     def ImportCurrentFromFile(self, filepath):
   481     def ImportCurrentFromEDSFile(self, filepath):
   479         # Generate node from definition in a xml file
   482         # Generate node from definition in a xml file
   480         node = eds_in.GenerateNode(filepath, self)
   483         result = eds_utils.GenerateNode(filepath, self, self.ScriptDirectory)
   481         if node:
   484         if isinstance(result, Node):
   482             self.CurrentNode = node
   485             self.CurrentNode = result
   483             self.GenerateTypeList()
   486             self.GenerateTypeList()
   484             self.GenerateMapList()
   487             self.GenerateMapList()
   485             if len(self.UndoBuffers) == 0:
   488             if len(self.UndoBuffers) == 0:
   486                 self.AddNodeBuffer()
   489                 self.AddNodeBuffer()
   487                 self.SetCurrentFilePath("")
   490                 self.SetCurrentFilePath("")
   488             self.BufferCurrentNode()
   491             self.BufferCurrentNode()
   489         return result
   492             return None
       
   493         else:
       
   494             return result
       
   495     
       
   496     """
       
   497     Export to an eds file and store it in a new buffer if no node edited
       
   498     """
       
   499     def ExportCurrentToEDSFile(self, filepath):
       
   500         return eds_utils.GenerateEDSFile(filepath, self)
   490     
   501     
   491     """
   502     """
   492     Build the C definition of Object Dictionary for current node 
   503     Build the C definition of Object Dictionary for current node 
   493     """
   504     """
   494     def ExportCurrentToFile(self, filepath):
   505     def ExportCurrentToCFile(self, filepath):
   495         return gen_cfile.GenerateFile(filepath, self)
   506         return gen_cfile.GenerateFile(filepath, self)
   496 
   507 
   497 #-------------------------------------------------------------------------------
   508 #-------------------------------------------------------------------------------
   498 #                        Add Entries to Current Functions
   509 #                        Add Entries to Current Functions
   499 #-------------------------------------------------------------------------------
   510 #-------------------------------------------------------------------------------
   643         # Remove all the entries in removinglist
   654         # Remove all the entries in removinglist
   644         for index in removinglist:
   655         for index in removinglist:
   645             self.RemoveCurrentVariable(index)
   656             self.RemoveCurrentVariable(index)
   646         self.BufferCurrentNode()
   657         self.BufferCurrentNode()
   647 
   658 
       
   659 
   648     """
   660     """
   649     Remove an entry from current node. Analize the index to perform the correct
   661     Remove an entry from current node. Analize the index to perform the correct
   650     method
   662     method
   651     """
   663     """
   652     def RemoveCurrentVariable(self, index, subIndex = None):
   664     def RemoveCurrentVariable(self, index, subIndex = None):
   769 
   781 
   770     def SetCurrentEntry(self, index, subIndex, value, name, editor):
   782     def SetCurrentEntry(self, index, subIndex, value, name, editor):
   771         if self.CurrentNode and self.CurrentNode.IsEntry(index):
   783         if self.CurrentNode and self.CurrentNode.IsEntry(index):
   772             if name == "value":
   784             if name == "value":
   773                 if editor == "map":
   785                 if editor == "map":
   774                     value = eval("0x%s"%self.NameTranslation[value])
   786                     try:
   775                     self.CurrentNode.SetEntry(index, subIndex, value)
   787                         value = int(self.NameTranslation[value], 16)
       
   788                         self.CurrentNode.SetEntry(index, subIndex, value)
       
   789                     except:
       
   790                         pass
   776                 elif editor == "bool":
   791                 elif editor == "bool":
   777                     value = value == "True"
   792                     value = value == "True"
   778                     self.CurrentNode.SetEntry(index, subIndex, value)
   793                     self.CurrentNode.SetEntry(index, subIndex, value)
   779                 elif editor == "time":
   794                 elif editor == "time":
       
   795                     self.CurrentNode.SetEntry(index, subIndex, value)
       
   796                 elif editor == "number":
   780                     self.CurrentNode.SetEntry(index, subIndex, value)
   797                     self.CurrentNode.SetEntry(index, subIndex, value)
   781                 elif editor == "domain":
   798                 elif editor == "domain":
   782                     try:
   799                     try:
   783                         if len(value) % 2 != 0:
   800                         if len(value) % 2 != 0:
   784                             value = "0" + value
   801                             value = "0" + value
   794                         dic[typeindex] = typevalue
   811                         dic[typeindex] = typevalue
   795                     if type not in dic:
   812                     if type not in dic:
   796                         type = self.CurrentNode.GetEntry(type)[1]
   813                         type = self.CurrentNode.GetEntry(type)[1]
   797                     if dic[type] == 0:
   814                     if dic[type] == 0:
   798                         try:
   815                         try:
   799                             value = eval(value, {})
   816                             value = int(value, 16)
   800                             self.CurrentNode.SetEntry(index, subIndex, value)
   817                             self.CurrentNode.SetEntry(index, subIndex, value)
   801                         except:
   818                         except:
   802                             pass
   819                             pass
   803                     else:
   820                     else:
   804                         self.CurrentNode.SetEntry(index, subIndex, value)
   821                         self.CurrentNode.SetEntry(index, subIndex, value)
  1005 
  1022 
  1006     def GetCurrentNodeInfos(self):
  1023     def GetCurrentNodeInfos(self):
  1007         name = self.CurrentNode.GetNodeName()
  1024         name = self.CurrentNode.GetNodeName()
  1008         id = self.CurrentNode.GetNodeID()
  1025         id = self.CurrentNode.GetNodeID()
  1009         type = self.CurrentNode.GetNodeType()
  1026         type = self.CurrentNode.GetNodeType()
  1010         return name, id, type
  1027         description = self.CurrentNode.GetNodeDescription()
       
  1028         return name, id, type, description
  1011         
  1029         
  1012     def SetCurrentNodeInfos(self, name, id, type):
  1030     def SetCurrentNodeInfos(self, name, id, type, description):
  1013         self.CurrentNode.SetNodeName(name)
  1031         self.CurrentNode.SetNodeName(name)
  1014         self.CurrentNode.SetNodeID(id)
  1032         self.CurrentNode.SetNodeID(id)
  1015         self.CurrentNode.SetNodeType(type)
  1033         self.CurrentNode.SetNodeType(type)
       
  1034         self.CurrentNode.SetNodeDescription(description)
  1016         self.BufferCurrentNode()
  1035         self.BufferCurrentNode()
  1017 
  1036 
  1018     def GetCurrentProfileName(self):
  1037     def GetCurrentProfileName(self):
  1019         if self.CurrentNode:
  1038         if self.CurrentNode:
  1020             return self.CurrentNode.GetProfileName()
  1039             return self.CurrentNode.GetProfileName()
  1140                             editor["value"] = "bool"
  1159                             editor["value"] = "bool"
  1141                             dic["value"] = BoolType[dic["value"]]
  1160                             dic["value"] = BoolType[dic["value"]]
  1142                         result = type_model.match(dic["type"])
  1161                         result = type_model.match(dic["type"])
  1143                         if result:
  1162                         if result:
  1144                             values = result.groups()
  1163                             values = result.groups()
  1145                             if values[0] in ["INTEGER", "UNSIGNED"]:
  1164                             if values[0] == "UNSIGNED":
  1146                                 format = "0x%0" + str(int(values[1])/4) + "X"
  1165                                 format = "0x%0" + str(int(values[1])/4) + "X"
  1147                                 dic["value"] = format%dic["value"]
  1166                                 dic["value"] = format%dic["value"]
  1148                                 editor["value"] = "string"
  1167                                 editor["value"] = "string"
       
  1168                             if values[0] == "INTEGER":
       
  1169                                 editor["value"] = "number"
  1149                             elif values[0] == "REAL":
  1170                             elif values[0] == "REAL":
  1150                                 editor["value"] = "float"
  1171                                 editor["value"] = "float"
  1151                             elif values[0] == "VISIBLE_STRING":
  1172                             elif values[0] == "VISIBLE_STRING":
  1152                                 editor["length"] = values[0]
  1173                                 editor["length"] = values[0]
  1153                         result = range_model.match(dic["type"])
  1174                         result = range_model.match(dic["type"])
  1154                         if result:
  1175                         if result:
  1155                             values = result.groups()
  1176                             values = result.groups()
  1156                             if values[0] in ("UNSIGNED", "REAL"):
  1177                             if values[0] in ["UNSIGNED", "INTEGER", "REAL"]:
  1157                                 editor["min"] = values[2]
  1178                                 editor["min"] = values[2]
  1158                                 editor["max"] = values[3]
  1179                                 editor["max"] = values[3]
  1159                 editors.append(editor)
  1180                 editors.append(editor)
  1160             return data, editors
  1181             return data, editors
  1161         else:
  1182         else:
  1168     def GetCustomisedTypeValues(self, index):
  1189     def GetCustomisedTypeValues(self, index):
  1169         values = self.CurrentNode.GetEntry(index)
  1190         values = self.CurrentNode.GetEntry(index)
  1170         customisabletypes = self.GetCustomisableTypes()
  1191         customisabletypes = self.GetCustomisableTypes()
  1171         return values, customisabletypes[values[1]][1]
  1192         return values, customisabletypes[values[1]][1]
  1172 
  1193 
  1173     def GetEntryName(self, index, node = True):
  1194     def GetEntryName(self, index, node = None):
  1174         result = None
  1195         result = None
  1175         if node:
  1196         if node == None:
  1176             NodeMappings = self.CurrentNode.GetMappings()
  1197             node = self.CurrentNode
  1177             i = 0
  1198         NodeMappings = node.GetMappings()
  1178             while not result and i < len(NodeMappings):
  1199         i = 0
  1179                 result = FindEntryName(index, NodeMappings[i])
  1200         while not result and i < len(NodeMappings):
  1180                 i += 1
  1201             result = FindEntryName(index, NodeMappings[i])
       
  1202             i += 1
  1181         if result == None:
  1203         if result == None:
  1182             result = FindEntryName(index, MappingDictionary)
  1204             result = FindEntryName(index, MappingDictionary)
  1183         return result
  1205         return result
  1184     
  1206     
  1185     def GetEntryInfos(self, index, node = True):
  1207     def GetEntryInfos(self, index, node = None):
  1186         result = None
  1208         result = None
  1187         if node:
  1209         if node == None:
  1188             NodeMappings = self.CurrentNode.GetMappings()
  1210             node = self.CurrentNode
  1189             i = 0
  1211         NodeMappings = node.GetMappings()
  1190             while not result and i < len(NodeMappings):
  1212         i = 0
  1191                 result = FindEntryInfos(index, NodeMappings[i])
  1213         while not result and i < len(NodeMappings):
  1192                 i += 1
  1214             result = FindEntryInfos(index, NodeMappings[i])
       
  1215             i += 1
  1193         if result == None:
  1216         if result == None:
  1194             result = FindEntryInfos(index, MappingDictionary)
  1217             result = FindEntryInfos(index, MappingDictionary)
  1195         return result
  1218         return result
  1196     
  1219     
  1197     def GetSubentryInfos(self, index, subIndex, node = True):
  1220     def GetSubentryInfos(self, index, subIndex, node = None):
  1198         result = None
  1221         result = None
  1199         if node:
  1222         if node == None:
  1200             NodeMappings = self.CurrentNode.GetMappings()
  1223             node = self.CurrentNode
  1201             i = 0
  1224         NodeMappings = node.GetMappings()
  1202             while not result and i < len(NodeMappings):
  1225         i = 0
  1203                 result = FindSubentryInfos(index, subIndex, NodeMappings[i])
  1226         while not result and i < len(NodeMappings):
  1204                 if result:
  1227             result = FindSubentryInfos(index, subIndex, NodeMappings[i])
  1205                     result["user_defined"] = i == len(NodeMappings) - 1 and index >= 0x1000
  1228             if result:
  1206                 i += 1
  1229                 result["user_defined"] = i == len(NodeMappings) - 1 and index >= 0x1000
       
  1230             i += 1
  1207         if result == None:    
  1231         if result == None:    
  1208             result = FindSubentryInfos(index, subIndex, MappingDictionary)
  1232             result = FindSubentryInfos(index, subIndex, MappingDictionary)
  1209             if result:
  1233             if result:
  1210                 result["user_defined"] = False
  1234                 result["user_defined"] = False
  1211         return result
  1235         return result
  1212     
  1236     
  1213     def GetTypeIndex(self, typename, node = True):
  1237     def GetTypeIndex(self, typename, node = None):
  1214         result = None
  1238         result = None
  1215         if node:
  1239         if node == None:
  1216             NodeMappings = self.CurrentNode.GetMappings()
  1240             node = self.CurrentNode
  1217             i = 0
  1241         NodeMappings = node.GetMappings()
  1218             while not result and i < len(NodeMappings):
  1242         i = 0
  1219                 result = FindTypeIndex(typename, NodeMappings[i])
  1243         while not result and i < len(NodeMappings):
  1220                 i += 1
  1244             result = FindTypeIndex(typename, NodeMappings[i])
       
  1245             i += 1
  1221         if result == None:
  1246         if result == None:
  1222             result = FindTypeIndex(typename, MappingDictionary)
  1247             result = FindTypeIndex(typename, MappingDictionary)
  1223         return result
  1248         return result
  1224     
  1249     
  1225     def GetTypeName(self, typeindex, node = True):
  1250     def GetTypeName(self, typeindex, node = None):
  1226         result = None
  1251         result = None
  1227         if node:
  1252         if node == None:
  1228             NodeMappings = self.CurrentNode.GetMappings()
  1253             node = self.CurrentNode
  1229             i = 0
  1254         NodeMappings = node.GetMappings()
  1230             while not result and i < len(NodeMappings):
  1255         i = 0
  1231                 result = FindTypeName(typeindex, NodeMappings[i])
  1256         while not result and i < len(NodeMappings):
  1232                 i += 1
  1257             result = FindTypeName(typeindex, NodeMappings[i])
       
  1258             i += 1
  1233         if result == None:
  1259         if result == None:
  1234             result = FindTypeName(typeindex, MappingDictionary)
  1260             result = FindTypeName(typeindex, MappingDictionary)
  1235         return result
  1261         return result
  1236     
  1262     
  1237     def GetTypeDefaultValue(self, typeindex, node = True):
  1263     def GetTypeDefaultValue(self, typeindex, node = None):
  1238         result = None
  1264         result = None
       
  1265         if node == None:
       
  1266             node = self.CurrentNode
  1239         if node:
  1267         if node:
  1240             NodeMappings = self.CurrentNode.GetMappings()
  1268             NodeMappings = node.GetMappings()
  1241             i = 0
  1269             i = 0
  1242             while not result and i < len(NodeMappings):
  1270             while not result and i < len(NodeMappings):
  1243                 result = FindTypeDefaultValue(typeindex, NodeMappings[i])
  1271                 result = FindTypeDefaultValue(typeindex, NodeMappings[i])
  1244                 i += 1
  1272                 i += 1
  1245         if result == None:
  1273         if result == None:
  1246             result = FindTypeDefaultValue(typeindex, MappingDictionary)
  1274             result = FindTypeDefaultValue(typeindex, MappingDictionary)
  1247         return result
  1275         return result
  1248     
  1276     
  1249     def GetTypeList(self, node = True):
  1277     def GetTypeList(self, node = None):
  1250         list = FindTypeList(MappingDictionary)
  1278         list = FindTypeList(MappingDictionary)
  1251         if node:
  1279         if node == None:
  1252             for NodeMapping in self.CurrentNode.GetMappings():
  1280             node = self.CurrentNode
  1253                 list.extend(FindTypeList(NodeMapping))
  1281         for NodeMapping in self.CurrentNode.GetMappings():
       
  1282             list.extend(FindTypeList(NodeMapping))
  1254         list.sort()
  1283         list.sort()
  1255         return list
  1284         return list
  1256     
  1285     
  1257     def GetMapVariableList(self):
  1286     def GetMapVariableList(self, node = None):
  1258         list = FindMapVariableList(MappingDictionary, self)
  1287         list = FindMapVariableList(MappingDictionary, self)
  1259         for NodeMapping in self.CurrentNode.GetMappings():
  1288         if node == None:
       
  1289             node = self.CurrentNode
       
  1290         for NodeMapping in node.GetMappings():
  1260             list.extend(FindMapVariableList(NodeMapping, self))
  1291             list.extend(FindMapVariableList(NodeMapping, self))
  1261         list.sort()
  1292         list.sort()
  1262         return list
  1293         return list
  1263     
  1294     
  1264     def GetMandatoryIndexes(self, node = True):
  1295     def GetMandatoryIndexes(self, node = None):
  1265         list = FindMandatoryIndexes(MappingDictionary)
  1296         list = FindMandatoryIndexes(MappingDictionary)
  1266         if node:
  1297         if node == None:
  1267             for NodeMapping in self.CurrentNode.GetMappings():
  1298             node = self.CurrentNode
  1268                 list.extend(FindMandatoryIndexes(NodeMapping))
  1299         for NodeMapping in node.GetMappings():
       
  1300             list.extend(FindMandatoryIndexes(NodeMapping))
  1269         return list
  1301         return list
  1270     
  1302     
  1271     def GetCustomisableTypes(self):
  1303     def GetCustomisableTypes(self):
  1272         dic = {}
  1304         dic = {}
  1273         for index, valuetype in CustomisableTypes:
  1305         for index, valuetype in CustomisableTypes:
  1278     def GetCurrentSpecificMenu(self):
  1310     def GetCurrentSpecificMenu(self):
  1279         if self.CurrentNode:
  1311         if self.CurrentNode:
  1280             return self.CurrentNode.GetSpecificMenu()
  1312             return self.CurrentNode.GetSpecificMenu()
  1281         return []
  1313         return []
  1282 
  1314 
  1283