Example #1
0
    def run(self, *args, **kwargs):
        args = ArgumentBuilder(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                               description='Convert Notebook to python script') \
            .add_work_dir_argument() \
            .add_conf_path_argument() \
            .add_docstring_conf() \
            .add_force_argument() \
            .add_argument('-n', '--notebook', type=str, required=True,
                          help='The notebook to convert') \
            .parse(args)
        self.set_log_level(args)
        conf = self.get_conf(args.working_directory, args.notebook,
                             args.conf_path)
        if not conf.path:
            raise MlVToolException('Configuration file is mandatory')
        docstring_conf_path = args.docstring_conf or conf.docstring_conf
        docstring_conf = load_docstring_conf(
            docstring_conf_path) if docstring_conf_path else None

        output_script = get_script_output_path(args.notebook, conf)
        out_dvc_cmd = get_dvc_cmd_output_path(output_script, conf)
        self.check_force(args.force, [output_script, out_dvc_cmd])

        export_to_script(args.notebook, output_script, conf)
        gen_dvc_command(output_script, out_dvc_cmd, conf, docstring_conf)
Example #2
0
    def run(self, *args, **kwargs):
        args = ArgumentBuilder(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                               description='Checks all notebooks and scripts consistency.\n'
                                           'Run the up to date checks on all notebooks from the notebook directory. '
                                           'Script names are deduce from the conf.') \
            .add_work_dir_argument() \
            .add_conf_path_argument() \
            .add_path_argument('-n', '--notebooks-dir', type=str, help='Notebooks directory') \
            .add_argument('-i', '--ignore', action='append', help='Notebook filename to ignore', default=[]) \
            .parse(args)

        self.set_log_level(args)
        conf = self.get_conf(args.working_directory, args.notebooks_dir,
                             args.conf_path)
        if not conf.path:
            raise MlVToolException('Configuration file is mandatory')

        equals = True
        for notebook in glob.glob(join(args.notebooks_dir, '*.ipynb')):
            if basename(notebook) in args.ignore:
                logging.info(f'Ignore notebook {notebook}')
                continue

            associated_script = get_script_output_path(notebook, conf)
            equals = run_consistency_check(notebook, associated_script,
                                           conf) and equals
        sys.exit(0 if equals else 1)
Example #3
0
def test_should_load_conf_file(work_dir):
    """ Test load valid conf file """

    conf_file = join(work_dir, '.mlvtools')
    write_conf(work_dir=work_dir, conf_path=conf_file, ignore_keys=['# No effect', "# Ignore"],
               script_dir='./scripts', dvc_cmd_dir='./dvc_cmd',
               dvc_py_cmd_name='VAR_Name', dvc_py_cmd_path='var_PATh3',
               dvc_meta_file_name='mETA_VAR_NaME')

    conf = load_conf_or_default(conf_file, working_directory=work_dir)

    assert conf.path.python_script_root_dir == './scripts'
    assert conf.path.dvc_cmd_root_dir == './dvc_cmd'
    assert '# No effect' in conf.ignore_keys
    assert '# Ignore' in conf.ignore_keys
    assert conf.dvc_var_python_cmd_path == 'var_PATh3'
    assert conf.dvc_var_python_cmd_name == 'VAR_Name'
    assert conf.dvc_var_meta_filename == 'mETA_VAR_NaME'

    script_path = join(conf.path.python_script_root_dir, 'mlvtools_pipeline_part1.py')
    assert get_script_output_path('./data/Pipeline Part1.ipynb', conf) == join(work_dir, script_path)
    dvc_cmd_path = join(conf.path.dvc_cmd_root_dir, 'pipeline_part1_dvc')
    assert get_dvc_cmd_output_path('./data/pipeline_part1.py', conf) == join(work_dir, dvc_cmd_path)
Example #4
0
    def run(self, *args, **kwargs):
        args = ArgumentBuilder(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                               description='Convert Notebook to python script') \
            .add_work_dir_argument() \
            .add_conf_path_argument() \
            .add_force_argument() \
            .add_path_argument('-n', '--notebook', type=str, required=True,
                               help='The notebook to convert') \
            .add_path_argument('-o', '--output', type=str,
                               help='The Python script output path') \
            .parse(args)
        self.set_log_level(args)
        conf = self.get_conf(args.working_directory, args.notebook,
                             args.conf_path)

        if not conf.path and not args.output:
            raise MlVToolException(
                'Parameter --output is mandatory if no conf provided')

        output = args.output or get_script_output_path(args.notebook, conf)

        self.check_force(args.force, [output])
        export_to_script(args.notebook, output, conf)