Example #1
0
    def edit_funk(self, funk=None):
        """Opens up funk definition using temp file in $EDITOR for editing.

        Args:
            funk (optional): The funk to edit. If not given, this function uses the funk defined
                at instance creation time.

        Returns (str):
            Contents of temp file after $EDITOR closes.
        """
        if funk is None:
            funk = self.funk

        tf = tempfile.NamedTemporaryFile(prefix='{}.'.format(funk),
                                         suffix='.sh',
                                         dir='/var/tmp',
                                         mode='w',
                                         delete=False)
        if funk in self.funk_dict:
            tf.write(self.funk_dict[funk])
        tf.close()

        if 'EDITOR' in os.environ:
            editor_cmd_list = shlex.split(os.environ['EDITOR'])
            log.logger.debug(
                'Editor command set to $EDITOR: {}'.format(editor_cmd_list))
        else:
            editor_cmd_list = ['vim']
            log.logger.debug(
                'Editor command falling back to default: {}'.format(
                    editor_cmd_list))

        editor_cmd_list.append(tf.name)
        try:
            sp.check_call(editor_cmd_list)
        except sp.CalledProcessError:
            raise errors.FunkyError(
                'Failed to open editor using: {}'.format(editor_cmd_list))

        tf = open(tf.name, 'r')
        edited_cmd_string = tf.read()
        tf.close()
        os.unlink(tf.name)

        if edited_cmd_string.strip() == '':
            raise errors.BlankDefinition('Funk definition cannot be blank.')

        log.logger.debug('New Command String: "%s"', edited_cmd_string)
        formatted_cmd_string = self._format_cmd_string(
            edited_cmd_string.strip())
        self.funk_dict[funk] = formatted_cmd_string
Example #2
0
    def edit_funk(self, startinsert: bool = False) -> None:
        """Opens up funk definition using temp file in $EDITOR for editing.

        Args:
            startinsert: If vim is your editor, should we start in insert mode?
        """
        tf = tempfile.NamedTemporaryFile(
            prefix="{}.".format(self.funk),
            suffix=".sh",
            dir="/var/tmp",
            mode="w",
            delete=False,
        )
        if self.funk in self.funk_dict:
            tf.write(self.funk_dict[self.funk])
        tf.close()

        editor_cmd_list = self._editor_cmd_list(startinsert)

        editor_cmd_list.append(tf.name)
        try:
            sp.check_call(editor_cmd_list)
        except sp.CalledProcessError as e:
            raise errors.FunkyError("Failed to open editor using: {}".format(
                editor_cmd_list)) from e

        tf = open(tf.name, "r")
        edited_cmd_string = tf.read()
        tf.close()
        os.unlink(tf.name)

        if edited_cmd_string.strip() == "":
            raise errors.BlankDefinition("Funk definition cannot be blank.")

        log.logger.debug('New Command String: "%s"', edited_cmd_string)
        formatted_cmd_string = self._apply_shortcuts(edited_cmd_string.strip())

        assert self.funk is not None, self.NONE_FUNK_ERROR
        self.funk_dict[self.funk] = formatted_cmd_string