Python tornado.options.Error() Examples

The following are 30 code examples of tornado.options.Error(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.options , or try the search function .
Example #1
Source File: options_test.py    From EventGhost with GNU General Public License v2.0 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"])
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #2
Source File: options_test.py    From pySINDy with MIT License 6 votes vote down vote up
def test_error_redefine_underscore(self):
        # Ensure that the dash/underscore normalization doesn't
        # interfere with the redefinition error.
        tests = [
            ('foo-bar', 'foo-bar'),
            ('foo_bar', 'foo_bar'),
            ('foo-bar', 'foo_bar'),
            ('foo_bar', 'foo-bar'),
        ]
        for a, b in tests:
            with subTest(self, a=a, b=b):
                options = OptionParser()
                options.define(a)
                with self.assertRaises(Error) as cm:
                    options.define(b)
                self.assertRegexpMatches(str(cm.exception),
                                         'Option.*foo.bar.*already defined') 
Example #3
Source File: options_test.py    From pySINDy with MIT License 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"])
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #4
Source File: options_test.py    From teleport with Apache License 2.0 6 votes vote down vote up
def test_error_redefine_underscore(self):
        # Ensure that the dash/underscore normalization doesn't
        # interfere with the redefinition error.
        tests = [
            ('foo-bar', 'foo-bar'),
            ('foo_bar', 'foo_bar'),
            ('foo-bar', 'foo_bar'),
            ('foo_bar', 'foo-bar'),
        ]
        for a, b in tests:
            with subTest(self, a=a, b=b):
                options = OptionParser()
                options.define(a)
                with self.assertRaises(Error) as cm:
                    options.define(b)
                self.assertRegexpMatches(str(cm.exception),
                                         'Option.*foo.bar.*already defined') 
Example #5
Source File: options_test.py    From tornado-zh with MIT License 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"])
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #6
Source File: options_test.py    From teleport with Apache License 2.0 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"])
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #7
Source File: settings.py    From jet-bridge with MIT License 6 votes vote down vote up
def parse_config_file(self, path, section, final=True):
    config_parser = configparser.ConfigParser()
    if not config_parser.read(path):
        raise IOError('Config file at path "{}" not found'.format(path))

    try:
        config = config_parser.items(section)
    except KeyError:
        raise ValueError('Config file does not have [{}] section]'.format(section))

    for (name, value) in config:
        normalized = self._normalize_name(name)
        normalized = normalized.lower()
        if normalized in self._options:
            option = self._options[normalized]
            if option.multiple:
                if not isinstance(value, (list, str)):
                    raise Error("Option %r is required to be a list of %s "
                                "or a comma-separated string" %
                                (option.name, option.type.__name__))

            if type(value) == str and option.type != str:
                option.parse(value)
            else:
                option.set(value) 
Example #8
Source File: settings.py    From jet-bridge with MIT License 6 votes vote down vote up
def parse_environment(self, final=True):
    for name in os.environ:
        normalized = self._normalize_name(name)
        normalized = normalized.lower()
        if normalized in self._options:
            option = self._options[normalized]
            if option.multiple:
                if not isinstance(os.environ[name], (list, str)):
                    raise Error("Option %r is required to be a list of %s "
                                "or a comma-separated string" %
                                (option.name, option.type.__name__))

            if type(os.environ[name]) == str and option.type != str:
                option.parse(os.environ[name])
            else:
                option.set(os.environ[name]) 
Example #9
Source File: options_test.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"])
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #10
Source File: options_test.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"])
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #11
Source File: options_test.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def test_error_redefine_underscore(self):
        # Ensure that the dash/underscore normalization doesn't
        # interfere with the redefinition error.
        tests = [
            ("foo-bar", "foo-bar"),
            ("foo_bar", "foo_bar"),
            ("foo-bar", "foo_bar"),
            ("foo_bar", "foo-bar"),
        ]
        for a, b in tests:
            with subTest(self, a=a, b=b):
                options = OptionParser()
                options.define(a)
                with self.assertRaises(Error) as cm:
                    options.define(b)
                self.assertRegexpMatches(
                    str(cm.exception), "Option.*foo.bar.*already defined"
                ) 
Example #12
Source File: options_test.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"]
        )
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #13
Source File: options_test.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"]
        )
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #14
Source File: options_test.py    From tornado-zh with MIT License 6 votes vote down vote up
def test_subcommand(self):
        base_options = OptionParser()
        base_options.define("verbose", default=False)
        sub_options = OptionParser()
        sub_options.define("foo", type=str)
        rest = base_options.parse_command_line(
            ["main.py", "--verbose", "subcommand", "--foo=bar"])
        self.assertEqual(rest, ["subcommand", "--foo=bar"])
        self.assertTrue(base_options.verbose)
        rest2 = sub_options.parse_command_line(rest)
        self.assertEqual(rest2, [])
        self.assertEqual(sub_options.foo, "bar")

        # the two option sets are distinct
        try:
            orig_stderr = sys.stderr
            sys.stderr = StringIO()
            with self.assertRaises(Error):
                sub_options.parse_command_line(["subcommand", "--verbose"])
        finally:
            sys.stderr = orig_stderr 
Example #15
Source File: options_test.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def test_error_redefine_underscore(self):
        # Ensure that the dash/underscore normalization doesn't
        # interfere with the redefinition error.
        tests = [
            ("foo-bar", "foo-bar"),
            ("foo_bar", "foo_bar"),
            ("foo-bar", "foo_bar"),
            ("foo_bar", "foo-bar"),
        ]
        for a, b in tests:
            with subTest(self, a=a, b=b):
                options = OptionParser()
                options.define(a)
                with self.assertRaises(Error) as cm:
                    options.define(b)
                self.assertRegexpMatches(
                    str(cm.exception), "Option.*foo.bar.*already defined"
                ) 
Example #16
Source File: options_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_setattr_type_check(self):
        # setattr requires that options be the right type and doesn't
        # parse from string formats.
        options = OptionParser()
        options.define('foo', default=1, type=int)
        with self.assertRaises(Error):
            options.foo = '2' 
Example #17
Source File: options_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_setattr_type_check(self):
        # setattr requires that options be the right type and doesn't
        # parse from string formats.
        options = OptionParser()
        options.define('foo', default=1, type=int)
        with self.assertRaises(Error):
            options.foo = '2' 
Example #18
Source File: app_terminal.py    From django-gateone with GNU General Public License v3.0 5 votes vote down vote up
def get_permissions(self, term):
        """
        Sends the client an object representing the permissions of the given
        *term*.  Example JavaScript:

        .. code-block:: javascript

            GateOne.ws.send(JSON.stringify({
                "terminal:get_permissions": 1
            }));
        """
        if 'shared' not in self.ws.persist['terminal']:
            error_msg = _("Error: Invalid share ID.")
            self.ws.send_message(error_msg)
            return
        out_dict = {'result': 'Success'}
        term_obj = self.loc_terms.get(term, None)
        if not term_obj:
            return # Term doesn't exist
        shared_terms = self.ws.persist['terminal']['shared']
        for share_id, share_dict in shared_terms.items():
            if share_dict['term_obj'] == term_obj:
                out_dict['write'] = share_dict['write']
                out_dict['read'] = share_dict['read']
                out_dict['share_id'] = share_id
                break
        message = {'terminal:sharing_permissions': out_dict}
        self.write_message(json_encode(message))
        self.trigger("terminal:get_sharing_permissions", term)

    #@require(authenticated(), policies('terminal')) 
Example #19
Source File: options_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_error_redefine(self):
        options = OptionParser()
        options.define('foo')
        with self.assertRaises(Error) as cm:
            options.define('foo')
        self.assertRegexpMatches(str(cm.exception),
                                 'Option.*foo.*already defined') 
Example #20
Source File: options_test.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def test_setattr_type_check(self):
        # setattr requires that options be the right type and doesn't
        # parse from string formats.
        options = OptionParser()
        options.define("foo", default=1, type=int)
        with self.assertRaises(Error):
            options.foo = "2" 
Example #21
Source File: app_terminal.py    From django-gateone with GNU General Public License v3.0 5 votes vote down vote up
def swap_terminals(self, settings):
        """
        Swaps the numbers of *settings['term1']* and *settings['term2']*.
        """
        term1 = int(settings.get('term1', 0))
        term2 = int(settings.get('term2', 0))
        if not term1 or not term2:
            return # Nothing to do
        missing_msg = _("Error: Terminal {term} does not exist.")
        if term1 not in self.loc_terms:
            self.ws.send_message(missing_msg.format(term=term1))
            return
        if term2 not in self.loc_terms:
            self.ws.send_message(missing_msg.format(term=term2))
            return
        term1_dict = self.loc_terms.pop(term1)
        term2_dict = self.loc_terms.pop(term2)
        self.remove_terminal_callbacks(
            term1_dict['multiplex'], self.callback_id)
        self.remove_terminal_callbacks(
            term2_dict['multiplex'], self.callback_id)
        self.loc_terms.update({term1: term2_dict})
        self.loc_terms.update({term2: term1_dict})
        self.add_terminal_callbacks(
            term1, term2_dict['multiplex'], self.callback_id)
        self.add_terminal_callbacks(
            term2, term1_dict['multiplex'], self.callback_id)
        self.trigger("terminal:swap_terminals", term1, term2)

    #@require(authenticated(), policies('terminal')) 
Example #22
Source File: options_test.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def test_error_redefine(self):
        options = OptionParser()
        options.define("foo")
        with self.assertRaises(Error) as cm:
            options.define("foo")
        self.assertRegexpMatches(str(cm.exception), "Option.*foo.*already defined") 
Example #23
Source File: configuration.py    From django-gateone with GNU General Public License v3.0 5 votes vote down vote up
def apply_cli_overrides(go_settings):
    """
    Updates *go_settings* in-place with values given on the command line.
    """
    # Figure out which options are being overridden on the command line
    arguments = []
    non_options = [
        # These are things that don't really belong in settings
        'new_api_key', 'help', 'kill', 'config', 'combine_js', 'combine_css',
        'combine_css_container', 'version', 'configure'
    ]
    for arg in list(sys.argv)[1:]:
        if not arg.startswith('-'):
            break
        else:
            arguments.append(arg.lstrip('-').split('=', 1)[0])
    go_settings['cli_overrides'] = arguments
    for argument in arguments:
        if argument in non_options:
            continue
        if argument in list(options):
            go_settings[argument] = options[argument]
    # Update Tornado's options from our settings.
    # NOTE: For options given on the command line this step should be redundant.
    for key, value in go_settings.items():
        if key in non_options:
            continue
        if key in list(options):
            if key in ('origins', 'api_keys'):
                # These two settings are special and taken care of elsewhere
                continue
            try:
                setattr(options, key, value)
            except Error:
                if isinstance(value, str):
                    if str == bytes: # Python 2
                        setattr(options, key, unicode(value))
                else:
                    setattr(options, key, str(value)) 
Example #24
Source File: options_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_error_redefine(self):
        options = OptionParser()
        options.define('foo')
        with self.assertRaises(Error) as cm:
            options.define('foo')
        self.assertRegexpMatches(str(cm.exception),
                                 'Option.*foo.*already defined') 
Example #25
Source File: options_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_setattr_type_check(self):
        # setattr requires that options be the right type and doesn't
        # parse from string formats.
        options = OptionParser()
        options.define('foo', default=1, type=int)
        with self.assertRaises(Error):
            options.foo = '2' 
Example #26
Source File: options_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_error_redefine(self):
        options = OptionParser()
        options.define('foo')
        with self.assertRaises(Error) as cm:
            options.define('foo')
        self.assertRegexpMatches(str(cm.exception),
                                 'Option.*foo.*already defined') 
Example #27
Source File: options_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_setattr_type_check(self):
        # setattr requires that options be the right type and doesn't
        # parse from string formats.
        options = OptionParser()
        options.define('foo', default=1, type=int)
        with self.assertRaises(Error):
            options.foo = '2' 
Example #28
Source File: options_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_error_redefine(self):
        options = OptionParser()
        options.define('foo')
        with self.assertRaises(Error) as cm:
            options.define('foo')
        self.assertRegexpMatches(str(cm.exception),
                                 'Option.*foo.*already defined') 
Example #29
Source File: options_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_setattr_type_check(self):
        # setattr requires that options be the right type and doesn't
        # parse from string formats.
        options = OptionParser()
        options.define('foo', default=1, type=int)
        with self.assertRaises(Error):
            options.foo = '2' 
Example #30
Source File: options_test.py    From opendevops with GNU General Public License v3.0 5 votes vote down vote up
def test_error_redefine(self):
        options = OptionParser()
        options.define("foo")
        with self.assertRaises(Error) as cm:
            options.define("foo")
        self.assertRegexpMatches(str(cm.exception), "Option.*foo.*already defined")