Exemple #1
0
 def __loadViaNode(self, projnum, node):
     idn = "%s.%s"%(projnum, node.getXPath())
     self.__util[ idn ] = (node.orderid, RCMenuNodeType.strType(node.type))
     
     if node.type in RCMenuNodeType.HAS_STRING:
         self.__data[idn] = copy.deepcopy( node.value )
     else: #is separator
         self.__seps.append( idn )
         
     #recurse if we are in a popup    
     if node.type == RCMenuNodeType.POPUP:
         for child in node._children:
             self.__loadViaNode(projnum, child)
Exemple #2
0
def _ReconstructMenus( data, util, seps ):
    """             ***DO NOT USE THIS FUNCTION DIRECTLY!*** 
    Use `ConvertMenuCSV2XML` as it calls this function through the correct
    conversion process. Reconstructs a list of RCMenus based on flat CSV data 
    in the form of XPaths and ID references. Its tougher to go from CSV back
    to XML than it was to get here. Returns a list of RCMenus.
    """
    #data = { xpath-id -> RCStringValue w/ {langcode->value}}
    #util = { xpath-id -> (order,type)}
    #seps = [ xpath-id ]
    # xpath-id = <menu-name>.<xpath>
    # idn = an id of a menu node
    
    # First expand the utility information into two mappings:
    ordermap = {} # xpath-id -> order (int)
    typemap  = {} # xpath-id -> RCMenuNodeType
    for xpathid, udata in util.items():
        order, typeid = udata
        ordermap[ xpathid ] = int(order)
        typemap [ xpathid ] = RCMenuNodeType.getType(typeid)
     
    # Second, break up the datalines by menuid and by level
    menus    = {} # { menuid -> { xpath-id -> value } }
    menulvls = {} # { menuid -> { lvl -> [xpath-id] } }
    for xpathid, value in data.items():
        menuid, xpath = xpathid.split('.', 1)
        lvl = _determineXPathLvl(xpath)
        if menus.get(menuid) is None: menus[menuid] = {}
        if menulvls.get(menuid) is None: menulvls[menuid] = {}
        if menulvls[menuid].get(lvl) is None: menulvls[menuid][lvl] = []

        menus[menuid][xpath] = value
        menulvls[menuid][lvl].append(xpath)
        
    #Third, add all the separaters to the menus and the menulvls maps.
    for sep in seps:
        menuid, xpath = sep.split('.',1)
        lvl = _determineXPathLvl(xpath)
        if menus.get(menuid) is None: menus[menuid] = {}
        if menulvls.get(menuid) is None: menulvls[menuid] = {}
        if menulvls[menuid].get(lvl) is None: menulvls[menuid][lvl] = []

        menus[menuid][xpath] = None
        menulvls[menuid][lvl].append(xpath)
        
    # Finally, for each menu in `menus`, loop by level adding the top 
    #    level first, and by order.
    menulst = [] #what gets returned!
    for menuid in menus:
        menu = RCMenu(menuid)
        for lvl in menulvls[menuid]:
            lvlorder = {} # order (int) -> xpath
            
            # loops through all the xpaths at this lvl and determines
            # the order to add them by adding them to the lvlorder
            # dictionary, which auto-sorts them.
            for txpath in menulvls[menuid][lvl]:
                orderid = ordermap[ menuid+"."+txpath ]
                if lvlorder.get( orderid ) is None:
                    lvlorder[ orderid ] = [txpath]
                else: lvlorder[ orderid ].append( txpath )
                
            # Now that we have what order to add them to the menu.
            # We loop through all the items at this level, and add them
            # by asking the menu to find the parent based on the xpath.
            for orderid, txpaths in sorted(lvlorder.items(), key=lambda x: x[0]):
                for txpath in txpaths:
                    idn = _determineBasename(txpath)
                    typeid = typemap[menuid+"."+txpath]
                    if typeid == RCMenuNodeType.POPUP:
                        node = RCMenuNode(menu,type=typeid,idn=idn,order=orderid)
                        node.value.combine( menus[menuid][txpath],suppressIDWarn=True )
                        parent = menu.findParentOfNode( txpath )
                        if parent is not None:
                            parent.addChild( node )
                    elif typeid == RCMenuNodeType.MENUITEM:
                        node = RCMenuNode(menu,id=idn,type=typeid,order=orderid)
                        node.value.combine( menus[menuid][txpath],suppressIDWarn=True )
                        parent = menu.findParentOfNode( txpath )
                        if parent is not None:
                            parent.addChild( node )
                    elif typeid == RCMenuNodeType.SEPARATOR:
                        node = RCMenuNode(menu,type=typeid,idn=idn,order=orderid)
                        parent = menu.findParentOfNode( txpath )
                        if parent is not None:
                            parent.addChild( node )
                    else: continue #TODO: log? this is an invalid type!
        # Now we can add that menu to the list of menus to return.            
        menulst.append(menu)
    return menulst