mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Move salt.utils.arg_lookup to salt.utils.args.py
This commit is contained in:
parent
f8ef7dce40
commit
9df868930a
7 changed files with 80 additions and 53 deletions
|
@ -29,6 +29,7 @@ import salt.config
|
|||
import salt.loader
|
||||
import salt.transport.client
|
||||
import salt.utils
|
||||
import salt.utils.args
|
||||
import salt.utils.files
|
||||
import salt.utils.minions
|
||||
import salt.utils.versions
|
||||
|
@ -69,7 +70,7 @@ class LoadAuth(object):
|
|||
if fstr not in self.auth:
|
||||
return ''
|
||||
try:
|
||||
pname_arg = salt.utils.arg_lookup(self.auth[fstr])['args'][0]
|
||||
pname_arg = salt.utils.args.arg_lookup(self.auth[fstr])['args'][0]
|
||||
return load[pname_arg]
|
||||
except IndexError:
|
||||
return ''
|
||||
|
@ -642,7 +643,7 @@ class Resolver(object):
|
|||
'not available').format(eauth))
|
||||
return ret
|
||||
|
||||
args = salt.utils.arg_lookup(self.auth[fstr])
|
||||
args = salt.utils.args.arg_lookup(self.auth[fstr])
|
||||
for arg in args['args']:
|
||||
if arg in self.opts:
|
||||
ret[arg] = self.opts[arg]
|
||||
|
|
|
@ -204,7 +204,7 @@ def send(func, *args, **kwargs):
|
|||
if mine_func not in __salt__:
|
||||
return False
|
||||
data = {}
|
||||
arg_data = salt.utils.arg_lookup(__salt__[mine_func])
|
||||
arg_data = salt.utils.args.arg_lookup(__salt__[mine_func])
|
||||
func_data = copy.deepcopy(kwargs)
|
||||
for ind, _ in enumerate(arg_data.get('args', [])):
|
||||
try:
|
||||
|
|
|
@ -860,7 +860,7 @@ def format_call(fun,
|
|||
|
||||
aspec = salt.utils.args.get_function_argspec(fun, is_class_method=is_class_method)
|
||||
|
||||
arg_data = arg_lookup(fun, aspec)
|
||||
arg_data = salt.utils.args.arg_lookup(fun, aspec)
|
||||
args = arg_data['args']
|
||||
kwargs = arg_data['kwargs']
|
||||
|
||||
|
@ -968,21 +968,6 @@ def format_call(fun,
|
|||
return ret
|
||||
|
||||
|
||||
def arg_lookup(fun, aspec=None):
|
||||
'''
|
||||
Return a dict containing the arguments and default arguments to the
|
||||
function.
|
||||
'''
|
||||
import salt.utils.args
|
||||
ret = {'kwargs': {}}
|
||||
if aspec is None:
|
||||
aspec = salt.utils.args.get_function_argspec(fun)
|
||||
if aspec.defaults:
|
||||
ret['kwargs'] = dict(zip(aspec.args[::-1], aspec.defaults[::-1]))
|
||||
ret['args'] = [arg for arg in aspec.args if arg not in ret['kwargs']]
|
||||
return ret
|
||||
|
||||
|
||||
@jinja_filter('sorted_ignorecase')
|
||||
def isorted(to_sort):
|
||||
'''
|
||||
|
@ -2549,6 +2534,25 @@ def shlex_split(s, **kwargs):
|
|||
return salt.utils.args.shlex_split(s, **kwargs)
|
||||
|
||||
|
||||
def arg_lookup(fun, aspec=None):
|
||||
'''
|
||||
Return a dict containing the arguments and default arguments to the
|
||||
function.
|
||||
|
||||
.. deprecated:: Oxygen
|
||||
'''
|
||||
# Late import to avoid circular import.
|
||||
import salt.utils.versions
|
||||
import salt.utils.args
|
||||
salt.utils.versions.warn_until(
|
||||
'Neon',
|
||||
'Use of \'salt.utils.arg_lookup\' detected. This function has been '
|
||||
'moved to \'salt.utils.args.arg_lookup\' as of Salt Oxygen. This '
|
||||
'warning will be removed in Salt Neon.'
|
||||
)
|
||||
return salt.utils.args.arg_lookup(fun, aspec=aspec)
|
||||
|
||||
|
||||
def which(exe=None):
|
||||
'''
|
||||
Python clone of /usr/bin/which
|
||||
|
|
|
@ -268,3 +268,17 @@ def shlex_split(s, **kwargs):
|
|||
return shlex.split(s, **kwargs)
|
||||
else:
|
||||
return s
|
||||
|
||||
|
||||
def arg_lookup(fun, aspec=None):
|
||||
'''
|
||||
Return a dict containing the arguments and default arguments to the
|
||||
function.
|
||||
'''
|
||||
ret = {'kwargs': {}}
|
||||
if aspec is None:
|
||||
aspec = get_function_argspec(fun)
|
||||
if aspec.defaults:
|
||||
ret['kwargs'] = dict(zip(aspec.args[::-1], aspec.defaults[::-1]))
|
||||
ret['args'] = [arg for arg in aspec.args if arg not in ret['kwargs']]
|
||||
return ret
|
||||
|
|
|
@ -49,7 +49,8 @@ class LoadAuthTestCase(TestCase):
|
|||
self.assertEqual(ret, '', "Did not bail when the auth loader didn't have the auth type.")
|
||||
|
||||
# Test a case with valid params
|
||||
with patch('salt.utils.arg_lookup', MagicMock(return_value={'args': ['username', 'password']})) as format_call_mock:
|
||||
with patch('salt.utils.args.arg_lookup',
|
||||
MagicMock(return_value={'args': ['username', 'password']})) as format_call_mock:
|
||||
expected_ret = call('fake_func_str')
|
||||
ret = self.lauth.load_name(valid_eauth_load)
|
||||
format_call_mock.assert_has_calls((expected_ret,), any_order=True)
|
||||
|
|
|
@ -5,10 +5,17 @@ from __future__ import absolute_import
|
|||
from collections import namedtuple
|
||||
|
||||
# Import Salt Libs
|
||||
from salt.exceptions import SaltInvocationError
|
||||
import salt.utils.args
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.unit import TestCase
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
DEFAULT,
|
||||
NO_MOCK,
|
||||
NO_MOCK_REASON,
|
||||
patch
|
||||
)
|
||||
|
||||
|
||||
class ArgsTestCase(TestCase):
|
||||
|
@ -45,3 +52,33 @@ class ArgsTestCase(TestCase):
|
|||
|
||||
ret = salt.utils.args.parse_kwarg('foobar')
|
||||
self.assertEqual(ret, (None, None))
|
||||
|
||||
def test_arg_lookup(self):
|
||||
def dummy_func(first, second, third, fourth='fifth'):
|
||||
pass
|
||||
|
||||
expected_dict = {'args': ['first', 'second', 'third'], 'kwargs': {'fourth': 'fifth'}}
|
||||
ret = salt.utils.args.arg_lookup(dummy_func)
|
||||
self.assertEqual(expected_dict, ret)
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
def test_format_call(self):
|
||||
with patch('salt.utils.args.arg_lookup') as arg_lookup:
|
||||
def dummy_func(first=None, second=None, third=None):
|
||||
pass
|
||||
|
||||
arg_lookup.return_value = {'args': ['first', 'second', 'third'], 'kwargs': {}}
|
||||
get_function_argspec = DEFAULT
|
||||
get_function_argspec.return_value = namedtuple('ArgSpec', 'args varargs keywords defaults')(
|
||||
args=['first', 'second', 'third', 'fourth'], varargs=None, keywords=None, defaults=('fifth',))
|
||||
|
||||
# Make sure we raise an error if we don't pass in the requisite number of arguments
|
||||
self.assertRaises(SaltInvocationError, salt.utils.format_call, dummy_func, {'1': 2})
|
||||
|
||||
# Make sure we warn on invalid kwargs
|
||||
ret = salt.utils.format_call(dummy_func, {'first': 2, 'second': 2, 'third': 3})
|
||||
self.assertGreaterEqual(len(ret['warnings']), 1)
|
||||
|
||||
ret = salt.utils.format_call(dummy_func, {'first': 2, 'second': 2, 'third': 3},
|
||||
expected_extra_kws=('first', 'second', 'third'))
|
||||
self.assertDictEqual(ret, {'args': [], 'kwargs': {}})
|
||||
|
|
|
@ -9,7 +9,7 @@ from __future__ import absolute_import
|
|||
# Import Salt Testing libs
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
from tests.support.mock import (
|
||||
patch, DEFAULT,
|
||||
patch,
|
||||
create_autospec,
|
||||
NO_MOCK,
|
||||
NO_MOCK_REASON
|
||||
|
@ -20,14 +20,13 @@ import salt.utils
|
|||
import salt.utils.jid
|
||||
import salt.utils.yamlencoding
|
||||
import salt.utils.zeromq
|
||||
from salt.exceptions import (SaltInvocationError, SaltSystemExit, CommandNotFoundError)
|
||||
from salt.exceptions import SaltSystemExit, CommandNotFoundError
|
||||
|
||||
# Import Python libraries
|
||||
import datetime
|
||||
import os
|
||||
import yaml
|
||||
import zmq
|
||||
from collections import namedtuple
|
||||
|
||||
# Import 3rd-party libs
|
||||
try:
|
||||
|
@ -100,35 +99,6 @@ class UtilsTestCase(TestCase):
|
|||
ret = salt.utils.build_whitespace_split_regex(' '.join(LOREM_IPSUM.split()[:5]))
|
||||
self.assertEqual(ret, expected_regex)
|
||||
|
||||
def test_arg_lookup(self):
|
||||
def dummy_func(first, second, third, fourth='fifth'):
|
||||
pass
|
||||
|
||||
expected_dict = {'args': ['first', 'second', 'third'], 'kwargs': {'fourth': 'fifth'}}
|
||||
ret = salt.utils.arg_lookup(dummy_func)
|
||||
self.assertEqual(expected_dict, ret)
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
def test_format_call(self):
|
||||
with patch('salt.utils.arg_lookup') as arg_lookup:
|
||||
def dummy_func(first=None, second=None, third=None):
|
||||
pass
|
||||
arg_lookup.return_value = {'args': ['first', 'second', 'third'], 'kwargs': {}}
|
||||
get_function_argspec = DEFAULT
|
||||
get_function_argspec.return_value = namedtuple('ArgSpec', 'args varargs keywords defaults')(
|
||||
args=['first', 'second', 'third', 'fourth'], varargs=None, keywords=None, defaults=('fifth',))
|
||||
|
||||
# Make sure we raise an error if we don't pass in the requisite number of arguments
|
||||
self.assertRaises(SaltInvocationError, salt.utils.format_call, dummy_func, {'1': 2})
|
||||
|
||||
# Make sure we warn on invalid kwargs
|
||||
ret = salt.utils.format_call(dummy_func, {'first': 2, 'second': 2, 'third': 3})
|
||||
self.assertGreaterEqual(len(ret['warnings']), 1)
|
||||
|
||||
ret = salt.utils.format_call(dummy_func, {'first': 2, 'second': 2, 'third': 3},
|
||||
expected_extra_kws=('first', 'second', 'third'))
|
||||
self.assertDictEqual(ret, {'args': [], 'kwargs': {}})
|
||||
|
||||
def test_isorted(self):
|
||||
test_list = ['foo', 'Foo', 'bar', 'Bar']
|
||||
expected_list = ['bar', 'Bar', 'foo', 'Foo']
|
||||
|
|
Loading…
Add table
Reference in a new issue