mise à jour 13.11.3

DecodeDatabase.py 23.3 KB
Newer Older
aleajactaest's avatar
update  
aleajactaest committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# module DecodeDatabase
#
#    Copyright (C) 2019  AleaJactaEst
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging
from tools import getPowerOf2
aleajactaest's avatar
aleajactaest committed
23
from tools import BitStream
aleajactaest's avatar
update  
aleajactaest committed
24 25 26 27

LOGGER='DecodeDatabase'

def show_dico(dico,  level=1):
aleajactaest's avatar
aleajactaest committed
28 29
    try:
        for ele in dico:
aleajactaest's avatar
update  
aleajactaest committed
30
            if isinstance(dico[ele], dict):
aleajactaest's avatar
update  
aleajactaest committed
31
                logging.getLogger(LOGGER).debug("%s %s %s" % ("." * level,  ele ,  ":"))
aleajactaest's avatar
aleajactaest committed
32 33 34
                if isinstance(dico[ele], dict):
                    show_dico(dico[ele],  level+1)
            else:
aleajactaest's avatar
update  
aleajactaest committed
35
                logging.getLogger(LOGGER).debug("%s %s %s" % ("." * level,  ele,  ':',  dico[ele]))
aleajactaest's avatar
aleajactaest committed
36
    except:
aleajactaest's avatar
update  
aleajactaest committed
37
        logging.getLogger(LOGGER).debug("empty")
aleajactaest's avatar
update  
aleajactaest committed
38 39 40 41 42 43 44 45 46 47 48

def child(ele):
    ret = {}
    ret_branch = {}
    ref_other = {}
    id_branch = 0
    id_other = 0
    min_i = -1
    max_i = -1
    for k in ele.keys():
        ret[k] = ele.get(k)
aleajactaest's avatar
update  
aleajactaest committed
49
        logging.getLogger(LOGGER).debug("%s %s" % (str(k), str(ele.get(k))))
aleajactaest's avatar
update  
aleajactaest committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    for _child in list(ele):
        x = child(_child)
        if x['name'] == 'branch':
            ret_branch.setdefault(id_branch,  x)
            id_branch += 1
        else:
            ref_other.setdefault(id_other,  x)
            id_other += 1
    if ret_branch or ref_other:
        ret['child'] = {}
    id = 0
    for x in ret_branch:
        min_i = max_i + 1
        max_i = min_i
        #show_dico( ret_branch[x])
        if  'count' in ret_branch[x]:
            max_i = min_i + int(ret_branch[x]['count']) - 1
        ret_branch[x]['min'] = min_i
        ret_branch[x]['max'] = max_i
        ret['child'].setdefault(id,  ret_branch[x])
        id += 1
    for x in ref_other:
        min_i = max_i + 1
        max_i = min_i
        #show_dico( ref_other[x])
        if  'count' in ref_other[x]:
            max_i = min_i + int(ref_other[x]['count']) - 1
        ref_other[x]['min'] = min_i
        ref_other[x]['max'] = max_i
        ret['child'].setdefault(id,  ref_other[x])
        id += 1
    return ret

def count_elements(head):
aleajactaest's avatar
aleajactaest committed
84 85 86 87 88
    #print(head)
    if 'child' in head.keys():
        list_ele = [x for x in list(head['child'].keys()) if isinstance(x, int)]
    else:
        list_ele = [x for x in list(head.keys()) if isinstance(x, int)]
aleajactaest's avatar
update  
aleajactaest committed
89
    logging.getLogger(LOGGER).debug(str(list_ele))
aleajactaest's avatar
aleajactaest committed
90
    last_key = max(list_ele)
aleajactaest's avatar
update  
aleajactaest committed
91
    logging.getLogger(LOGGER).debug(str(last_key ))
aleajactaest's avatar
update  
aleajactaest committed
92
    try:
aleajactaest's avatar
aleajactaest committed
93 94 95 96 97
        last_key = max(list_ele)
        if 'child' in head.keys():
            return head['child'][last_key]['max'] + 1
        else:
            return head[last_key]['max'] + 1
aleajactaest's avatar
update  
aleajactaest committed
98
    except TypeError:
aleajactaest's avatar
aleajactaest committed
99
        return 0
aleajactaest's avatar
update  
aleajactaest committed
100 101
    
def get_element(head,  id):
aleajactaest's avatar
update  
aleajactaest committed
102
    logging.getLogger(LOGGER).debug("id: %s" %  str(id))
aleajactaest's avatar
aleajactaest committed
103
    for ele in head['child']:
aleajactaest's avatar
update  
aleajactaest committed
104 105 106 107
        logging.getLogger(LOGGER).debug("ele:%s" % str(ele))
        logging.getLogger(LOGGER).debug("head:%s" %  str(head))
        logging.getLogger(LOGGER).debug("max:%s",  str(head['child'][ele]['max']))
        logging.getLogger(LOGGER).debug("min:%s",  str(head['child'][ele]['min']))
aleajactaest's avatar
aleajactaest committed
108 109
        if id <= head['child'][ele]['max'] and id >= head['child'][ele]['min']:
            return head['child'][ele]
aleajactaest's avatar
update  
aleajactaest committed
110
    return None
aleajactaest's avatar
aleajactaest committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124


class LeafDatabase():
    def __init__(self):
        self.name = ""
        self.type = ""
        self.count = None
        
    def loadXml(self,  xmldata):
        self.name = xmldata.get('name')
        self.type = xmldata.get('type')
        if xmldata.get('count'):
            self.count = int(xmldata.get('count'))

aleajactaest's avatar
aleajactaest committed
125 126 127 128
    def loadXmlWithoutCount(self,  xmldata, extra=""):
        self.name = xmldata.get('name') + extra
        self.type = xmldata.get('type')

aleajactaest's avatar
aleajactaest committed
129 130
    def countLeaves(self):
        if self.count:
aleajactaest's avatar
update  
aleajactaest committed
131
            logging.getLogger(LOGGER).debug("countLeaves leaf %s (nb:%s)" % (self.name, str(self.count)))
aleajactaest's avatar
aleajactaest committed
132 133
            return self.count
        else:
aleajactaest's avatar
update  
aleajactaest committed
134
            logging.getLogger(LOGGER).debug("countLeaves leaf %s (nb:1)" % (self.name))
aleajactaest's avatar
aleajactaest committed
135 136 137
            return 1

    def show(self, level=1):
aleajactaest's avatar
aleajactaest committed
138 139
        #logging.getLogger(LOGGER).debug("%s %s Leaf %s : %s : %s" % ("*" * level,  str(level), self.name, str(self.count), str(self.type)))
        logging.getLogger(LOGGER).debug("%s %s Leaf %s [%s]" % ("*" * level,  str(level), self.name, str(self.type)))
aleajactaest's avatar
aleajactaest committed
140 141 142

    def execute(self,  msgin,  name=""):
        if name:
aleajactaest's avatar
update  
aleajactaest committed
143
            tmp = name
aleajactaest's avatar
aleajactaest committed
144
        else:
aleajactaest's avatar
update  
aleajactaest committed
145 146
            tmp = self.name
        if self.type[0] == 'I': # Unsigned
aleajactaest's avatar
aleajactaest committed
147 148
            logging.getLogger(LOGGER).debug("Read:" + str (self.type))
            value = int(self.type[1:])
149
            _ = msgin.readSerialUint64(value, name=tmp, typeName="Uint" + str(value))
aleajactaest's avatar
update  
aleajactaest committed
150 151 152 153
        elif self.type[0] == 'S': # Signed
            logging.getLogger(LOGGER).debug("Read:" + str (self.type))
            value = int(self.type[1:])
            # _ = msgin.readNbChar(value, name='DatabaseXML' + tmp)
154
            _ = msgin.readSerialUint64(value, name=tmp, typeName="Sint" + str(value))
aleajactaest's avatar
update  
aleajactaest committed
155 156 157
        elif self.type == 'TEXT':
            logging.getLogger(LOGGER).debug("type:" + str (self.type))
            value = 32
158
            _ = msgin.readSerialSint64(value,  name=idname, typeName="Uint32/TEXT")
aleajactaest's avatar
aleajactaest committed
159
        else:
aleajactaest's avatar
update  
aleajactaest committed
160
            logging.getLogger(LOGGER).error("Type inconnu:" + str (self.type))
aleajactaest's avatar
aleajactaest committed
161 162 163 164
            raise "type not managed"
            
    def execute_atom(self,  level,  pos,  msgin,  name=""):
        if name:
aleajactaest's avatar
update  
aleajactaest committed
165
            extraName = name + "/" + self.name
aleajactaest's avatar
aleajactaest committed
166
        else:
aleajactaest's avatar
update  
aleajactaest committed
167
            extraName = self.name
aleajactaest's avatar
aleajactaest committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        if self.count:
            step = self.count
        else:
            step = 1
        for step in range(0, step):
            if self.count:
                idname = extraName + str(step)
            else:
                idname = extraName
            logging.getLogger(LOGGER).debug('step:' + str(step) + ' level:'+ str(level) + ' pos:' + str(pos) + ' idname:' + idname)
            if level < pos:
                level += 1
                continue
            if self.type[0] == 'I':
                value = int(self.type[1:])
aleajactaest's avatar
update  
aleajactaest committed
183 184 185
                if value > 64:
                    raise "type not managed"
                logging.getLogger(LOGGER).debug('value:' + str(value))
186
                _ = msgin.readSerialUint64(value, name=idname, typeName="Uint" + str(value))
aleajactaest's avatar
aleajactaest committed
187
                return level+1
aleajactaest's avatar
update  
aleajactaest committed
188 189 190
            elif self.type[0] == 'S':
                logging.getLogger(LOGGER).debug("type:" + str (self.type))
                value = int(self.type[1:])
191 192
                _ = msgin.readSerialSint64(value, name=idname, typeName="Sint" + str(value))
                return level+1
aleajactaest's avatar
update  
aleajactaest committed
193 194 195
            elif self.type == 'TEXT':
                logging.getLogger(LOGGER).debug("type:" + str (self.type))
                value = 32
196 197
                _ = msgin.readSerialUint64(value,  name=idname, typeName="Uint32/TEXT")
                return level+1
aleajactaest's avatar
aleajactaest committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            else:
                logging.getLogger(LOGGER).debug("Type inconnu:",  self.type)
                raise "type not managed"
        return level


class BranchDatabase():
    def __init__(self):
        self.name = None
        self.branch = []
        self.leaf = []
        self.min = None
        self.max = None
        self.atom = False
        self.count = None
        
aleajactaest's avatar
aleajactaest committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    def loadXmlCount(self,  xmldata, extra=""):
        if  xmldata.get('atom'):
            self.atom = True
        self.name = xmldata.get('name') + extra
        for ele in xmldata:
            if ele.tag == 'branch':
                newbranch = BranchDatabase()
                newbranch.loadXml(ele,  extra)
                self.branch.append(newbranch)
            elif ele.tag == 'leaf':
                newleaf = LeafDatabase()
                newleaf.loadXml(ele)
                self.leaf.append(newleaf)
        
    def loadXml2(self,  xmldata, extra="",  filter=None):
aleajactaest's avatar
aleajactaest committed
229 230 231 232 233 234
        if filter:
            if 'bank' in xmldata:
                if filter != xmldata['bank']:
                    return
            else:
                return
aleajactaest's avatar
aleajactaest committed
235 236 237 238 239 240 241 242 243 244 245
        if xmldata.get('count'):
            self.count = int(xmldata.get('count'))
            self.name = xmldata.get('name')
            newbranch = BranchDatabase()
            for i in range(0,  self.count):
                newbranch.loadXmlCount(xmldata,  str(i))
            self.branch.append(newbranch)
        else:
            self.loadXmlCount(xmldata,  extra)

    def loadXmlWithoutCount(self,  xmldata, extra=""):
aleajactaest's avatar
aleajactaest committed
246 247
        if  xmldata.get('atom'):
            self.atom = True
aleajactaest's avatar
aleajactaest committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        self.name = xmldata.get('name') + extra
        for ele in xmldata:
            if ele.tag == 'branch':
                newbranch = BranchDatabase()
                newbranch.loadXml(ele)
                self.branch.append(newbranch)
            elif ele.tag == 'leaf':
                if ele.get('count'):
                    count = int(ele.get('count'))
                    for i in range(0,  count):
                        newleaf = LeafDatabase()
                        newleaf.loadXmlWithoutCount(ele,  str(i))
                        self.leaf.append(newleaf)
                else:
                    newleaf = LeafDatabase()
                    newleaf.loadXmlWithoutCount(ele)
                    self.leaf.append(newleaf)
        
    def loadXml(self,  xmldata, extra="",  filter=None):
        if filter:
            if 'bank' in xmldata:
                if filter != xmldata['bank']:
                    return
            else:
                return
        self.name = xmldata.get('name') + extra
        if  xmldata.get('atom'):
            self.atom = True
        for ele in xmldata:
            if ele.get('count'):
                count = int(ele.get('count'))
                if ele.tag == 'branch':
                    for i in range(0,  count):
                        newbranch = BranchDatabase()
                        newbranch.loadXml(ele,  str(i))
                        self.branch.append(newbranch)
                elif ele.tag == 'leaf':
                    for i in range(0,  count):
                        newleaf = LeafDatabase()
                        newleaf.loadXmlWithoutCount(ele,  str(i))
                        self.leaf.append(newleaf)
            else:
                if ele.tag == 'branch':
                    newbranch = BranchDatabase()
                    newbranch.loadXml(ele,  "")
                    self.branch.append(newbranch)
                elif ele.tag == 'leaf':
                    newleaf = LeafDatabase()
                    newleaf.loadXmlWithoutCount(ele,  "")
                    self.leaf.append(newleaf)
                
    def loadXmlOld(self,  xmldata, extra="",  filter=None):
        if filter:
            if 'bank' in xmldata:
                if filter != xmldata['bank']:
                    return
            else:
                return
aleajactaest's avatar
aleajactaest committed
306 307
        if xmldata.get('count'):
            self.count = int(xmldata.get('count'))
aleajactaest's avatar
aleajactaest committed
308 309
        if  xmldata.get('atom'):
            self.atom = True
aleajactaest's avatar
aleajactaest committed
310 311 312 313 314 315 316 317 318 319
        self.name = xmldata.get('name')
        for ele in xmldata:
            if ele.tag == 'branch':
                newbranch = BranchDatabase()
                newbranch.loadXml(ele)
                self.branch.append(newbranch)
            elif ele.tag == 'leaf':
                newleaf = LeafDatabase()
                newleaf.loadXml(ele)
                self.leaf.append(newleaf)
aleajactaest's avatar
aleajactaest committed
320

aleajactaest's avatar
aleajactaest committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    def loadRootXml(self,  xmldata,  filter=None):
        for ele in xmldata:
            if ele.tag == 'branch':
                if filter:
                    if ele.get('bank') == filter:
#                        print(ele.get('bank'), filter)
                        newbranch = BranchDatabase()
                        newbranch.loadXml(ele)
                        self.branch.append(newbranch)
                        
    def getIdBits(self):
        count = 0
        for ele in self.branch:
            if ele.count:
                count += ele.count
            else:
                count += 1
        for ele in self.leaf:
            if ele.count:
                count += ele.count
            else:
                count += 1
        return count

    def countLeaves(self):
        count = 0
        for ele in self.branch:
            count += ele.countLeaves()
        for ele in self.leaf:
            count += ele.countLeaves()
aleajactaest's avatar
aleajactaest committed
351
#        if self.count and self.atom:
aleajactaest's avatar
update  
aleajactaest committed
352
#            logging.getLogger(LOGGER).debug("countLeaves branch <- %s (nb:%s)" % (self.name, str(self.count)))
aleajactaest's avatar
aleajactaest committed
353
#            count *= self.count
aleajactaest's avatar
aleajactaest committed
354 355 356 357 358 359 360 361
        return count 
        
    def execute_atom_found(self, level,  pos,  msgin,  name=""):
        if self.count:
            step = self.count
        else:
            step=1
        if name:
aleajactaest's avatar
update  
aleajactaest committed
362
            extraName = name + self.name
aleajactaest's avatar
aleajactaest committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
        else:
            extraName = self.name
        for step in range(0, step):
            logging.getLogger(LOGGER).debug('step:' + str(step)+ ' level:' + str(level) + ' pos:' + str(pos) + ' name:' + name)
            if self.count:
                idname = extraName + str(step)
            else:
                idname = extraName
            for ele in self.branch:
                level = ele.execute_atom_found(level,  pos,  msgin, idname)
                logging.getLogger(LOGGER).debug('step:' + str(step)+ ' level:' + str(level) + ' pos:' + str(pos) + ' name:' + name + ' idname:' + str(idname))
                if level > pos:
                    return level
            for ele in self.leaf:
                level = ele.execute_atom(level,  pos,  msgin, idname)
                logging.getLogger(LOGGER).debug('step:' + str(step)+ ' level:' + str(level) + ' pos:' + str(pos) + ' name:' + name + ' idname:' + str(idname))
                if level > pos:
                    return level
        return level 

    def show(self, level=0,  pos=0,  filterlevel=None):
aleajactaest's avatar
aleajactaest committed
384 385
        # logging.getLogger(LOGGER).debug( "%s %s pos:%s Branch:%s : %s : %s - %s" % ("*" * level, str(level), str(pos), str(self.name), str(self.count), str(self.atom), str(self.getIdBits())))
        logging.getLogger(LOGGER).debug( "%s %s pos:%s Branch:%s atom:%s getIdBits:%s" % ("*" * level, str(level), str(pos), str(self.name), str(self.atom), str(self.getIdBits())))
aleajactaest's avatar
aleajactaest committed
386 387 388 389 390 391 392 393 394 395
        if filterlevel is not None:
            if filterlevel <= level:
                return
        i = 0
        for ele in self.branch:
            ele.show(level + 1,  i, filterlevel)
            i += 1
        for ele in self.leaf:
            ele.show(level + 1)

aleajactaest's avatar
update  
aleajactaest committed
396
    def execute_atom(self, msgin,  parent):
aleajactaest's avatar
aleajactaest committed
397
        nbchild = self.countLeaves()
aleajactaest's avatar
update  
aleajactaest committed
398
        # nbchild = len(self.leaf) + len(self.branch)
aleajactaest's avatar
aleajactaest committed
399 400 401 402
        #nbBit = getPowerOf2.getPowerOf2_Bis(nbchild) 
        
        logging.getLogger(LOGGER).debug("needRead:" + str(msgin.needRead()) + " nbchild:" + str(nbchild))
        logging.getLogger(LOGGER).debug(msgin.showAllData())
aleajactaest's avatar
update  
aleajactaest committed
403 404 405 406
        if self.name:
            idname = parent + self.name +'/'
        else:
            idname = parent
407
        cBitSet = msgin.readCBitSet(nbchild,  idname + ":Param",  typeName = 'Uint' + str(nbchild))
aleajactaest's avatar
aleajactaest committed
408 409 410 411 412 413 414 415 416
        #cBitSet = CBitSet.CBitSet()
        #cBitSet.readSerialExtra(msgin, nbchild, self.name + "/Param")
        logging.getLogger(LOGGER).debug(msgin.showAllData())
        todelete_count_true = 0
        for i in range(0, nbchild):
            #ii = nbchild - i
            logging.getLogger(LOGGER).debug(str(i) + " - " + str(cBitSet.get(i)))
            if cBitSet.get(i):
                todelete_count_true += 1
aleajactaest's avatar
update  
aleajactaest committed
417
                _ = self.execute_atom_found(0, i, msgin,  parent)
aleajactaest's avatar
aleajactaest committed
418 419 420 421 422 423
        if todelete_count_true > 1:
            logging.getLogger(LOGGER).debug(msgin.showAllData())
            #raise "A controler"
        logging.getLogger(LOGGER).debug(msgin.showAllDataRaw())
        logging.getLogger(LOGGER).debug(msgin.showAllData())
        #raise "A faire"
aleajactaest's avatar
update  
aleajactaest committed
424 425
        #if self.name == "GROUP":
        #    raise "A controler"
aleajactaest's avatar
aleajactaest committed
426

aleajactaest's avatar
update  
aleajactaest committed
427
    def execute_normal(self,  msgin,  parent):
aleajactaest's avatar
aleajactaest committed
428 429 430 431
        nbchild = self.getIdBits()
        #nbBit = getPowerOf2.getPowerOf2_Bis(nbchild)
        nbBit = getPowerOf2.getPowerOf2_ter(nbchild)
        if nbBit > msgin.needRead() :
aleajactaest's avatar
aleajactaest committed
432 433
            logging.getLogger(LOGGER).debug("nbBit:" + str(nbBit) + " nbchild:" + str(nbchild) + " needRead:" + str(msgin.needRead() ))
            return False
aleajactaest's avatar
aleajactaest committed
434
        logging.getLogger(LOGGER).debug("needRead:" + str(msgin.needRead()) + " nbBit:" + str(nbBit))
aleajactaest's avatar
aleajactaest committed
435 436
        id = msgin.readSerial(nbBit, name=parent, typeName='Uint'+str(nbBit), emulate=True)
        logging.getLogger(LOGGER).debug("read : needRead:" + str(msgin.needRead()) + " nbBit:" + str(nbBit) + " id:" + str(id))
aleajactaest's avatar
aleajactaest committed
437 438 439
        i = 0
        ii = 0
        for ele in self.branch:
aleajactaest's avatar
update  
aleajactaest committed
440
            #logging.getLogger(LOGGER).debug(str(i) + " id:" + str(id) + " name:" + str(ele.name))
aleajactaest's avatar
aleajactaest committed
441 442 443 444 445 446 447 448 449 450 451
            if ele.count:
                i += ele.count
            else:
                i += 1
            if i > id:
                comment = ""
                if ele.count:
                    pos = id - ii
                    comment = str(pos)
                logging.getLogger(LOGGER).debug(str(i) + " id:" + str(id) + " name:" + str(ele.name))
                if self.name:
aleajactaest's avatar
update  
aleajactaest committed
452
                    idnameshort = parent + self.name + '/'
aleajactaest's avatar
aleajactaest committed
453
                else:
aleajactaest's avatar
update  
aleajactaest committed
454 455
                    idnameshort = parent
                idname = idnameshort + ele.name
aleajactaest's avatar
aleajactaest committed
456
                _= msgin.readSerial(nbBit, name=idname, typeName='Uint'+str(nbBit), emulate=False, commentValue=ele.name+comment)
aleajactaest's avatar
aleajactaest committed
457
                logging.getLogger(LOGGER).debug("name:" + ele.name + ", count:" + str(ele.count) + ", atom:" + str(ele.atom))
aleajactaest's avatar
update  
aleajactaest committed
458
                ele.execute(msgin,  idnameshort)
aleajactaest's avatar
aleajactaest committed
459
                return True
aleajactaest's avatar
aleajactaest committed
460 461 462 463 464 465 466 467 468 469 470 471 472
            ii = i
        for ele in self.leaf:
            logging.getLogger(LOGGER).debug(str(i) + " id:" + str(id) + " name:" + str(ele.name))
            if ele.count:
                i += ele.count
            else:
                i += 1
            if i > id:
                comment = ""
                if ele.count:
                    pos = id - ii
                    comment = str(pos)
                if self.name:
aleajactaest's avatar
update  
aleajactaest committed
473
                    idnameshort = parent + self.name + '/'
aleajactaest's avatar
aleajactaest committed
474
                else:
aleajactaest's avatar
update  
aleajactaest committed
475 476
                    idnameshort = parent
                idname = idnameshort + ele.name + comment
aleajactaest's avatar
aleajactaest committed
477
                logging.getLogger(LOGGER).debug(str(i) + " id:" + str(id) + " name:" + str(ele.name))
aleajactaest's avatar
aleajactaest committed
478
                _ = msgin.readSerial(nbBit, name= idname, typeName='Uint'+str(nbBit), emulate=False, commentValue=ele.name+comment)
aleajactaest's avatar
aleajactaest committed
479
                logging.getLogger(LOGGER).debug("name:" + ele.name + ", count:" + str(ele.count))
aleajactaest's avatar
update  
aleajactaest committed
480
                ele.execute(msgin, name=idnameshort)
aleajactaest's avatar
aleajactaest committed
481
                return True
aleajactaest's avatar
aleajactaest committed
482
            ii = i
aleajactaest's avatar
aleajactaest committed
483 484 485 486 487 488
        
        id = msgin.readSerial(nbBit, name=parent, typeName='Uint'+str(nbBit))
        logging.getLogger(LOGGER).debug("OUT : needRead:" + str(msgin.needRead()) + " nbBit:" + str(nbBit) + " id:" + str(id)+ " i:" + str(ii))
        print(msgin.showAllData())
        raise "Oh"
        return False
aleajactaest's avatar
update  
aleajactaest committed
489
    
aleajactaest's avatar
update  
aleajactaest committed
490
    def execute(self, msgin,  parent='DatabaseXML/'):
aleajactaest's avatar
aleajactaest committed
491
        if self.atom:
aleajactaest's avatar
aleajactaest committed
492
            logging.getLogger(LOGGER).debug("execute:" + parent)
aleajactaest's avatar
update  
aleajactaest committed
493
            self.execute_atom(msgin, parent)
aleajactaest's avatar
aleajactaest committed
494
        else:
aleajactaest's avatar
aleajactaest committed
495 496 497 498 499 500 501 502 503 504 505 506
            logging.getLogger(LOGGER).debug("execute:" + parent)
            ok = self.execute_normal(msgin,  parent)

    def execute2(self, msgin,  parent='DatabaseXML/'):
        ok = True
        while ok:
            if self.atom:
                logging.getLogger(LOGGER).debug("execute:" + parent)
                self.execute_atom(msgin, parent)
            else:
                logging.getLogger(LOGGER).debug("execute:" + parent)
                ok = self.execute_normal(msgin,  parent)
aleajactaest's avatar
aleajactaest committed
507
        
aleajactaest's avatar
aleajactaest committed
508 509 510 511 512 513 514 515 516 517 518 519 520
#    def execute_root(self,  msgin):
#        if self.atom:
#            nbchild = self.countLeaves()
#            #nbBit = getPowerOf2.getPowerOf2_Bis(nbchild) 
#            nbBit = getPowerOf2.getPowerOf2(nbchild) 
#            raise "A faire"
#        else:
#            nbchild = self.getIdBits()
#            #nbBit = getPowerOf2.getPowerOf2_Bis(nbchild)
#            nbBit = getPowerOf2.getPowerOf2(nbchild)
#            while msgin.needRead() > nbBit:
#                logging.getLogger(LOGGER).debug("needRead:" + str(msgin.needRead()) + ", nbBit:" + str(nbBit))
#                self.execute(msgin)
aleajactaest's avatar
aleajactaest committed
521 522


aleajactaest's avatar
update  
aleajactaest committed
523 524 525 526 527 528
class DecodeDatabase():
    def __init__(self):
        self.databaseXml = None
        self.databasePlr = None

    def loadDatabase(self, databaseXml):
aleajactaest's avatar
aleajactaest committed
529 530
        self.databasePlr = BranchDatabase()
        self.databasePlr.loadRootXml(databaseXml, 'PLR')
aleajactaest's avatar
aleajactaest committed
531 532
        self.databasePlr.show()
        #raise "ok"
aleajactaest's avatar
aleajactaest committed
533 534
    
    def execute(self, msgin, world):
aleajactaest's avatar
aleajactaest committed
535
        logging.getLogger(LOGGER).debug("Start execute")
aleajactaest's avatar
aleajactaest committed
536
        self.databasePlr.execute(msgin)
aleajactaest's avatar
aleajactaest committed
537
        logging.getLogger(LOGGER).debug("End execute")
aleajactaest's avatar
aleajactaest committed
538 539

    def loadDatabase2(self, databaseXml):
aleajactaest's avatar
update  
aleajactaest committed
540
        logging.getLogger(LOGGER).debug("loadDatabase")
aleajactaest's avatar
aleajactaest committed
541 542 543 544 545 546 547 548 549 550
        base = BranchDatabase()
        #base.loadXml(databaseXml,  'PLR')
        base.loadRootXml(databaseXml, 'PLR')
        base.show(filterlevel=2)
        base.show()
        tmp = BitStream.BitStream()
        tmp.internalSerial(23, 5)
        tmp.internalSerial(0,1)
        tmp.internalSerial(13237038,32)
        base.execute_root(tmp)
aleajactaest's avatar
update  
aleajactaest committed
551
        logging.getLogger(LOGGER).debug(tmp.showAllData())
aleajactaest's avatar
aleajactaest committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        tmp = BitStream.BitStream()
        tmp.internalSerial(21, 5)
        tmp.internalSerial(8,5)
        tmp.internalSerial(2,3)
        tmp.internalSerial(0,3)
        tmp.internalSerial(0,5)
        tmp.internalSerial(3,9)
        tmp.internalSerial(69,9)
        tmp.internalSerial(10,5)
        tmp.internalSerial(10,5)        
        tmp.internalSerial(0,5)
        tmp.internalSerial(0,9)
        tmp.internalSerial(200,9)
        tmp.internalSerial(21,5)
        tmp.internalSerial(10,5)
        tmp.internalSerial(8,5)
        tmp.internalSerial(0,1)
        tmp.internalSerial(821933,32)
        tmp.internalSerial(0,5)
        tmp.internalSerial(0,9)
        tmp.internalSerial(100,9)
        tmp.internalSerial(14,5)
        tmp.internalSerial(16,5)
        tmp.internalSerial(4,5)
        tmp.internalSerial(0,2)
        tmp.internalSerial(15,8)
        tmp.internalSerial(64,8)
        tmp.internalSerial(0,8)
        tmp.internalSerial(1,2)
        base.execute_root(tmp)
aleajactaest's avatar
update  
aleajactaest committed
582
        logging.getLogger(LOGGER).debug(tmp.showAllData())
aleajactaest's avatar
aleajactaest committed
583 584
        
        raise "Stop"
aleajactaest's avatar
update  
aleajactaest committed
585 586
        self.databaseXml = databaseXml
        id = 0
aleajactaest's avatar
aleajactaest committed
587
        self.databasePlr = {'name': 'root', 'bank': 'PLR', 'child': {}, 'min':0, 'max':0 ,  'atom': False}
aleajactaest's avatar
update  
aleajactaest committed
588 589 590
        for ele in self.databaseXml:
            if ele.tag == 'branch':
                if ele.get('bank') == "PLR":
aleajactaest's avatar
aleajactaest committed
591 592 593 594 595 596 597
                    if  'atom' in ele:
                        if int(ele.get('atom')) > 0:
                            #self.databasePlr
                            pass
                    self.databasePlr['child'][id] = child(ele)
                    self.databasePlr['child'][id]['min'] = id
                    self.databasePlr['child'][id]['max'] = id
aleajactaest's avatar
update  
aleajactaest committed
598
                    id += 1
aleajactaest's avatar
update  
aleajactaest committed
599
            #print(dir(ele))
aleajactaest's avatar
aleajactaest committed
600 601 602
        nb = len(self.databasePlr) - 1
        self.databasePlr['min'] = 0
        self.databasePlr['max'] = nb
aleajactaest's avatar
update  
aleajactaest committed
603
        show_dico(self.databasePlr)
aleajactaest's avatar
update  
aleajactaest committed
604
        logging.getLogger(LOGGER).debug("max %d" % str(self.databasePlr['max']))
aleajactaest's avatar
update  
aleajactaest committed
605
        #raise "Decode"