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')
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'
def test_parse_config_file(self): options = OptionParser() options.define("port", default=80) options.define("username", default='foo') options.parse_config_file(os.path.join(os.path.dirname(__file__), "options_test.cfg")) self.assertEquals(options.port, 443) self.assertEqual(options.username, "李康")
def test_parse_config_file(self): options = OptionParser() options.define("port", default=80) options.define("username", default='foo') options.parse_config_file( os.path.join(os.path.dirname(__file__), "options_test.cfg")) self.assertEquals(options.port, 443) self.assertEqual(options.username, "李康")
def test_dash_underscore_file(self): # No matter how an option was defined, it can be set with underscores # in a config file. for defined_name in ['foo-bar', 'foo_bar']: options = OptionParser() options.define(defined_name) options.parse_config_file(os.path.join(os.path.dirname(__file__), "options_test.cfg")) self.assertEqual(options.foo_bar, 'a')
def test_dash_underscore_cli(self): # Dashes and underscores should be interchangeable. for defined_name in ["foo-bar", "foo_bar"]: for flag in ["--foo-bar=a", "--foo_bar=a"]: options = OptionParser() options.define(defined_name) options.parse_command_line(["main.py", flag]) # Attr-style access always uses underscores. self.assertEqual(options.foo_bar, "a") # Dict-style access allows both. self.assertEqual(options["foo-bar"], "a") self.assertEqual(options["foo_bar"], "a")
def test_dash_underscore_cli(self): # Dashes and underscores should be interchangeable. for defined_name in ['foo-bar', 'foo_bar']: for flag in ['--foo-bar=a', '--foo_bar=a']: options = OptionParser() options.define(defined_name) options.parse_command_line(['main.py', flag]) # Attr-style access always uses underscores. self.assertEqual(options.foo_bar, 'a') # Dict-style access allows both. self.assertEqual(options['foo-bar'], 'a') self.assertEqual(options['foo_bar'], 'a')
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()) b_group_dict = options.group_dict("b_group") self.assertEqual({"b": 2}, b_group_dict) self.assertEqual({}, options.group_dict("nonexistent"))
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()) b_group_dict = options.group_dict('b_group') self.assertEqual({'b': 2}, b_group_dict) self.assertEqual({}, options.group_dict('nonexistent'))
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')
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")
def test_dash_underscore_introspection(self): # Original names are preserved in introspection APIs. options = OptionParser() options.define("with-dash", group="g") options.define("with_underscore", group="g") all_options = ["help", "with-dash", "with_underscore"] self.assertEqual(sorted(options), all_options) self.assertEqual(sorted(k for (k, v) in options.items()), all_options) self.assertEqual(sorted(options.as_dict().keys()), all_options) self.assertEqual(sorted(options.group_dict("g")), ["with-dash", "with_underscore"]) # --help shows CLI-style names with dashes. buf = StringIO() options.print_help(buf) self.assertIn("--with-dash", buf.getvalue()) self.assertIn("--with-underscore", buf.getvalue())
def test_dash_underscore_introspection(self): # Original names are preserved in introspection APIs. options = OptionParser() options.define('with-dash', group='g') options.define('with_underscore', group='g') all_options = ['help', 'with-dash', 'with_underscore'] self.assertEqual(sorted(options), all_options) self.assertEqual(sorted(k for (k, v) in options.items()), all_options) self.assertEqual(sorted(options.as_dict().keys()), all_options) self.assertEqual(sorted(options.group_dict('g')), ['with-dash', 'with_underscore']) # --help shows CLI-style names with dashes. buf = StringIO() options.print_help(buf) self.assertIn('--with-dash', buf.getvalue()) self.assertIn('--with-underscore', buf.getvalue())
def test_dash_underscore_introspection(self): # Original names are preserved in introspection APIs. options = OptionParser() options.define("with-dash", group="g") options.define("with_underscore", group="g") all_options = ["help", "with-dash", "with_underscore"] self.assertEqual(sorted(options), all_options) self.assertEqual(sorted(k for (k, v) in options.items()), all_options) self.assertEqual(sorted(options.as_dict().keys()), all_options) self.assertEqual( sorted(options.group_dict("g")), ["with-dash", "with_underscore"] ) # --help shows CLI-style names with dashes. buf = StringIO() options.print_help(buf) self.assertIn("--with-dash", buf.getvalue()) self.assertIn("--with-underscore", buf.getvalue())
def test_mock_patch(self): # ensure that our setattr hooks don't interfere with mock.patch options = OptionParser() options.define('foo', default=1) options.parse_command_line(['main.py', '--foo=2']) self.assertEqual(options.foo, 2) with mock.patch.object(options.mockable(), 'foo', 3): self.assertEqual(options.foo, 3) self.assertEqual(options.foo, 2) # Try nested patches mixed with explicit sets with mock.patch.object(options.mockable(), 'foo', 4): self.assertEqual(options.foo, 4) options.foo = 5 self.assertEqual(options.foo, 5) with mock.patch.object(options.mockable(), 'foo', 6): self.assertEqual(options.foo, 6) self.assertEqual(options.foo, 5) self.assertEqual(options.foo, 2)
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
def generate_options(envfile=None, autosetup=False): ''' This generates a Tornado options object for use in testing. ''' generated_options = OptionParser() # load all of the conf vars as command-line options for cv in confvars.CONF: generated_options.define(confvars.CONF[cv]['cmdline'], default=confvars.CONF[cv]['default'], help=confvars.CONF[cv]['help'], type=confvars.CONF[cv]['type']) # the path to an env file containing environment variables generated_options.define( 'envfile', default=envfile, help=('Path to a file containing environ variables ' 'for testing/development.'), type=str) # whether to make a new authdb if none exists generated_options.define( 'autosetup', default=autosetup, help=("If this is True, will automatically generate an SQLite " "authentication database in the basedir if there isn't one " "present and the value of the authdb option is also None."), type=bool) return generated_options
def test_parse_config_file(self): options = OptionParser() options.define("port", default=80) options.define("username", default="foo") options.define("my_path") config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "options_test.cfg") options.parse_config_file(config_path) self.assertEqual(options.port, 443) self.assertEqual(options.username, "李康") self.assertEqual(options.my_path, config_path)
def test_parse_config_file(self): options = OptionParser() options.define("port", default=80) options.define("username", default="foo") options.define("my_path") config_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "options_test.cfg" ) options.parse_config_file(config_path) self.assertEqual(options.port, 443) self.assertEqual(options.username, "李康") self.assertEqual(options.my_path, config_path)
def generate_options(envfile=None, autosetup=False): """ This generates a Tornado options object for use in testing. """ generated_options = OptionParser() # load all of the conf vars as command-line options for cv in confvars.CONF: generated_options.define( confvars.CONF[cv]["cmdline"], default=confvars.CONF[cv]["default"], help=confvars.CONF[cv]["help"], type=confvars.CONF[cv]["type"], ) # the path to an env file containing environment variables generated_options.define( "envfile", default=envfile, help=( "Path to a file containing environ variables " "for testing/development." ), type=str, ) # whether to make a new authdb if none exists generated_options.define( "autosetup", default=autosetup, help=( "If this is True, will automatically generate an SQLite " "authentication database in the basedir if there isn't one " "present and the value of the authdb option is also None." ), type=bool, ) return generated_options
def test_types(self): options = OptionParser() options.define('str', type=str) options.define('basestring', type=basestring_type) options.define('int', type=int) options.define('float', type=float) options.define('datetime', type=datetime.datetime) options.define('timedelta', type=datetime.timedelta) options.parse_command_line(['main.py', '--str=asdf', '--basestring=qwer', '--int=42', '--float=1.5', '--datetime=2013-04-28 05:16', '--timedelta=45s']) self.assertEqual(options.str, 'asdf') self.assertEqual(options.basestring, 'qwer') self.assertEqual(options.int, 42) self.assertEqual(options.float, 1.5) self.assertEqual(options.datetime, datetime.datetime(2013, 4, 28, 5, 16)) self.assertEqual(options.timedelta, datetime.timedelta(seconds=45))
def test_multiple_int(self): options = OptionParser() options.define("foo", type=int, multiple=True) options.parse_command_line(["main.py", "--foo=1,3,5:7"]) self.assertEqual(options.foo, [1, 3, 5, 6, 7])
def test_setattr_with_callback(self): values = [] options = OptionParser() options.define('foo', default=1, type=int, callback=values.append) options.foo = 2 self.assertEqual(values, [2])
def test_parse_command_line(self): options = OptionParser() options.define("port", default=80) options.parse_command_line(["main.py", "--port=443"]) self.assertEqual(options.port, 443)
def test_setitem(self): options = OptionParser() options.define("foo", default=1, type=int) options["foo"] = 2 self.assertEqual(options["foo"], 2)
def test_multiple_string(self): options = OptionParser() options.define("foo", type=str, multiple=True) options.parse_command_line(["main.py", "--foo=a,b,c"]) self.assertEqual(options.foo, ["a", "b", "c"])
def test_setattr(self): options = OptionParser() options.define('foo', default=1, type=int) options.foo = 2 self.assertEqual(options.foo, 2)
def load_config(): options = OptionParser() options.define('port', default=8888, help='run on the given port', type=int) options.define('debug', default=False, help='run in debug mode') options.define('api_allowed_host', default='localhost', help='origin host that has access to API') options.define('history_size', default=50, help='number of tweets stored in memoty', type=int) options.define('config', type=str, help='path to config file', callback=lambda path: options.parse_config_file(path, final=False)) options.define('logging', default='error', help='logging level') options.parse_command_line() return options
def _define_options(self): options = OptionParser() options.define('str', type=str) options.define('basestring', type=basestring_type) options.define('int', type=int) options.define('float', type=float) options.define('datetime', type=datetime.datetime) options.define('timedelta', type=datetime.timedelta) options.define('email', type=Email) options.define('list-of-int', type=int, multiple=True) return options
def test_multiple_string(self): options = OptionParser() options.define('foo', type=str, multiple=True) options.parse_command_line(['main.py', '--foo=a,b,c']) self.assertEqual(options.foo, ['a', 'b', 'c'])
def test_multiple_int(self): options = OptionParser() options.define('foo', type=int, multiple=True) options.parse_command_line(['main.py', '--foo=1,3,5:7']) self.assertEqual(options.foo, [1, 3, 5, 6, 7])
def _define_options(self): options = OptionParser() options.define("str", type=str) options.define("basestring", type=basestring_type) options.define("int", type=int) options.define("float", type=float) options.define("datetime", type=datetime.datetime) options.define("timedelta", type=datetime.timedelta) options.define("email", type=Email) options.define("list-of-int", type=int, multiple=True) return options
def _sample_options(self): options = OptionParser() options.define('a', default=1) options.define('b', default=2) return options
def test_setitem(self): options = OptionParser() options.define('foo', default=1, type=int) options['foo'] = 2 self.assertEqual(options['foo'], 2)
ListenerRouter = SockJSRouter(ListenerConnection, '/listener') handlers.extend(ListenerRouter.urls) BodyRouter = SockJSRouter(BodyConnection, '/body') handlers.extend(BodyRouter.urls) # Create multiplexer router = MultiplexConnection.get(objClass=HijackConnection) # Register multiplexer HijackRouter = SockJSRouter(router, '/hijack') handlers.extend(HijackRouter.urls) options = OptionParser() options.define("port", default=8002, help="run on the given port", type=int) options.define("proxyport", default=8989, help="port the proxy is running on", type=int) options.define("proxyhost", default=None, help="host the proxy is running on", type=str) options.define("mongourl", default="localhost:27017", help="mongodb url", type=str) options.parse_command_line() db = motor.MotorClient('mongodb://'+options.mongourl, tz_aware=True) db.proxyservice['origins'].create_index("origin", background=True) ui_methods={'nice_headers': BaseRequestHandler.nice_headers} settings = dict( handlers=handlers, static_path=os.path.join(os.path.dirname(__file__), 'static'), db=db,
def define_options(): options = OptionParser() options.define('port', default=8181, type=int, help='port to listen on') options.define('microscope_name', default="", type=str, help='Name of the microscope') options.define( 'warp_prefix', help= "Parent folder of all warp folders in the user facility's organization scheme.", group="folders") options.define( 'warp_suffix', default=None, help= "Child of the session-specific folder where warp output will be kept. Depends on workflow.", type=str, group="folders") options.define( 'live2d_prefix', help= "Parent where live2d output folders will be generated. If this is the same as the warp_prefix, a live_2d suffix is recommended.", group="folders") options.define( 'live2d_suffix', default=None, help= "Child of the session-specific folder where live2d output will be saved.", type=str, group="folders") options.define( 'listening_period_ms', default=120000, type=int, help='How long to wait between automatic job trigger checks, in ms') options.define( 'tail_log_period_ms', default=15000, type=int, help= 'How long to wait between sending the latest log lines to the clients, in ms' ) options.define( 'websocket_ping_interval', default=30, type=int, help='Period between ping pongs to keep connections alive, in seconds', group='settings') options.define( 'process_pool_size', default=32, type=int, help= 'Total number of logical processors to use for multi-process refine2d jobs' ) # Settings related to actually operating the webpage options.parse_config_file(os.path.join(config_folder, "server_settings.conf"), final=False) options.parse_command_line() return options