Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
c2023f6f
Commit
c2023f6f
authored
Feb 19, 2015
by
Brian Coca
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #10285 from bcoca/math_filter_rearrange
rearranged math filters
parents
8be8a7e2
c92c4e73
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
58 deletions
+62
-58
lib/ansible/runner/filter_plugins/core.py
+1
-54
lib/ansible/runner/filter_plugins/mathstuff.py
+58
-2
test/units/TestFilters.py
+3
-2
No files found.
lib/ansible/runner/filter_plugins/core.py
View file @
c2023f6f
...
@@ -23,7 +23,6 @@ import types
...
@@ -23,7 +23,6 @@ import types
import
pipes
import
pipes
import
glob
import
glob
import
re
import
re
import
collections
import
crypt
import
crypt
import
hashlib
import
hashlib
import
string
import
string
...
@@ -35,6 +34,7 @@ import uuid
...
@@ -35,6 +34,7 @@ import uuid
import
yaml
import
yaml
from
jinja2.filters
import
environmentfilter
from
jinja2.filters
import
environmentfilter
from
distutils.version
import
LooseVersion
,
StrictVersion
from
distutils.version
import
LooseVersion
,
StrictVersion
from
__future__
import
absolute_import
from
ansible
import
errors
from
ansible
import
errors
from
ansible.utils.hashing
import
md5s
,
checksum_s
from
ansible.utils.hashing
import
md5s
,
checksum_s
...
@@ -182,51 +182,6 @@ def ternary(value, true_val, false_val):
...
@@ -182,51 +182,6 @@ def ternary(value, true_val, false_val):
else
:
else
:
return
false_val
return
false_val
def
unique
(
a
):
if
isinstance
(
a
,
collections
.
Hashable
):
c
=
set
(
a
)
else
:
c
=
[]
for
x
in
a
:
if
x
not
in
c
:
c
.
append
(
x
)
return
c
def
intersect
(
a
,
b
):
if
isinstance
(
a
,
collections
.
Hashable
)
and
isinstance
(
b
,
collections
.
Hashable
):
c
=
set
(
a
)
&
set
(
b
)
else
:
c
=
unique
(
filter
(
lambda
x
:
x
in
b
,
a
))
return
c
def
difference
(
a
,
b
):
if
isinstance
(
a
,
collections
.
Hashable
)
and
isinstance
(
b
,
collections
.
Hashable
):
c
=
set
(
a
)
-
set
(
b
)
else
:
c
=
unique
(
filter
(
lambda
x
:
x
not
in
b
,
a
))
return
c
def
symmetric_difference
(
a
,
b
):
if
isinstance
(
a
,
collections
.
Hashable
)
and
isinstance
(
b
,
collections
.
Hashable
):
c
=
set
(
a
)
^
set
(
b
)
else
:
c
=
unique
(
filter
(
lambda
x
:
x
not
in
intersect
(
a
,
b
),
union
(
a
,
b
)))
return
c
def
union
(
a
,
b
):
if
isinstance
(
a
,
collections
.
Hashable
)
and
isinstance
(
b
,
collections
.
Hashable
):
c
=
set
(
a
)
|
set
(
b
)
else
:
c
=
unique
(
a
+
b
)
return
c
def
min
(
a
):
_min
=
__builtins__
.
get
(
'min'
)
return
_min
(
a
);
def
max
(
a
):
_max
=
__builtins__
.
get
(
'max'
)
return
_max
(
a
);
def
version_compare
(
value
,
version
,
operator
=
'eq'
,
strict
=
False
):
def
version_compare
(
value
,
version
,
operator
=
'eq'
,
strict
=
False
):
''' Perform a version comparison on a value '''
''' Perform a version comparison on a value '''
...
@@ -386,14 +341,6 @@ class FilterModule(object):
...
@@ -386,14 +341,6 @@ class FilterModule(object):
'ternary'
:
ternary
,
'ternary'
:
ternary
,
# list
# list
'unique'
:
unique
,
'intersect'
:
intersect
,
'difference'
:
difference
,
'symmetric_difference'
:
symmetric_difference
,
'union'
:
union
,
'min'
:
min
,
'max'
:
max
,
# version comparison
# version comparison
'version_compare'
:
version_compare
,
'version_compare'
:
version_compare
,
...
...
lib/ansible/runner/filter_plugins/math.py
→
lib/ansible/runner/filter_plugins/math
stuff
.py
View file @
c2023f6f
...
@@ -15,10 +15,56 @@
...
@@ -15,10 +15,56 @@
# You should have received a copy of the GNU General Public License
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from
__future__
import
absolute_import
import
math
import
math
import
collections
from
ansible
import
errors
from
ansible
import
errors
from
__future__
import
absolute_import
def
unique
(
a
):
if
isinstance
(
a
,
collections
.
Hashable
):
c
=
set
(
a
)
else
:
c
=
[]
for
x
in
a
:
if
x
not
in
c
:
c
.
append
(
x
)
return
c
def
intersect
(
a
,
b
):
if
isinstance
(
a
,
collections
.
Hashable
)
and
isinstance
(
b
,
collections
.
Hashable
):
c
=
set
(
a
)
&
set
(
b
)
else
:
c
=
unique
(
filter
(
lambda
x
:
x
in
b
,
a
))
return
c
def
difference
(
a
,
b
):
if
isinstance
(
a
,
collections
.
Hashable
)
and
isinstance
(
b
,
collections
.
Hashable
):
c
=
set
(
a
)
-
set
(
b
)
else
:
c
=
unique
(
filter
(
lambda
x
:
x
not
in
b
,
a
))
return
c
def
symmetric_difference
(
a
,
b
):
if
isinstance
(
a
,
collections
.
Hashable
)
and
isinstance
(
b
,
collections
.
Hashable
):
c
=
set
(
a
)
^
set
(
b
)
else
:
c
=
unique
(
filter
(
lambda
x
:
x
not
in
intersect
(
a
,
b
),
union
(
a
,
b
)))
return
c
def
union
(
a
,
b
):
if
isinstance
(
a
,
collections
.
Hashable
)
and
isinstance
(
b
,
collections
.
Hashable
):
c
=
set
(
a
)
|
set
(
b
)
else
:
c
=
unique
(
a
+
b
)
return
c
def
min
(
a
):
_min
=
__builtins__
.
get
(
'min'
)
return
_min
(
a
);
def
max
(
a
):
_max
=
__builtins__
.
get
(
'max'
)
return
_max
(
a
);
def
isnotanumber
(
x
):
def
isnotanumber
(
x
):
try
:
try
:
...
@@ -61,9 +107,19 @@ class FilterModule(object):
...
@@ -61,9 +107,19 @@ class FilterModule(object):
return
{
return
{
# general math
# general math
'isnan'
:
isnotanumber
,
'isnan'
:
isnotanumber
,
'min'
:
min
,
'max'
:
max
,
# exponents and logarithms
# exponents and logarithms
'log'
:
logarithm
,
'log'
:
logarithm
,
'pow'
:
power
,
'pow'
:
power
,
'root'
:
inversepower
,
'root'
:
inversepower
,
# set theory
'unique'
:
unique
,
'intersect'
:
intersect
,
'difference'
:
difference
,
'symmetric_difference'
:
symmetric_difference
,
'union'
:
union
,
}
}
test/units/TestFilters.py
View file @
c2023f6f
...
@@ -6,6 +6,7 @@ import os.path
...
@@ -6,6 +6,7 @@ import os.path
import
unittest
,
tempfile
,
shutil
import
unittest
,
tempfile
,
shutil
from
ansible
import
playbook
,
inventory
,
callbacks
from
ansible
import
playbook
,
inventory
,
callbacks
import
ansible.runner.filter_plugins.core
import
ansible.runner.filter_plugins.core
import
ansible.runner.filter_plugins.mathstuff
INVENTORY
=
inventory
.
Inventory
([
'localhost'
])
INVENTORY
=
inventory
.
Inventory
([
'localhost'
])
...
@@ -182,9 +183,9 @@ class TestFilters(unittest.TestCase):
...
@@ -182,9 +183,9 @@ class TestFilters(unittest.TestCase):
self
.
assertTrue
(
ansible
.
runner
.
filter_plugins
.
core
.
version_compare
(
'12.04'
,
12
,
'ge'
))
self
.
assertTrue
(
ansible
.
runner
.
filter_plugins
.
core
.
version_compare
(
'12.04'
,
12
,
'ge'
))
def
test_min
(
self
):
def
test_min
(
self
):
a
=
ansible
.
runner
.
filter_plugins
.
core
.
min
([
3
,
2
,
5
,
4
])
a
=
ansible
.
runner
.
filter_plugins
.
mathstuff
.
min
([
3
,
2
,
5
,
4
])
assert
a
==
2
assert
a
==
2
def
test_max
(
self
):
def
test_max
(
self
):
a
=
ansible
.
runner
.
filter_plugins
.
core
.
max
([
3
,
2
,
5
,
4
])
a
=
ansible
.
runner
.
filter_plugins
.
mathstuff
.
max
([
3
,
2
,
5
,
4
])
assert
a
==
5
assert
a
==
5
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment