Skip to content

Commit

Permalink
provider: add glob hook
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Dec 2, 2022
1 parent 8f536da commit 1a4b20f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tackle/providers/paths/hooks/globs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import sys
import glob

from tackle.models import BaseHook, Field
from tackle import exceptions

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from tackle.models import Context


def raise_version_error_msg(field_name, version: int, context: 'Context'):
raise exceptions.HookCallException(
f"The field={field_name} in the glob hook not available in version 3.{version}",
context=context,
) from None


class GlobHook(BaseHook):
"""
Hook for running python's glob module. Return a possibly empty list of path names
that match pathname, which must be a string containing a path specification.
"""

hook_type: str = 'glob'
pathname: str = Field(..., description="The path to file or directory")
root_dir: str = Field(None, description="The root dir to run glob from.")
dir_fd: int = Field(
None,
description="Similar to root_dir, but it specifies the root directory as an "
"open directory descriptor instead of a path",
)
recursive: bool = Field(False, description="Search underlying directories.")
include_hidden: bool = Field(False, description="Include hidden files / dirs.")

args: list = ['pathname']

def exec(self) -> list:
options = {
'recursive': self.recursive,
}

if sys.version_info.major >= 10:
options['root_dir'] = self.root_dir
options['dir_fd'] = self.dir_fd
else:
if self.root_dir is not None:
raise_version_error_msg('root_dir', 10, self)
if self.dir_fd is not None:
raise_version_error_msg('dir_fd', 10, self)

if sys.version_info.major >= 11:
options['include_hidden'] = self.include_hidden
else:
if self.include_hidden:
raise_version_error_msg('include_hidden', 11, self)

return glob.glob(
self.pathname,
**options,
)
1 change: 1 addition & 0 deletions tackle/providers/paths/tests/glob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g->: glob *
6 changes: 6 additions & 0 deletions tackle/providers/paths/tests/test_provider_system_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ def test_provider_paths_base_dir_name(change_dir):

assert output['base'] == 'tests'
assert 'paths' in output['dir']


def test_provider_paths_glob(change_dir):
output = tackle('glob.yaml')

assert 'dirs' in output['g']

0 comments on commit 1a4b20f

Please sign in to comment.