Exemplo n.º 1
0
def file_chooser(prompt_text = "Enter File: ", default=None, filearg=[], filekwarg={}):
    """A simple tool to get a file from the user. Takes keyworded arguemnts
    and passes them to open().
    
    If the user enters nothing the function will return the ``default`` value.
    Otherwise it continues to prompt the user until it get's a decent response.
    
    filekwarg may contain arguements passed on to ``open()``.
    """
    try:
        import readline, rlcomplete
        completer = rlcomplete.PathCompleter()
        readline.set_completer_delims(completer.delims)
        readline.parse_and_bind("tab: complete")
        readline.set_completer(completer.complete)
    except ImportError:
        pass
    while True:
        f = raw_input(prompt_text)
        if f == '': return default
        f = os.path.expanduser(f)
        if len(f) != 0 and f[0] == os.path.sep:
            f = os.path.abspath(f)
        try:
            return open(f, *filearg, **filekwarg)
        except IOError, e:
            stderr.write(ERROR_MESSAGE % ("unable to open %s : %s" % (f, e)))
Exemplo n.º 2
0
def file_chooser(prompt_text="Enter File: ",
                 default=None,
                 filearg=[],
                 filekwarg={}):
    """A simple tool to get a file from the user. Takes keyworded arguemnts
    and passes them to open().
    
    If the user enters nothing the function will return the ``default`` value.
    Otherwise it continues to prompt the user until it get's a decent response.
    
    filekwarg may contain arguements passed on to ``open()``.
    """
    try:
        import readline, rlcomplete
        completer = rlcomplete.PathCompleter()
        readline.set_completer_delims(completer.delims)
        readline.parse_and_bind("tab: complete")
        readline.set_completer(completer.complete)
    except ImportError:
        pass
    while True:
        f = raw_input(prompt_text)
        if f == '': return default
        f = os.path.expanduser(f)
        if len(f) != 0 and f[0] == os.path.sep:
            f = os.path.abspath(f)
        try:
            return open(f, *filearg, **filekwarg)
        except IOError as e:
            stderr.write(ERROR_MESSAGE % ("unable to open %s : %s" % (f, e)))
Exemplo 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 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
Exemplo n.º 4
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