Esempio n. 1
0
 def __init__(self, dic):
     #print "construct StyleNode: ",
     self._preview = ''
     self._imp = 0
     self._count = 1
     self._children = []
     self.type = 'stylenode'
     self.doter = StyleNodeDoter()
     self.doter.init(self)
     self.dic = dic
Esempio n. 2
0
class StyleNode:
    def __init__(self, dic):
        #print "construct StyleNode: ",
        self._preview = ''
        self._imp = 0
        self._count = 1
        self._children = []
        self.type = 'stylenode'
        self.doter = StyleNodeDoter()
        self.doter.init(self)
        self.dic = dic

    def generateStyleNode(self, node):
        '''
        @node : PyQuery
        '''
        childnodes = node.children()
        for i in range(len(childnodes)):
            childnode = childnodes.eq(i)
            #skip data nodes
            tagname = getTagName(childnode)
            if tagname in nodenames: continue
            #generate ...
            element = ElementNode(self._getTag(childnode), self.dic)
            #print '.. Element : ',element
            self.addChildElement(element)

    def getPreview(self):
        #return self._preview
        return trim(self.generatePreview())


    def getCount(self):
        return self._count

    def _setPreview(self, s):
        self._preview = s

    def addChildElement(self, childElement):
        self._children.append(childElement)

    def getChild(self, i):
        return self._children[i]

    def generatePreview(self):
        res = ''
        for child in self.getChildrenElements():
            res += child.getName()
            res += ' '
        #self._setPreview(res)
        return res
            
    def getChildrenElements(self):
        return self._children

    def incCount(self):
        self._count += 1
        for element in self.getChildrenElements():
            element.incCount()

    def getP(self):
        return self._count / pageNum

    def getCompImp(self):
        if self._imp:
            return self._imp

        k = len(self._children)

        res = sum([child.getCompImp() for child in self._children])
        self._imp = res / k
        return self._imp
    
    def __str__(self):
        res = ''
        res += self.doter.initDotNode() + '\n'
        for i,e in enumerate(self.getChildrenElements()):
            e.doter.initIndex(self.doter.index)
            e.doter.incIndex()
            res += '%s -- %s;\n' % (
                self.doter.getDotNode(),
                e.doter.getDotNode() 
            )
            res += str(e)
        self.doter.incIndex()
        return res

    def _getTag(self, node):
        end = str(node).index('>')
        res = str(node)[:end+1]
        #print "** tag: ", res
        return res