def checkpoints(self, path): cachestore = self.cachestore dirpath = paths.directory(path) cpdirpath = CHECKPOINT_DIR + dirpath if cachestore.exists(cpdirpath): names = cachestore.list_dir(cpdirpath) else: return [] cps = [] for name in names: if not name.endswith(CHECKPOINT_EXT): continue basename = name[:0 - len(CHECKPOINT_EXT)] fname, userid, cpid = basename.rsplit(".", 2) if userid != self.userid: continue cppath = paths.join(cpdirpath, name) fpath = paths.join(dirpath, fname) if fpath != path: continue cps.append({ "path": cppath, "modified": cachestore.last_modified(cppath), "id": cpid, }) cps.sort(key=lambda d: d["modified"]) return cps
def _get_include_content(self, path, root, context, icache, ref): incpath, name, value, unwrap = self._parse_include_path(ref) if incpath and incpath != paths.basepath(path): # The include is in another page incpath = paths.join(path, incpath) _, ext = paths.split_extension(incpath) if not ext: incpath = context.pages.source_path(incpath) incpath = paths.join(path, incpath) return self._load_include(incpath, root, context, icache, name, value, unwrap) elif name and value: # If no path was given, or it was this page's path, grab the target # from this page return self._target(root, name, value, unwrap)
def directory_page(pages, dirpath): """ Renders a simple template to show the files in a directory. """ store = pages.store names = store.list_dir(dirpath) files = [] for name in names: path = paths.join(dirpath, name) link = path if pages.is_wiki(link): link = paths.basepath(link) isdir = store.is_dir(path) if isdir: size = -1 mod = -1 else: size = store.size(path) mod = store.last_modified(path) files.append({ "path": path, "link": link, "name": name, "ext": paths.extension(name), "isdir": isdir, "size": size, "modified": mod, }) return flask.render_template("/templates/dir.jinja2", path=dirpath, files=files)
def list_all(self, path="/"): if not path.endswith("/"): path += "/" for name in self.list_dir(path): p = paths.join(path, name) if self.is_dir(p): for sp in self.list_all(p): yield sp else: yield p
def _read_labels(pages, basepath, labelspath, labels): labelspath = paths.join(basepath, labelspath) labelspath, section = paths.split_fragment(labelspath) section = section[1:] if section else "Labels" if pages.exists(labelspath): content = pages.content(labelspath, encoding=None) bio = BytesIO(content) parser = configparser.SafeConfigParser() parser.readfp(bio) if parser.has_section(section): labels.update(dict(parser.items(section)))
def available_languages(self, path): store = self.store dirpath, filename = paths.split_dirpath(path) basename, ext = paths.split_extension(filename) result = [] for lang in self.langauges: if lang == self.default_language: langfilename = filename else: langfilename = "%s.%s.%s" % (basename, lang, ext) if store.exists(paths.join(dirpath, langfilename)): result.append(lang) return result
def _process_example_page(self, block, context, is_node_eg, is_panel_eg): path = context["path"] attrs = block.get("attrs", {}) # Example authors are very lax about giving the example documents # titles; if the document doesn't have a title, make one up from the # file name title = functions.first_subblock_of_type(block, "title") if not title: name = text_type(paths.barename(path)) body = block.setdefault("body", []) body.insert(0, { "type": "title", "indent": 0, "text": [name] }) # Check for an explicit exampleFor property, otherwise guess it # from the example's directory tree if is_node_eg: block.setdefault("attrs", {})["type"] = "example" if "exampleFor" in attrs: egfor = attrs["exampleFor"] elif "examplefor" in attrs: egfor = attrs["examplefor"] else: egfor = self._node_path_from_example_path(path) # Attach the list of nodes to the root block["examplefor"] = egfor egpath = None # Check for an explicit exampleFile property, otherwise guess it # by looking for the example name with an extension if "exampleFile" in attrs: egpath = attrs["exampleFile"] elif "examplefile" in attrs: egpath = attrs["examplefile"] elif is_node_eg: base = paths.basepath(path) for ext in (".hda", ".otl"): egpath = base + ext if context.pages.exists(egpath): break elif is_panel_eg: egpath = self._file_path_from_panel_path(path) if egpath: egpath = paths.join(path, egpath) if context.pages.exists(egpath): block["examplefile"] = egpath
def _get_parent_path(pages, path, block): # Find the path to the parent document attrs = block.get("attrs", {}) if "parent" in attrs: # If the author specified a parent, use that parent = attrs.get("parent") parentpath = pages.source_path(paths.join(path, parent)) elif pages.is_index_page(path): # If this is an index page, assume its parent is the _index page of # the parent directory parentpath = Parents._find_ancestor(pages, paths.parent(path)) else: # Assume the parent is the _index page for this directory parentpath = Parents._find_ancestor(pages, paths.directory(path)) return parentpath
def _replace_includes(self, objs, context, root, icache): i = 0 thispath = context["path"] while i < len(objs): sub = objs[i] if not isinstance(sub, dict): i += 1 continue stype = sub.get("type") icontent = None if stype == "source": attrs = sub.get("attrs", {}) srcpath = paths.join(thispath, attrs.get("path")) content = context.pages.content(srcpath) if content: lang = attrs.get("lang") or get_lexer_for_filename(srcpath).name icontent = [{"type": "pre", "lang": lang, "text": [content]}] else: ref = None if stype == "link" and sub.get("scheme") == "Include": ref = sub.get("value") elif stype == "include": ref = sub.get("ref") if ref: icontent = self._get_include_content(thispath, root, context, icache, ref) if icontent: # Splice the content into the block list objs[i : i + 1] = icontent # ...then move the pointer to after the spliced in content i += len(icontent) else: if sub.get("text"): self._replace_includes(sub["text"], context, root, icache) if sub.get("body"): self._replace_includes(sub["body"], context, root, icache) i += 1
def full_path(self, origin, relpath): path = paths.join(origin, relpath) base, frag = paths.split_fragment(path) if base.endswith("/"): base += self.index_page_name return base + frag
def _get_path_attr(root, path, attrs, name): value = attrs.get(name) if value: return paths.join(path, value)
def test_join(): assert paths.join("a", "b") == "b" assert paths.join("/a", "/b") == "/b" assert paths.join("/a/b/c", "../d") == "/a/d" assert paths.join("/a/b/c", "./d") == "/a/b/d"
def rel(p): if p.startswith("http:") or p.startswith("https:"): return p return paths.join(path, p)