Created
August 21, 2015 19:08
-
-
Save sash13/4cb1f525b3653af755fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pprint | |
| TAB = '\t' | |
| class LazyTree(object): | |
| def __init__(self): | |
| self.index = 0 | |
| self.types = {} | |
| self.path = [] | |
| self.temp_path = [] | |
| def __getattr__(self, n): | |
| if self.types.has_key(n): | |
| self.temp_path.append(n) | |
| return self | |
| else: | |
| return super(LazyTree, self).__getattribute__(n) | |
| def getRoot(self, i, t): | |
| for c in i: | |
| if c['id'] is t: | |
| yield c['child'] | |
| else: | |
| for cc in self.getRoot(c['child'], t): | |
| yield cc | |
| def passTeg(self, d, t, a): | |
| if len(list(self.getRoot(d, t))): | |
| for i, c in enumerate(list(self.getRoot(d, t))): | |
| c.append(a) | |
| break | |
| else: | |
| d.append(a) | |
| def addType(self, n, f): | |
| self.types[n] = f | |
| def treeWalk(self, f, p): | |
| for tag in f: | |
| if tag['class'] in self.temp_path[0]: | |
| self.temp_path.pop(0) | |
| return self.treeWalk(tag['child'], tag['id']) | |
| else: | |
| return p | |
| def createTemplate(self, f, p = -1): | |
| prev = p | |
| for tag in f: | |
| new = {'id': self.index, 'class':tag, 'child':[]} | |
| self.passTeg(self.path, prev, new) | |
| prev = self.index | |
| self.index = self.index + 1 | |
| self.temp_path = [] | |
| def create(self, multi_root = -1): | |
| if not self.path: | |
| self.createTemplate(self.temp_path) | |
| return self.path | |
| '''for tag in self.path: | |
| if not self.temp_path: | |
| return self.path | |
| first = self.temp_path[0] | |
| if first in tag['class']: | |
| self.createTemplate(self.temp_path[1:], tag['id']) | |
| else: | |
| self.createTemplate(self.temp_path)''' | |
| parent = self.treeWalk(self.path, multi_root) | |
| print parent | |
| self.createTemplate(self.temp_path, parent) | |
| return self.path | |
| def build(self): | |
| e = True | |
| for n in self.path[::-1]: | |
| c = self.types[n](n, c if e else TAB.join(c), e) | |
| e = False | |
| self.path = [] | |
| return "".join(c) | |
| def div(n, c, e): | |
| return ('<div class = "{0}">\n'.format(n), | |
| TAB+c+'\n', | |
| TAB+'</div>' if e else '</div>' | |
| ) | |
| layout = LazyTree() | |
| layout.addType("body", div); | |
| layout.addType("top", div); | |
| layout.addType("right", div); | |
| layout.addType("center", div); | |
| layout.addType("woof", div); | |
| c = ('\twoof') | |
| content = layout.body.center.right.create() | |
| layout.body.center.top.create() # new only "top" | |
| layout.center.top.create() # create new root element | |
| layout.center.top.create(0) # attach "center" to "body" | |
| layout.woof.create() # one element | |
| pp = pprint.PrettyPrinter(width=1) | |
| pp.pprint(content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment