Skip to content

Commit

Permalink
chore: nits across many providers
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Jan 8, 2024
1 parent 634a488 commit 821e339
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 33 deletions.
1 change: 1 addition & 0 deletions providers/.native
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\
3 changes: 3 additions & 0 deletions providers/console/tests/frontmatter-none.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Stuff!

foo bar
8 changes: 8 additions & 0 deletions providers/console/tests/test_provider_console_rich.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ def test_provider_console_markdown_frontmatter():

assert output['foo'] == 'bar'
assert len(output['stuff']) == 1


def test_provider_console_markdown_frontmatter_none():
"""Check that an empty dict returns when there is no frontmatter."""
Hook = get_hook('markdown_frontmatter')
output = Hook(path='frontmatter-none.md').exec()

assert output == {}
7 changes: 5 additions & 2 deletions providers/generate/hooks/update_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class UpdateSectionHook(BaseHook):
"""Hook for updating a section of a document."""

hook_name: str = 'update_section'
document: str = Field(..., description="Path to a document to render a section of.")
document: str = Field(
...,
description="Path to a document to render a section of.",
)
content: str = Field(
..., description="A string to update within the section of the document"
)
Expand All @@ -21,7 +24,7 @@ class UpdateSectionHook(BaseHook):
description="Marker generally in some kind of comment to end rendering from.",
)

args: list = ['document', 'content']
args: list = ['document', 'content', 'start_render', 'end_render']

def split_document(self, doc: List[str]) -> str:
upper_section = []
Expand Down
2 changes: 1 addition & 1 deletion providers/logic/hooks/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def exec(self, context: Context) -> bool:
return True
except AssertionError:
raise exceptions.HookCallException(
f"Error asserting {self.input}=={self.value}", context=context
f"Error asserting {self.input}", context=context
) from None
else:
if not self.exit_on_failure:
Expand Down
5 changes: 5 additions & 0 deletions providers/logic/tests/returns/special-key-bool.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo: bar

return->: true

should-not-be-here: true
5 changes: 5 additions & 0 deletions providers/logic/tests/returns/special-key-render.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo: bar

returns->: hello {{foo}}

should-not-be-here: true
2 changes: 2 additions & 0 deletions providers/logic/tests/returns/tackle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(target str)<-:
return->: target
18 changes: 16 additions & 2 deletions providers/logic/tests/returns/test_provider_logic_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,31 @@
[
'basic',
'render',
'special-key',
'special-key-bool',
],
)
def test_hook_return_basic(fixture):
output = tackle(f'{fixture}.yaml')

# Output should always just be a true bool
assert isinstance(output, bool)
assert output


def test_hook_return_render():
"""String values are just rendered."""
output = tackle('special-key-render.yaml')

assert output == 'hello bar'


# def test_hook_return_hook_call():
# output = tackle('hook-call.yaml')
#
# assert output


def test_hook_return_hook_call():
output = tackle('hook-call.yaml')
output = tackle('foo')

assert output
26 changes: 6 additions & 20 deletions providers/tackle/hooks/run_hook.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
from typing import Any

from tackle import BaseHook, Context, Field
from tackle.factory import new_context
from tackle.parser import walk_document
from tackle.hooks import get_hook_from_context
from tackle.parser import run_hook_exec


class RunHookHook(BaseHook):
"""Hook to run other hooks dynamically."""

hook_name: str = 'run_hook'
hook: str = Field(..., description="The name of the hook to run.")
hook_dict: dict = Field(
None,
description="A dict of keys to use with the hook.",
)

args: list = ['hook_name', 'hook_dict']
args: list = ['hook', 'hook_dict']
kwargs: str = 'hook_dict'

def exec(self, context: 'Context') -> Any:
# TODO: this can be done more easily if we have a clean way to just call a hook
# without having to build a new context. Don't need to assemble if we just have
# a simple hook call function.
element = {'tmp': {context.key_path[-1]: self.hook_name, **self.hook_dict}}

tmp_context = new_context(
_hooks=context.hooks,
existing_data=context.data.public,
)
tmp_context.key_path = ['->']
tmp_context.key_path_block = ['->']
tmp_context.hooks.public.update(context.hooks.public)
tmp_context.hooks.private.update(context.hooks.private)

walk_document(context=tmp_context, value=element)

return tmp_context.data.public['tmp']
Hook = get_hook_from_context(context, hook_name=self.hook, args=[], throw=True)
return run_hook_exec(context, Hook(**self.hook_dict))
8 changes: 2 additions & 6 deletions providers/tackle/tests/tackle/kwargs-default-hook-target.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@

<-:
foo: str
args: ['foo']
exec:
v->: var {{foo}}
(foo str)<-:
v->: var {{foo}}
2 changes: 0 additions & 2 deletions providers/toml/requirements.txt

This file was deleted.

0 comments on commit 821e339

Please sign in to comment.