def test_pages(): paths = [ os.path.normpath( os.path.join(pheasant.__file__, "../../tests/docs/docs")) ] pages = Pages(paths, "py,md") pages.collect() d = pages[0].to_dict() assert "path" in d d = pages.to_dict() assert "pages" in d assert isinstance(d['pages'], list)
def __init__(self, paths, ext): self.api = responder.API() self.store = {} self.store["pages"] = Pages(paths, ext) self.store["pages"].collect() self.api.add_route("/pages", self.pages) self.api.add_route("/pages/{id}", self.page)
def convert(paths, ext, max, restart, shutdown, force, verbose): pages = Pages(paths, ext).collect() length = len(pages) click.secho(f"collected {length} files.", bold=True) if length > max: # pragma: no cover click.secho("Too many files. Aborted.", fg="yellow") sys.exit() if force: for page in pages: if page.has_cache: page.cache.delete() click.echo(page.cache.path + " was deleted.") from pheasant.core.pheasant import Pheasant converter = Pheasant(restart=restart, shutdown=shutdown, verbose=verbose) converter.jupyter.safe = True outputs = converter.convert_from_files(page.path for page in pages) for page, output in zip(pages, outputs): path = page.path.replace(".py", ".md").replace(".md", ".out.md") with open(path, "w", encoding="utf-8") as f: f.write(output)
def list(paths, ext): pages = Pages(paths, ext).collect() def size(cache): size = cache.size / 1024 if size > 1024: size /= 1024 return f"{size:.01f}MB" else: return f"{size:.01f}KB" for page in pages: click.echo(("* " if page.modified else " ") + page.path + ( f" (cached, {size(page.cache)})" if page.has_cache else "")) click.secho(f"collected {len(pages)} files.", bold=True)
def clean(paths, ext, yes): pages = Pages(paths, ext).collect() caches = [page.cache for page in pages if page.has_cache] if not caches: click.secho(f"No cache found. Aborted.", bold=True) sys.exit() for cache in caches: click.echo(cache.path) click.secho(f"collected {len(caches)} files.", bold=True) if not yes: click.confirm( "Are you sure you want to delete the caches for these files?", abort=True) for cache in caches: cache.delete() click.echo(cache.path + " was deleted.")
def run(paths, ext, max, restart, shutdown, force, verbose): pages = Pages(paths, ext).collect() length = len(pages) click.secho(f"collected {length} files.", bold=True) if length > max: # pragma: no cover click.secho(f"Too many files. Aborted.", fg="yellow") sys.exit() if force: for page in pages: if page.has_cache: page.cache.delete() click.echo(page.cache.path + " was deleted.") from pheasant.core.pheasant import Pheasant converter = Pheasant(restart=restart, shutdown=shutdown, verbose=verbose) converter.jupyter.safe = True converter.convert_from_files(page.path for page in pages) click.secho(f"{converter.log.info}", bold=True)