コード例 #1
0
def export_notebook_code(notebook_name, path=None):
    # notebook_path = import_notebooks.find_notebook(notebook_name, path)
    notebook_path = notebook_name

    # load the notebook
    with io.open(notebook_path, 'r', encoding='utf-8') as f:
        notebook = nbformat.read(f, 4)

    # shell = InteractiveShell.instance()

    # Get versioning info
    notebook_modification_time = os.path.getmtime(notebook_path)    
    timestamp = datetime.datetime.fromtimestamp(notebook_modification_time) \
        .astimezone().isoformat(sep=' ', timespec='seconds')
    module = os.path.splitext(os.path.basename(notebook_name))[0]

    header = HEADER.format(module=module, timestamp=timestamp)
    print_utf8(header)
    sep = ''

    for cell in notebook.cells:
        if cell.cell_type == 'code':
            code = cell.source
            if len(code.strip()) == 0:
                # Empty code
                continue
            
            bang = False
            if code.startswith('!'):
                code = "import os\nos.system(r" + repr(code[1:]) + ")"
                bang = True

            if RE_IMPORT_FUZZINGBOOK_UTILS.match(code):
                # Don't import all of fuzzingbook_utils (requires nbformat & Ipython)
                print_if_main(SET_FIXED_SEED)
            elif RE_IGNORE.match(code):
                # Code to ignore - comment out
                print_utf8("\n" + prefix_code(code, "# ") + "\n")
            elif RE_CODE.match(code) and not bang:
                # imports and defs
                code = fix_imports(code)
                print_utf8("\n" + code + "\n")
            elif is_all_comments(code):
                # Only comments
                print_utf8("\n" + code + "\n")
            else:
                print_if_main(code)
        else:
            # Anything else
            contents = cell.source
            if contents.startswith('#'):
                # Header
                line = first_line(contents)
                print_utf8("\n" + prefix_code(decode_title(line), "# ") + "\n")
                print_if_main("print(" + repr(sep + decode_title(line)) + ")\n\n")
                sep = '\n'
            else:
                # We don't include contents, as they fall under a different license
                # print_utf8("\n" + prefix_code(contents, "# ") + "\n")
                pass
コード例 #2
0
def export_notebook_code(notebook_name, project="fuzzingbook", path=None):
    # notebook_path = import_notebooks.find_notebook(notebook_name, path)
    notebook_path = notebook_name

    title, synopsis = get_notebook_synopsis(notebook_name, path)

    if project == "debuggingbook":
        booktitle = "The Debugging Book"
    else:
        booktitle = "The Fuzzing Book"

    # load the notebook
    with io.open(notebook_path, 'r', encoding='utf-8') as f:
        notebook = nbformat.read(f, 4)

    # shell = InteractiveShell.instance()

    # Get versioning info
    notebook_modification_time = os.path.getmtime(notebook_path)
    timestamp = datetime.datetime.fromtimestamp(notebook_modification_time) \
        .astimezone().isoformat(sep=' ', timespec='seconds')
    module = os.path.splitext(os.path.basename(notebook_name))[0]

    header = HEADER.format(module=module,
                           timestamp=timestamp,
                           project=project,
                           booktitle=booktitle,
                           title=title,
                           synopsis=synopsis)

    print_utf8(header)
    sep = ''

    for cell in notebook.cells:
        if cell.cell_type == 'code':
            code = cell.source

            while code.startswith('#') or code.startswith('\n'):
                # Skip leading comments
                if code.find('\n') >= 0:
                    code = code[code.find('\n') + 1:]
                else:
                    code = ''

            if len(code.strip()) == 0:
                # Empty code
                continue

            bang = False
            if code.startswith('!'):
                code = "import os\nos.system(f" + repr(code[1:]) + ")"
                bang = True

            if RE_IMPORT_BOOKUTILS.match(code):
                # Don't import all of bookutils (requires nbformat & Ipython)
                print_if_main(SET_FIXED_SEED)
            elif RE_IMPORT_IF_MAIN.match(code):
                code = fix_imports(code)
                print_if_main(code)
            elif RE_FROM_BOOKUTILS.match(code):
                # This would be "from bookutils import HTML"
                # print ("Code: ", repr(code))
                code = fix_imports(code)
                print_utf8("\n" + code + "\n")
            elif RE_IGNORE.match(code):
                # Code to ignore - comment out
                print_utf8("\n" + prefix_code(code, "# ") + "\n")
            elif RE_CODE.match(code) and not bang:
                # imports, classes, and defs
                code = fix_imports(code)
                code = fix_code(code)
                print_utf8("\n" + code + "\n")
            elif is_all_comments(code):
                # Only comments
                print_utf8("\n" + code + "\n")
            else:
                # Regular code
                code = fix_code(code)
                print_if_main(code)
        else:
            # Anything else
            contents = cell.source
            if contents.startswith('#'):
                # Header
                line = first_line(contents)
                decoded_title = decode_title(line)
                hashes, text = split_title(decoded_title)
                underline = '=' if hashes == '#' else '-'
                print_utf8("\n")
                print_utf8(prefix_code(decoded_title, "") + "\n")
                if len(hashes) <= 2:
                    print_utf8('#' * len(hashes) + ' ' +
                               underline * len(text) + '\n')
                print_if_main("print(" + repr(sep + decoded_title) + ")\n\n")
                sep = '\n'
            else:
                # We don't include contents, as they fall under a different license
                # print_utf8("\n" + prefix_code(contents, "# ") + "\n")
                pass