Example #1
0
def main():
    from subprocess import check_output, CalledProcessError
    parser = ArgumentParser(epilog=__doc__,
                            formatter_class=RawDescriptionHelpFormatter)
    task = parser.add_mutually_exclusive_group()
    task.add_argument('--install',
                      action='store_true',
                      help="""Install nbstripout in the current repository (set
                              up the git filter and attributes)""")
    task.add_argument('--uninstall',
                      action='store_true',
                      help="""Uninstall nbstripout from the current repository
                              (remove the git filter and attributes)""")
    task.add_argument(
        '--is-installed',
        action='store_true',
        help='Check if nbstripout is installed in current repository')
    task.add_argument(
        '--status',
        action='store_true',
        help=
        'Print status of nbstripout installation in current repository and configuration summary if installed'
    )
    parser.add_argument('--keep-count',
                        action='store_true',
                        help='Do not strip the execution count/prompt number')
    parser.add_argument('--keep-output',
                        action='store_true',
                        help='Do not strip output')
    parser.add_argument('--attributes',
                        metavar='FILEPATH',
                        help="""Attributes
        file to add the filter to (in combination with --install/--uninstall),
        defaults to .git/info/attributes""")
    task.add_argument('--version', action='store_true', help='Print version')
    parser.add_argument(
        '--force',
        '-f',
        action='store_true',
        help='Strip output also from files with non ipynb extension')

    parser.add_argument('--textconv',
                        '-t',
                        action='store_true',
                        help='Prints stripped files to STDOUT')

    parser.add_argument('files', nargs='*', help='Files to strip output from')
    args = parser.parse_args()

    if args.install:
        sys.exit(install(args.attributes))
    if args.uninstall:
        sys.exit(uninstall(args.attributes))
    if args.is_installed:
        sys.exit(status(verbose=False))
    if args.status:
        sys.exit(status(verbose=True))
    if args.version:
        print(__version__)
        sys.exit(0)

    try:
        extra_keys = check_output(
            ['git', 'config', 'filter.nbstripout.extrakeys']).strip()
    except CalledProcessError:
        extra_keys = ''

    input_stream = None
    if sys.version_info < (3, 0):
        import codecs
        # Use UTF8 reader/writer for stdin/stdout
        # http://stackoverflow.com/a/1169209
        if sys.stdin:
            input_stream = codecs.getreader('utf8')(sys.stdin)
        output_stream = codecs.getwriter('utf8')(sys.stdout)
    else:
        # Wrap input/output stream in UTF-8 encoded text wrapper
        # https://stackoverflow.com/a/16549381
        if sys.stdin:
            input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
        output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

    for filename in args.files:
        if not (args.force or filename.endswith('.ipynb')):
            continue
        try:
            with io.open(filename, 'r', encoding='utf8') as f:
                nb = read(f, as_version=NO_CONVERT)
            nb = strip_output(nb, args.keep_output, args.keep_count,
                              extra_keys)
            if args.textconv:
                write(nb, output_stream)
                output_stream.flush()
            else:
                with io.open(filename, 'w', encoding='utf8') as f:
                    write(nb, f)
        except NotJSONError:
            print("'{}' is not a valid notebook".format(filename),
                  file=sys.stderr)
            sys.exit(1)
        except Exception:
            # Ignore exceptions for non-notebook files.
            print("Could not strip '{}'".format(filename), file=sys.stderr)
            raise

    if not args.files and input_stream:
        try:
            nb = strip_output(read(input_stream, as_version=NO_CONVERT),
                              args.keep_output, args.keep_count, extra_keys)
            write(nb, output_stream)
            output_stream.flush()
        except NotJSONError:
            print('No valid notebook detected', file=sys.stderr)
            sys.exit(1)
Example #2
0
def main():
    parser = ArgumentParser(epilog=__doc__, formatter_class=RawDescriptionHelpFormatter)
    task = parser.add_mutually_exclusive_group()
    task.add_argument('--dry-run', action='store_true',
                      help='Print which notebooks would have been stripped')
    task.add_argument('--install', action='store_true',
                      help='Install nbstripout in the current repository (set '
                      'up the git filter and attributes)')
    task.add_argument('--uninstall', action='store_true',
                      help='Uninstall nbstripout from the current repository '
                      '(remove the git filter and attributes)')
    task.add_argument('--is-installed', action='store_true',
                      help='Check if nbstripout is installed in current repository')
    task.add_argument('--status', action='store_true',
                      help='Print status of nbstripout installation in current '
                      'repository and configuration summary if installed')
    task.add_argument('--version', action='store_true',
                      help='Print version')
    parser.add_argument('--keep-count', action='store_true',
                        help='Do not strip the execution count/prompt number')
    parser.add_argument('--keep-output', action='store_true',
                        help='Do not strip output', default=None)
    parser.add_argument('--extra-keys', default='',
                        help='Space separated list of extra keys to strip '
                        'from metadata, e.g. metadata.foo cell.metadata.bar')
    parser.add_argument('--drop-empty-cells', action='store_true',
                        help='Remove cells where `source` is empty or contains only whitepace')
    parser.add_argument('--drop-tagged-cells', default='',
                        help='Space separated list of cell-tags that remove an entire cell')
    parser.add_argument('--strip-init-cells', action='store_true',
                        help='Remove cells with `init_cell: true` metadata (default: False)')
    parser.add_argument('--attributes', metavar='FILEPATH',
                        help='Attributes file to add the filter to (in '
                        'combination with --install/--uninstall), '
                        'defaults to .git/info/attributes')
    location = parser.add_mutually_exclusive_group()
    location.add_argument('--global', dest='_global', action='store_true',
                          help='Use global git config (default is local config)')
    location.add_argument('--system', dest='_system', action='store_true',
                          help='Use system git config (default is local config)')
    parser.add_argument('--force', '-f', action='store_true',
                        help='Strip output also from files with non ipynb extension')
    parser.add_argument('--max-size', metavar='SIZE',
                        help='Keep outputs smaller than SIZE', default='0')
    parser.add_argument('--mode', '-m', default='jupyter', choices=['jupyter', 'zeppelin'],
                        help='Specify mode between [jupyter (default) | zeppelin] (to be used in combination with -f)')

    parser.add_argument('--textconv', '-t', action='store_true',
                        help='Prints stripped files to STDOUT')

    parser.add_argument('files', nargs='*', help='Files to strip output from')
    args = parser.parse_args()

    git_config = ['git', 'config']

    if args._system:
        git_config.append('--system')
        install_location = INSTALL_LOCATION_SYSTEM
    elif args._global:
        git_config.append('--global')
        install_location = INSTALL_LOCATION_GLOBAL
    else:
        git_config.append('--local')
        install_location = INSTALL_LOCATION_LOCAL

    if args.install:
        sys.exit(install(git_config, install_location, attrfile=args.attributes))
    if args.uninstall:
        sys.exit(uninstall(git_config, install_location, attrfile=args.attributes))
    if args.is_installed:
        sys.exit(status(git_config, install_location, verbose=False))
    if args.status:
        sys.exit(status(git_config, install_location, verbose=True))
    if args.version:
        print(__version__)
        sys.exit(0)

    extra_keys = [
        'metadata.signature',
        'metadata.widgets',
        'cell.metadata.collapsed',
        'cell.metadata.ExecuteTime',
        'cell.metadata.execution',
        'cell.metadata.heading_collapsed',
        'cell.metadata.hidden',
        'cell.metadata.scrolled',
    ]

    try:
        extra_keys.extend(check_output((git_config if args._system or args._global else ['git', 'config']) + ['filter.nbstripout.extrakeys'], universal_newlines=True).strip().split())
    except (CalledProcessError, FileNotFoundError):
        pass

    extra_keys.extend(args.extra_keys.split())

    # Wrap input/output stream in UTF-8 encoded text wrapper
    # https://stackoverflow.com/a/16549381
    input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') if sys.stdin else None
    output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', newline='')

    for filename in args.files:
        if not (args.force or filename.endswith('.ipynb') or filename.endswith('.zpln')):
            continue

        try:
            with io.open(filename, 'r', encoding='utf8') as f:
                if args.mode == 'zeppelin' or filename.endswith('.zpln'):
                    if args.dry_run:
                        output_stream.write(f'Dry run: would have stripped {filename}\n')
                        continue
                    nb = json.load(f, object_pairs_hook=collections.OrderedDict)
                    nb_stripped = strip_zeppelin_output(nb)

                    with open(filename, 'w') as f:
                        json.dump(nb_stripped, f, indent=2)
                    continue
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    nb = read(f, as_version=NO_CONVERT)

            nb = strip_output(nb, args.keep_output, args.keep_count, extra_keys, args.drop_empty_cells,
                              args.drop_tagged_cells.split(), args.strip_init_cells, _parse_size(args.max_size))

            if args.dry_run:
                output_stream.write(f'Dry run: would have stripped {filename}\n')

                continue

            if args.textconv:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    write(nb, output_stream)

                output_stream.flush()
            else:
                with io.open(filename, 'w', encoding='utf8', newline='') as f:
                    with warnings.catch_warnings():
                        warnings.simplefilter("ignore", category=UserWarning)
                        write(nb, f)
        except NotJSONError:
            print(f"'{filename}' is not a valid notebook", file=sys.stderr)
            sys.exit(1)
        except FileNotFoundError:
            print(f"Could not strip '{filename}': file not found", file=sys.stderr)
            sys.exit(1)
        except Exception:
            # Ignore exceptions for non-notebook files.
            print(f"Could not strip '{filename}'", file=sys.stderr)
            raise

    if not args.files and input_stream:
        try:
            if args.mode == 'zeppelin':
                if args.dry_run:
                    output_stream.write('Dry run: would have stripped input from stdin\n')
                    sys.exit(0)
                nb = json.load(input_stream, object_pairs_hook=collections.OrderedDict)
                nb_stripped = strip_zeppelin_output(nb)
                json.dump(nb_stripped, output_stream, indent=2)
                output_stream.write('\n')
                output_stream.flush()
                sys.exit(0)
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=UserWarning)
                nb = read(input_stream, as_version=NO_CONVERT)

            nb = strip_output(nb, args.keep_output, args.keep_count, extra_keys, args.drop_empty_cells,
                              args.drop_tagged_cells.split(), args.strip_init_cells, _parse_size(args.max_size))

            if args.dry_run:
                output_stream.write('Dry run: would have stripped input from '
                                    'stdin\n')
            else:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    write(nb, output_stream)

                output_stream.flush()
        except NotJSONError:
            print('No valid notebook detected', file=sys.stderr)
            sys.exit(1)
Example #3
0
def main():
    parser = ArgumentParser(epilog=__doc__,
                            formatter_class=RawDescriptionHelpFormatter)
    task = parser.add_mutually_exclusive_group()
    task.add_argument('--dry-run',
                      action='store_true',
                      help='Print which notebooks would have been stripped')
    task.add_argument('--install',
                      action='store_true',
                      help='Install nbstripout in the current repository (set '
                      'up the git filter and attributes)')
    task.add_argument('--uninstall',
                      action='store_true',
                      help='Uninstall nbstripout from the current repository '
                      '(remove the git filter and attributes)')
    task.add_argument(
        '--is-installed',
        action='store_true',
        help='Check if nbstripout is installed in current repository')
    task.add_argument(
        '--status',
        action='store_true',
        help='Print status of nbstripout installation in current '
        'repository and configuration summary if installed')
    parser.add_argument('--keep-count',
                        action='store_true',
                        help='Do not strip the execution count/prompt number')
    parser.add_argument('--keep-output',
                        action='store_true',
                        help='Do not strip output',
                        default=None)
    parser.add_argument(
        '--extra-keys',
        default='',
        help=
        'Extra keys to strip from metadata, e.g. metadata.foo cell.metadata.bar'
    )
    parser.add_argument('--attributes',
                        metavar='FILEPATH',
                        help='Attributes file to add the filter to (in '
                        'combination with --install/--uninstall), '
                        'defaults to .git/info/attributes')
    parser.add_argument('--global',
                        dest='_global',
                        action='store_true',
                        help='Use global git config (default is local config)')
    task.add_argument('--version', action='store_true', help='Print version')
    parser.add_argument(
        '--force',
        '-f',
        action='store_true',
        help='Strip output also from files with non ipynb extension')

    parser.add_argument('--textconv',
                        '-t',
                        action='store_true',
                        help='Prints stripped files to STDOUT')

    parser.add_argument('files', nargs='*', help='Files to strip output from')
    args = parser.parse_args()

    git_config = ['git', 'config'] + (['--global'] if args._global else [])
    if args.install:
        sys.exit(
            install(git_config, user=args._global, attrfile=args.attributes))
    if args.uninstall:
        sys.exit(
            uninstall(git_config, user=args._global, attrfile=args.attributes))
    if args.is_installed:
        sys.exit(status(git_config, user=args._global, verbose=False))
    if args.status:
        sys.exit(status(git_config, user=args._global, verbose=True))
    if args.version:
        print(__version__)
        sys.exit(0)

    try:
        extra_keys = check_output(git_config +
                                  ['filter.nbstripout.extrakeys']).strip()
        if args.extra_keys:
            extra_keys = ' '.join((extra_keys, args.extra_keys))
    except (CalledProcessError, FileNotFoundError):
        extra_keys = args.extra_keys

    input_stream = None
    if sys.version_info < (3, 0):
        import codecs
        # Use UTF8 reader/writer for stdin/stdout
        # http://stackoverflow.com/a/1169209
        if sys.stdin:
            input_stream = codecs.getreader('utf8')(sys.stdin)
        output_stream = codecs.getwriter('utf8')(sys.stdout)
    else:
        # Wrap input/output stream in UTF-8 encoded text wrapper
        # https://stackoverflow.com/a/16549381
        if sys.stdin:
            input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
        output_stream = io.TextIOWrapper(sys.stdout.buffer,
                                         encoding='utf-8',
                                         newline='')

    for filename in args.files:
        if not (args.force or filename.endswith('.ipynb')):
            continue
        try:
            with io.open(filename, 'r', encoding='utf8') as f:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    nb = read(f, as_version=NO_CONVERT)
            nb = strip_output(nb, args.keep_output, args.keep_count,
                              extra_keys)
            if args.dry_run:
                output_stream.write(
                    'Dry run: would have stripped {}\n'.format(filename))
                continue
            if args.textconv:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    output_stream.write(writes(nb))
                output_stream.flush()
            else:
                with io.open(filename, 'w', encoding='utf8', newline='') as f:
                    with warnings.catch_warnings():
                        warnings.simplefilter("ignore", category=UserWarning)
                        f.write(writes(nb))
        except NotJSONError:
            print("'{}' is not a valid notebook".format(filename),
                  file=sys.stderr)
            sys.exit(1)
        except FileNotFoundError:
            print("Could not strip '{}': file not found".format(filename),
                  file=sys.stderr)
            sys.exit(1)
        except Exception:
            # Ignore exceptions for non-notebook files.
            print("Could not strip '{}'".format(filename), file=sys.stderr)
            raise

    if not args.files and input_stream:
        try:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=UserWarning)
                nb = read(input_stream, as_version=NO_CONVERT)
            nb = strip_output(nb, args.keep_output, args.keep_count,
                              extra_keys)
            if args.dry_run:
                output_stream.write('Dry run: would have stripped input from '
                                    'stdin\n')
            else:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    output_stream.write(writes(nb))
                output_stream.flush()
        except NotJSONError:
            print('No valid notebook detected', file=sys.stderr)
            sys.exit(1)