-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathosa_filters.py
More file actions
201 lines (163 loc) · 6.47 KB
/
osa_filters.py
File metadata and controls
201 lines (163 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright 2015, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# (c) 2015, Kevin Carter <kevin.carter@rackspace.com>
import hashlib
import os
import re
from ansible import errors
from ansible.utils.display import Display
from jinja2 import pass_environment
from jinja2 import runtime
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
"""Filter usage:
Simple filters that may be useful from within the stack
"""
def _deprecated(new_var, old_var=None, old_var_name=None,
new_var_name=None, removed_in=None, fatal=False):
"""Provide a deprecation warning on deprecated variables.
This filter will return the old_var value if defined along with a
deprecation warning that will inform the user that the old variable
should no longer be used.
In order to use this filter the old and new variable names must be provided
to the filter as a string which is used to render the warning message. The
removed_in option is used to give a date or release name where the old
option will be removed. Optionally, if fatal is set to True, the filter
will raise an exception if the old variable is used.
USAGE: {{ new_var | deprecated(old_var,
"old_var_name",
"new_var_name",
"removed_in",
false) }}
:param new_var: ``object``
:param old_var: ``object``
:param old_var_name: ``str``
:param new_var_name: ``str``
:param removed_in: ``str``
:param fatal: ``bol``
"""
_usage = (
'USAGE: '
'{{ new_var | deprecated(old_var=old_var, old_var_name="old_var_name",'
' new_var_name="new_var_name", removed_in="removed_in",'
' fatal=false) }}'
)
if not old_var_name:
raise errors.AnsibleUndefinedVariable(
'To use this filter you must provide the "old_var_name" option'
' with the string name of the old variable that will be'
' replaced. ' + _usage
)
if not new_var_name:
raise errors.AnsibleUndefinedVariable(
'To use this filter you must provide the "new_var_name" option'
' with the string name of the new variable that will replace the'
' deprecated one. ' + _usage
)
if not removed_in:
raise errors.AnsibleUndefinedVariable(
'To use this filter you must provide the "removed_in" option with'
' the string name of the release where the old_var will be'
' removed. ' + _usage
)
# If old_var is undefined or has a None value return the new_var value
if isinstance(old_var, runtime.Undefined) or not old_var:
return new_var
display = Display()
message = (
'Deprecated Option provided: Deprecated variable: "%(old)s", Removal'
' timeframe: "%(removed_in)s", Future usage: "%(new)s"'
% {'old': old_var_name, 'new': new_var_name, 'removed_in': removed_in}
)
if str(fatal).lower() in ['yes', 'true']:
message = 'Fatally %s' % message
raise RuntimeError(message)
else:
display.warning(message)
return old_var
def _pip_requirement_split(requirement):
version_descriptors = "(>=|<=|>|<|==|~=|!=)"
requirement = requirement.split(';')
requirement_info = re.split(r'%s\s*' % version_descriptors, requirement[0])
name = requirement_info[0]
marker = None
if len(requirement) > 1:
marker = requirement[1]
versions = None
if len(requirement_info) > 1:
versions = requirement_info[1]
return name, versions, marker
def _lower_set_lists(list_one, list_two):
_list_one = set([i.lower() for i in list_one])
_list_two = set([i.lower() for i in list_two])
return _list_one, _list_two
def string_2_int(string):
"""Return the an integer from a string.
The string is hashed, converted to a base36 int, and the modulo of 10240
is returned.
:param string: string to retrieve an int from
:type string: ``str``
:returns: ``int``
"""
# Try to encode utf-8 else pass
try:
string = string.encode('utf-8')
except AttributeError:
pass
hashed_name = hashlib.sha256(string).hexdigest()
return int(hashed_name, 36) % 10240
def splitlines(string_with_lines):
"""Return a ``list`` from a string with lines."""
return string_with_lines.splitlines()
@pass_environment
def osa_rejectattr(environment, data, attribute, *args, **kwargs):
"""A wrapper for rejectattr that doesn't fail on missing attributes.
If an element is missing the attribute, it is kept. This is to preserve
the behavior of rejectattr before ansible-core 2.19.
This filter leverages the original rejectattr logic for elements that
do have the attribute.
"""
# Resolve the test name and arguments. Default is 'defined' as per Jinja2.
test_name = args[0] if args else 'defined'
test_args = args[1:]
result = []
for item in data:
# Check if the attribute is present on the item.
if (isinstance(item, dict) and attribute in item) or \
(not isinstance(item, dict) and hasattr(item, attribute)):
if isinstance(item, dict):
val = item.get(attribute)
else:
val = getattr(item, attribute)
# Apply the test using the environment.
# rejectattr logic: if test(value) is True, the item is REJECTED.
if not environment.call_test(test_name, val,
args=test_args, kwargs=kwargs):
result.append(item)
else:
result.append(item)
return result
class FilterModule(object):
"""Ansible jinja2 filters."""
@staticmethod
def filters():
return {
'string_2_int': string_2_int,
'splitlines': splitlines,
'deprecated': _deprecated,
'rejectattr': osa_rejectattr
}