Skip to content

[master][WIP] Additions to utils.data: get_type(), glob_list(), list_rm_match(), replace_list_element(), updated_dict()#64703

Open
gluf-technik wants to merge 8 commits intosaltstack:masterfrom
gluf-technik:utils_to_upstream
Open

[master][WIP] Additions to utils.data: get_type(), glob_list(), list_rm_match(), replace_list_element(), updated_dict()#64703
gluf-technik wants to merge 8 commits intosaltstack:masterfrom
gluf-technik:utils_to_upstream

Conversation

@gluf-technik
Copy link

What does this PR do?

Adds some functions to salt/utils/data.py

  • get_type() to query a Python type inside a Jinja template
  • glob_list() to get only the elements of a list matching the glob
  • list_rm_match() to get a news list without the elements matching a regex
  • replace_list_element() to turn [a, bx, c] into [a, b1, b2 ,b3, c]
  • updated_dict() to get an update of a dict without modifying the original one

I think all five are results of trying to make Jinja do things it's not made for
and at lease some of them may even be suitable for becoming Jinja filters.

Pointers to other modules doing those things are welcome, too, so we don't
have to maintain custom modules until this PR gets merged, even if only
partially ; )

What issues does this PR fix or reference?

Fixes: None

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes/No

Please review Salt's Contributing Guide for best practices.

See GitHub's page on GPG signing for more information about signing commits with GPG.

Takes a list `orig` and inserts the elements of `updates_list`
anyplace where an element equal `placeholder` has been.
In list ['a', 'bx', 'c'] placeholder 'bx' would be replaced
with the elements of ['b1', 'b2', 'b3'] resulting in list
['a', 'b1', 'b2', 'b3', 'c'].

TODO: Update docs
Uses fnmatch for "globbing" elements of a list:
The globbing the list [eth0, eth2, lo] with pattern
'eth*' results in [eth0, eth2]

Same function as used in Salt's match.glob.
https://docs.python.org/3/library/fnmatch.html

TODO:
  - Update docs
  - Add `jinja_filter("glob_list")`?
Returns a list with those elements of the
argument `listing`  which __do not__  match
the regular expression `rgx`.

TODO:
  - Update docs
  - Add `jinja_filter("list_rm_match")`?
Very useful for debugging templates.

TODO:
  - Update docs
  - Add `jinja_filter("get_type")`
AFAIK it's not possible in Jinja to get a new, updated/merged
dictionary, just using `{% do ...%}` with `.update()` to update
one inplace.

TODO:
  - Update docs
  - Add `@jinja_filter("update")`?
@gluf-technik gluf-technik requested a review from a team as a code owner July 24, 2023 11:00
@gluf-technik gluf-technik requested review from dwoz and removed request for a team July 24, 2023 11:00
@salt-project-bot-prod-environment salt-project-bot-prod-environment bot changed the title [WIP] Additions to utils.data: get_type(), glob_list(), list_rm_match(), replace_list_element(), updated_dict() [master][WIP] Additions to utils.data: get_type(), glob_list(), list_rm_match(), replace_list_element(), updated_dict() Jul 24, 2023
@gluf-technik
Copy link
Author

gluf-technik commented Jul 24, 2023

Just as I wanted to mention I'm trying to get things in alphabetical order (I guess the file was kinda "append only"?) I realized updated_dict() ended up before def t... thus 9d2bb64 ^^"
I guess the commits of this PR will get squashed anyway.

Copy link
Contributor

@dwoz dwoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gluf-technik This needs to be re-based, tests added, and a changelog entry.

@dwoz dwoz added this to the Argon v3008.0 milestone Dec 18, 2023
@dwoz dwoz requested a review from a team as a code owner March 16, 2025 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants