Exemplo n.º 1
0
 def test_choose(self):
     with captured_stdout("foo\n2\n") as stream:
         self.assertEqual(
             choose("What is your favorite color?",
                    ["blue", "yellow", "green"]), "yellow")
     self.assertEqual(
         stream.getvalue(),
         "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: "
     )
     with captured_stdout("foo\n2\n") as stream:
         self.assertEqual(
             choose("What is your favorite color?", [("blue", 10),
                                                     ("yellow", 11),
                                                     ("green", 12)]), 11)
     self.assertEqual(
         stream.getvalue(),
         "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: "
     )
     with captured_stdout("foo\n\n") as stream:
         self.assertEqual(
             choose("What is your favorite color?",
                    ["blue", "yellow", "green"],
                    default="yellow"), "yellow")
     self.assertEqual(
         stream.getvalue(),
         "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice [2]: Invalid choice, please try again\nChoice [2]: "
     )
Exemplo n.º 2
0
    def test_choose(self, capsys):
        with send_stdin("foo\n2\n"):
            assert (choose("What is your favorite color?",
                           ["blue", "yellow", "green"]) == "yellow")
        assert (
            capsys.readouterr()[0] ==
            "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: "
        )

        with send_stdin("foo\n2\n"):
            assert (choose(
                "What is your favorite color?",
                [("blue", 10), ("yellow", 11), ("green", 12)],
            ) == 11)
        assert (
            capsys.readouterr()[0] ==
            "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: "
        )

        with send_stdin("foo\n\n"):
            assert (choose(
                "What is your favorite color?",
                ["blue", "yellow", "green"],
                default="yellow",
            ) == "yellow")
        assert (
            capsys.readouterr()[0] ==
            "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice [2]: Invalid choice, please try again\nChoice [2]: "
        )
Exemplo n.º 3
0
 def test_ordered_dict(self):
     dic = OrderedDict()
     dic["one"] = "a"
     dic["two"] = "b"
     with send_stdin("1"):
         value = choose("Pick", dic)
         assert value == "a"
     with send_stdin("2"):
         value = choose("Pick", dic)
         assert value == "b"
Exemplo n.º 4
0
 def test_ordered_dict(self):
     dic = OrderedDict()
     dic["one"] = "a"
     dic["two"] = "b"
     with send_stdin("1"):
         value = choose("Pick", dic)
         assert value == "a"
     with send_stdin("2"):
         value = choose("Pick", dic)
         assert value == "b"
Exemplo n.º 5
0
 def test_choose(self):
     with captured_stdout("foo\n2\n") as stream:
         self.assertEqual(choose("What is your favorite color?", ["blue", "yellow", "green"]), "yellow")
     self.assertEqual(stream.getvalue(), "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: ")
     with captured_stdout("foo\n2\n") as stream:
         self.assertEqual(choose("What is your favorite color?", [("blue", 10), ("yellow", 11), ("green", 12)]), 11)
     self.assertEqual(stream.getvalue(), "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: ")
     with captured_stdout("foo\n\n") as stream:
         self.assertEqual(choose("What is your favorite color?", ["blue", "yellow", "green"], default = "yellow"), "yellow")
     self.assertEqual(stream.getvalue(), "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice [2]: Invalid choice, please try again\nChoice [2]: ")
Exemplo n.º 6
0
    def test_choose(self, capsys):
        with send_stdin("foo\n2\n"):
            assert choose("What is your favorite color?", ["blue", "yellow", "green"]) == "yellow"
        assert capsys.readouterr()[0] == "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: "

        with send_stdin("foo\n2\n"):
            assert choose("What is your favorite color?", [("blue", 10), ("yellow", 11), ("green", 12)]) == 11
        assert capsys.readouterr()[0] == "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice: Invalid choice, please try again\nChoice: "

        with send_stdin("foo\n\n"):
            assert choose("What is your favorite color?", ["blue", "yellow", "green"], default = "yellow") == "yellow"
        assert capsys.readouterr()[0] == "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice [2]: Invalid choice, please try again\nChoice [2]: "
Exemplo n.º 7
0
 def test_choose_dict_default(self, capsys):
     dic = OrderedDict()
     dic["one"] = "a"
     dic["two"] = "b"
     with send_stdin():
         assert choose("Pick", dic, default="a") == "a"
     assert "[1]" in capsys.readouterr()[0]
Exemplo n.º 8
0
 def act(self, vid):
     action = "replay"
     while action == "replay":
         try:
             mpv(vid)
         except ProcessExecutionError as e:
             print(e, file=sys.stderr)
             break
         try:
             action = choose(
                 f"\n{vid}:\n\n",
                 ["let it be", "quit", "kill it dead", "replay", "move to . . ."],
                 "let it be"
             )
         except KeyboardInterrupt:
             sys.exit(0)
     if action == "quit":
         sys.exit(0)
     elif action == "kill it dead":
         vid.delete()
         print("SPLAT!")
     elif action == "move to . . .":
         while True:
             dest = local.path(input("Destination folder: "))
             if not dest.is_dir():
                 print(dest, "not found.")
             else:
                 try:
                     vid.move(local.path(dest))
                 except shutil.Error as e:
                     print(e)
                     continue
                 print("WHOOSH!")
                 break
Exemplo n.º 9
0
 def test_choose_dict_default(self, capsys):
     dic = OrderedDict()
     dic["one"] = "a"
     dic["two"] = "b"
     with send_stdin():
         assert choose("Pick", dic, default="a") == "a"
     assert "[1]" in capsys.readouterr()[0]
Exemplo n.º 10
0
 def main(self, name):
     versions = ['10.0', '9.0', '8.0', '7.0']
     version = choose("Select your Odoo project template",
                      versions,
                      default="10.0")
     self._run(git["clone", self.parent.template, name])
     with local.cwd(name):
         self._run(git["checkout", version])
Exemplo n.º 11
0
 def _update_config_file(self):
     super(RubyGenerateDevComposeFile, self)._update_config_file()
     networks = [net['Name'] for net in docker.Client().networks()]
     network = choose(
         "Select the network where your odoo is running",
         networks)
     self.config['networks'] = {
         'default': {'external': {'name': str(network)}}}
Exemplo n.º 12
0
 def main(self, name):
     versions = ['develop', '2015.1', '10.0', '9.0', '8.0', '7.0']
     version = choose(
         "Select your Odoo project template",
         versions,
         default = "develop")
     self._run(git["clone", self.parent.template, name])
     with local.cwd(name):
         self._run(git["checkout", version])
Exemplo n.º 13
0
Arquivo: vpn.py Projeto: bfg/dotfiles
def connect(location=None, default=None):
    if location:
        with local.as_root():
            wg_quick('up', f'azirevpn-{location}1')
    else:
        try:
            choice = choose("Where to?", LOCATIONS, default)
        except KeyboardInterrupt:
            print()
        else:
            with local.as_root():
                wg_quick('up', f'azirevpn-{choice}1')
Exemplo n.º 14
0
 def _update_config_file(self):
     super(RubyGenerateDevComposeFile, self)._update_config_file()
     networks = [net['Name'] for net in docker.Client().networks()]
     network = choose("Select the network where your odoo is running",
                      networks)
     self.config['networks'] = {
         'default': {
             'external': {
                 'name': str(network)
             }
         }
     }
Exemplo n.º 15
0
def query_user_data(
    questions_data: AnyByStrDict,
    answers_data: AnyByStrDict,
    ask_user: bool,
    envops: EnvOps,
) -> AnyByStrDict:
    """Query the user for questions given in the config file."""
    type_maps: Dict[str, Callable] = {
        "bool": bool,
        "float": float,
        "int": int,
        "json": json.loads,
        "str": str,
        "yaml": parse_yaml_string,
    }
    env = get_jinja_env(envops=envops)
    result = DEFAULT_DATA.copy()

    _render_value = partial(render_value, env=env, context=result)
    _render_choices = partial(render_choices, env=env, context=result)

    for question, details in questions_data.items():
        # Get default answer
        answer = default = answers_data.get(
            question, _render_value(details.get("default")))
        # Get question type; by default let YAML decide it
        type_name = _render_value(details.get("type", "yaml"))
        try:
            type_fn = type_maps[type_name]
        except KeyError:
            raise InvalidTypeError()
        if ask_user:
            # Generate message to ask the user
            emoji = "🕵️" if details.get("secret", False) else "🎤"
            message = f"\n{bold | question}? Format: {type_name}\n{emoji} "
            if details.get("help"):
                message = (
                    f"\n{info & italics | _render_value(details['help'])}{message}"
                )
            # Use the right method to ask
            if type_fn is bool:
                answer = ask(message, default)
            elif details.get("choices"):
                choices = _render_choices(details["choices"])
                answer = choose(message, choices, default)
            else:
                answer = prompt(message, type_fn, default)
        result[question] = cast_answer_type(answer, type_fn)

    for key in DEFAULT_DATA:
        del result[key]
    return result
Exemplo n.º 16
0
 def test_choose_dict(self):
     with send_stdin("23\n1"):
         value = choose("Pick", dict(one="a",two="b"))
         assert value in ("a","b")
Exemplo n.º 17
0
 def test_choose_dict(self):
     with send_stdin("23\n1"):
         value = choose("Pick", dict(one="a", two="b"))
         assert value in ("a", "b")
Exemplo n.º 18
0
def choose_result(results):
    results.update({"Nothing": 'None'})
    uri = choose("What should we get?" | magenta, results, default='None')
    return {'None': None}.get(uri, uri)