def test_on_validated_called(self):
        on_validated_txt = ""

        def on_validated(value):
            nonlocal on_validated_txt
            on_validated_txt = value + " on validated"

        async def async_on_validated(value):
            nonlocal on_validated_txt
            on_validated_txt = value + " async on validated"

        loop = asyncio.get_event_loop()
        var = ConfigVar("key",
                        'prompt',
                        validator=lambda v: None,
                        on_validated=on_validated)
        loop.run_until_complete(var.validate("a"))
        self.assertEqual("a on validated", on_validated_txt)
        on_validated_txt = ""
        var = ConfigVar("key",
                        'prompt',
                        validator=lambda v: None,
                        on_validated=async_on_validated)
        loop.run_until_complete(var.validate("b"))
        self.assertEqual("b async on validated", on_validated_txt)
        on_validated_txt = ""
        var = ConfigVar("key",
                        'prompt',
                        validator=lambda v: "validate error",
                        on_validated=async_on_validated)
        loop.run_until_complete(var.validate("b"))
        self.assertEqual("", on_validated_txt)
 def test_validate_assertion_errors(self):
     loop = asyncio.get_event_loop()
     var = ConfigVar("key", 'prompt', validator="a")
     with self.assertRaises(AssertionError):
         loop.run_until_complete(var.validate("1"))
     var = ConfigVar("key",
                     'prompt',
                     validator=lambda v: None,
                     on_validated="a")
     with self.assertRaises(AssertionError):
         loop.run_until_complete(var.validate("1"))
    def test_validator_functions_called(self):
        def validator(_):
            return "validator error"

        async def async_validator(_):
            return "async validator error"

        loop = asyncio.get_event_loop()
        var = ConfigVar("key", 'prompt', validator=validator)
        self.assertEqual("validator error",
                         loop.run_until_complete(var.validate("a")))
        var = ConfigVar("key", 'prompt', validator=async_validator)
        self.assertEqual("async validator error",
                         loop.run_until_complete(var.validate("a")))
Example #4
0
 async def single_prompt(cvar: ConfigVar):
     if cvar.required or single_key:
         if cvar.key == "strategy_file_path":
             val = await self._import_or_create_strategy_config()
         elif cvar.key == "wallet":
             wallets = list_wallets()
             if len(wallets) > 0:
                 val = await self._unlock_wallet()
             else:
                 val = await self._create_or_import_wallet()
             logging.getLogger("hummingbot.public_eth_address").info(
                 val)
         else:
             val = await self.app.prompt(prompt=cvar.prompt,
                                         is_password=cvar.is_secure)
         if not cvar.validate(val):
             self.app.log("%s is not a valid %s value" %
                          (val, cvar.key))
             val = await single_prompt(cvar)
     else:
         val = cvar.value
     if val is None or (isinstance(val, string_types)
                        and len(val) == 0):
         val = cvar.default
     return val
Example #5
0
 async def prompt_single_variable(
         self,  # type: HummingbotApplication
         cvar: ConfigVar,
         requirement_overwrite: bool = False) -> Any:
     """
     Prompt a single variable in the input pane, validates and returns the user input
     :param cvar: the config var to be prompted
     :param requirement_overwrite: Set to true when a config var is forced to be prompted,
            even if it is not required by default setting
     :return: a validated user input or the variable's default value
     """
     if cvar.required or requirement_overwrite:
         if cvar.key == "strategy_file_path":
             val = await self._import_or_create_strategy_config()
         elif cvar.key == "wallet":
             wallets = list_wallets()
             if len(wallets) > 0:
                 val = await self._unlock_wallet()
             else:
                 val = await self._create_or_import_wallet()
             logging.getLogger("hummingbot.public_eth_address").info(val)
         else:
             val = await self.app.prompt(prompt=cvar.prompt,
                                         is_password=cvar.is_secure)
         if not cvar.validate(val):
             self._notify("%s is not a valid %s value" % (val, cvar.key))
             val = await self.prompt_single_variable(
                 cvar, requirement_overwrite)
     else:
         val = cvar.value
     if val is None or (isinstance(val, string_types) and len(val) == 0):
         val = cvar.default
     return val
 def test_validate_value_required(self):
     loop = asyncio.get_event_loop()
     var = ConfigVar("key",
                     'prompt',
                     required_if=lambda: True,
                     validator=lambda v: None)
     self.assertEqual("Value is required.",
                      loop.run_until_complete(var.validate(None)))
     self.assertEqual("Value is required.",
                      loop.run_until_complete(var.validate("")))
     var = ConfigVar("key",
                     'prompt',
                     required_if=lambda: False,
                     validator=lambda v: None)
     self.assertEqual(None, loop.run_until_complete(var.validate(None)))
     self.assertEqual(None, loop.run_until_complete(var.validate("")))
     self.assertEqual(None, loop.run_until_complete(var.validate(1)))
Example #7
0
 async def prompt_a_config(
         self,  # type: HummingbotApplication
         config: ConfigVar,
         input_value=None,
         assign_default=True):
     if input_value is None:
         if assign_default:
             self.app.set_text(parse_config_default_to_text(config))
         input_value = await self.app.prompt(prompt=config.prompt,
                                             is_password=config.is_secure)
     err_msg = config.validate(input_value)
     if err_msg is not None:
         self._notify(err_msg)
         await self.prompt_a_config(config)
     else:
         config.value = parse_cvar_value(config, input_value)
    async def prompt_single_variable(
            self,  # type: HummingbotApplication
            cvar: ConfigVar,
            requirement_overwrite: bool = False) -> Any:
        """
        Prompt a single variable in the input pane, validates and returns the user input
        :param cvar: the config var to be prompted
        :param requirement_overwrite: Set to true when a config var is forced to be prompted,
               even if it is not required by default setting
        :return: a validated user input or the variable's default value
        """
        if cvar.required or requirement_overwrite:
            if cvar.key == "password":
                return await self._one_password_config()
            if cvar.key == "strategy_file_path":
                val = await self._import_or_create_strategy_config()
            elif cvar.key == "wallet":
                wallets = list_wallets()
                if len(wallets) > 0:
                    val = await self._unlock_wallet()
                else:
                    val = await self._create_or_import_wallet()
            else:
                if cvar.value is None:
                    self.app.set_text(parse_cvar_default_value_prompt(cvar))
                val = await self.app.prompt(prompt=cvar.prompt,
                                            is_password=cvar.is_secure)

            if not cvar.validate(val):
                # If the user inputs an empty string, use the default
                val_is_empty = val is None or (isinstance(val, str)
                                               and len(val) == 0)
                if cvar.default is not None and val_is_empty:
                    val = cvar.default
                else:
                    self._notify("%s is not a valid %s value" %
                                 (val, cvar.key))
                    val = await self.prompt_single_variable(
                        cvar, requirement_overwrite)
        else:
            val = cvar.value
        if val is None or (isinstance(val, str) and len(val) == 0):
            val = cvar.default
        return val