def test_editor_option(self, crypt_id, tmp_path, setup_and_teardown): self.my_locker = Locker.create(password=self.password, crypt_id=crypt_id) new_item = self.my_locker.create_item(item_name=self.test_item_name) new_item.content = self.start_content self.my_locker.add_item(item=new_item) conf = CliConfig() conf.store = { 'store_type': conf.store['store_type'], 'store_path': tmp_path } conf.editor = f'echo {self.edit_content}> ' write_config_file(tmp_path, update=True) load_config_file(tmp_path) # change the configured working path to the test directory update_config_option_default(self.target_cmd, self.test_path) replacement_content = "emacswhatwhat" cli_editor_option = f"echo {replacement_content}> " result = self.prep_and_run({ 'crypt_id': crypt_id, 'editor': cli_editor_option }) assert result assert result.exit_code == 0, (f"{crypt_id=}\n" f"{result.exception=}\n" f"{result.output=}\n") assert self.start_content not in result.output assert self.edit_content not in result.output inst = self.my_locker.get_item(self.test_item_name) assert replacement_content == inst.content.strip()
def anon_lockers(): conf = CliConfig() conf.store_path = getcwd() build_cli_app(command_dict=ANON_COMMAND_DICT, click_group=main, named_locker=False) main_func()
def edit_item(password: str, item: str, locker: str = None, **kwargs): """Edit the contents of an Item in a Locker""" # if any env_var options are in there, calling the config # constructor should set the relevant environment variable CliConfig(**kwargs) set_store_config(**kwargs) try: item_inst = views.get_item( password=password, locker_name=locker, item_name=item, **kwargs ) if not item_inst: raise PhibesNotFoundError except PhibesNotFoundError: raise PhibesCliNotFoundError( f"{item} does not exist in locker\n" ) content = user_edit_local_item( item_name=item, initial_content=item_inst['body'] ) return views.update_item( password=password, locker_name=locker, item_name=item, content=content, **kwargs )
def create_item( password: str, item: str, template: str = None, locker: str = None, **kwargs ): """Create an Item in a Locker""" # if any env_var options are in there, calling the config # constructor should set the relevant environment variable CliConfig(**kwargs) store_info = set_store_config(**kwargs) if template == 'Empty': template = None try: item_inst = views.get_item( password=password, locker_name=locker, item_name=item, **kwargs ) if item_inst: raise PhibesCliExistsError( f"{item} already exists in locker {store_info}\n" f"please use the `edit` command to modify\n" ) except PhibesNotFoundError: pass template_is_file = False if template: try: found = views.get_item( password=password, locker_name=locker, item_name=template, **kwargs ) content = found['body'] except PhibesNotFoundError: try: # try to find a local file by that name content = Path(template).read_text() template_is_file = True except PhibesNotFoundError: raise PhibesCliNotFoundError(f"{template} not found") except FileNotFoundError: raise PhibesCliNotFoundError(f"{template} not found") else: content = '' if not template_is_file: content = user_edit_local_item(item_name=item, initial_content=content) return views.create_item( password=password, locker_name=locker, item_name=item, content=content, **kwargs )
def custom_setup(self, tmp_path): super(TestEditBase, self).custom_setup(tmp_path) my_item = self.my_locker.create_item(self.good_template_name) my_item.content = f"{self.good_template_name}:secret" self.my_locker.add_item(my_item) CliConfig().editor = "echo 'happyclappy' >> " write_config_file(tmp_path, update=True) self.setup_command() update_config_option_default(self.target_cmd, tmp_path)
def get_editor() -> str: """ Get the user's configured editor, or raise an exception @return: editor as configured in environment """ editor = CliConfig().editor if not editor: raise PhibesCliError( "`PHIBES_EDITOR` or `EDITOR` must be set in environment") return editor
def user_edit_file(work_path: str, name: str, content: str): editor = CliConfig().editor work_file = pathlib.Path(work_path) / f"{name}.tmp" try: work_file.write_text(content) edit_cmd = f"{editor} {work_file}" subprocess.call(edit_cmd, shell=True) return work_file.read_text() except Exception as err: work_file = None raise PhibesCliError(err) finally: if work_file: work_file.unlink()
def edit_cli_config(create=True, **kwargs): """ Provide values for a Phibes CLI config file """ # There are options required for this command, # and there are options required to populate the config file # Maybe let the CliConfig class raise the errors for the latter # because e.g. if we are changing just our editor, we # shouldn't have to provide the store_path try: path = kwargs.get('path') except KeyError as err: raise PhibesCliError(f'missing required param {err}') try: new_config = CliConfig(**kwargs) new_config.validate() write_config_file(path, new_config, update=not create) except ValueError as err: raise PhibesCliError(err) except FileExistsError as err: raise PhibesCliExistsError(err) except FileNotFoundError as err: raise PhibesCliExistsError(err) return
def user_edit_local_item(item_name: str, initial_content: str = ''): config = CliConfig() work_path = config.store['store_path'] return user_edit_file(work_path=work_path, name=item_name, content=initial_content)