Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
refresh-examples: add examples/task_selector parameter (#14)
Browse files Browse the repository at this point in the history
refresh-examples: add examples/task_selector parameter

This supersedes include_task_with_docs_tag
  • Loading branch information
goneri authored Dec 9, 2022
1 parent 98eea35 commit f74b8dc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 40 deletions.
44 changes: 22 additions & 22 deletions gouttelette/cmd/refresh_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import collections
import io
from pathlib import Path
from typing import Dict, List, Any, Union
from typing import Dict, List, Any, Union, Callable
import re
import ruamel.yaml
import yaml
Expand All @@ -22,10 +22,13 @@ class ContentInjectionFailure(Exception):


def _task_to_string(task: TaskType) -> str:
sanatized_task = {
k: v for k, v in task.items() if k not in ["ignore_errors", "tags"]
}
a = io.StringIO()
_yaml = ruamel.yaml.YAML()
_yaml.width = 160 # type: ignore
_yaml.dump([task], a)
_yaml.dump([sanatized_task], a)
a.seek(0)
return a.read().rstrip()

Expand Down Expand Up @@ -116,19 +119,23 @@ def extract(
tasks: List[TaskType],
collection_name: str,
dont_look_up_vars: List[str],
include_task_with_docs_tag: bool = False,
task_selector: str = "name",
) -> Dict[str, Any]:
by_modules: Dict[str, Any] = collections.defaultdict(dict)
registers: Dict[str, Any] = {}

for task in tasks:
if "name" not in task:
continue

if task["name"].startswith("_"):
print(f"Skip task {task['name']} because of the _")
continue
def valid_task(task: Dict[str, Any]) -> bool:
if task_selector == "tag":
return "docs" in task.get("tags", [])
elif task_selector == "name":
try:
return not task["name"].startswith("_")
except KeyError:
return False
else:
raise ValueError

for task in tasks:
depends_on = []
for r in list_dependencies(value=task):
if r in dont_look_up_vars:
Expand All @@ -141,8 +148,8 @@ def extract(
depends_on += registers[r]

if "register" in task:
if task["register"].startswith("_"):
print(f"Hiding register {task['register']} because of the _ prefix.")
if not valid_task(task):
print(f"Hiding register {task['register']}.")
del task["register"]
else:
registers[task["register"]] = depends_on + [task]
Expand All @@ -163,17 +170,12 @@ def extract(
if not module_fqcn:
continue

if include_task_with_docs_tag:
if "docs" in task.get("tags", []):
del task["tags"]
else:
continue

if module_fqcn not in by_modules:
by_modules[module_fqcn] = {
"blocks": [],
}
by_modules[module_fqcn]["blocks"] += depends_on + [task]
if valid_task(task):
by_modules[module_fqcn]["blocks"] += depends_on + [task]

return by_modules

Expand Down Expand Up @@ -261,9 +263,7 @@ def main() -> None:
tasks,
collection_name,
dont_look_up_vars=gouttelette["examples"]["dont_look_up_vars"],
include_task_with_docs_tag=gouttelette["examples"][
"include_task_with_docs_tag"
],
task_selector=gouttelette["examples"]["task_selector"],
)
inject(args.target_dir, extracted_examples)

Expand Down
60 changes: 43 additions & 17 deletions tests/cmd/test_refresh_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,59 @@ def test_list_dependencies():
) == ["castor"]


def test_extract_identify_valid_block():
my_tasks = [
{
"name": "This would be a great example",
"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"},
},
{
"name": "_name starts with an underscope",
"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"},
},
{"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"}},
{"foo.bar.another_module": {}, "ingore_errors": True},
{"another.collection.another_module": {"g:": 1}},
{"assert": {"that": ["something"]}, "ignore_errors": True},
]
my_tasks = [
{
"name": "This would be a great example",
"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"},
},
{
"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"},
"tags": ["docs"],
},
{
"name": "_name starts with an underscope",
"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"},
},
{"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"}},
{"foo.bar.another_module": {}, "ingore_errors": True},
{"another.collection.another_module": {"g:": 1}},
{"assert": {"that": ["something"]}, "ignore_errors": True},
]


def test_extract_identify_valid_block_using_name():
expectation = {
"foo.bar.another_module": {"blocks": []},
"foo.bar.my_module": {
"blocks": [
{
"name": "This would be a great example",
"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"},
}
]
}
},
}
assert (
refresh_examples.extract(my_tasks, "foo.bar", []) == expectation == expectation
)

assert refresh_examples.extract(my_tasks, "foo.bar", []) == expectation

def test_extract_identify_valid_block_using_tag():
expectation = {
"foo.bar.another_module": {"blocks": []},
"foo.bar.my_module": {
"blocks": [
{
"foo.bar.my_module": {"first_param": 1, "second_param": "a_string"},
"tags": ["docs"],
}
]
},
}
assert (
refresh_examples.extract(my_tasks, "foo.bar", [], task_selector="tag")
== expectation
)


def test_extract_with_dependencies():
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description = Run the test-suite and generate a HTML coverage report
deps =
pytest-cov
pytest
commands = pytest tests
commands = pytest tests -vvv

[testenv:clean]
deps = coverage
Expand Down

0 comments on commit f74b8dc

Please sign in to comment.