Adding two new os.path filters and its docs (#78794)

* Adding two new os.path filters and its docs

Signed-off-by: Shivam Durgbuns <sdurgbun@redhat.com>
pull/78798/head
Shivam Durgbuns 2 years ago committed by GitHub
parent 4115ddd135
commit 8444d47b11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2021,6 +2021,7 @@ To join one or more path components:
.. versionadded:: 2.10 .. versionadded:: 2.10
Manipulating strings Manipulating strings
==================== ====================

@ -0,0 +1,24 @@
DOCUMENTATION:
name: commonpath
author: Shivam Durgbuns
version_added: "2.15"
short_description: gets the common path
description:
- Returns the longest common path from the given list of paths.
options:
_input:
description: A list of paths
type: list of path
required: true
seealso:
- plugin: ansible.builtin.basename
plugin_type: filter
EXAMPLES: |
# To get the longest common path(ex. '/foo/bar') from the given list of paths(ex. ['/foo/bar/foobar','/foo/bar'])
{{ listofpaths | commonpath }}
RETURN:
_value:
description: The longest common path from the given list of paths.
type: path

@ -565,6 +565,21 @@ def path_join(paths):
raise AnsibleFilterTypeError("|path_join expects string or sequence, got %s instead." % type(paths)) raise AnsibleFilterTypeError("|path_join expects string or sequence, got %s instead." % type(paths))
def commonpath(paths):
"""
Retrieve the longest common path from the given list.
:param paths: A list of file system paths.
:type paths: List[str]
:returns: The longest common path.
:rtype: str
"""
if not is_sequence(paths):
raise AnsibleFilterTypeError("|path_join expects sequence, got %s instead." % type(paths))
return os.path.commonpath(paths)
class FilterModule(object): class FilterModule(object):
''' Ansible core jinja2 filters ''' ''' Ansible core jinja2 filters '''
@ -600,6 +615,7 @@ class FilterModule(object):
'win_basename': partial(unicode_wrap, ntpath.basename), 'win_basename': partial(unicode_wrap, ntpath.basename),
'win_dirname': partial(unicode_wrap, ntpath.dirname), 'win_dirname': partial(unicode_wrap, ntpath.dirname),
'win_splitdrive': partial(unicode_wrap, ntpath.splitdrive), 'win_splitdrive': partial(unicode_wrap, ntpath.splitdrive),
'commonpath': commonpath,
# file glob # file glob
'fileglob': fileglob, 'fileglob': fileglob,

Loading…
Cancel
Save