svghmi/hmi_tree.py
branchpython3
changeset 3750 f62625418bff
parent 3381 3a0908b0319d
child 3817 3deeda82636a
equal deleted inserted replaced
3749:fda6c1a37662 3750:f62625418bff
     4 # This file is part of Beremiz
     4 # This file is part of Beremiz
     5 # Copyright (C) 2021: Edouard TISSERANT
     5 # Copyright (C) 2021: Edouard TISSERANT
     6 #
     6 #
     7 # See COPYING file for copyrights details.
     7 # See COPYING file for copyrights details.
     8 
     8 
     9 from __future__ import absolute_import
     9 
    10 from itertools import izip, imap
    10 
    11 from pprint import pformat
    11 from pprint import pformat
    12 import weakref
    12 import weakref
    13 import hashlib
    13 import hashlib
    14 
    14 
    15 from lxml import etree
    15 from lxml import etree
    20     "HMI_INT":{},
    20     "HMI_INT":{},
    21     "HMI_BOOL":{},
    21     "HMI_BOOL":{},
    22     "HMI_REAL":{}
    22     "HMI_REAL":{}
    23 }
    23 }
    24 
    24 
    25 HMI_TYPES = HMI_TYPES_DESC.keys()
    25 HMI_TYPES = list(HMI_TYPES_DESC.keys())
    26 
    26 
    27 class HMITreeNode(object):
    27 class HMITreeNode(object):
    28     def __init__(self, path, name, nodetype, iectype = None, vartype = None, cpath = None, hmiclass = None):
    28     def __init__(self, path, name, nodetype, iectype = None, vartype = None, cpath = None, hmiclass = None):
    29         self.path = path
    29         self.path = path
    30         self.name = name
    30         self.name = name
    54         known_best_match = 0
    54         known_best_match = 0
    55         potential_siblings = {}
    55         potential_siblings = {}
    56         for child in self.children:
    56         for child in self.children:
    57             if child.path is not None:
    57             if child.path is not None:
    58                 in_common = 0
    58                 in_common = 0
    59                 for child_path_item, node_path_item in izip(child.path, node.path):
    59                 for child_path_item, node_path_item in zip(child.path, node.path):
    60                     if child_path_item == node_path_item:
    60                     if child_path_item == node_path_item:
    61                         in_common +=1
    61                         in_common +=1
    62                     else:
    62                     else:
    63                         break
    63                         break
    64                 # Match can only be HMI_NODE, and the whole path of node
    64                 # Match can only be HMI_NODE, and the whole path of node
   103             attribs["hash"] = ",".join(map(str,self.hash()))
   103             attribs["hash"] = ",".join(map(str,self.hash()))
   104 
   104 
   105         res = etree.Element(self.nodetype, **attribs)
   105         res = etree.Element(self.nodetype, **attribs)
   106 
   106 
   107         if hasattr(self, "children"):
   107         if hasattr(self, "children"):
   108             for child_etree in imap(lambda c:c.etree(), self.children):
   108             for child_etree in map(lambda c:c.etree(), self.children):
   109                 res.append(child_etree)
   109                 res.append(child_etree)
   110 
   110 
   111         return res
   111         return res
   112 
   112 
   113     @classmethod
   113     @classmethod
   148     def hash(self):
   148     def hash(self):
   149         """ Produce a hash, any change in HMI tree structure change that hash """
   149         """ Produce a hash, any change in HMI tree structure change that hash """
   150         s = hashlib.new('md5')
   150         s = hashlib.new('md5')
   151         self._hash(s)
   151         self._hash(s)
   152         # limit size to HMI_HASH_SIZE as in svghmi.c
   152         # limit size to HMI_HASH_SIZE as in svghmi.c
   153         return map(ord,s.digest())[:8]
   153         return list(map(ord,s.digest()))[:8]
   154 
   154 
   155     def _hash(self, s):
   155     def _hash(self, s):
   156         s.update(str((self.name,self.nodetype)))
   156         s.update(str((self.name,self.nodetype)))
   157         if hasattr(self, "children"):
   157         if hasattr(self, "children"):
   158             for c in self.children:
   158             for c in self.children: