def test_extension(): assert paths.extension("") == "" assert paths.extension("/") == "" assert paths.extension("/a") == "" assert paths.extension("/a/b") == "" assert paths.extension("/a/b.") == "." assert paths.extension("/a/b.c") == ".c" assert paths.extension("/a/b.txt") == ".txt" assert paths.extension("/a/b.abcdefghijklmnop") == ".abcdefghijklmnop" assert paths.extension("/a/b.ABCDEFGHIJKLM") == ".ABCDEFGHIJKLM" assert paths.extension("/a/b.c/") == ""
def edit_wiki(path): config = flask.current_app.config editable = config["EDITABLE"] if not editable: flask.abort(500) pages = get_wikipages() path = paths.normalize("/" + path) path = pages.source_path(path) if paths.extension(path) != config["WIKI_EXT"]: # TODO: better error here! flask.abort(500) userid = get_request_userid() cp = Checkpoints(userid, pages.store, pages.cachestore) from_autosave = False if pages.exists(path): lastmod = pages.last_modified(path) if cp.has_autosave_after(path, lastmod): source = cp.get_autosave(path) from_autosave = True else: source = pages.content(path, reformat=True) else: lastmod = 0 source = "" return flask.render_template("/templates/edit.jinja2", source=source, path=path, rel=null_rel, lastmod=lastmod, from_autosave=from_autosave)
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 find_scss(self, partials=False): for name in self.store.list_dir(self.asset_dir): if paths.extension(name) == ".scss": ispartial = name.startswith("_") if ispartial and not partials: continue yield self.asset_dir + name, ispartial
def missing(images=False, links=False, unused=False, prefix="/", verbose=False): if not (images or links or unused): images = links = unused = True pages = flaskapp.get_wikipages(manager.app) all_images = set() used_images = set() for path in get_prefixed_paths(pages, prefix): if paths.extension(path) in (".png", ".jpg", ".jpeg", ".gif"): all_images.add(path) if not pages.is_wiki_source(path): continue if verbose: print(path) printed = False json = pages.json(path) for link in functions.find_links(json): value = link["value"] scheme = link.get("scheme") if value.startswith("#") or scheme in ("Icon", "Smallicon", "Largeicon"): continue fullpath = link.get("fullpath") if not fullpath: continue if pages.is_wiki(fullpath): fullpath = pages.source_path(fullpath) exists = pages.exists(fullpath) isimage = scheme in ("Image", "Anim") if isimage: used_images.add(fullpath) if exists: continue if (images and isimage) or (links and not isimage): if not verbose and not printed: print(path) printed = True print(" ", value, " ", fullpath) if unused: unused_images = all_images - used_images bytes = 0 for imgpath in sorted(unused_images): size = pages.size(imgpath) bytes += size print("Unused image:", imgpath, size) print(len(unused_images), "unused images (", bytes, ") out of", len(all_images))
def is_wiki_source(self, path): return paths.extension(path) == self.wiki_ext
def is_wiki(self, path): ext = paths.extension(path) return (not ext) or ext == self.wiki_ext
def load_example(source, launch=False): global index, manager global _PYTHON_PANEL_EXAMPLE # Convert string to boolean # launch = str(launch).lower() == "true" if bookish_app is None: # If the flask app is None, then houdinihelp.initialize() was not # called. # # If initialize() was not called, then this function must be running # inside a central help server. In which case, we will not be able to # load examples in this process. So we raise an exception and exit # early. raise Exception("Cannot load example from a central help server.") import hou ext = paths.extension(source) if ext not in (".hda", ".otl", ".pypanel"): hou.ui.displayMessage("Don't know how to load example file %r" % source, severity=hou.severityType.Error) return if launch: # Launch a new Houdini to load the example # We'll use the HScript 'unix' command instead of shelling out # from Python just so we know $HFS will work... command = "unix %s %s %s" % (hou.applicationName(), load_script_path, source) hou.hscript(command) elif source.endswith(".pypanel"): # We need to open a Python Panel in the desktop which can only be done # by the main thread. So we register a callback with Houdini's event # loop to guarantee that the actual work is executed in the main thread. _PYTHON_PANEL_EXAMPLE = source hou.ui.addEventLoopCallback(_load_python_panel_example) else: # Load the OTL into Houdini and instantiate the first Object asset we # find inside hou.hda.installFile(source) target_hda = None hda_defs = hou.hda.definitionsInFile(source) for hda in hda_defs: if hda.nodeTypeCategory().name() == "Object": target_hda = hda break if target_hda is None: hou.ui.displayMessage("Could not find example HDA in OTL file %r" % source, severity=hou.severityType.Error) nodetypename = hda.nodeType().name() objnet = hou.node("/obj") hda_node = objnet.createNode(nodetypename, exact_type_name=True) # Make sure that the HDA node is unlocked so that the user can play # around with it. propagate = True hda_node.allowEditingOfContents(propagate)
def show(path): app = flask.current_app config = app.config pages = get_wikipages() indexer = get_indexer() searcher = indexer.searcher() editable = config["EDITABLE"] store = pages.store path = paths.normalize("/" + path) pathexists = store.exists(path) spath = pages.source_path(path) cond = not is_unconditional() # print("path=", path, "cond=", cond) isdir = pathexists and store.is_dir(path) if isdir: if not path.endswith("/"): return flask.redirect(path + "/", 302) if not store.exists(spath): return directory_page(pages, path) ext = paths.extension(path) if pathexists and not isdir: fpath = store.file_path(path) if fpath: return flask.send_file(fpath, add_etags=True, conditional=cond) else: try: fp = pages.store.open(path) if hasattr(fp, "name"): fp.name = None mimetype, encoding = mimetypes.guess_type(path) resp = flask.send_file(fp, conditional=cond, mimetype=mimetype) etag = "%s.%s" % (path, str(store.last_modified(path))) resp.set_etag(etag) return resp except stores.ResourceNotFoundError: raise werkzeug.exceptions.NotFound elif not ext and store.exists(spath): etag = pages.etag(spath) if cond and etag: inm = flask.request.if_none_match if etag in inm: raise NotModified() try: extras = { "editable": editable, "q": flask.request.args.get('q', ''), "pages": pages, "searcher": searcher, "paths": paths, } try: html = pages.html(path, conditional=cond, searcher=searcher, extras=extras, allow_redirect=True) except wikipages.Redirect: e = sys.exc_info()[1] return flask.redirect(e.newpath, 302) resp = flask.Response(html) if etag: resp.set_etag(etag) return resp except stores.ResourceNotFoundError: e = sys.exc_info()[1] app.logger.error(e) raise werkzeug.exceptions.NotFound else: raise werkzeug.exceptions.NotFound
def list_templates(self, extensions=None, filter_func=None): for path in self.store.list_dir(self.prefix): if filter_func and filter_func(path): yield path elif extensions and paths.extension(path) in extensions: yield path