2024-12-02 19:20:13 +00:00
|
|
|
# Copyright (c) Max Gautier <mg@max.gautier.name>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2025-01-07 05:33:28 +00:00
|
|
|
DOCUMENTATION = r"""
|
|
|
|
name: accumulate
|
|
|
|
short_description: Produce a list of accumulated sums of the input list contents
|
|
|
|
version_added: 10.1.0
|
|
|
|
author: Max Gautier (@VannTen)
|
|
|
|
description:
|
|
|
|
- Passthrough to the L(Python itertools.accumulate function,https://docs.python.org/3/library/itertools.html#itertools.accumulate).
|
|
|
|
- Transforms an input list into the cumulative list of results from applying addition to the elements of the input list.
|
|
|
|
- Addition means the default Python implementation of C(+) for input list elements type.
|
|
|
|
options:
|
|
|
|
_input:
|
|
|
|
description: A list.
|
2024-12-02 19:20:13 +00:00
|
|
|
type: list
|
|
|
|
elements: any
|
2025-01-07 05:33:28 +00:00
|
|
|
required: true
|
|
|
|
"""
|
2024-12-02 19:20:13 +00:00
|
|
|
|
2025-01-07 05:33:28 +00:00
|
|
|
RETURN = r"""
|
|
|
|
_value:
|
|
|
|
description: A list of cumulated sums of the elements of the input list.
|
|
|
|
type: list
|
|
|
|
elements: any
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = r"""
|
2024-12-02 19:20:13 +00:00
|
|
|
- name: Enumerate parent directories of some path
|
|
|
|
ansible.builtin.debug:
|
|
|
|
var: >
|
2025-01-07 05:33:28 +00:00
|
|
|
"/some/path/to/my/file"
|
|
|
|
| split('/') | map('split', '/')
|
|
|
|
| community.general.accumulate | map('join', '/')
|
2024-12-02 19:20:13 +00:00
|
|
|
# Produces: ['', '/some', '/some/path', '/some/path/to', '/some/path/to/my', '/some/path/to/my/file']
|
2025-01-07 05:33:28 +00:00
|
|
|
|
2024-12-02 19:20:13 +00:00
|
|
|
- name: Growing string
|
|
|
|
ansible.builtin.debug:
|
|
|
|
var: "'abc' | community.general.accumulate"
|
|
|
|
# Produces ['a', 'ab', 'abc']
|
2025-01-07 05:33:28 +00:00
|
|
|
"""
|
2024-12-02 19:20:13 +00:00
|
|
|
|
|
|
|
from itertools import accumulate
|
|
|
|
from collections.abc import Sequence
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleFilterError
|
|
|
|
|
|
|
|
|
|
|
|
def list_accumulate(sequence):
|
|
|
|
if not isinstance(sequence, Sequence):
|
|
|
|
raise AnsibleFilterError('Invalid value type (%s) for accumulate (%r)' %
|
|
|
|
(type(sequence), sequence))
|
|
|
|
|
|
|
|
return accumulate(sequence)
|
|
|
|
|
|
|
|
|
|
|
|
class FilterModule(object):
|
|
|
|
|
|
|
|
def filters(self):
|
|
|
|
return {
|
|
|
|
'accumulate': list_accumulate,
|
|
|
|
}
|