async def get_logger(argv, **conf_options): # loggly requires loop, so we do this in async function logger = logging.getLogger('test-root-logger') temp_dir = tempfile.mkdtemp() temp_config = os.path.join(temp_dir, 'settings.yml') try: # create a config (to be loaded on startup) _conf = Config.create_from_arguments( SimpleNamespace(config=temp_config)) with _conf.update_config(): for opt_name, opt_value in conf_options.items(): setattr(_conf, opt_name, opt_value) # do what happens on startup argv.extend(['--data-dir', temp_dir]) argv.extend(['--wallet-dir', temp_dir]) argv.extend(['--config', temp_config]) parser = cli.get_argument_parser() args, command_args = parser.parse_known_args(argv) conf: Config = Config.create_from_arguments(args) setup_logging(logger, args, conf) yield logger finally: shutil.rmtree(temp_dir, ignore_errors=True) for mod in cli.LOG_MODULES: log = logger.getChild(mod) log.setLevel(logging.NOTSET) while log.handlers: h = log.handlers[0] log.removeHandler(log.handlers[0]) h.close()
def test_max_key_fee_from_args(self): parser = argparse.ArgumentParser() Config.contribute_to_argparse(parser) # default args = parser.parse_args([]) c = Config.create_from_arguments(args) self.assertEqual(c.max_key_fee, {'amount': 50.0, 'currency': 'USD'}) # disabled args = parser.parse_args(['--no-max-key-fee']) c = Config.create_from_arguments(args) self.assertEqual(c.max_key_fee, None) # set args = parser.parse_args(['--max-key-fee', '1.0', 'BTC']) c = Config.create_from_arguments(args) self.assertEqual(c.max_key_fee, {'amount': 1.0, 'currency': 'BTC'})
def main(argv=None): argv = argv or sys.argv[1:] parser = get_argument_parser() args, command_args = parser.parse_known_args(argv) conf = Config.create_from_arguments(args) for directory in (conf.data_dir, conf.download_dir, conf.wallet_dir): ensure_directory_exists(directory) if args.cli_version: print(f"lbrynet {lbrynet_version}") elif args.command == 'start': if args.help: args.start_parser.print_help() else: if args.initial_headers: ledger_path = os.path.join(conf.wallet_dir, 'lbc_mainnet') ensure_directory_exists(ledger_path) current_size = 0 headers_path = os.path.join(ledger_path, 'headers') if os.path.exists(headers_path): current_size = os.stat(headers_path).st_size if os.stat(args.initial_headers).st_size > current_size: log.info('Copying header from %s to %s', args.initial_headers, headers_path) shutil.copy(args.initial_headers, headers_path) run_daemon(args, conf) elif args.command is not None: doc = args.doc api_method_name = args.api_method_name if args.replaced_by: print( f"{args.api_method_name} is deprecated, using {args.replaced_by['api_method_name']}." ) doc = args.replaced_by['doc'] api_method_name = args.replaced_by['api_method_name'] if args.help: print(doc) else: parsed = docopt(doc, command_args) params = set_kwargs(parsed) asyncio.get_event_loop().run_until_complete( execute_command(conf, api_method_name, params)) elif args.group is not None: args.group_parser.print_help() else: parser.print_help() return 0
def test_max_key_fee_from_yaml(self): with tempfile.TemporaryDirectory() as temp_dir: config = os.path.join(temp_dir, 'settings.yml') with open(config, 'w') as fd: fd.write('max_key_fee: {currency: USD, amount: 1}\n') c = Config.create_from_arguments( types.SimpleNamespace(config=config) ) self.assertEqual(c.max_key_fee['currency'], 'USD') self.assertEqual(c.max_key_fee['amount'], 1) with self.assertRaises(InvalidCurrencyError): c.max_key_fee = {'currency': 'BCH', 'amount': 1} with c.update_config(): c.max_key_fee = {'currency': 'BTC', 'amount': 1} with open(config, 'r') as fd: self.assertEqual(fd.read(), 'max_key_fee:\n amount: 1\n currency: BTC\n') with c.update_config(): c.max_key_fee = None with open(config, 'r') as fd: self.assertEqual(fd.read(), 'max_key_fee: null\n')
def main(argv=None): argv = argv or sys.argv[1:] parser = get_argument_parser() args, command_args = parser.parse_known_args(argv) conf = Config.create_from_arguments(args) for directory in (conf.data_dir, conf.download_dir, conf.wallet_dir): ensure_directory_exists(directory) if args.cli_version: print(f"lbrynet {lbrynet_version}") elif args.command == 'start': if args.help: args.start_parser.print_help() else: run_daemon(args, conf) elif args.command is not None: doc = args.doc api_method_name = args.api_method_name if args.replaced_by: print( f"{args.api_method_name} is deprecated, using {args.replaced_by['api_method_name']}." ) doc = args.replaced_by['doc'] api_method_name = args.replaced_by['api_method_name'] if args.help: print(doc) else: parsed = docopt(doc, command_args) params = set_kwargs(parsed) asyncio.get_event_loop().run_until_complete( execute_command(conf, api_method_name, params)) elif args.group is not None: args.group_parser.print_help() else: parser.print_help() return 0
def setup(argv): parser = cli.get_argument_parser() args, command_args = parser.parse_known_args(argv) conf = Config.create_from_arguments(args) conf.data_dir = '/tmp' setup_logging(args, conf, logger)
def main(argv=None): argv = argv or sys.argv[1:] parser = get_argument_parser() args, command_args = parser.parse_known_args(argv) conf = Config.create_from_arguments(args) for directory in (conf.data_dir, conf.download_dir, conf.wallet_dir): ensure_directory_exists(directory) if args.cli_version: print(f"lbrynet {lbrynet_version}") return 0 elif args.command == 'start': if args.help: args.start_parser.print_help() return 0 default_formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s") file_handler = logging.handlers.RotatingFileHandler( conf.log_file_path, maxBytes=2097152, backupCount=5 ) file_handler.setFormatter(default_formatter) log.addHandler(file_handler) logging.getLogger('torba').addHandler(file_handler) if not args.quiet: handler = logging.StreamHandler() handler.setFormatter(default_formatter) log.addHandler(handler) logging.getLogger('torba').addHandler(handler) logging.getLogger('torba').setLevel(logging.INFO) logging.getLogger('aioupnp').setLevel(logging.WARNING) logging.getLogger('aiohttp').setLevel(logging.CRITICAL) loop = asyncio.get_event_loop() if args.verbose: log.setLevel(logging.DEBUG) loop.set_debug(True) else: log.setLevel(logging.INFO) if conf.share_usage_data: loggly_handler = get_loggly_handler() loggly_handler.setLevel(logging.ERROR) log.addHandler(loggly_handler) daemon = Daemon(conf) started = False def __exit(): if started: daemon.stop_event.set() else: raise GracefulExit() try: loop.add_signal_handler(signal.SIGINT, __exit) loop.add_signal_handler(signal.SIGTERM, __exit) except NotImplementedError: pass # Not implemented on Windows try: loop.run_until_complete(daemon.start()) started = True loop.run_until_complete(daemon.stop_event.wait()) except (GracefulExit, KeyboardInterrupt): pass finally: loop.run_until_complete(daemon.stop()) if hasattr(loop, 'shutdown_asyncgens'): loop.run_until_complete(loop.shutdown_asyncgens()) elif args.command is not None: doc = args.doc api_method_name = args.api_method_name if args.replaced_by: print(f"{args.api_method_name} is deprecated, using {args.replaced_by['api_method_name']}.") doc = args.replaced_by['doc'] api_method_name = args.replaced_by['api_method_name'] if args.help: print(doc) else: parsed = docopt(doc, command_args) params = set_kwargs(parsed) loop = asyncio.get_event_loop() loop.run_until_complete(execute_command(conf, api_method_name, params)) elif args.group is not None: args.group_parser.print_help() else: parser.print_help() return 0
def setup(argv): parser = cli.get_argument_parser() args, command_args = parser.parse_known_args(argv) loop = asyncio.get_event_loop() conf = Config.create_from_arguments(args) cli.setup_logging(args, conf, loop)