"""A sequence of (name, value) pairs."""
return [(name, opt.value()) for name, opt in self._options.items()]
- def as_dict(self):
- """A dict of names and values.
+ def groups(self):
+ """The set of option-groups created by ``define``."""
+ return set(opt.group_name for opt in self._options.values())
+
+ def group_dict(self, group=None):
+ """A dict of option names and values.
+
+ If ``group`` is given, get options only from the named group; otherwise
+ all options are included.
Useful for copying options into Application settings::
- from tornado import options
- options.parse_command_line()
- application = Application(handler, **options.options.as_dict())
+ from tornado.options import define, parse_command_line, options
+
+ define('template_path', group='application')
+ define('static_path', group='application')
+
+ parse_command_line()
+
+ application = Application(
+ handlers, **options.group_dict('application'))
"""
- return dict(self.items())
+ return dict(
+ (name, opt.value()) for name, opt in self._options.items()
+ if not group or group == opt.group_name)
def define(self, name, default=None, type=None, help=None, metavar=None,
multiple=False, group=None, callback=None):
actual = sorted(options.items())
self.assertEqual(expected, actual)
- def test_as_dict(self):
- options = self._sample_options()
- options_dict = options.as_dict()
- # OptionParsers always define 'help'.
+ def test_group_dict(self):
+ options = OptionParser()
+ options.define('a', default=1)
+ options.define('b', group='b_group', default=2)
+
+ frame = sys._getframe(0)
+ this_file = frame.f_code.co_filename
+ self.assertEqual(set(['b_group', '', this_file]), options.groups())
+
+ default_group_dict = options.group_dict()
expected = {'a': 1, 'b': 2, 'help': options.help}
- self.assertEqual(expected, options_dict)
+ self.assertEqual(expected, default_group_dict)
+
+ b_group_dict = options.group_dict('b_group')
+ self.assertEqual({'b': 2}, b_group_dict)
+
+ self.assertEqual({}, options.group_dict('nonexistent'))
@unittest.skipIf(mock is None, 'mock package not present')
def test_mock_patch(self):