コード例 #1
0
ファイル: test_dotted_path.py プロジェクト: cxz/ploomber
def test_test_create_intermediate_modules_existing_package(
        backup_test_pkg, tmp_directory):
    modules_and_function = [
        'test_pkg',
        'new_module',
        'another',
    ]
    create_intermediate_modules(modules_and_function)

    assert Path(backup_test_pkg, 'new_module', 'another.py').is_file()
コード例 #2
0
ファイル: test_dotted_path.py プロジェクト: cxz/ploomber
def test_create_intermediate_modules(tmp_directory):

    modules_and_function = ["sweet", "home", "alabama"]

    create_intermediate_modules(modules_and_function)

    assert Path(tmp_directory, "sweet").exists()
    assert Path(tmp_directory, "sweet", "__init__.py").exists()
    assert Path(tmp_directory, "sweet", "home").exists()
    assert Path(tmp_directory, "sweet", "home", "__init__.py").exists()
    assert Path(tmp_directory, "sweet", "home", "alabama.py").exists()
コード例 #3
0
ファイル: test_dotted_path.py プロジェクト: cxz/ploomber
def test_create_intermediate_modules_error_if_exists(tmp_directory,
                                                     tmp_imports):
    Path('my_functions.py').write_text("""
def my_function():
    pass
""")

    with pytest.raises(ValueError) as excinfo:
        create_intermediate_modules(['my_functions', 'my_function'])

    expected = "Dotted path 'my_functions.my_function' already exists"
    assert str(excinfo.value) == expected
コード例 #4
0
    def create(self, source, params, class_):
        """Scaffold a task if they don't exist

        Returns
        -------
        bool
            True if it created a task, False if it didn't
        """
        did_create = False

        if class_ is tasks.PythonCallable:
            source_parts = source.split('.')
            (*module_parts, fn_name) = source_parts
            params['function_name'] = fn_name

            try:
                spec = locate_dotted_path(source)
            except ModuleNotFoundError:
                create_intermediate_modules(module_parts)
                spec = locate_dotted_path(source)

            source = Path(spec.origin)
            source.parent.mkdir(parents=True, exist_ok=True)
            original = source.read_text()

            module = ast.parse(original)

            names = {
                element.name
                for element in module.body if hasattr(element, 'name')
            }

            if fn_name not in names:
                print(f'Adding {fn_name!r} to module {source!s}...')
                fn_str = self.render('function.py', params=params)
                source.write_text(original + fn_str)

                did_create = True

        #  script task...
        else:
            path = Path(source)

            if not path.exists():
                if path.suffix in {'.py', '.sql', '.ipynb', '.R', '.Rmd'}:
                    # create parent folders if needed
                    source.parent.mkdir(parents=True, exist_ok=True)

                    content = self.render('task' + source.suffix,
                                          params=params)

                    print('Adding {}...'.format(source))
                    source.write_text(content)

                    did_create = True
                else:
                    print('Error: This command does not support adding '
                          'tasks with extension "{}", valid ones are '
                          '.py and .sql. Skipping {}'.format(
                              path.suffix, path))

        return did_create
コード例 #5
0
ファイル: test_dotted_path.py プロジェクト: cxz/ploomber
def test_create_intermediate_modules_single(tmp_directory):
    with pytest.raises(ValueError):
        create_intermediate_modules(['something'])
コード例 #6
0
ファイル: test_dotted_path.py プロジェクト: cxz/ploomber
def test_create_intermediate_modules_single_namespace(tmp_directory):
    Path('namespace_pkg').mkdir()

    create_intermediate_modules(['namespace_pkg', 'another'])

    assert Path('namespace_pkg', 'another.py').is_file()