Skip to content

Commit a48d176

Browse files
Creator- Refactor plugin templates to rename the scaffolded file and plugin name in the same. (#383)
* Updated add.py * updated names of jinja templates * chore: auto fixes from pre-commit.com hooks * updated tests * generic module scaffold now happens in modules folder * chore: auto fixes from pre-commit.com hooks * updated tests * chore: auto fixes from pre-commit.com hooks * reverted changes to launch.json * chore: auto fixes from pre-commit.com hooks * updated tests * removed hello_world" * reverted changes * changed plugin name * updated test files * Updated it for test plugin * updated test_add.py * updated doc strings * print file list * reverted * made changes to utils add and init * chore: auto fixes from pre-commit.com hooks * updated test files * chore: auto fixes from pre-commit.com hooks * updated test_add.py * chore: auto fixes from pre-commit.com hooks * updated add.py * chore: auto fixes from pre-commit.com hooks * fixed add.py * chore: auto fixes from pre-commit.com hooks * updated test_init to give a diff if dcmp fails * chore: auto fixes from pre-commit.com hooks * updated utils.py * chore: auto fixes from pre-commit.com hooks * fixes precommit issues * fixed comments * Updated sample_test.py.j2 and sample_test.py * reverted * updated to fix license issue * reverted * fixed license * reverted * updated the templates * chore: auto fixes from pre-commit.com hooks * changed the plugin_name in types * reverted * added diff * chore: auto fixes from pre-commit.com hooks * imported subprocess * chore: auto fixes from pre-commit.com hooks * updated add and utils * chore: auto fixes from pre-commit.com hooks * updated test_init * chore: auto fixes from pre-commit.com hooks * fixed init test * chore: auto fixes from pre-commit.com hooks * reverted * removed hello world * removed the diff * chore: auto fixes from pre-commit.com hooks * added @Property * chore: auto fixes from pre-commit.com hooks * removed path_replacers from Walker * removed comments * chore: auto fixes from pre-commit.com hooks * refactored each_obj * chore: auto fixes from pre-commit.com hooks * added docstring * chore: auto fixes from pre-commit.com hooks * updated params * chore: auto fixes from pre-commit.com hooks * updated params * chore: auto fixes from pre-commit.com hooks * removed params * chore: auto fixes from pre-commit.com hooks * decluttered walker class * chore: auto fixes from pre-commit.com hooks * Removed subcommand * reverted each_obj change * chore: auto fixes from pre-commit.com hooks * refactored each obj * chore: auto fixes from pre-commit.com hooks * reverted change * chore: auto fixes from pre-commit.com hooks * simplified if block * Updated the comments * chore: auto fixes from pre-commit.com hooks --------- Co-authored-by: Shashank Venkat <shvenkat> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 89403bb commit a48d176

File tree

20 files changed

+109
-80
lines changed

20 files changed

+109
-80
lines changed

src/ansible_creator/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ def __post_init__(self) -> None:
6868

6969
if isinstance(self.init_path, str):
7070
object.__setattr__(self, "init_path", expand_path(self.init_path))
71+
72+
if self.plugin_type == "module":
73+
object.__setattr__(self, "plugin_type", "modules")

src/ansible_creator/resources/collection_project/plugins/action/hello_world.py.j2 renamed to src/ansible_creator/resources/collection_project/plugins/action/sample_action.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{# action_plugin_template.j2 #}
2-
{%- set action_name = plugin_name | default("hello_world") -%}
2+
{%- set action_name = plugin_name | default("sample_action",true) -%}
33
{%- set author = author | default("Your Name") -%}
44
{%- set description = description | default("A custom action plugin for Ansible.") -%}
55
{%- set license = license | default("GPL-3.0-or-later") -%}

src/ansible_creator/resources/collection_project/plugins/filter/hello_world.py.j2 renamed to src/ansible_creator/resources/collection_project/plugins/filter/sample_filter.py.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{# filter_plugin_template.j2 #}
2-
{%- set filter_name = plugin_name | default("hello_world") -%}
2+
{%- set filter_name = plugin_name | default("sample_filter",true) -%}
33
{%- set author = author | default("Your Name") -%}
44
{%- set description = description | default("A custom filter plugin for Ansible.") -%}
55
{%- set license = license | default("GPL-3.0-or-later") -%}
@@ -41,7 +41,7 @@ EXAMPLES = """
4141
"""
4242

4343

44-
def _hello_world(name: str) -> str:
44+
def _sample_filter(name: str) -> str:
4545
"""Returns Hello message.
4646

4747
Args:
@@ -62,4 +62,4 @@ class FilterModule:
6262
Returns:
6363
dict: The filter plugin functions.
6464
"""
65-
return {"{{ filter_name }}": _hello_world}
65+
return {"{{ filter_name }}": _sample_filter}

src/ansible_creator/resources/collection_project/plugins/lookup/hello_world.py.j2 renamed to src/ansible_creator/resources/collection_project/plugins/lookup/sample_lookup.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{# lookup_plugin_template.j2 #}
2-
{%- set lookup_name = plugin_name | default("hello_world") -%}
2+
{%- set lookup_name = plugin_name | default("sample_lookup",true) -%}
33
{%- set author = author | default("Your Name (@username)") -%}
44
{%- set description = description | default("A custom lookup plugin for Ansible.") -%}
55
{%- set license = license | default("GPL-3.0-or-later") -%}

src/ansible_creator/resources/collection_project/plugins/modules/hello_world.py.j2 renamed to src/ansible_creator/resources/collection_project/plugins/modules/sample_action.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{%- set module_name = plugin_name | default("hello_world") -%}
1+
{%- set module_name = plugin_name | default("sample_action",true) -%}
22
{%- set author = author | default("Your Name (@username)") -%}
33
# {{ module_name }}.py
44
# GNU General Public License v3.0+

src/ansible_creator/resources/collection_project/plugins/sample_module/hello_world.py.j2 renamed to src/ansible_creator/resources/collection_project/plugins/modules/sample_module.py.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{# module_plugin_template.j2 #}
2-
{%- set module_name = plugin_name | default("hello_world") -%}
2+
{%- set module_name = plugin_name | default("sample_module",true) -%}
33
{%- set author = author | default("Your Name (@username)") -%}
44
{%- set description = description | default("A custom module plugin for Ansible.") -%}
55
{%- set license = license | default("GPL-3.0-or-later") -%}
@@ -41,7 +41,7 @@ EXAMPLES = """
4141
"""
4242

4343

44-
def _hello_world(name: str) -> str:
44+
def _sample_module(name: str) -> str:
4545
"""Returns Hello message.
4646

4747
Args:
@@ -62,4 +62,4 @@ class SampleModule:
6262
Returns:
6363
dict: The module plugin functions.
6464
"""
65-
return {"{{ module_name }}": _hello_world}
65+
return {"{{ module_name }}": _sample_module}

src/ansible_creator/resources/collection_project/plugins/sample_module/__init__.py.j2

Whitespace-only changes.

src/ansible_creator/resources/collection_project/plugins/test/hello_world.py.j2 renamed to src/ansible_creator/resources/collection_project/plugins/test/sample_test.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{# test_plugin_template.j2 #}
2-
{%- set test_name = plugin_name | default("hello_world") -%}
2+
{%- set test_name = plugin_name | default("sample_test",true) -%}
33
{%- set author = author | default("Your Name") -%}
44
{%- set description = description | default("A custom test plugin for Ansible.") -%}
55
{%- set license = license | default("GPL-3.0-or-later") -%}

src/ansible_creator/subcommands/add.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ def __init__(
4848
self.output: Output = config.output
4949
self.templar = Templar()
5050

51+
@property
52+
def _plugin_type_output(self) -> str:
53+
"""Format the plugin type for output.
54+
55+
Returns:
56+
Formatted plugin type string for display in output messages.
57+
"""
58+
if self._plugin_type == "modules":
59+
return "Module"
60+
return self._plugin_type.capitalize()
61+
5162
def run(self) -> None:
5263
"""Start scaffolding the resource file."""
5364
self._check_path_exists()
@@ -209,10 +220,8 @@ def _plugin_scaffold(self, plugin_path: Path) -> None:
209220
template_data = self._get_plugin_template_data()
210221
self._perform_lookup_plugin_scaffold(template_data, plugin_path)
211222

212-
elif self._plugin_type == "module":
223+
elif self._plugin_type == "modules":
213224
template_data = self._get_plugin_template_data()
214-
plugin_path = self._add_path / "plugins" / "sample_module"
215-
plugin_path.mkdir(parents=True, exist_ok=True)
216225
self._perform_module_plugin_scaffold(template_data, plugin_path)
217226

218227
elif self._plugin_type == "test":
@@ -258,7 +267,7 @@ def _perform_module_plugin_scaffold(
258267
template_data: TemplateData,
259268
plugin_path: Path,
260269
) -> None:
261-
resources = ("collection_project.plugins.sample_module",)
270+
resources = (f"collection_project.plugins.{self._plugin_type}",)
262271
self._perform_plugin_scaffold(resources, template_data, plugin_path)
263272

264273
def _perform_test_plugin_scaffold(
@@ -311,7 +320,7 @@ def _perform_plugin_scaffold(
311320

312321
if not paths.has_conflicts() or self._force or self._overwrite:
313322
copier.copy_containers(paths)
314-
self.output.note(f"{self._plugin_type.capitalize()} plugin added to {plugin_path}")
323+
self.output.note(f"{self._plugin_type_output} plugin added to {plugin_path}")
315324
return
316325

317326
if not self._overwrite:
@@ -327,7 +336,7 @@ def _perform_plugin_scaffold(
327336
)
328337
raise CreatorError(msg)
329338

330-
self.output.note(f"{self._plugin_type.capitalize()} plugin added to {plugin_path}")
339+
self.output.note(f"{self._plugin_type_output} plugin added to {plugin_path}")
331340

332341
def _get_devfile_template_data(self) -> TemplateData:
333342
"""Get the template data for devfile resources.

src/ansible_creator/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TemplateData:
3434

3535
resource_type: str = ""
3636
plugin_type: str = ""
37-
plugin_name: str = "hello_world"
37+
plugin_name: str = ""
3838
additions: dict[str, dict[str, dict[str, str | bool]]] = field(default_factory=dict)
3939
collection_name: str = ""
4040
creator_version: str = ""

src/ansible_creator/utils.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
PATH_REPLACERS = {
2929
"project_org": "namespace",
3030
"project_repo": "collection_name",
31-
"hello_world": "plugin_name",
31+
"sample_module": "plugin_name",
32+
"sample_action": "plugin_name",
33+
"sample_filter": "plugin_name",
34+
"sample_lookup": "plugin_name",
35+
"sample_test": "plugin_name",
3236
}
3337

3438

@@ -210,6 +214,8 @@ def _recursive_walk(
210214
self.output.debug(msg=f"current root set to {root}")
211215

212216
file_list = FileList()
217+
218+
# Process all objects in the directory
213219
for obj in root.iterdir():
214220
file_list.extend(
215221
self.each_obj(
@@ -244,10 +250,11 @@ def each_obj(
244250
resource.replace(".", "/") + "/",
245251
maxsplit=1,
246252
)[-1]
253+
247254
# replace placeholders in destination path with real values
248255
for key, val in PATH_REPLACERS.items():
249-
if key in dest_name:
250-
repl_val = getattr(template_data, val)
256+
repl_val = getattr(template_data, val)
257+
if key in dest_name and repl_val:
251258
dest_name = dest_name.replace(key, repl_val)
252259
dest_name = dest_name.removesuffix(".j2")
253260

@@ -259,18 +266,13 @@ def each_obj(
259266
)
260267
else:
261268
# If self.dest is a single Path
262-
dest_path = DestinationFile(
263-
dest=self.dest / dest_name,
264-
source=obj,
265-
)
269+
dest_path = DestinationFile(dest=self.dest / dest_name, source=obj)
266270

267271
self.output.debug(f"Looking at {dest_path}")
268-
269272
if obj.is_file():
270273
dest_path.set_content(template_data, self.templar)
271274

272275
if dest_path.needs_write:
273-
# Warn on conflict
274276
conflict_msg = dest_path.conflict
275277
if conflict_msg:
276278
self.output.warning(conflict_msg)
@@ -289,15 +291,13 @@ def each_obj(
289291
)
290292
if obj.is_file():
291293
return FileList([dest_path])
292-
293294
if obj.is_dir() and obj.name not in SKIP_DIRS:
294295
return self._recursive_walk(
295296
root=obj,
296297
resource=resource,
297298
current_index=current_index,
298299
template_data=template_data,
299300
)
300-
301301
return FileList()
302302

303303
def _per_container(self, resource: str, current_index: int) -> FileList:
@@ -337,6 +337,7 @@ def _per_container(self, resource: str, current_index: int) -> FileList:
337337
data=template_data,
338338
)
339339
deserialized = yaml.safe_load(templated)
340+
# Use the deserialized templated value
340341
setattr(template_data, key, deserialized)
341342
else:
342343
setattr(template_data, key, value["value"])

tests/fixtures/collection/testorg/testcol/plugins/action/hello_world.py renamed to tests/fixtures/collection/testorg/testcol/plugins/action/sample_action.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# hello_world.py - A custom action plugin for Ansible.
1+
# sample_action.py - A custom action plugin for Ansible.
22
# Author: Your Name
33
# License: GPL-3.0-or-later
44
# pylint: disable=E0401
@@ -21,7 +21,7 @@
2121

2222
class ActionModule(ActionBase): # type: ignore[misc]
2323
"""
24-
Custom Ansible action plugin: hello_world
24+
Custom Ansible action plugin: sample_action
2525
A custom action plugin for Ansible.
2626
"""
2727

tests/fixtures/collection/testorg/testcol/plugins/filter/hello_world.py renamed to tests/fixtures/collection/testorg/testcol/plugins/filter/sample_filter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# hello_world.py - A custom filter plugin for Ansible.
1+
# sample_filter.py - A custom filter plugin for Ansible.
22
# Author: Your Name
33
# License: GPL-3.0-or-later
44

@@ -15,7 +15,7 @@
1515

1616

1717
DOCUMENTATION = """
18-
name: hello_world
18+
name: sample_filter
1919
author: Your Name
2020
version_added: "1.0.0"
2121
short_description: A custom filter plugin for Ansible.
@@ -28,15 +28,15 @@
2828
"""
2929

3030
EXAMPLES = """
31-
# hello_world filter example
31+
# sample_filter filter example
3232
3333
- name: Display a hello message
3434
ansible.builtin.debug:
35-
msg: "{{ 'ansible-creator' | hello_world }}"
35+
msg: "{{ 'ansible-creator' | sample_filter }}"
3636
"""
3737

3838

39-
def _hello_world(name: str) -> str:
39+
def _sample_filter(name: str) -> str:
4040
"""Returns Hello message.
4141
4242
Args:
@@ -57,4 +57,4 @@ def filters(self) -> dict[str, Callable[[str], str]]:
5757
Returns:
5858
dict: The filter plugin functions.
5959
"""
60-
return {"hello_world": _hello_world}
60+
return {"sample_filter": _sample_filter}

tests/fixtures/collection/testorg/testcol/plugins/lookup/hello_world.py renamed to tests/fixtures/collection/testorg/testcol/plugins/lookup/sample_lookup.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# hello_world.py - A custom lookup plugin for Ansible.
1+
# sample_lookup.py - A custom lookup plugin for Ansible.
22

33
# pylint: disable=E0401
4-
# hello_world.py - A custom lookup plugin for Ansible.
4+
# sample_lookup.py - A custom lookup plugin for Ansible.
55
# Author: Your Name (@username)
66
# Copyright 2020 Red Hat
77
# GNU General Public License v3.0+
88
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
99

1010
DOCUMENTATION = """
11-
name: hello_world
11+
name: sample_lookup
1212
author: Your Name (@username)
1313
version_added: "1.0.0"
1414
short_description: A custom lookup plugin for Ansible.
@@ -23,9 +23,9 @@
2323
"""
2424

2525
EXAMPLES = """
26-
- name: Example usage of hello_world
26+
- name: Example usage of sample_lookup
2727
ansible.builtin.debug:
28-
msg: "{{ lookup('hello_world', 'example_term') }}"
28+
msg: "{{ lookup('sample_lookup', 'example_term') }}"
2929
"""
3030

3131
RETURN = """
@@ -45,7 +45,7 @@
4545

4646
class LookupModule(LookupBase): # type: ignore[misc]
4747
"""
48-
Custom Ansible lookup plugin: hello_world
48+
Custom Ansible lookup plugin: sample_lookup
4949
A custom lookup plugin for Ansible.
5050
"""
5151

@@ -72,14 +72,14 @@ def run(
7272
if not isinstance(terms, list):
7373
raise AnsibleError("The 'terms' parameter must be a list.")
7474

75-
display.vvv(f"Running hello_world lookup plugin with terms: {terms}")
75+
display.vvv(f"Running sample_lookup lookup plugin with terms: {terms}")
7676

7777
try:
7878
# Example processing logic - Replace this with actual lookup code
7979
result = [term.upper() for term in terms]
8080

81-
display.vvv(f"Result from hello_world lookup: {result}")
81+
display.vvv(f"Result from sample_lookup lookup: {result}")
8282
return result
8383

8484
except Exception as e:
85-
raise AnsibleError(f"Error in hello_world plugin: {e}") from e
85+
raise AnsibleError(f"Error in sample_lookup plugin: {e}") from e

tests/fixtures/collection/testorg/testcol/plugins/modules/hello_world.py renamed to tests/fixtures/collection/testorg/testcol/plugins/modules/sample_action.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# hello_world.py
1+
# sample_action.py
22
# GNU General Public License v3.0+
33

44
DOCUMENTATION = """
5-
module: hello_world
5+
module: sample_action
66
author: Your Name (@username)
77
version_added: "1.0.0"
88
short_description: A custom action plugin for Ansible.
@@ -28,7 +28,7 @@
2828
- name: Example Action Plugin
2929
hosts: localhost
3030
tasks:
31-
- name: Example hello_world plugin
31+
- name: Example sample_action plugin
3232
with_prefix:
3333
prefix: "Hello, World"
3434
msg: "Ansible!"

tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py renamed to tests/fixtures/collection/testorg/testcol/plugins/modules/sample_module.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# hello_world.py - A custom module plugin for Ansible.
1+
# sample_module.py - A custom module plugin for Ansible.
22
# Author: Your Name (@username)
33
# License: GPL-3.0-or-later
44

@@ -15,7 +15,7 @@
1515

1616

1717
DOCUMENTATION = """
18-
name: hello_world
18+
name: sample_module
1919
author: Your Name (@username)
2020
version_added: "1.0.0"
2121
short_description: A custom module plugin for Ansible.
@@ -28,15 +28,15 @@
2828
"""
2929

3030
EXAMPLES = """
31-
# hello_world module example
31+
# sample_module module example
3232
3333
- name: Display a hello message
3434
ansible.builtin.debug:
35-
msg: "{{ 'ansible-creator' | hello_world }}"
35+
msg: "{{ 'ansible-creator' | sample_module }}"
3636
"""
3737

3838

39-
def _hello_world(name: str) -> str:
39+
def _sample_module(name: str) -> str:
4040
"""Returns Hello message.
4141
4242
Args:
@@ -57,4 +57,4 @@ def modules(self) -> dict[str, Callable[[str], str]]:
5757
Returns:
5858
dict: The module plugin functions.
5959
"""
60-
return {"hello_world": _hello_world}
60+
return {"sample_module": _sample_module}

tests/fixtures/collection/testorg/testcol/plugins/sample_module/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)