Add as_string option to showOpts for raw string or dict output
Fixes: https://pagure.io/koji/issue/3312
This commit is contained in:
parent
b6e7c99301
commit
f42c1aa863
4 changed files with 99 additions and 32 deletions
|
|
@ -10859,10 +10859,17 @@ class RootExports(object):
|
||||||
return None
|
return None
|
||||||
return context.session.session_data
|
return context.session.session_data
|
||||||
|
|
||||||
def showOpts(self):
|
def showOpts(self, as_string=True):
|
||||||
"""Returns hub options"""
|
"""Returns hub options
|
||||||
|
|
||||||
|
:param bool as_string: if True (default) returns hub options as raw string,
|
||||||
|
if False, returns hub options as dict
|
||||||
|
"""
|
||||||
context.session.assertPerm('admin')
|
context.session.assertPerm('admin')
|
||||||
|
if as_string:
|
||||||
return "%r" % context.opts
|
return "%r" % context.opts
|
||||||
|
else:
|
||||||
|
return context.opts
|
||||||
|
|
||||||
def getEvent(self, id):
|
def getEvent(self, id):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ from __future__ import absolute_import
|
||||||
|
|
||||||
import types
|
import types
|
||||||
|
|
||||||
|
import re
|
||||||
import six
|
import six
|
||||||
import six.moves.xmlrpc_client as xmlrpc_client
|
import six.moves.xmlrpc_client as xmlrpc_client
|
||||||
|
|
||||||
|
|
@ -50,6 +51,13 @@ class ExtendedMarshaller(xmlrpc_client.Marshaller):
|
||||||
return xmlrpc_client.Marshaller.dump_int(self, value, write)
|
return xmlrpc_client.Marshaller.dump_int(self, value, write)
|
||||||
dispatch[int] = dump_int
|
dispatch[int] = dump_int
|
||||||
|
|
||||||
|
def dump_re(self, value, write):
|
||||||
|
return self._dump(repr(value), write)
|
||||||
|
if six.PY2:
|
||||||
|
dispatch[re._pattern_type] = dump_re
|
||||||
|
else:
|
||||||
|
dispatch[re.Pattern] = dump_re
|
||||||
|
|
||||||
|
|
||||||
if six.PY2:
|
if six.PY2:
|
||||||
ExtendedMarshaller.dispatch[long] = ExtendedMarshaller.dump_int # noqa: F821
|
ExtendedMarshaller.dispatch[long] = ExtendedMarshaller.dump_int # noqa: F821
|
||||||
|
|
|
||||||
31
tests/test_hub/test_show_opts.py
Normal file
31
tests/test_hub/test_show_opts.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
import mock
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import re
|
||||||
|
import kojihub
|
||||||
|
|
||||||
|
UP = kojihub.UpdateProcessor
|
||||||
|
IP = kojihub.InsertProcessor
|
||||||
|
|
||||||
|
|
||||||
|
class TestShowOpts(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.maxDiff = None
|
||||||
|
self.context = mock.patch('kojihub.context').start()
|
||||||
|
self.context.session.assertPerm = mock.MagicMock()
|
||||||
|
self.exports = kojihub.RootExports()
|
||||||
|
self.context.opts = {'MaxNameLengthInternal': 15,
|
||||||
|
'RegexNameInternal.compiled': re.compile('^[A-Za-z0-9/_.+-]+$')}
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
mock.patch.stopall()
|
||||||
|
|
||||||
|
def test_as_string(self):
|
||||||
|
rv = self.exports.showOpts()
|
||||||
|
self.assertEqual(rv, "{'MaxNameLengthInternal': 15, "
|
||||||
|
"'RegexNameInternal.compiled': re.compile('^[A-Za-z0-9/_.+-]+$')}")
|
||||||
|
|
||||||
|
def test_as_dict(self):
|
||||||
|
rv = self.exports.showOpts(as_string=False)
|
||||||
|
self.assertEqual(rv, {'MaxNameLengthInternal': 15,
|
||||||
|
'RegexNameInternal.compiled': re.compile('^[A-Za-z0-9/_.+-]+$')})
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from six.moves import range
|
from six.moves import range
|
||||||
import unittest
|
import unittest
|
||||||
|
import re
|
||||||
|
|
||||||
from six.moves import xmlrpc_client
|
from six.moves import xmlrpc_client
|
||||||
from koji import xmlrpcplus
|
from koji import xmlrpcplus
|
||||||
|
|
@ -9,6 +10,8 @@ from koji import xmlrpcplus
|
||||||
|
|
||||||
class TestDump(unittest.TestCase):
|
class TestDump(unittest.TestCase):
|
||||||
|
|
||||||
|
maxDiff = None
|
||||||
|
|
||||||
standard_data = [
|
standard_data = [
|
||||||
"Hello World",
|
"Hello World",
|
||||||
5,
|
5,
|
||||||
|
|
@ -90,6 +93,24 @@ class TestDump(unittest.TestCase):
|
||||||
self.assertEqual(params, value)
|
self.assertEqual(params, value)
|
||||||
self.assertEqual(method, method)
|
self.assertEqual(method, method)
|
||||||
|
|
||||||
|
def test_dict_data(self):
|
||||||
|
dict_data = {'MaxNameLengthInternal': 15,
|
||||||
|
'RegexNameInternal.compiled': re.compile('^[A-Za-z0-9/_.+-]+$')}
|
||||||
|
dist_data_output = ({'MaxNameLengthInternal': 15,
|
||||||
|
'RegexNameInternal.compiled': "re.compile('^[A-Za-z0-9/_.+-]+$')"},)
|
||||||
|
dict_data = (dict_data,)
|
||||||
|
enc = xmlrpcplus.dumps(dict_data, methodresponse=1)
|
||||||
|
params, method = xmlrpc_client.loads(enc)
|
||||||
|
self.assertEqual(params, dist_data_output)
|
||||||
|
self.assertEqual(method, None)
|
||||||
|
# and as a call
|
||||||
|
method = "foomethod"
|
||||||
|
value = tuple(self.long_data)
|
||||||
|
enc = xmlrpcplus.dumps(value, methodname=method)
|
||||||
|
params, method = xmlrpc_client.loads(enc)
|
||||||
|
self.assertEqual(params, value)
|
||||||
|
self.assertEqual(method, method)
|
||||||
|
|
||||||
def test_overflow(self):
|
def test_overflow(self):
|
||||||
value = (2**64,)
|
value = (2**64,)
|
||||||
with self.assertRaises(OverflowError):
|
with self.assertRaises(OverflowError):
|
||||||
|
|
@ -154,7 +175,7 @@ class TestDump(unittest.TestCase):
|
||||||
-1024,
|
-1024,
|
||||||
2 ** 31 - 1,
|
2 ** 31 - 1,
|
||||||
-2 ** 31,
|
-2 ** 31,
|
||||||
[2**31 -1],
|
[2**31 - 1],
|
||||||
{"a": -2 ** 31, "b": 3.14},
|
{"a": -2 ** 31, "b": 3.14},
|
||||||
]
|
]
|
||||||
for value in data:
|
for value in data:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue