def raise_error(opt: int, verbose: bool = True) -> None: del verbose if opt == 1: raise errors.FunkyError(returncode=5) if opt == 2: raise TestError("Test Exception")
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
def __call__(self): Command.__call__(self) already_exists = False if self.funk in self.funk_dict: already_exists = True msg_fmt = 'Funk "{}" is already defined. Running edit command.' log.logger.info(msg_fmt.format(self.funk)) try: self.edit_funk(startinsert=(not already_exists)) log.logger.info('%s funk "%s".', 'Edited' if already_exists else 'Added', self.funk) except errors.BlankDefinition as e: raise errors.FunkyError(str(e)) self.commit()
def __call__(self) -> None: """See Command.__call__.""" Command.__call__(self) already_exists = False if self.funk in self.funk_dict: already_exists = True log.logger.info( 'Funk "%s" is already defined. Running edit command.', self.funk, ) try: self.edit_funk(startinsert=(not already_exists)) log.logger.info( '%s funk "%s".', "Edited" if already_exists else "Added", self.funk, ) except errors.BlankDefinition as e: raise errors.FunkyError(str(e)) self.commit()
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
def raise_error(opt, verbose=False): if opt == 1: raise errors.FunkyError(returncode=5) elif opt == 2: raise TestError('Test Exception')