Ejemplo n.º 1
0
class Interpreter(InterpreterBase):
    """Particularisation of the cmd command for MA5"""

    def __init__(self, main,*arg, **opt):

        # Calling constructor from InterpreterBase
        InterpreterBase.__init__(self, *arg, **opt)

        # Getting back main
        self.main = main

        # Getting back all commands
        self.cmd_set                    = CmdSet(main)
        self.cmd_define                 = CmdDefine(main)
        self.cmd_display                = CmdDisplay(main)
        self.cmd_display_datasets       = CmdDisplayDatasets(main)
        self.cmd_display_particles      = CmdDisplayParticles(main)
        self.cmd_display_multiparticles = CmdDisplayMultiparticles(main)
        self.cmd_import                 = CmdImport(main)
        self.cmd_remove                 = CmdRemove(main)
        self.cmd_swap                   = CmdSwap(main) 
        self.cmd_plot                   = CmdPlot(main)
        self.cmd_reject                 = CmdCut(main,CutType.REJECT)
        self.cmd_select                 = CmdCut(main,CutType.SELECT)
        self.cmd_reset                  = CmdReset(main)
        self.cmd_open                   = CmdOpen(main)
        self.cmd_submit                 = CmdSubmit(main)
        self.cmd_resubmit               = CmdSubmit(main,resubmit=True)
        self.cmd_install                = CmdInstall(main)
        
        # Initializing multiparticle
        self.InitializeParticle()
        self.InitializeMultiparticle()

        # Importing history
        self.history_file = os.path.normpath(self.main.ma5dir + '/.ma5history')
        try:
            readline.read_history_file(self.history_file)
        except:
            pass
                                    
    def __del__(self):
        try:
            readline.set_history_length(100)
            readline.write_history_file(self.history_file)
        except:
            pass
                

    def do_set(self,line):
        self.cmd_set.do(self.split_arg(line),line)

    def help_set(self):
        self.cmd_set.help()

    def complete_set(self,text,line,begidx,endidx):
        return self.cmd_set.complete(text,line,begidx,endidx)

    def do_define(self,line):
        self.cmd_define.do(self.split_arg(line))

    def help_define(self):
        self.cmd_define.help()

    def complete_define(self,text,line,begidx,endidx):
        return self.cmd_define.complete(text,line,begidx,endidx)

    def do_display(self,line):
        self.cmd_display.do(self.split_arg(line))

    def help_display(self):
        self.cmd_display.help()

    def complete_display(self,text,line,begidx,endidx):
        return self.cmd_display.complete(text,line,begidx,endidx)

    def do_display_particles(self,line):
        self.cmd_display_particles.do(self.split_arg(line))

    def help_display_particles(self):
        self.cmd_display_particles.help()

    def complete_display_particles(self,text,line,begidx,endidx):
        return self.cmd_display_particles.complete(text,line,begidx,endidx)

    def do_display_multiparticles(self,line):
        self.cmd_display_multiparticles.do(self.split_arg(line))

    def help_display_multiparticles(self):
        self.cmd_display_multiparticles.help()

    def complete_display_multiparticles(self,text,line,begidx,endidx):
        return self.cmd_display_multiparticles.complete(text,line,begidx,endidx)

    def do_display_datasets(self,line):
        self.cmd_display_datasets.do(self.split_arg(line))

    def help_display_datasets(self):
        self.cmd_display_datasets.help()

    def complete_display_datasets(self,text,line,begidx,endidx):
        return self.cmd_display_datasets.complete(text,line,begidx,endidx)

    def do_import(self,line):
        self.cmd_import.do(self.split_arg(line),self,self.history)

    def help_import(self):
        self.cmd_import.help()

    def complete_import(self,text,line,begidx,endidx):
        return self.cmd_import.complete(text,line,begidx,endidx)

     # Restart
    def do_restart(self, line):
        """ sending a signal allowing to restart the interpreter """
        if self.main.script:
            logging.warning("'restart' command is not allowed in script mode.")
        else:
            YES=False
            # Asking the safety question
            if not Main.forced:
                logging.warning("Are you sure to restart the MadAnalysis 5 session? (Y/N)")
                allowed_answers=['n','no','y','yes']
                answer=""
                while answer not in  allowed_answers:
                   answer=raw_input("Answer: ")
                   answer=answer.lower()
                   if answer=="no" or answer=="n":
                       YES=False
                       break
                   elif answer=='yes' or answer=='y':
                       YES=True
                       break
            if YES:
                self.main.repeatSession=True
                return True
            else:
                pass

    def help_restart(self):
        logging.info("   Syntax: restart ")
        logging.info("   Quit the current MadAnalysis sessiona and open a new one.")
        logging.info("   All the information will be discarded.")

    def do_remove(self,line):
        self.cmd_remove.do(self.split_arg(line))

    def help_remove(self):
        self.cmd_remove.help()

    def complete_remove(self,text,line,begidx,endidx):
        return self.cmd_remove.complete(text,line,begidx,endidx)

    def do_swap(self,line):
        self.cmd_swap.do(self.split_arg(line))

    def help_swap(self):
        self.cmd_swap.help()

    def complete_swap(self,text,line,begidx,endidx):
        return self.cmd_swap.complete(text,line,begidx,endidx)

    def do_install(self,line):
        result = self.cmd_install.do(self.split_arg(line))
        if result=='restart':
            logging.info(" ")
            logging.info("MadAnalysis 5 must be restarted for taking into account the present installation.")
            return self.do_restart('restart')

    def help_install(self):
        self.cmd_install.help()

    def complete_install(self,text,line,begidx,endidx):
        return self.cmd_install.complete(text,self.split_arg(line),begidx,endidx)

    def do_open(self,line):
        self.cmd_open.do(self.split_arg(line))

    def help_open(self):
        self.cmd_open.help()

    def complete_open(self,text,line,begidx,endidx):
        return self.cmd_open.complete(text,line,begidx,endidx)

    def do_reset(self,line):
        self.cmd_reset.do(self.split_arg(line),self)

    def help_reset(self):
        self.cmd_reset.help()

    def complete_reset(self,text,line,begidx,endidx):
        return self.cmd_reset.complete(text,line,begidx,endidx)

    def do_submit(self,line):
        self.cmd_submit.do(self.split_arg(line),self.history)

    def help_submit(self):
        self.cmd_submit.help()

    def complete_submit(self,text,line,begidx,endidx):
        return self.cmd_submit.complete(text,line,begidx,endidx)

    def do_resubmit(self,line):
        self.cmd_resubmit.do(self.split_arg(line),self.history)

    def help_resubmit(self):
        self.cmd_resubmit.help()

    def complete_resubmit(self,text,line,begidx,endidx):
        return self.cmd_resubmit.complete(text,line,begidx,endidx)

    def do_plot(self,line):
        self.cmd_plot.do(self.split_arg(line))

    def help_plot(self):
        self.cmd_plot.help()

    def complete_plot(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace(")"," ) ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(","," , ")
        return self.cmd_plot.complete(text,self.split_arg(tmp),begidx,endidx)

    def do_reject(self,line):
        self.cmd_reject.do(self.split_arg(line))

    def help_reject(self):
        self.cmd_reject.help()

    def complete_reject(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(")"," ) ")
        return self.cmd_reject.complete(text,self.split_arg(tmp),begidx,endidx)

    def do_select(self,line):
        self.cmd_select.do(self.split_arg(line))

    def help_select(self):
        self.cmd_select.help()

    def complete_select(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(")"," ) ")
        return self.cmd_select.complete(text,self.split_arg(tmp),begidx,endidx)

    def InitializeParticle(self):
        input = ParticleReader(self.main.ma5dir,self.cmd_define,self.main.mode)
        input.Load()

    def InitializeMultiparticle(self):
        input = MultiparticleReader(self.main.ma5dir,self.cmd_define,self.main.mode,self.main.forced)
        input.Load()

    # PreLoop
    def preloop(self):
        """Initializing before starting the main loop"""
        self.prompt = 'ma5>'
#        if readline and not 'libedit' in readline.__doc__:
#            readline.set_completion_display_matches_hook(self.print_suggestions)
            

    def deal_multiple_categories(self, dico):
        """convert the multiple category in a formatted list understand by our
        specific readline parser"""

        if 'libedit' in readline.__doc__:
            # No parser in this case, just send all the valid options
            out = []
            for name, opt in dico.items():
                out += opt
            return out

        # That's the real work
        out = []
        valid=0
        # if the key starts with number order the key with that number.
        for name, opt in dico.items():
            if not opt:
                continue
            name = name.replace(' ', '_')
            valid += 1
            out.append(opt[0].rstrip()+'@@'+name+'@@')
            # Remove duplicate
            d = {}
            for x in opt:
                d[x] = 1    
            opt = list(d.keys())
            opt.sort()
            out += opt

            
        if valid == 1:
            out = out[1:]
        return out
    
    def print_suggestions(self, substitution, matches, longest_match_length) :
        """print auto-completions by category"""
        longest_match_length += len(self.completion_prefix)
        try:
            if len(matches) == 1:
                self.stdout.write(matches[0]+' ')
                return
            self.stdout.write('\n')
            l2 = [a[-2:] for a in matches]
            if '@@' in l2:
                nb_column = self.getTerminalSize()//(longest_match_length+1)
                pos=0
                for val in self.completion_matches:
                    if val.endswith('@@'):
                        category = val.rsplit('@@',2)[1]
                        category = category.replace('_',' ')
                        self.stdout.write('\n %s:\n%s\n' % (category, '=' * (len(category)+2)))
                        start = 0
                        pos = 0
                        continue
                    elif pos and pos % nb_column ==0:
                        self.stdout.write('\n')
                    self.stdout.write(self.completion_prefix + val + \
                                      ' ' * (longest_match_length +1 -len(val)))
                    pos +=1
                self.stdout.write('\n')
            else:
                # nb column
                nb_column = self.getTerminalSize()//(longest_match_length+1)
                for i,val in enumerate(matches):
                    if i and i%nb_column ==0:
                        self.stdout.write('\n')
                    self.stdout.write(self.completion_prefix + val + \
                                     ' ' * (longest_match_length +1 -len(val)))
                self.stdout.write('\n')
    
            self.stdout.write(self.prompt+readline.get_line_buffer())
            self.stdout.flush()
        except Exception, error:
            if __debug__:
                 print error
Ejemplo n.º 2
0
class Interpreter(InterpreterBase):
    """Particularisation of the cmd command for MA5"""

    def __init__(self, main,*arg, **opt):

        # Calling constructor from InterpreterBase
        InterpreterBase.__init__(self, *arg, **opt)

        # Getting back main
        self.main = main

        # Getting back all commands
        self.cmd_set                    = CmdSet(main)
        self.cmd_define                 = CmdDefine(main)
        self.cmd_display                = CmdDisplay(main)
        self.cmd_display_datasets       = CmdDisplayDatasets(main)
        self.cmd_display_particles      = CmdDisplayParticles(main)
        self.cmd_display_multiparticles = CmdDisplayMultiparticles(main)
        self.cmd_import                 = CmdImport(main)
        self.cmd_remove                 = CmdRemove(main)
        self.cmd_swap                   = CmdSwap(main) 
        self.cmd_plot                   = CmdPlot(main)
        self.cmd_reject                 = CmdCut(main,CutType.REJECT)
        self.cmd_select                 = CmdCut(main,CutType.SELECT)
        self.cmd_preview                = CmdPreview(main)
        self.cmd_reset                  = CmdReset(main)
        self.cmd_open                   = CmdOpen(main)
        self.cmd_submit                 = CmdSubmit(main)
        self.cmd_resubmit               = CmdSubmit(main,resubmit=True)
        self.cmd_generate_latex         = CmdGenerate(main,\
                                                      ReportFormatType.LATEX) 
        self.cmd_generate_pdflatex      = CmdGenerate(main,\
                                                      ReportFormatType.PDFLATEX) 
        self.cmd_generate_html          = CmdGenerate(main,\
                                                      ReportFormatType.HTML)
        self.cmd_install                = CmdInstall(main)
        
        # Initializing multiparticle
        self.InitializeParticle()
        self.InitializeMultiparticle()

        # Importing history
        self.history_file = os.path.normpath(self.main.ma5dir + '/.ma5history')
        try:
            readline.read_history_file(self.history_file)
        except:
            pass
                                    
    def __del__(self):
        try:
            readline.set_history_length(100)
            readline.write_history_file(self.history_file)
        except:
            pass
                

    def do_set(self,line):
        self.cmd_set.do(self.split_arg(line),line)

    def help_set(self):
        self.cmd_set.help()

    def complete_set(self,text,line,begidx,endidx):
        return self.cmd_set.complete(text,line,begidx,endidx)

    def do_define(self,line):
        self.cmd_define.do(self.split_arg(line))

    def help_define(self):
        self.cmd_define.help()

    def complete_define(self,text,line,begidx,endidx):
        return self.cmd_define.complete(text,line,begidx,endidx)

    def do_display(self,line):
        self.cmd_display.do(self.split_arg(line))

    def help_display(self):
        self.cmd_display.help()

    def complete_display(self,text,line,begidx,endidx):
        return self.cmd_display.complete(text,line,begidx,endidx)

    def do_display_particles(self,line):
        self.cmd_display_particles.do(self.split_arg(line))

    def help_display_particles(self):
        self.cmd_display_particles.help()

    def complete_display_particles(self,text,line,begidx,endidx):
        return self.cmd_display_particles.complete(text,line,begidx,endidx)

    def do_display_multiparticles(self,line):
        self.cmd_display_multiparticles.do(self.split_arg(line))

    def help_display_multiparticles(self):
        self.cmd_display_multiparticles.help()

    def complete_display_multiparticles(self,text,line,begidx,endidx):
        return self.cmd_display_multiparticles.complete(text,line,begidx,endidx)

    def do_display_datasets(self,line):
        self.cmd_display_datasets.do(self.split_arg(line))

    def help_display_datasets(self):
        self.cmd_display_datasets.help()

    def complete_display_datasets(self,text,line,begidx,endidx):
        return self.cmd_display_datasets.complete(text,line,begidx,endidx)

    def do_generate_html(self,line):
        self.cmd_generate_html.do(self.split_arg(line),self.history)

    def help_generate_html(self):
        self.cmd_generate_html.help()

    def complete_generate_html(self,text,line,begidx,endidx):
        return self.cmd_generate_html.complete(text,line,begidx,endidx)

    def do_generate_latex(self,line):
        self.cmd_generate_latex.do(self.split_arg(line),self.history)

    def help_generate_latex(self):
        self.cmd_generate_latex.help()

    def complete_generate_latex(self,text,line,begidx,endidx):
        return self.cmd_generate_latex.complete(text,line,begidx,endidx)

    def do_generate_pdflatex(self,line):
        self.cmd_generate_pdflatex.do(self.split_arg(line),self.history)

    def help_generate_pdflatex(self):
        self.cmd_generate_pdflatex.help()

    def complete_generate_pdflatex(self,text,line,begidx,endidx):
        return self.cmd_generate_pdflatex.complete(text,line,begidx,endidx)

    def do_import(self,line):
        self.cmd_import.do(self.split_arg(line),self)

    def help_import(self):
        self.cmd_import.help()

    def complete_import(self,text,line,begidx,endidx):
        return self.cmd_import.complete(text,line,begidx,endidx)

    def do_remove(self,line):
        self.cmd_remove.do(self.split_arg(line))

    def help_remove(self):
        self.cmd_remove.help()

    def complete_remove(self,text,line,begidx,endidx):
        return self.cmd_remove.complete(text,line,begidx,endidx)

    def do_swap(self,line):
        self.cmd_swap.do(self.split_arg(line))

    def help_swap(self):
        self.cmd_swap.help()

    def complete_swap(self,text,line,begidx,endidx):
        return self.cmd_swap.complete(text,line,begidx,endidx)

    def do_install(self,line):
        self.cmd_install.do(self.split_arg(line))

    def help_install(self):
        self.cmd_install.help()

    def complete_install(self,text,line,begidx,endidx):
        return self.cmd_install.complete(text,self.split_arg(line),begidx,endidx)

    def do_preview(self,line):
        self.cmd_preview.do(self.split_arg(line))

    def help_preview(self):
        self.cmd_preview.help()

    def complete_preview(self,text,line,begidx,endidx):
        return self.cmd_preview.complete(text,line,begidx,endidx)

    def do_open(self,line):
        self.cmd_open.do(self.split_arg(line))

    def help_open(self):
        self.cmd_open.help()

    def complete_open(self,text,line,begidx,endidx):
        return self.cmd_open.complete(text,line,begidx,endidx)

    def do_reset(self,line):
        self.cmd_reset.do(self.split_arg(line),self)

    def help_reset(self):
        self.cmd_reset.help()

    def complete_reset(self,text,line,begidx,endidx):
        return self.cmd_reset.complete(text,line,begidx,endidx)

    def do_submit(self,line):
        self.cmd_submit.do(self.split_arg(line),self.history)

    def help_submit(self):
        self.cmd_submit.help()

    def complete_submit(self,text,line,begidx,endidx):
        return self.cmd_submit.complete(text,line,begidx,endidx)

    def do_resubmit(self,line):
        self.cmd_resubmit.do(self.split_arg(line),self.history)

    def help_resubmit(self):
        self.cmd_resubmit.help()

    def complete_resubmit(self,text,line,begidx,endidx):
        return self.cmd_resubmit.complete(text,line,begidx,endidx)

    def do_plot(self,line):
        self.cmd_plot.do(self.split_arg(line))

    def help_plot(self):
        self.cmd_plot.help()

    def complete_plot(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace(")"," ) ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(","," , ")
        return self.cmd_plot.complete(text,self.split_arg(tmp),begidx,endidx)

    def do_reject(self,line):
        self.cmd_reject.do(self.split_arg(line))

    def help_reject(self):
        self.cmd_reject.help()

    def complete_reject(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(")"," ) ")
        return self.cmd_reject.complete(text,self.split_arg(tmp),begidx,endidx)

    def do_select(self,line):
        self.cmd_select.do(self.split_arg(line))

    def help_select(self):
        self.cmd_select.help()

    def complete_select(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(")"," ) ")
        return self.cmd_select.complete(text,self.split_arg(tmp),begidx,endidx)

    def InitializeParticle(self):
        input = ParticleReader(self.main.ma5dir,self.cmd_define,self.main.mode)
        input.Load()

    def InitializeMultiparticle(self):
        input = MultiparticleReader(self.main.ma5dir,self.cmd_define,self.main.mode)
        input.Load()

    # PreLoop
    def preloop(self):
        """Initializing before starting the main loop"""
        self.prompt = 'ma5>'
#        if readline and not 'libedit' in readline.__doc__:
#            readline.set_completion_display_matches_hook(self.print_suggestions)
            

    def deal_multiple_categories(self, dico):
        """convert the multiple category in a formatted list understand by our
        specific readline parser"""

        if 'libedit' in readline.__doc__:
            # No parser in this case, just send all the valid options
            out = []
            for name, opt in dico.items():
                out += opt
            return out

        # That's the real work
        out = []
        valid=0
        # if the key starts with number order the key with that number.
        for name, opt in dico.items():
            if not opt:
                continue
            name = name.replace(' ', '_')
            valid += 1
            out.append(opt[0].rstrip()+'@@'+name+'@@')
            # Remove duplicate
            d = {}
            for x in opt:
                d[x] = 1    
            opt = list(d.keys())
            opt.sort()
            out += opt

            
        if valid == 1:
            out = out[1:]
        return out
    
    def print_suggestions(self, substitution, matches, longest_match_length) :
        """print auto-completions by category"""
        longest_match_length += len(self.completion_prefix)
        try:
            if len(matches) == 1:
                self.stdout.write(matches[0]+' ')
                return
            self.stdout.write('\n')
            l2 = [a[-2:] for a in matches]
            if '@@' in l2:
                nb_column = self.getTerminalSize()//(longest_match_length+1)
                pos=0
                for val in self.completion_matches:
                    if val.endswith('@@'):
                        category = val.rsplit('@@',2)[1]
                        category = category.replace('_',' ')
                        self.stdout.write('\n %s:\n%s\n' % (category, '=' * (len(category)+2)))
                        start = 0
                        pos = 0
                        continue
                    elif pos and pos % nb_column ==0:
                        self.stdout.write('\n')
                    self.stdout.write(self.completion_prefix + val + \
                                      ' ' * (longest_match_length +1 -len(val)))
                    pos +=1
                self.stdout.write('\n')
            else:
                # nb column
                nb_column = self.getTerminalSize()//(longest_match_length+1)
                for i,val in enumerate(matches):
                    if i and i%nb_column ==0:
                        self.stdout.write('\n')
                    self.stdout.write(self.completion_prefix + val + \
                                     ' ' * (longest_match_length +1 -len(val)))
                self.stdout.write('\n')
    
            self.stdout.write(self.prompt+readline.get_line_buffer())
            self.stdout.flush()
        except Exception, error:
            if __debug__:
                 print error
Ejemplo n.º 3
0
class Interpreter(InterpreterBase):
    """Particularisation of the cmd command for MA5"""

    def __init__(self, main,*arg, **opt):

        # Calling constructor from InterpreterBase
        InterpreterBase.__init__(self, *arg, **opt)

        # Getting back main
        self.main = main

        # Getting back all commands
        self.cmd_set                    = CmdSet(main)
        self.cmd_define                 = CmdDefine(main)
        self.cmd_define_region          = CmdDefineRegion(main)
        self.cmd_display                = CmdDisplay(main)
        self.cmd_display_datasets       = CmdDisplayDatasets(main)
        self.cmd_display_multiparticles = CmdDisplayMultiparticles(main)
        self.cmd_display_particles      = CmdDisplayParticles(main)
        self.cmd_display_regions        = CmdDisplayRegions(main)
        self.cmd_import                 = CmdImport(main)
        self.cmd_remove                 = CmdRemove(main)
        self.cmd_swap                   = CmdSwap(main) 
        self.cmd_plot                   = CmdPlot(main)
        self.cmd_reject                 = CmdCut(main,CutType.REJECT)
        self.cmd_select                 = CmdCut(main,CutType.SELECT)
        self.cmd_reset                  = CmdReset(main)
        self.cmd_open                   = CmdOpen(main)
        self.cmd_submit                 = CmdSubmit(main)
        self.cmd_resubmit               = CmdSubmit(main,resubmit=True)
        self.cmd_install                = CmdInstall(main)

        # Initializing multiparticle
        self.InitializeParticle()
        self.InitializeMultiparticle()

        # Importing history
        self.history_file = os.path.normpath(self.main.archi_info.ma5dir + '/.ma5history')
        try:
            readline.read_history_file(self.history_file)
        except:
            pass
                                    
    def __del__(self):
        try:
            readline.set_history_length(100)
            readline.write_history_file(self.history_file)
        except:
            pass
                

    def do_set(self,line):
        self.cmd_set.do(self.split_arg(line),line)

    def help_set(self):
        self.cmd_set.help()

    def complete_set(self,text,line,begidx,endidx):
        return self.cmd_set.complete(text,line,begidx,endidx)

    def do_define(self,line):
        self.cmd_define.do(self.split_arg(line))

    def do_define_region(self,line):
        self.cmd_define_region.do(self.split_arg(line))

    def help_define(self):
        self.cmd_define.help()

    def help_define_region(self):
        self.cmd_define_region.help()

    def complete_define(self,text,line,begidx,endidx):
        return self.cmd_define.complete(text,line,begidx,endidx)

    def complete_define_region(self,text,line,begidx,endidx):
        return self.cmd_define_region.complete(text,line,begidx,endidx)

    def do_display(self,line):
        self.cmd_display.do(self.split_arg(line))

    def help_display(self):
        self.cmd_display.help()

    def complete_display(self,text,line,begidx,endidx):
        return self.cmd_display.complete(text,line,begidx,endidx)

    def do_display_particles(self,line):
        self.cmd_display_particles.do(self.split_arg(line))

    def help_display_particles(self):
        self.cmd_display_particles.help()

    def complete_display_particles(self,text,line,begidx,endidx):
        return self.cmd_display_particles.complete(text,line,begidx,endidx)

    def do_display_multiparticles(self,line):
        self.cmd_display_multiparticles.do(self.split_arg(line))

    def help_display_multiparticles(self):
        self.cmd_display_multiparticles.help()

    def complete_display_multiparticles(self,text,line,begidx,endidx):
        return self.cmd_display_multiparticles.complete(text,line,begidx,endidx)

    def do_display_datasets(self,line):
        self.cmd_display_datasets.do(self.split_arg(line))

    def do_display_regions(self,line):
        self.cmd_display_regions.do(self.split_arg(line))

    def help_display_datasets(self):
        self.cmd_display_datasets.help()

    def help_display_regions(self):
        self.cmd_display_regions.help()

    def complete_display_datasets(self,text,line,begidx,endidx):
        return self.cmd_display_datasets.complete(text,line,begidx,endidx)

    def complete_display_regionss(self,text,line,begidx,endidx):
        return self.cmd_display_regions.complete(text,line,begidx,endidx)

    def do_import(self,line):
        self.cmd_import.do(self.split_arg(line),self,self.history)

    def help_import(self):
        self.cmd_import.help()

    def complete_import(self,text,line,begidx,endidx):
        return self.cmd_import.complete(text,line,begidx,endidx)

     # Restart
    def do_restart(self, line):
        """ sending a signal allowing to restart the interpreter """

        # Note: Restart is available in script mode
        # Asking the safety question
        YES=True
        if not Main.forced:
           logging.getLogger('MA5').warning("Are you sure to restart the MadAnalysis 5 session? (Y/N)")
           allowed_answers=['n','no','y','yes']
           answer=""
           while answer not in  allowed_answers:
              answer=raw_input("Answer: ")
              answer=answer.lower()
              if answer=="no" or answer=="n":
                   YES=False
                   break
              elif answer=='yes' or answer=='y':
                   YES=True
                   break

        # Restart?
        if YES:
            self.main.repeatSession=True
            return True
        else:
            return None

    def help_restart(self):
        logging.getLogger('MA5').info("   Syntax: restart ")
        logging.getLogger('MA5').info("   Quit the current MadAnalysis sessiona and open a new one.")
        logging.getLogger('MA5').info("   All the information will be discarded.")

    def do_remove(self,line):
        self.cmd_remove.do(self.split_arg(line))

    def help_remove(self):
        self.cmd_remove.help()

    def complete_remove(self,text,line,begidx,endidx):
        return self.cmd_remove.complete(text,line,begidx,endidx)

    def do_swap(self,line):
        self.cmd_swap.do(self.split_arg(line))

    def help_swap(self):
        self.cmd_swap.help()

    def complete_swap(self,text,line,begidx,endidx):
        return self.cmd_swap.complete(text,line,begidx,endidx)

    def do_install(self,line):
        result = self.cmd_install.do(self.split_arg(line))
        if result=='restart':
            logging.getLogger('MA5').info(" ")
            logging.getLogger('MA5').info("MadAnalysis 5 must be restarted for taking into account the present installation.")
            return self.do_restart('restart')

    def help_install(self):
        self.cmd_install.help()

    def complete_install(self,text,line,begidx,endidx):
        return self.cmd_install.complete(text,self.split_arg(line),begidx,endidx)

    def do_open(self,line):
        self.cmd_open.do(self.split_arg(line))

    def help_open(self):
        self.cmd_open.help()

    def complete_open(self,text,line,begidx,endidx):
        return self.cmd_open.complete(text,line,begidx,endidx)

    def do_reset(self,line):
        self.cmd_reset.do(self.split_arg(line),self)

    def help_reset(self):
        self.cmd_reset.help()

    def complete_reset(self,text,line,begidx,endidx):
        return self.cmd_reset.complete(text,line,begidx,endidx)

    def do_submit(self,line):
        self.cmd_submit.do(self.split_arg(line),self.history)

    def help_submit(self):
        self.cmd_submit.help()

    def complete_submit(self,text,line,begidx,endidx):
        return self.cmd_submit.complete(text,line,begidx,endidx)

    def do_resubmit(self,line):
        self.cmd_resubmit.do(self.split_arg(line),self.history)

    def help_resubmit(self):
        self.cmd_resubmit.help()

    def complete_resubmit(self,text,line,begidx,endidx):
        return self.cmd_resubmit.complete(text,line,begidx,endidx)

    def do_plot(self,line):
        self.cmd_plot.do(self.split_arg(line))

    def help_plot(self):
        self.cmd_plot.help()

    def complete_plot(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace(")"," ) ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(","," , ")
        return self.cmd_plot.complete(text,self.split_arg(tmp),begidx,endidx)

    def do_reject(self,line):
        self.cmd_reject.do(self.split_arg(line))

    def help_reject(self):
        self.cmd_reject.help()

    def complete_reject(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(")"," ) ")
        return self.cmd_reject.complete(text,self.split_arg(tmp),begidx,endidx)

    def do_select(self,line):
        self.cmd_select.do(self.split_arg(line))

    def help_select(self):
        self.cmd_select.help()

    def complete_select(self,text,line,begidx,endidx):
        tmp = line.replace("["," [ ")
        tmp = tmp.replace("]"," ] ")
        tmp = tmp.replace("("," ( ")
        tmp = tmp.replace(")"," ) ")
        return self.cmd_select.complete(text,self.split_arg(tmp),begidx,endidx)

    def InitializeParticle(self):
        input = ParticleReader(self.main.archi_info.ma5dir,self.cmd_define,self.main.mode)
        input.Load()

    def InitializeMultiparticle(self):
        input = MultiparticleReader(self.main.archi_info.ma5dir,self.cmd_define,self.main.mode,self.main.forced)
        input.Load()

    # PreLoop
    def preloop(self):
        """Initializing before starting the main loop"""
        self.prompt = 'ma5>'
#        if readline and not 'libedit' in readline.__doc__:
#            readline.set_completion_display_matches_hook(self.print_suggestions)
            

    def deal_multiple_categories(self, dico):
        """convert the multiple category in a formatted list understand by our
        specific readline parser"""

        if 'libedit' in readline.__doc__:
            # No parser in this case, just send all the valid options
            out = []
            for name, opt in dico.items():
                out += opt
            return out

        # That's the real work
        out = []
        valid=0
        # if the key starts with number order the key with that number.
        for name, opt in dico.items():
            if not opt:
                continue
            name = name.replace(' ', '_')
            valid += 1
            out.append(opt[0].rstrip()+'@@'+name+'@@')
            # Remove duplicate
            d = {}
            for x in opt:
                d[x] = 1    
            opt = list(d.keys())
            opt.sort()
            out += opt

            
        if valid == 1:
            out = out[1:]
        return out
    
    def print_suggestions(self, substitution, matches, longest_match_length) :
        """print auto-completions by category"""
        longest_match_length += len(self.completion_prefix)
        try:
            if len(matches) == 1:
                self.stdout.write(matches[0]+' ')
                return
            self.stdout.write('\n')
            l2 = [a[-2:] for a in matches]
            if '@@' in l2:
                nb_column = self.getTerminalSize()//(longest_match_length+1)
                pos=0
                for val in self.completion_matches:
                    if val.endswith('@@'):
                        category = val.rsplit('@@',2)[1]
                        category = category.replace('_',' ')
                        self.stdout.write('\n %s:\n%s\n' % (category, '=' * (len(category)+2)))
                        start = 0
                        pos = 0
                        continue
                    elif pos and pos % nb_column ==0:
                        self.stdout.write('\n')
                    self.stdout.write(self.completion_prefix + val + \
                                      ' ' * (longest_match_length +1 -len(val)))
                    pos +=1
                self.stdout.write('\n')
            else:
                # nb column
                nb_column = self.getTerminalSize()//(longest_match_length+1)
                for i,val in enumerate(matches):
                    if i and i%nb_column ==0:
                        self.stdout.write('\n')
                    self.stdout.write(self.completion_prefix + val + \
                                     ' ' * (longest_match_length +1 -len(val)))
                self.stdout.write('\n')
    
            self.stdout.write(self.prompt+readline.get_line_buffer())
            self.stdout.flush()
        except Exception, error:
            if __debug__:
                 print error
Ejemplo n.º 4
0
class Interpreter(InterpreterBase):
    """Particularisation of the cmd command for MA5"""
    def __init__(self, main, *arg, **opt):

        # Calling constructor from InterpreterBase
        InterpreterBase.__init__(self, *arg, **opt)

        # Getting back main
        self.main = main

        # Getting back all commands
        self.cmd_set = CmdSet(main)
        self.cmd_define = CmdDefine(main)
        self.cmd_display = CmdDisplay(main)
        self.cmd_display_datasets = CmdDisplayDatasets(main)
        self.cmd_display_particles = CmdDisplayParticles(main)
        self.cmd_display_multiparticles = CmdDisplayMultiparticles(main)
        self.cmd_import = CmdImport(main)
        self.cmd_remove = CmdRemove(main)
        self.cmd_swap = CmdSwap(main)
        self.cmd_plot = CmdPlot(main)
        self.cmd_reject = CmdCut(main, CutType.REJECT)
        self.cmd_select = CmdCut(main, CutType.SELECT)
        self.cmd_preview = CmdPreview(main)
        self.cmd_reset = CmdReset(main)
        self.cmd_open = CmdOpen(main)
        self.cmd_submit = CmdSubmit(main)
        self.cmd_resubmit = CmdSubmit(main, resubmit=True)
        self.cmd_generate_latex         = CmdGenerate(main,\
                                                      ReportFormatType.LATEX)
        self.cmd_generate_pdflatex      = CmdGenerate(main,\
                                                      ReportFormatType.PDFLATEX)
        self.cmd_generate_html          = CmdGenerate(main,\
                                                      ReportFormatType.HTML)
        self.cmd_install = CmdInstall(main)

        # Initializing multiparticle
        self.InitializeParticle()
        self.InitializeMultiparticle()

        # Importing history
        self.history_file = os.path.normpath(self.main.ma5dir + '/.ma5history')
        try:
            readline.read_history_file(self.history_file)
        except:
            pass

    def __del__(self):
        try:
            readline.set_history_length(100)
            readline.write_history_file(self.history_file)
        except:
            pass

    def do_set(self, line):
        self.cmd_set.do(self.split_arg(line), line)

    def help_set(self):
        self.cmd_set.help()

    def complete_set(self, text, line, begidx, endidx):
        return self.cmd_set.complete(text, line, begidx, endidx)

    def do_define(self, line):
        self.cmd_define.do(self.split_arg(line))

    def help_define(self):
        self.cmd_define.help()

    def complete_define(self, text, line, begidx, endidx):
        return self.cmd_define.complete(text, line, begidx, endidx)

    def do_display(self, line):
        self.cmd_display.do(self.split_arg(line))

    def help_display(self):
        self.cmd_display.help()

    def complete_display(self, text, line, begidx, endidx):
        return self.cmd_display.complete(text, line, begidx, endidx)

    def do_display_particles(self, line):
        self.cmd_display_particles.do(self.split_arg(line))

    def help_display_particles(self):
        self.cmd_display_particles.help()

    def complete_display_particles(self, text, line, begidx, endidx):
        return self.cmd_display_particles.complete(text, line, begidx, endidx)

    def do_display_multiparticles(self, line):
        self.cmd_display_multiparticles.do(self.split_arg(line))

    def help_display_multiparticles(self):
        self.cmd_display_multiparticles.help()

    def complete_display_multiparticles(self, text, line, begidx, endidx):
        return self.cmd_display_multiparticles.complete(
            text, line, begidx, endidx)

    def do_display_datasets(self, line):
        self.cmd_display_datasets.do(self.split_arg(line))

    def help_display_datasets(self):
        self.cmd_display_datasets.help()

    def complete_display_datasets(self, text, line, begidx, endidx):
        return self.cmd_display_datasets.complete(text, line, begidx, endidx)

    def do_generate_html(self, line):
        self.cmd_generate_html.do(self.split_arg(line), self.history)

    def help_generate_html(self):
        self.cmd_generate_html.help()

    def complete_generate_html(self, text, line, begidx, endidx):
        return self.cmd_generate_html.complete(text, line, begidx, endidx)

    def do_generate_latex(self, line):
        self.cmd_generate_latex.do(self.split_arg(line), self.history)

    def help_generate_latex(self):
        self.cmd_generate_latex.help()

    def complete_generate_latex(self, text, line, begidx, endidx):
        return self.cmd_generate_latex.complete(text, line, begidx, endidx)

    def do_generate_pdflatex(self, line):
        self.cmd_generate_pdflatex.do(self.split_arg(line), self.history)

    def help_generate_pdflatex(self):
        self.cmd_generate_pdflatex.help()

    def complete_generate_pdflatex(self, text, line, begidx, endidx):
        return self.cmd_generate_pdflatex.complete(text, line, begidx, endidx)

    def do_import(self, line):
        self.cmd_import.do(self.split_arg(line), self)

    def help_import(self):
        self.cmd_import.help()

    def complete_import(self, text, line, begidx, endidx):
        return self.cmd_import.complete(text, line, begidx, endidx)

    def do_remove(self, line):
        self.cmd_remove.do(self.split_arg(line))

    def help_remove(self):
        self.cmd_remove.help()

    def complete_remove(self, text, line, begidx, endidx):
        return self.cmd_remove.complete(text, line, begidx, endidx)

    def do_swap(self, line):
        self.cmd_swap.do(self.split_arg(line))

    def help_swap(self):
        self.cmd_swap.help()

    def complete_swap(self, text, line, begidx, endidx):
        return self.cmd_swap.complete(text, line, begidx, endidx)

    def do_install(self, line):
        self.cmd_install.do(self.split_arg(line))

    def help_install(self):
        self.cmd_install.help()

    def complete_install(self, text, line, begidx, endidx):
        return self.cmd_install.complete(text, self.split_arg(line), begidx,
                                         endidx)

    def do_preview(self, line):
        self.cmd_preview.do(self.split_arg(line))

    def help_preview(self):
        self.cmd_preview.help()

    def complete_preview(self, text, line, begidx, endidx):
        return self.cmd_preview.complete(text, line, begidx, endidx)

    def do_open(self, line):
        self.cmd_open.do(self.split_arg(line))

    def help_open(self):
        self.cmd_open.help()

    def complete_open(self, text, line, begidx, endidx):
        return self.cmd_open.complete(text, line, begidx, endidx)

    def do_reset(self, line):
        self.cmd_reset.do(self.split_arg(line), self)

    def help_reset(self):
        self.cmd_reset.help()

    def complete_reset(self, text, line, begidx, endidx):
        return self.cmd_reset.complete(text, line, begidx, endidx)

    def do_submit(self, line):
        self.cmd_submit.do(self.split_arg(line), self.history)

    def help_submit(self):
        self.cmd_submit.help()

    def complete_submit(self, text, line, begidx, endidx):
        return self.cmd_submit.complete(text, line, begidx, endidx)

    def do_resubmit(self, line):
        self.cmd_resubmit.do(self.split_arg(line), self.history)

    def help_resubmit(self):
        self.cmd_resubmit.help()

    def complete_resubmit(self, text, line, begidx, endidx):
        return self.cmd_resubmit.complete(text, line, begidx, endidx)

    def do_plot(self, line):
        self.cmd_plot.do(self.split_arg(line))

    def help_plot(self):
        self.cmd_plot.help()

    def complete_plot(self, text, line, begidx, endidx):
        tmp = line.replace("[", " [ ")
        tmp = tmp.replace("]", " ] ")
        tmp = tmp.replace(")", " ) ")
        tmp = tmp.replace("(", " ( ")
        tmp = tmp.replace(",", " , ")
        return self.cmd_plot.complete(text, self.split_arg(tmp), begidx,
                                      endidx)

    def do_reject(self, line):
        self.cmd_reject.do(self.split_arg(line))

    def help_reject(self):
        self.cmd_reject.help()

    def complete_reject(self, text, line, begidx, endidx):
        tmp = line.replace("[", " [ ")
        tmp = tmp.replace("]", " ] ")
        tmp = tmp.replace("(", " ( ")
        tmp = tmp.replace(")", " ) ")
        return self.cmd_reject.complete(text, self.split_arg(tmp), begidx,
                                        endidx)

    def do_select(self, line):
        self.cmd_select.do(self.split_arg(line))

    def help_select(self):
        self.cmd_select.help()

    def complete_select(self, text, line, begidx, endidx):
        tmp = line.replace("[", " [ ")
        tmp = tmp.replace("]", " ] ")
        tmp = tmp.replace("(", " ( ")
        tmp = tmp.replace(")", " ) ")
        return self.cmd_select.complete(text, self.split_arg(tmp), begidx,
                                        endidx)

    def InitializeParticle(self):
        input = ParticleReader(self.main.ma5dir, self.cmd_define,
                               self.main.mode)
        input.Load()

    def InitializeMultiparticle(self):
        input = MultiparticleReader(self.main.ma5dir, self.cmd_define,
                                    self.main.mode)
        input.Load()

    # PreLoop
    def preloop(self):
        """Initializing before starting the main loop"""
        self.prompt = 'ma5>'
#        if readline and not 'libedit' in readline.__doc__:
#            readline.set_completion_display_matches_hook(self.print_suggestions)

    def deal_multiple_categories(self, dico):
        """convert the multiple category in a formatted list understand by our
        specific readline parser"""

        if 'libedit' in readline.__doc__:
            # No parser in this case, just send all the valid options
            out = []
            for name, opt in dico.items():
                out += opt
            return out

        # That's the real work
        out = []
        valid = 0
        # if the key starts with number order the key with that number.
        for name, opt in dico.items():
            if not opt:
                continue
            name = name.replace(' ', '_')
            valid += 1
            out.append(opt[0].rstrip() + '@@' + name + '@@')
            # Remove duplicate
            d = {}
            for x in opt:
                d[x] = 1
            opt = list(d.keys())
            opt.sort()
            out += opt

        if valid == 1:
            out = out[1:]
        return out

    def print_suggestions(self, substitution, matches, longest_match_length):
        """print auto-completions by category"""
        longest_match_length += len(self.completion_prefix)
        try:
            if len(matches) == 1:
                self.stdout.write(matches[0] + ' ')
                return
            self.stdout.write('\n')
            l2 = [a[-2:] for a in matches]
            if '@@' in l2:
                nb_column = self.getTerminalSize() // (longest_match_length +
                                                       1)
                pos = 0
                for val in self.completion_matches:
                    if val.endswith('@@'):
                        category = val.rsplit('@@', 2)[1]
                        category = category.replace('_', ' ')
                        self.stdout.write('\n %s:\n%s\n' %
                                          (category, '=' *
                                           (len(category) + 2)))
                        start = 0
                        pos = 0
                        continue
                    elif pos and pos % nb_column == 0:
                        self.stdout.write('\n')
                    self.stdout.write(self.completion_prefix + val + \
                                      ' ' * (longest_match_length +1 -len(val)))
                    pos += 1
                self.stdout.write('\n')
            else:
                # nb column
                nb_column = self.getTerminalSize() // (longest_match_length +
                                                       1)
                for i, val in enumerate(matches):
                    if i and i % nb_column == 0:
                        self.stdout.write('\n')
                    self.stdout.write(self.completion_prefix + val + \
                                     ' ' * (longest_match_length +1 -len(val)))
                self.stdout.write('\n')

            self.stdout.write(self.prompt + readline.get_line_buffer())
            self.stdout.flush()
        except Exception, error:
            if __debug__:
                print error