def main_patch(args): base_filename = args.base patch_filename = args.patch output_filename = args.output for fn in (base_filename, patch_filename): if not os.path.exists(fn) and fn != EXPLICIT_MISSING_FILE: print("Missing file {}".format(fn)) return 1 before = read_notebook(base_filename, on_null='empty') with io.open(patch_filename, encoding="utf8") as patch_file: diff = json.load(patch_file) diff = to_diffentry_dicts(diff) after = patch_notebook(before, diff) if output_filename: nbformat.write(after, output_filename) else: try: nbformat.validate(after, version=4) except nbformat.ValidationError: print("Patch result is not a valid notebook, printing as JSON:") json.dump(after, sys.stdout) else: pretty_print_notebook(after) return 0
def main_patch(args): base_filename = args.base patch_filename = args.patch output_filename = args.output for fn in (base_filename, patch_filename): if not os.path.exists(fn) and fn != EXPLICIT_MISSING_FILE: print("Missing file {}".format(fn)) return 1 before = read_notebook(base_filename, on_null='empty') with io.open(patch_filename, encoding="utf8") as patch_file: diff = json.load(patch_file) diff = to_diffentry_dicts(diff) after = patch_notebook(before, diff) if output_filename: nbformat.write(after, output_filename) else: try: nbformat.validate(after, version=4) except nbformat.ValidationError: print("Patch result is not a valid notebook, printing as JSON:") json.dump(after, sys.stdout) else: # This printer is to keep the unit tests passing, # some tests capture output with capsys which doesn't # pick up on sys.stdout.write() class Printer: def write(self, text): print(text, end="") config = PrettyPrintConfig(out=Printer()) pretty_print_notebook(after, config=config) return 0
def main_patch(args): base_filename = args.base path_filename = args.patch output_filename = args.output for fn in (base_filename, path_filename): if not os.path.exists(fn): print("Missing file {}".format(fn)) return 1 before = nbformat.read(base_filename, as_version=4) with io.open(path_filename, encoding="utf8") as patch_file: diff = json.load(patch_file) diff = to_diffentry_dicts(diff) after = patch_notebook(before, diff) if output_filename: nbformat.write(after, output_filename) else: print(after) return 0