Ejemplo n.º 1
0
def do_prompt(d, key, text, default=None, validator=nonempty):
    while True:
        if default:
            prompt = purple(PROMPT_PREFIX + '%s [%s]: ' % (text, default))
        else:
            prompt = purple(PROMPT_PREFIX + text + ': ')
        x = term_input(prompt).strip()
        if default and not x:
            x = default
        if not isinstance(x, unicode):
            # for Python 2.x, try to get a Unicode string out of it
            if x.decode('ascii', 'replace').encode('ascii', 'replace') != x:
                if TERM_ENCODING:
                    x = x.decode(TERM_ENCODING)
                else:
                    print turquoise(
                        '* Note: non-ASCII characters entered '
                        'and terminal encoding unknown -- assuming '
                        'UTF-8 or Latin-1.')
                    try:
                        x = x.decode('utf-8')
                    except UnicodeDecodeError:
                        x = x.decode('latin1')
        try:
            x = validator(x)
        except ValidationError, err:
            print red('* ' + str(err))
            continue
        break
Ejemplo n.º 2
0
def do_prompt(d, key, text, default=None, validator=nonempty):
    while True:
        if default:
            prompt = purple(PROMPT_PREFIX + "%s [%s]: " % (text, default))
        else:
            prompt = purple(PROMPT_PREFIX + text + ": ")
        x = term_input(prompt).strip()
        if default and not x:
            x = default
        if not isinstance(x, unicode):
            # for Python 2.x, try to get a Unicode string out of it
            if x.decode("ascii", "replace").encode("ascii", "replace") != x:
                if TERM_ENCODING:
                    x = x.decode(TERM_ENCODING)
                else:
                    print turquoise(
                        "* Note: non-ASCII characters entered "
                        "and terminal encoding unknown -- assuming "
                        "UTF-8 or Latin-1."
                    )
                    try:
                        x = x.decode("utf-8")
                    except UnicodeDecodeError:
                        x = x.decode("latin1")
        try:
            x = validator(x)
        except ValidationError, err:
            print red("* " + str(err))
            continue
        break
Ejemplo n.º 3
0
def do_prompt(d, key, text, default=None, validator=nonempty):
    while True:
        if default:
            prompt = purple(PROMPT_PREFIX + '%s [%s]: ' % (text, default))
        else:
            prompt = purple(PROMPT_PREFIX + text + ': ')
        x = term_input(prompt)
        if default and not x:
            x = default
        if not isinstance(x, unicode):
            # for Python 2.x, try to get a Unicode string out of it
            if x.decode('ascii', 'replace').encode('ascii', 'replace') != x:
                if TERM_ENCODING:
                    x = x.decode(TERM_ENCODING)
                else:
                    print turquoise('* Note: non-ASCII characters entered '
                                    'and terminal encoding unknown -- assuming '
                                    'UTF-8 or Latin-1.')
                    try:
                        x = x.decode('utf-8')
                    except UnicodeDecodeError:
                        x = x.decode('latin1')
        try:
            x = validator(x)
        except ValidationError, err:
            print red('* ' + str(err))
            continue
        break
Ejemplo n.º 4
0
def do_prompt(d, key, text, default=None, validator=nonempty):
    while True:
        if default:
            prompt = purple(PROMPT_PREFIX + '%s [%s]: ' % (text, default))
        else:
            prompt = purple(PROMPT_PREFIX + text + ': ')
        x = raw_input(prompt)
        if default and not x:
            x = default
        if x.decode('ascii', 'replace').encode('ascii', 'replace') != x:
            if TERM_ENCODING:
                x = x.decode(TERM_ENCODING)
            else:
                print turquoise(
                    '* Note: non-ASCII characters entered and terminal '
                    'encoding unknown -- assuming UTF-8 or Latin-1.')
                try:
                    x = x.decode('utf-8')
                except UnicodeDecodeError:
                    x = x.decode('latin1')
        if validator and not validator(x):
            print red('* ' + validator.__doc__)
            continue
        break
    d[key] = x
Ejemplo n.º 5
0
def do_prompt(d, key, text, default=None, validator=nonempty):
    # type: (Dict, unicode, unicode, unicode, Callable[[unicode], Any]) -> None
    while True:
        if default is not None:
            prompt = PROMPT_PREFIX + '%s [%s]: ' % (text, default)  # type: unicode
        else:
            prompt = PROMPT_PREFIX + text + ': '
        if PY2:
            # for Python 2.x, try to get a Unicode string out of it
            if prompt.encode('ascii', 'replace').decode('ascii', 'replace') \
                    != prompt:
                if TERM_ENCODING:
                    prompt = prompt.encode(TERM_ENCODING)
                else:
                    print(turquoise('* Note: non-ASCII default value provided '
                                    'and terminal encoding unknown -- assuming '
                                    'UTF-8 or Latin-1.'))
                    try:
                        prompt = prompt.encode('utf-8')
                    except UnicodeEncodeError:
                        prompt = prompt.encode('latin1')
        prompt = purple(prompt)
        x = term_input(prompt).strip()
        if default and not x:
            x = default
        x = term_decode(x)
        try:
            x = validator(x)
        except ValidationError as err:
            print(red('* ' + str(err)))
            continue
        break
    d[key] = x
Ejemplo n.º 6
0
def term_decode(text):
    # type: (Union[bytes,str]) -> str
    warnings.warn('term_decode() is deprecated.',
                  RemovedInSphinx40Warning,
                  stacklevel=2)

    if isinstance(text, text_type):
        return text

    # Use the known encoding, if possible
    if TERM_ENCODING:
        return text.decode(TERM_ENCODING)

    # If ascii is safe, use it with no warning
    if text.decode('ascii', 'replace').encode('ascii', 'replace') == text:
        return text.decode('ascii')

    print(
        turquoise(
            __('* Note: non-ASCII characters entered '
               'and terminal encoding unknown -- assuming '
               'UTF-8 or Latin-1.')))
    try:
        return text.decode()
    except UnicodeDecodeError:
        return text.decode('latin1')
Ejemplo n.º 7
0
def do_prompt(text, default=None, validator=nonempty):
    # type: (unicode, unicode, Callable[[unicode], Any]) -> Union[unicode, bool]
    while True:
        if default is not None:
            prompt = PROMPT_PREFIX + '%s [%s]: ' % (text, default)  # type: unicode
        else:
            prompt = PROMPT_PREFIX + text + ': '
        if PY2:
            # for Python 2.x, try to get a Unicode string out of it
            if prompt.encode('ascii', 'replace').decode('ascii', 'replace') \
                    != prompt:
                if TERM_ENCODING:
                    prompt = prompt.encode(TERM_ENCODING)
                else:
                    print(turquoise(__('* Note: non-ASCII default value provided '
                                       'and terminal encoding unknown -- assuming '
                                       'UTF-8 or Latin-1.')))
                    try:
                        prompt = prompt.encode('utf-8')
                    except UnicodeEncodeError:
                        prompt = prompt.encode('latin1')
        prompt = purple(prompt)
        x = term_input(prompt).strip()
        if default and not x:
            x = default
        x = term_decode(x)
        try:
            x = validator(x)
        except ValidationError as err:
            print(red('* ' + str(err)))
            continue
        break
    return x
Ejemplo n.º 8
0
def do_prompt(d, key, text, default=None, validator=nonempty):
    while True:
        if default is not None:
            prompt = PROMPT_PREFIX + "%s [%s]: " % (text, default)
        else:
            prompt = PROMPT_PREFIX + text + ": "
        if PY2:
            # for Python 2.x, try to get a Unicode string out of it
            if prompt.encode("ascii", "replace").decode("ascii", "replace") != prompt:
                if TERM_ENCODING:
                    prompt = prompt.encode(TERM_ENCODING)
                else:
                    print(
                        turquoise(
                            "* Note: non-ASCII default value provided "
                            "and terminal encoding unknown -- assuming "
                            "UTF-8 or Latin-1."
                        )
                    )
                    try:
                        prompt = prompt.encode("utf-8")
                    except UnicodeEncodeError:
                        prompt = prompt.encode("latin1")
        prompt = purple(prompt)
        x = term_input(prompt).strip()
        if default and not x:
            x = default
        x = term_decode(x)
        try:
            x = validator(x)
        except ValidationError as err:
            print(red("* " + str(err)))
            continue
        break
    d[key] = x
Ejemplo n.º 9
0
def do_prompt(d, key, text, default=None, validator=nonempty):
    while True:
        if default:
            prompt = PROMPT_PREFIX + '%s [%s]: ' % (text, default)
        else:
            prompt = PROMPT_PREFIX + text + ': '
        if sys.version_info < (3, 0):
            # for Python 2.x, try to get a Unicode string out of it
            if prompt.encode('ascii', 'replace').decode('ascii', 'replace') \
                    != prompt:
                if TERM_ENCODING:
                    prompt = prompt.encode(TERM_ENCODING)
                else:
                    print(turquoise('* Note: non-ASCII default value provided '
                                    'and terminal encoding unknown -- assuming '
                                    'UTF-8 or Latin-1.'))
                    try:
                        prompt = prompt.encode('utf-8')
                    except UnicodeEncodeError:
                        prompt = prompt.encode('latin1')
        prompt = purple(prompt)
        x = term_input(prompt).strip()
        if default and not x:
            x = default
        if not isinstance(x, str):
            # for Python 2.x, try to get a Unicode string out of it
            if x.decode('ascii', 'replace').encode('ascii', 'replace') != x:
                if TERM_ENCODING:
                    x = x.decode(TERM_ENCODING)
                else:
                    print(turquoise('* Note: non-ASCII characters entered '
                                    'and terminal encoding unknown -- assuming '
                                    'UTF-8 or Latin-1.'))
                    try:
                        x = x.decode('utf-8')
                    except UnicodeDecodeError:
                        x = x.decode('latin1')
        try:
            x = validator(x)
        except ValidationError as err:
            print(red('* ' + str(err)))
            continue
        break
    d[key] = x
Ejemplo n.º 10
0
def do_prompt(d, key, text, default=None, validator=nonempty):
    while True:
        if default:
            prompt = purple(PROMPT_PREFIX + '%s [%s]: ' % (text, default))
        else:
            prompt = purple(PROMPT_PREFIX + text + ': ')
        x = raw_input(prompt)
        if default and not x:
            x = default
        if x.decode('ascii', 'replace').encode('ascii', 'replace') != x:
            if TERM_ENCODING:
                x = x.decode(TERM_ENCODING)
            else:
                print turquoise('* Note: non-ASCII characters entered and terminal '
                                'encoding unknown -- assuming UTF-8 or Latin-1.')
                try:
                    x = x.decode('utf-8')
                except UnicodeDecodeError:
                    x = x.decode('latin1')
        if validator and not validator(x):
            print red('* ' + validator.__doc__)
            continue
        break
    d[key] = x
Ejemplo n.º 11
0
def term_decode(text):
    if isinstance(text, text_type):
        return text

    # for Python 2.x, try to get a Unicode string out of it
    if text.decode('ascii', 'replace').encode('ascii', 'replace') == text:
        return text

    if TERM_ENCODING:
        text = text.decode(TERM_ENCODING)
    else:
        print(turquoise('* Note: non-ASCII characters entered '
                        'and terminal encoding unknown -- assuming '
                        'UTF-8 or Latin-1.'))
        try:
            text = text.decode('utf-8')
        except UnicodeDecodeError:
            text = text.decode('latin1')
    return text
Ejemplo n.º 12
0
def term_decode(text):
    if isinstance(text, text_type):
        return text

    # for Python 2.x, try to get a Unicode string out of it
    if text.decode('ascii', 'replace').encode('ascii', 'replace') == text:
        return text

    if TERM_ENCODING:
        text = text.decode(TERM_ENCODING)
    else:
        print(turquoise('* Note: non-ASCII characters entered '
                        'and terminal encoding unknown -- assuming '
                        'UTF-8 or Latin-1.'))
        try:
            text = text.decode('utf-8')
        except UnicodeDecodeError:
            text = text.decode('latin1')
    return text
Ejemplo n.º 13
0
def do_prompt(text, default=None, validator=nonempty):
    # type: (unicode, unicode, Callable[[unicode], Any]) -> Union[unicode, bool]
    while True:
        if default is not None:
            prompt = PROMPT_PREFIX + '%s [%s]: ' % (text, default
                                                    )  # type: unicode
        else:
            prompt = PROMPT_PREFIX + text + ': '
        if PY2:
            # for Python 2.x, try to get a Unicode string out of it
            if prompt.encode('ascii', 'replace').decode('ascii', 'replace') \
                    != prompt:
                if TERM_ENCODING:
                    prompt = prompt.encode(TERM_ENCODING)
                else:
                    print(
                        turquoise(
                            __('* Note: non-ASCII default value provided '
                               'and terminal encoding unknown -- assuming '
                               'UTF-8 or Latin-1.')))
                    try:
                        prompt = prompt.encode('utf-8')
                    except UnicodeEncodeError:
                        prompt = prompt.encode('latin1')
        if USE_LIBEDIT:
            # Note: libedit has a problem for combination of ``input()`` and escape
            # sequence (see #5335).  To avoid the problem, all prompts are not colored
            # on libedit.
            pass
        else:
            prompt = colorize(COLOR_QUESTION, prompt, input_mode=True)
        x = term_input(prompt).strip()
        if default and not x:
            x = default
        x = term_decode(x)
        try:
            x = validator(x)
        except ValidationError as err:
            print(red('* ' + str(err)))
            continue
        break
    return x
Ejemplo n.º 14
0
def term_decode(text):
    # type: (Union[bytes,unicode]) -> unicode
    if isinstance(text, text_type):
        return text

    # Use the known encoding, if possible
    if TERM_ENCODING:
        return text.decode(TERM_ENCODING)

    # If ascii is safe, use it with no warning
    if text.decode('ascii', 'replace').encode('ascii', 'replace') == text:
        return text.decode('ascii')

    print(turquoise(__('* Note: non-ASCII characters entered '
                       'and terminal encoding unknown -- assuming '
                       'UTF-8 or Latin-1.')))
    try:
        return text.decode('utf-8')
    except UnicodeDecodeError:
        return text.decode('latin1')
Ejemplo n.º 15
0
def term_decode(text):
    if isinstance(text, text_type):
        return text

    # for Python 2.x, try to get a Unicode string out of it
    if text.decode("ascii", "replace").encode("ascii", "replace") == text:
        return text

    if TERM_ENCODING:
        text = text.decode(TERM_ENCODING)
    else:
        print(
            turquoise(
                "* Note: non-ASCII characters entered " "and terminal encoding unknown -- assuming " "UTF-8 or Latin-1."
            )
        )
        try:
            text = text.decode("utf-8")
        except UnicodeDecodeError:
            text = text.decode("latin1")
    return text
Ejemplo n.º 16
0
def term_decode(text):
    # type: (Union[bytes,str]) -> str
    warnings.warn('term_decode() is deprecated.',
                  RemovedInSphinx40Warning, stacklevel=2)

    if isinstance(text, str):
        return text

    # Use the known encoding, if possible
    if TERM_ENCODING:
        return text.decode(TERM_ENCODING)

    # If ascii is safe, use it with no warning
    if text.decode('ascii', 'replace').encode('ascii', 'replace') == text:
        return text.decode('ascii')

    print(turquoise(__('* Note: non-ASCII characters entered '
                       'and terminal encoding unknown -- assuming '
                       'UTF-8 or Latin-1.')))
    try:
        return text.decode()
    except UnicodeDecodeError:
        return text.decode('latin1')