[docs]classHelperTabComplete(ComputationStep):""" Adds required command to `.bashrc` Linux or .bash_profile for mac so that Command autocomplete works for oasislmf CLI """
[docs]step_params=[{'name':'bash_rc_file','flag':'-p','help':'Path to bash configuration RC file, "~/.bashrc". '},{'name':'no_confirm','flag':'-y','action':'store_true','default':False,'help':'Skip the confirmation prompt'},]
[docs]defconfirm_action(self,question_str,no_confirm=False):self.logger.debug('Prompt user for confirmation')ifno_confirm:returnTruetry:check=str(input("%s (Y/N): "%question_str)).lower().strip()ifcheck[:1]=='y':returnTrueelifcheck[:1]=='n':returnFalseelse:self.logger.error('Enter "y" for Yes, "n" for No or Ctrl-C to exit.\n')returnself.confirm_action(question_str)exceptKeyboardInterrupt:self.logger.error('\nexiting.')
[docs]definstall_autocomplete(self,target_file=None):msg_success='Auto-Complete installed.'msg_failed='install failed'msg_installed='Auto-Complete feature is already enabled.'msg_reload_bash='\n To activate reload bash by running: \n source {}'.format(target_file)cmd_header='# Added by OasisLMF\n'cmd_autocomplete='complete -C completer_oasislmf oasislmf\n'try:ifos.path.isfile(target_file):# Check command is in filewithopen(target_file,"r")asrc:ifcmd_autocompleteinrc.read():self.logger.info(msg_installed)self.logger.info(msg_reload_bash)sys.exit(0)else:# create new file at set locationbasedir=os.path.dirname(target_file)ifnotos.path.isdir(basedir):os.makedirs(basedir)# Add complete commandwithopen(target_file,"a")asrc:rc.write(cmd_header)rc.write(cmd_autocomplete)self.logger.info(msg_success)self.logger.info(msg_reload_bash)exceptExceptionase:self.logger.error('{}: {}'.format(msg_failed,e))
[docs]defrun(self):# select default bashrc if not setifnotself.bash_rc_file:default_file='.bash_profile'ifsystem=='Darwin'else'.bashrc'self.bash_rc_file=os.path.join(os.path.expanduser('~'),default_file)# Prompt user, and then installmsg_user='Running this will append a command to the following file:\n'ifself.confirm_action("{}{}".format(msg_user,self.bash_rc_file),self.no_confirm):self.install_autocomplete(self.bash_rc_file)