def build_namespace_tree(pagesdir: str, exclude_templates: bool = True) -> Namespace: """ Walk the pagesdir and build a tree representing the structures of the wiki. The nodes are Namepspace objects, which contain a list of pages. Returns the root node of the tree, i.e. the root namespace. """ r = Resolver('name') rootns = Namespace(name='pages', parent=None) for root, dirs, files in os.walk(pagesdir): relpath = os.path.relpath(root, pagesdir) if relpath == '.': basens = rootns relpath = '' else: basens = r.get(rootns, relpath) for directory in dirs: Namespace(name=directory, parent=basens) basens.pages = [] for file in files: page_name = os.path.splitext(file)[0] page_file_path = os.path.join(relpath, file) if exclude_templates and is_template(page_name): print("{} is template".format(page_name)) else: basens.pages.append((page_name, page_file_path)) return rootns
def __init__(self, base, name): d = cs.index[name] self.d = d self.res = Resolver('name') self.base = base self.class_dict = {} self.k = {} self.root = thing(base) self.build()
def __init__(self, base, name, prefix="cache", export="model"): self.name = name self.d = cs.index.copy() self.res = Resolver("name") self.base = base self.class_dict = {} self.k = {} self.export_path = export self.export_prefix = prefix self.root = thing(base) self.build_tree(name, self.root) self.build_tree("showcase", self.root) self.build_other() self.alter_build(name) self.store = db.Store(prefix=prefix)
def __init__(self, filename, nlu: NLUHandler, resp: ResponseHandler): super().__init__(filename) self.filename = filename self.tree = BaseNode(BaseItem(name="root")) self.nlu = nlu self.resp = resp self.resolver = Resolver("name")
def validatePath(ctx, param, prompted_path): if prompted_path is None: return None hierarchy = prompted_path.split("/") topnode = ctx.obj.rc.topnode r = Resolver('name') try: node = r.get(topnode, prompted_path) except Exception as ex: raise click.BadParameter( f"Couldn't find {prompted_path} in the tree") from ex return hierarchy
class Directory: def __init__(self, base, name, prefix="cache", export="model"): self.name = name self.d = cs.index.copy() self.res = Resolver("name") self.base = base self.class_dict = {} self.k = {} self.export_path = export self.export_prefix = prefix self.root = thing(base) self.build_tree(name, self.root) self.build_tree("showcase", self.root) self.build_other() self.alter_build(name) self.store = db.Store(prefix=prefix) def alter_build(self, name): reg = cs.index.copy() s = reg[name] keys = s.keys() for i in keys: sub = s[i].copy() print(i, sub) def build_other(self): p = thing("lib", parent=self.root) k = list(self.d.keys()) for i in k: self.build_tree(i, p) def build_tree(self, name, root): print(name, root) if name not in self.d: return p = thing(name, parent=root) tr = self.d.pop(name) for j in tr: b = thing(j, parent=p) for k in tr[j]: cn = type(k()).__module__ + "." + k.__name__ t = thing(k.__name__, parent=b, c=k, classname=cn, doc=k.__doc__) self.class_dict[cn] = t self.k[p.get_path() + "/" + j + "/" + k.__name__] = t def get_path(self, path): r = self.res.get(self.root, path) return r def get_name(self, name): n = findall_by_attr(self.root, name) return n[0] def exists(self, key): if "/" + key in self.k: return True return False def prefix(self, key): v = self.res.get(self.root, "/" + key) data = {} if v.parent != None: data["parent"] = v.parent.get_path() l = [] for i in v.children: if i.loaded == False: self.store.fetch(i) l.append(i.dir()) data["list"] = l data["leaf"] = False data["name"] = v.name data["path"] = v.get_path() return data def build_part(self, params): key = params.pop("classname", None) if key in self.class_dict: fixes = {} for i in params: try: fixes[i] = float(params[i]) except: pass item = self.class_dict[key] item.params.update(fixes) item.rendered = False self.export(item) self.store.upsert(item) def export(self, t): folder_name = self.export_prefix + "/" + self.export_path + "/" + t.name try: os.removedirs(folder_name) except: pass try: os.makedirs(folder_name) except: pass # create the object o = t.c(**t.params) gltf_path = folder_name + "/out.gltf" r = o.exporter("gltf") # build the gltf v = r(gltf_path) view = [r.scene_min, r.scene_max] t.view = views.placed(view) t.gltf_path = "/" + self.export_prefix + "/" + self.export_path + "/" + t.name def set_image(self, imgname): name = imgname.split(".")[0] t = self.get_name(name) if t is not None: path = os.sep + self.export_prefix + os.sep + "img" + os.sep t.image_path = path + imgname t.pending = False t.rendered = True self.store.upsert(t) def fetch(self, t): self.store.fetch(t) # due to multiple export paths (for dumping) # this is set by itself t.gltf_path = "/" + self.export_prefix + "/" + self.export_path + "/" + t.name def params(self, key): if self.exists(key) == False: print(str(key) + "keyfail") print(self.k.keys()) t = self.k["/" + key] d = {} if t.loaded == False: self.fetch(t) if t.built == False: inst = t.c() pi = inst.params().items() for i in pi: # only grab the floats for now if isinstance(i[1], float): d[i[0]] = i[1] if isinstance(i[1], int): d[i[0]] = i[1] t.params = d self.export(t) t.built = True t.rendered = False self.store.upsert(t) info = t.info() return info def get_zipped(self, t): r = self.export_prefix + os.sep + self.export_path + os.sep + t.name + os.sep export_folder = self.export_prefix + os.sep + "zip" zip_file = export_folder + os.sep + t.name + ".zip" f = os.listdir(r) try: os.makedirs(export_folder) except: pass z = zipfile.ZipFile(zip_file, "w") for i in f: z.write(r + i, t.name + os.sep + i) z.close() return zip_file def treeiter(self, key): nodes = [] item = self.get_path(key) for node in PreOrderIter(item): if node.loaded == False: self.fetch(node) nodes.append(node) return nodes def rerender(self): j = self.treeiter('export') for i in j: if i.is_leaf: i.rendered = False def stats(self): j = self.treeiter('export') counter = 0 build_counter = 0 render_count = 0 for i in j: if i.is_leaf: counter = counter + 1 if i.built: build_counter = build_counter + 1 if i.rendered: render_count = render_count + 1 return { "total": counter, "built": build_counter, "rendered": render_count }
except ValueError: link = match src = '' links.append((link, src)) return links def getmedia(self): return re_embeddedmedia.findall(self.src) # Build tree # In[84]: r = Resolver('name') rootns = Namespace(name='pages', parent=None) for root, dirs, files in os.walk(pagesdir): relpath = os.path.relpath(root, pagesdir) if relpath == '.': basens = rootns else: basens = r.get(rootns, relpath) for directory in dirs: ns = Namespace(name=directory, parent=basens) basens.pages = [] for file in files:
def _init_trees(self, height, limit): if self.methods is None: self._init_methods(limit) if not self.methods: return root = Node('root') r = Resolver(pathattr='name') def find_node(path): """查找节点 Args: root (TYPE): Description path (TYPE): Description Returns: TYPE: Description """ try: return r.glob(root, path)[0] except Exception: return None def to_nodes(mtd): """把一个方法,转化成节点 Args: root (TYPE): 根节点 mtd (TYPE): Description Returns: TYPE: Node """ current = root node_path = '/root' for item in mtd.split('/'): node_path = node_path + '/' + item tnode = find_node(node_path) if tnode: current = tnode else: current = Node(item, parent=current) count = 0 # TODO 节点插入的顺序,决定了树的遍历顺序,及其计算结果 # 假设2个结构一样,但是,因为名字顺序不一样,导致插入顺序不一致 # 有可能导致一样的结构不一样的结果。 for mtd in self.methods: count += 1 to_nodes(mtd) def serialize_node(root_node): snum = '' for pre, _, node in RenderTree(root_node): snum = snum + str(node.height) return snum self.trees = {} for pre, _, node in RenderTree(root): if node.height > height: key = hash.hash(serialize_node(node), 'md5') if key in self.trees: self.trees[key].append(node) else: self.trees[key] = [node]
class directory(): def __init__(self, base, name): d = cs.index[name] self.d = d self.res = Resolver('name') self.base = base self.class_dict = {} self.k = {} self.root = thing(base) self.build() def build(self): for i in cs.index.keys(): p = thing(i, parent=self.root) for j in cs.index[i]: b = thing(j, parent=p) for k in cs.index[i][j]: cn = type(k()).__module__ + '.' + k.__name__ t = thing(k.__name__, parent=b, c=k, classname=cn) self.class_dict[cn] = t self.k[self.base + '/' + i + '/' + j + '/' + k.__name__] = t def children(self, path): r = self.res.get(self.root, path) print r def exists(self, key): if key in self.k: return True return False def prefix(self, key): v = self.res.get(self.root, '/' + key) return v.dir() def build_part(self, params): key = params.pop('classname', None) if key in self.class_dict: fixes = {} for i in params: try: fixes[i] = float(params[i]) except: pass item = self.class_dict[key] o = item.c(**fixes) display(o) def params(self, key): if self.exists(key) == False: abort(404) t = self.k[key] if t.built == False: t.inst = t.c() display(t.inst) t.built = True d = {} pi = t.inst.params().items() for i in pi: # only grab the floats for now if isinstance(i[1], float): d[i] = i[1] if isinstance(i[1], int): d[i] = i[1] info = t.info() info['params'] = d #if isinstance(t.inst,cqparts.Assembly): # info['tree'] = t.inst.tree_str() return info
class directory(): def __init__(self, base, name): d = cs.index[name] self.name = name self.d = d self.res = Resolver('name') self.base = base self.class_dict = {} self.k = {} self.root = thing(base) self.build() def build(self): p = thing(self.name, parent=self.root) for j in self.d: b = thing(j, parent=p) for k in self.d[j]: cn = type(k()).__module__ + '.' + k.__name__ t = thing(k.__name__, parent=b, c=k, classname=cn, doc=k.__doc__) self.class_dict[cn] = t self.k[self.base + '/' + self.name + '/' + j + '/' + k.__name__] = t def children(self, path): r = self.res.get(self.root, path) print r def exists(self, key): if key in self.k: return True return False def prefix(self, key): v = self.res.get(self.root, '/' + key) return v.dir() def build_part(self, params): key = params.pop('classname', None) if key in self.class_dict: fixes = {} for i in params: try: fixes[i] = float(params[i]) except: pass item = self.class_dict[key] item.params.update(fixes) self.export(item) def export(self, t): try: os.removedirs('static/cache/' + t.name) except: pass try: os.makedirs('static/cache/' + t.name) except: pass o = t.c(**t.params) app.logger.error("%s", o) o.exporter('gltf')('static/cache/' + t.name + '/out.gltf') app.logger.error("export finished") def params(self, key): if self.exists(key) == False: abort(404) t = self.k[key] d = {} if t.built == False: inst = t.c() pi = inst.params().items() for i in pi: # only grab the floats for now if isinstance(i[1], float): d[i[0]] = i[1] if isinstance(i[1], int): d[i[0]] = i[1] t.params = d self.export(t) t.built = True info = t.info() return info