def main(): os.chdir(get_project_dir()) cell_types = set() args = sys.argv[1:] # XXX if both code and markdown should indent code 4 spaces as emitted while args and args[0].startswith("-"): arg = args[0] del args[0] if arg=="-c": cell_types.add("code") elif arg=="-m": cell_types.add("markdown") elif arg=="-h": cell_types.add("heading") else: sys.exit("Usage: extract.py [-c] [-m] [-h] filename ...") for src_file in args: # Establish jinja templating environment (Do we even need to render?) #template_env = Environment(loader=FileSystemLoader("data/templates")) #src_template = template_env.get_template("markdown-only.ipynb") now = datetime.datetime.today() render_context = {"title": "Markdown extracted from "+os.path.abspath(src_file), "date": now.date(), "time": now.time(), "src_file": src_file, "template": "UNKNOWN"} # All cells of the chosen type in the source notebook are copied across. text_out = sys.stdout src_nb = nbf.read(open(src_file, "r", encoding="UTF-8"), "ipynb") # At present we don't render the extracted # cell content, but that possibility remains open. for worksheet in src_nb.worksheets: cells_in = worksheet.cells for cell in cells_in: if cell.cell_type in cell_types: if cell.cell_type == "code": if "markdown" not in cell_types: text_out.write(cell.input) else: text_out.write("".join(" "+line for line in cell.input.splitlines(keepends=True))) elif cell.cell_type == "markdown": text_out.write(cell.source) elif cell.cell_type == "heading": text_out.write("{} {}\n".format( "#"*cell.level, cell.source)) else: sys.exit("Should not have been called to process %s cells", cell.cell_type) text_out.write("\n\n") text_out.close()
def mktopic(title): "Takes a list of the words of a topic title and builds the appropriate notebook file." os.chdir(lib.get_project_dir()) template_env = Environment(loader=FileSystemLoader("data/templates")) # -t template would be nice, but this will do for now src_template = template_env.get_template("base.ipynb") now = datetime.datetime.today() title = " ".join(title) slug = lib.slugify(title) nb_name = slug+".ipynb" dst_file = os.path.join("nbsource", nb_name) render_context = {"slug": slug, "title": title, "date": now.date(), "time": now.time(), "src_file": dst_file, "template": src_template.filename} # we are writing a source ... if os.path.isfile(dst_file): # If the topic exists do not overwrite it XXX [unless option set]. sys.exit("file {} already exists".format(dst_file)) dst_nb_file = open(dst_file, "w") nb_content = src_template.render(render_context) dst_nb_file.write(nb_content) dst_nb_file.close()
sys.exit("No notebooks with more than one cell") totals = [0, 0, 0, 0, 0, 0] format_str = "{:>{length}} {:4d} {:6,d} {:6,d} {:4d} {:6,d} {:6,d}" format_hdr = "{:>{length}} {}" print(format_hdr.format("", "------ code ------ ---- markdown ----", length=longest)) print(format_hdr.format("", "cells lines chars cells lines chars", length=longest)) for result in results: result = list(result) result[0] = result[0][:-6] totals = [total+count for (total, count) in zip(totals, result[1:])] print(format_str. format(*result, length=longest)) print(format_str.format(*["TOTALS"]+totals, length=longest)) def get_lines_and_bytes(source): lines = sum(l != "\n" for l in source) bytes = sum(len(l) for l in source) return lines, bytes if __name__ == "__main__": if len(sys.argv) == 1: os.chdir(os.path.join(get_project_dir(), "nbsource")) paths = glob("*.ipynb") elif len(sys.argv) == 2 and os.path.isdir(sys.argv[1]): os.chdir(sys.argv[1]) paths = glob("*.ipynb") else: paths = sys.argv[1:] nbstats(paths)
"""Test topic matching and the like""" import os import unittest import lib import topics os.chdir(lib.get_project_dir()) topic_list = topics.get_topics() class TestMatching(unittest.TestCase): def test_matching(self): title_list = "the quick brown fox".split() for word in title_list: self.assertTrue(topics.matching(word, title_list)) for word in title_list: self.assertTrue(topics.matching(word[1:-1], title_list)) self.assertTrue(topics.matching("", [])) self.assertTrue(topics.matching("unicode", ["unicode?"])) if __name__ == "__main__": unittest.main()
comment = " ***" if slug not in file_slugs else "" print(os.path.join("project", "nbsource", slug + ".ipynb"), comment) if __name__ == "__main__": # possible -d option for directory? exists = orphaned = False # XXX one switch only ... use proper arg parsing if len(sys.argv) > 1 and sys.argv[1][0] == "-": if sys.argv[1] == "-u": exists = True elif sys.argv[1] == "-o": orphaned = True elif sys.argv[1] == "-a": exists = None else: import sys sys.exit("topics.py [-o | -u | -a] [List of words]") del sys.argv[1] os.chdir(get_project_dir()) if orphaned: orphaned_topic_files() else: topic_list = slugify(" ".join(sys.argv[1:])).split("-") # if topic_list == [""]: # Annoying special case? # topic_list = [] topic_and_file(topic_list, exists=exists)