예제 #1
0
 def display_table(headers: List[str], entities: List[CrudEntity],
                   title: str) -> None:
     MenuUtils.title('LISTING ALL {}'.format(title))
     tr = TableRenderer(headers, [c.to_values() for c in entities], title)
     tr.adjust_sizes_by_largest_cell()
     tr.render()
     MenuUtils.wait_enter()
예제 #2
0
    def execute(self, *args) -> ExitCode:
        # Include the widget's main code
        sysout('')
        sysout('My widget is running')
        sysout('')

        MenuUtils.wait_enter()

        return ExitCode.SUCCESS
예제 #3
0
    def _loop_actions(self) -> None:
        """Wait for the user interactions"""
        while not self.done:
            if self.org and self.space and not self.cf.is_targeted():
                self._target()
            else:
                self._set_org()
                self._set_space()

            if not self.org or not self.space or not self.cf.is_targeted():
                raise CFExecutionError(
                    f"Unable to target ORG={self.org}  SPACE={self.space} => {self.cf.last_result}"
                )

            action = mselect(CFManager.CF_ACTIONS,
                             'Please select an action to perform')

            if not action:
                self.done = True
            else:
                if self._is_callable(action):
                    if self._allow_multiple(action.lower()):
                        apps = self._choose_apps()
                    else:
                        app = self._select_app()
                        apps = [app] if app else None
                    if apps:
                        for app in apps:
                            self._perform(action,
                                          app=app.name,
                                          org=self.org,
                                          space=self.space)
                else:
                    if action.lower() == 'status':
                        apps = self._get_apps(refresh=True)
                        if len(apps) > 0:
                            sysout("{}  {}  {}  {}  {}  {}".format(
                                'Name'.ljust(CFApplication.max_name_length),
                                'State'.ljust(7),
                                'Inst'.ljust(5),
                                'Mem'.ljust(4),
                                'Disk'.ljust(4),
                                'URLs',
                            ))
                            for app in apps:
                                app.print_status()
                    elif action.lower() == 'target':
                        self.space = None
                        self.org = None
                        self.cf.targeted = {
                            'org': None,
                            'space': None,
                            'targeted': False
                        }
                        continue

                    MenuUtils.wait_enter()
예제 #4
0
 def display_contacts(persons, companies) -> None:
     if len(persons) > 0 or len(companies) > 0:
         SearchView.display_table(
             ["UUID", "NAME", "PHONE", "ADDRESS", "CPL", "AGE", "EMAIL"],
             persons, 'PERSONS')
         SearchView.display_table(
             ["UUID", "NAME", "PHONE", "ADDRESS", "CPL", "WEBSITE"],
             companies, 'COMPANIES')
     else:
         sysout('-=- No results to be displayed -=-')
         MenuUtils.wait_enter()
예제 #5
0
 def by_uuid(self) -> None:
     MenuUtils.title('SEARCH BY UUID')
     try:
         name = MenuUtils.prompt('Person or Company uuid')
         all_persons = self.person_service.list(
             filters='uuid={}'.format(name))
         all_companies = self.company_service.list(
             filters='uuid={}'.format(name))
         self.display_contacts(all_persons, all_companies)
     except InputAbortedError:
         pass
예제 #6
0
 def close(self) -> None:
     """Close the Vault file and cleanup temporary file_paths"""
     try:
         if self.is_open:
             self._lock_vault()
             log.debug("Vault closed and locked")
     except UnicodeDecodeError as err:
         log.error("Authentication failure => {}".format(str(err)))
         MenuUtils.print_error('Authentication failure')
     except Exception as err:
         raise VaultCloseError(
             f"Unable to close Vault file => {self.configs.vault_file()}",
             err) from err
예제 #7
0
 def open(self) -> None:
     """Open and read the Vault file"""
     self.passphrase = self._read_passphrase()
     try:
         if not self.is_open:
             self._unlock_vault()
             log.debug("Vault open and unlocked")
     except UnicodeDecodeError as err:
         log.error("Authentication failure => {}".format(str(err)))
         MenuUtils.print_error('Authentication failure')
     except Exception as err:
         raise VaultOpenError(
             f"Unable to open Vault file => {self.configs.vault_file()}",
             err) from err
예제 #8
0
    def execute(self) -> Optional[Menu]:
        """TODO"""

        while self.selected != 0 and not self.done:
            sysout(str(self))
            try:
                self.selected = MenuUtils.prompt(end='$ ')
                if not self.selected:
                    continue
                if self.selected.isalnum() and self.is_valid_option():
                    return self.trigger_menu_item()
                MenuUtils.print_error("Invalid option", self.selected)
                self.selected = None
            except InputAbortedError:
                continue
예제 #9
0
    def execute(self, *args) -> ExitCode:
        ret_val = ExitCode.SUCCESS
        signal.signal(signal.SIGINT, self.cleanup)
        signal.signal(signal.SIGTERM, self.cleanup)

        if args and args[0] in ['-h', '--help']:
            sysout(self.usage())
            return ExitCode.SUCCESS
        elif args and args[0] in ['-v', '--version']:
            sysout(self.version())
            return ExitCode.SUCCESS

        if not args:
            if not args and not self._read_args():
                return ExitCode.ERROR
        else:
            if not self._parse_args(*args):
                return ExitCode.ERROR

        self.net_type = self.args.net_type or self.NET_TYPE_TCP
        self.host = (self.args.address or '127.0.0.1', self.args.port or 12345)
        self.packets = self.args.packets or 100
        self.interval = self.args.interval or 1
        self.threads = self.args.threads or 1

        if self.args.message and os.path.isfile(self.args.message):
            file_size = os.stat(self.args.message).st_size
            sysout(
                f"Reading contents from file: {self.args.message} ({file_size}) [Bs] instead"
            )
            with open(self.args.message, 'r') as f_msg:
                self.message = f_msg.read()
        else:
            self.message = self.args.message or f"This is a {self.args.net_type} test"

        self._start_send()

        MenuUtils.wait_enter()

        return ret_val
예제 #10
0
    def company(self) -> None:
        MenuUtils.title('CREATE COMPANY')
        company = Company()
        try:
            company.name = MenuUtils.prompt('Name',
                                            ContactValidator.validate_name)
            company.phone = MenuUtils.prompt('Phone',
                                             ContactValidator.validate_phone)
            company.website = MenuUtils.prompt(
                'WebSite', CompanyValidator.validate_website)
            company.address = MenuUtils.prompt(
                'Address',
                on_blank_abort=False,
                validator=ContactValidator.validate_address)
            company.complement = MenuUtils.prompt(
                'Complement',
                on_blank_abort=False,
                validator=ContactValidator.validate_complement)
            self.company_service.save(company)
        except InputAbortedError:
            MenuUtils.wait_enter('Operation aborted. Press [Enter]...')

        MenuUtils.wait_enter()
예제 #11
0
    def person(self) -> None:
        MenuUtils.title('CREATE PERSON')
        person = Person()
        try:
            person.name = MenuUtils.prompt('Name',
                                           ContactValidator.validate_name)
            person.age = MenuUtils.prompt('Age', PersonValidator.validate_age)
            person.phone = MenuUtils.prompt('Phone',
                                            ContactValidator.validate_phone)
            person.email = MenuUtils.prompt('Email',
                                            PersonValidator.validate_email)
            person.address = MenuUtils.prompt(
                'Address',
                on_blank_abort=False,
                validator=ContactValidator.validate_address)
            person.complement = MenuUtils.prompt(
                'Complement',
                on_blank_abort=False,
                validator=ContactValidator.validate_complement)
            self.person_service.save(person)
        except InputAbortedError:
            MenuUtils.wait_enter('Operation aborted. Press [Enter]...')

        MenuUtils.wait_enter()
예제 #12
0
    def company(self) -> None:
        MenuUtils.title('EDIT COMPANY')
        uuid = MenuUtils.prompt('Enter uuid')
        found = self.company_service.get(uuid)
        if not found:
            MenuUtils.print_error("Company does not exist", uuid)
        else:
            try:
                found.name = MenuUtils.prompt('Name',
                                              ContactValidator.validate_name,
                                              found.name)
                found.phone = MenuUtils.prompt('Phone',
                                               ContactValidator.validate_phone,
                                               found.phone)
                found.website = MenuUtils.prompt(
                    'WebSite', CompanyValidator.validate_website,
                    found.website)
                found.address = MenuUtils.prompt(
                    'Address',
                    on_blank_abort=False,
                    validator=ContactValidator.validate_address)
                found.complement = MenuUtils.prompt(
                    'Complement',
                    on_blank_abort=False,
                    validator=ContactValidator.validate_complement)
                self.company_service.save(found)
            except InputAbortedError:
                MenuUtils.wait_enter('Operation aborted. Press [Enter]...')

            MenuUtils.wait_enter()
예제 #13
0
    def person(self) -> None:
        MenuUtils.title('EDIT PERSON')
        uuid = MenuUtils.prompt('Enter uuid')
        found = self.person_service.get(uuid)
        if not found:
            MenuUtils.print_error("Person does not exist", uuid)
        else:
            try:
                found.name = MenuUtils.prompt('Name',
                                              ContactValidator.validate_name,
                                              found.name)
                found.age = MenuUtils.prompt('Age',
                                             PersonValidator.validate_age,
                                             found.age)
                found.phone = MenuUtils.prompt('Phone',
                                               ContactValidator.validate_phone,
                                               found.phone)
                found.email = MenuUtils.prompt('Email',
                                               PersonValidator.validate_email,
                                               found.email)
                found.address = MenuUtils.prompt(
                    'Address',
                    on_blank_abort=False,
                    validator=ContactValidator.validate_address)
                found.complement = MenuUtils.prompt(
                    'Complement',
                    on_blank_abort=False,
                    validator=ContactValidator.validate_complement)
                self.person_service.save(found)
            except InputAbortedError:
                MenuUtils.wait_enter('Operation aborted. Press [Enter]...')

            MenuUtils.wait_enter()