def run(self, name, backend): # Validate the parameters form = AddAccountForm(name=name, backend=backend) if not form.validate(): self.err(**form.errors) return # Ask the user for the backend configuration options backend = Backend.get_backend(backend) config = {'backend': form.data['backend']} for field in backend.config_form(): self.out((field.label.text, 'blue')) value = input('> ').strip() if value: config[field.name] = value # Validate the users configuration result = backend.validate_config(**config) if not result[0]: self.err('Invalid backend config:', **result[1]) return # Create the new account account = Account(name=form.data['name'], backend=config) account.insert() self.out(('Account added: {0}'.format(account.api_key), 'bold_green'))
def run(self, name): # Validate the parameters form = ConfigAccountBackendForm(name=name) if not form.validate(): self.err(**form.errors) return # Find the account to be configured account = Account.one(Q.name == form.data['name']) # Let the user know to use dash to clear existing values self.out(('* Enter dash (-) to clear the existing value', 'underline_bold_blue')) # Ask the user for the backend configuration options backend = Backend.get_backend(account.backend['backend']) config = {'backend': account.backend['backend']} for field in backend.config_form(): # Request the value self.out((field.label.text, 'blue')) value = input('({0}) > '.format(account.backend.get( field.name, ''))) value = value.strip() # Check if the value should be set to the original, cleared or used # as provided. if value: if value == '-': continue else: config[field.name] = value else: if account.backend.get(field.name): config[field.name] = account.backend.get(field.name) # Validate the users configuration result = backend.validate_config(**config) if not result[0]: self.err('Invalid backend config:', **result[1]) return # Update the accounts backend account.backend = config account.update('modified', 'backend') self.out(('Account configured', 'bold_green'))
def validate_backend(form, field): """Validate that the backend is supported""" if not Backend.get_backend(field.data): raise ValidationError('Not a supported backend.')
def get_backend_instance(self): """Return a configured instance of the backend for the account""" backendCls = Backend.get_backend(self.backend['backend']) return backendCls(**self.backend)