Example #1
0
def add_account(whiptail, bitshares_instance):
    """ "Add account" dialog

        :param whiptail.Whiptail whiptail: instance of Whiptail or NoWhiptail
        :param bitshares.BitShares bitshares_instance: an instance of BitShares class
        :return str: user-supplied account name
    """
    validator = ConfigValidator(bitshares_instance)

    account = whiptail.prompt("Your Account Name")
    private_key = whiptail.prompt("Your Private Key", password=True)

    if not validator.validate_account_name(account):
        whiptail.alert("Account name does not exist.")
        return False
    if not validator.validate_private_key(account, private_key):
        whiptail.alert("Private key is invalid")
        return False
    if private_key and not validator.validate_private_key_type(
            account, private_key):
        whiptail.alert("Please use active private key.")
        return False

    # User can supply empty private key if it was added earlier
    if private_key:
        validator.add_private_key(private_key)
        whiptail.alert("Private Key added successfully.")

    return account
Example #2
0
def configure_dexbot(config, ctx):
    """ Main `cli configure` entrypoint

        :param dexbot.config.Config config: dexbot config
    """
    whiptail = get_whiptail('DEXBot configure')
    workers = config.get('workers', {})
    bitshares_instance = ctx.bitshares
    validator = ConfigValidator(bitshares_instance)

    if not workers:
        while True:
            txt = whiptail.prompt("Your name for the worker")
            if len(txt) == 0:
                whiptail.alert("Worker name cannot be blank. ")
            else:
                config['workers'] = {
                    txt: configure_worker(whiptail, {}, bitshares_instance)
                }
                if not whiptail.confirm(
                        "Set up another worker?\n(DEXBot can run multiple workers in one instance)"
                ):
                    break
        setup_systemd(whiptail, config)
    else:
        while True:
            action = whiptail.menu(
                "You have an existing configuration.\nSelect an action:",
                [('LIST', 'List your workers'), ('NEW', 'Create a new worker'),
                 ('EDIT', 'Edit a worker'), ('DEL_WORKER', 'Delete a worker'),
                 ('ADD', 'Add a bitshares account'),
                 ('DEL_ACCOUNT', 'Delete a bitshares account'),
                 ('SHOW', 'Show bitshares accounts'),
                 ('NODES', 'Edit Node Selection'),
                 ('ADD_NODE', 'Add Your Node'), ('HELP', 'Where to get help'),
                 ('EXIT', 'Quit this application')])

            my_workers = [(index, index) for index in workers]

            if action == 'EXIT':
                # Cancel will also exit the application. but this is a clearer label
                # Todo: modify cancel to be "Quit" or "Exit" for the whiptail menu item.
                break
            elif action == 'LIST':
                if len(my_workers):
                    # List workers, then provide option to list config of workers
                    worker_name = whiptail.menu(
                        "List of Your Workers. Select to view Configuration.",
                        my_workers)
                    content = config['workers'][worker_name]
                    text = '\n'
                    for key, value in content.items():
                        text += '{}: {}\n'.format(key, value)
                    whiptail.view_text(text, pager=False)
                else:
                    whiptail.alert('No workers to view.')
            elif action == 'EDIT':
                if len(my_workers):
                    worker_name = whiptail.menu("Select worker to edit",
                                                my_workers)
                    config['workers'][worker_name] = configure_worker(
                        whiptail, config['workers'][worker_name],
                        bitshares_instance)
                else:
                    whiptail.alert('No workers to edit.')
            elif action == 'DEL_WORKER':
                if len(my_workers):
                    worker_name = whiptail.menu("Select worker to delete",
                                                my_workers)
                    del config['workers'][worker_name]
                    # Pass ctx.config which is a loaded config (see ui.py configfile()), while `config` in a Config()
                    # instance, which is empty dict, but capable of returning keys via __getitem__(). We need to pass
                    # loaded config into StrategyBase to avoid loading a default config and preserve `--configfile`
                    # option
                    strategy = StrategyBase(
                        worker_name,
                        bitshares_instance=bitshares_instance,
                        config=ctx.config)
                    strategy.clear_all_worker_data()
                else:
                    whiptail.alert('No workers to delete.')
            elif action == 'NEW':
                worker_name = whiptail.prompt("Your name for the new worker. ")
                if not worker_name:
                    whiptail.alert("Worker name cannot be blank. ")
                elif not validator.validate_worker_name(worker_name):
                    whiptail.alert(
                        'Worker name needs to be unique. "{}" is already in use.'
                        .format(worker_name))
                else:
                    config['workers'][worker_name] = configure_worker(
                        whiptail, {}, bitshares_instance)
            elif action == 'ADD':
                add_account(whiptail, bitshares_instance)
            elif action == 'DEL_ACCOUNT':
                del_account(whiptail, bitshares_instance)
            elif action == 'SHOW':
                account_list = list_accounts(bitshares_instance)
                if account_list:
                    action = whiptail.menu(
                        "Bitshares Account List (Name - Type)", account_list)
                else:
                    whiptail.alert(
                        'You do not have any bitshares accounts in the wallet')
            elif action == 'ADD_NODE':
                txt = whiptail.prompt(
                    "Your name for the new node: e.g. wss://dexnode.net/ws")
                # Insert new node on top of the list
                config['node'].insert(0, txt)
            elif action == 'NODES':
                choice = whiptail.node_radiolist(
                    msg="Choose your preferred node",
                    items=select_choice(config['node'][0],
                                        [(index, index)
                                         for index in config['node']]))
                # Move selected node as first item in the config file's node list
                config['node'].remove(choice)
                config['node'].insert(0, choice)
                setup_systemd(whiptail, config)
            elif action == 'HELP':
                whiptail.alert(
                    "Please see https://github.com/Codaone/DEXBot/wiki")

    whiptail.clear()
    return config
Example #3
0
 def __init__(self, view, bitshares_instance, mode):
     self.view = view
     self.mode = mode
     self.validator = ConfigValidator(Config(), bitshares_instance or shared_bitshares_instance())