Ejemplo n.º 1
0
 def spin(self):
     if self.drawn == True:
         self.clear()
     else:
         self.drawn = True
     stdout.write(self.spinners[self.state])
     self.state = (self.state + 1) % len(self.spinners)
Ejemplo n.º 2
0
def input_object(prompt_text,
                 cast=None,
                 default=None,
                 prompt_ext=': ',
                 castarg=[],
                 castkwarg={}):
    """Gets input from the command line and validates it.
    
    prompt_text
        A string. Used to prompt the user. Do not include a trailing
        space.
        
    prompt_ext
        Added on to the prompt at the end. At the moment this must not
        include any control stuff because it is send directly to
        raw_input
        
    cast
        This can be any callable object (class, function, type, etc). It
        simply calls the cast with the given arguements and returns the 
        result. If a ValueError is raised, it
        will output an error message and prompt the user again.

        Because some builtin python objects don't do casting in the way
        that we might like you can easily write a wrapper function that
        looks and the input and returns the appropriate object or exception.
        Look in the cast submodule for examples.
        
        If cast is None, then it will do nothing (and you will have a string)
        
    default
        function returns this value if the user types nothing in. This is
        can be used to cancel the input so-to-speek
        
    castarg, castkwarg
        list and dictionary. Extra arguments passed on to the cast.
    """
    while True:
        stdout.write(prompt_text)
        value = stdout.raw_input(prompt_ext)
        if value == '': return default
        try:
            if cast != None: value = cast(value, *castarg, **castkwarg)
        except ValueError as details:
            if cast in NICE_INPUT_ERRORS:  # see comment above this constant
                stderr.write(ERROR_MESSAGE %
                             (NICE_INPUT_ERRORS[cast] % details))
            else:
                stderr.write(ERROR_MESSAGE %
                             (DEFAULT_INPUT_ERRORS % str(details)))
            continue
        return value
Ejemplo n.º 3
0
def input_object(prompt_text, cast = None, default = None,
                 prompt_ext = ': ', castarg = [], castkwarg = {}):
    """Gets input from the command line and validates it.
    
    prompt_text
        A string. Used to prompt the user. Do not include a trailing
        space.
        
    prompt_ext
        Added on to the prompt at the end. At the moment this must not
        include any control stuff because it is send directly to
        raw_input
        
    cast
        This can be any callable object (class, function, type, etc). It
        simply calls the cast with the given arguements and returns the 
        result. If a ValueError is raised, it
        will output an error message and prompt the user again.

        Because some builtin python objects don't do casting in the way
        that we might like you can easily write a wrapper function that
        looks and the input and returns the appropriate object or exception.
        Look in the cast submodule for examples.
        
        If cast is None, then it will do nothing (and you will have a string)
        
    default
        function returns this value if the user types nothing in. This is
        can be used to cancel the input so-to-speek
        
    castarg, castkwarg
        list and dictionary. Extra arguments passed on to the cast.
    """
    while True:
        stdout.write(prompt_text)
        value = stdout.raw_input(prompt_ext)
        if value == '': return default
        try:
            if cast != None: value = cast(value, *castarg, **castkwarg)
        except ValueError, details:
            if cast in NICE_INPUT_ERRORS: # see comment above this constant
                stderr.write(ERROR_MESSAGE % (NICE_INPUT_ERRORS[cast] % details))
            else: stderr.write(ERROR_MESSAGE % (DEFAULT_INPUT_ERRORS % str(details)))
            continue
        return value
Ejemplo n.º 4
0
 def update(self, percent, message = None, test = False):
     """
     """
     if self.refresh:
         self.clear()
     if self.drawn:
         stdout.move('beginning of line')
         stdout.move('up', len(self.message) + self.barlines)
     else:
         title = self.get_title()
         if title != None:
             for line in self.get_title():
                 stdout.write(line + os.linesep)
         self.drawn = True
     bar = self.get_bar(percent)
     refresh =  (len(bar) != self.barlines)
     self.barlines = len(bar)
     for line in bar:
         stdout.clear('line')
         stdout.write(line)
         stdout.move('down')
         stdout.move('beginning of line')
     if (message != self.get_message()) or refresh:
         stdout.clear('end of screen')
         self.set_message(message)
         for line in self.message:
             stdout.write(line)
             stdout.move('down')
     else: stdout.move('down', len(self.message))
Ejemplo n.º 5
0
 def update(self, percent, message=None, test=False):
     """
     """
     if self.refresh:
         self.clear()
     if self.drawn:
         stdout.move('beginning of line')
         stdout.move('up', len(self.message) + self.barlines)
     else:
         title = self.get_title()
         if title != None:
             for line in self.get_title():
                 stdout.write(line + os.linesep)
         self.drawn = True
     bar = self.get_bar(percent)
     refresh = (len(bar) != self.barlines)
     self.barlines = len(bar)
     for line in bar:
         stdout.clear('line')
         stdout.write(line)
         stdout.move('down')
         stdout.move('beginning of line')
     if (message != self.get_message()) or refresh:
         stdout.clear('end of screen')
         self.set_message(message)
         for line in self.message:
             stdout.write(line)
             stdout.move('down')
     else:
         stdout.move('down', len(self.message))
Ejemplo n.º 6
0
 def spin(self):
     if self.drawn == True: self.clear()
     else: self.drawn = True
     stdout.write(self.spinners[self.state])
     self.state = (self.state + 1) % len(self.spinners)