Example #1
0
    def test_choices_3(self):
        input_str_blank = "\n\n"

        input_str_y = " a\ny"

        input_str_n = "a\n  no"

        strip_cleaner = StripCleaner()

        with redirect_stdin(StringIO(input_str_y)):
            result = get_input(cleaners=strip_cleaner,
                               convertor=YesNoConvertor(),
                               default='Y')
            assert (result == 'yes')

        with redirect_stdin(StringIO(input_str_blank)):
            result = get_input(cleaners=strip_cleaner,
                               convertor=YesNoConvertor(),
                               default='Y')
            assert (result == 'yes')

        with redirect_stdin(StringIO(input_str_n)):
            result = get_input(cleaners=strip_cleaner,
                               convertor=YesNoConvertor(),
                               default='Y')
            assert (result == 'no')
Example #2
0
    def test_get_input_list_3(self):
        input_str = u"""
            foo, bar
            -1,2,3
            """

        lc = ListConvertor(delimiter=None,
                           elem_get_input=GetInput(convertor=IntConvertor()))
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=lc)
            print(result)
            assert (result == [-1, 2, 3])
Example #3
0
    def test_capitalize(self):
        input_str = '  \t  bOb JoNeS\t  \t '

        strip_cleaner = StripCleaner()
        rstrip_cleaner = StripCleaner(lstrip=False, rstrip=True)
        lower_cleaner = CapitalizationCleaner(style='lower')
        upper_cleaner = CapitalizationCleaner(style=UPPER_CAP_STYLE)
        strip_and_lower_cleaners = [strip_cleaner, lower_cleaner]
        capitalize_cleaner = CapitalizationCleaner(style='capitalize')
        capitalize_all_cleaner = CapitalizationCleaner(
            style=ALL_WORDS_CAP_STYLE)

        with redirect_stdin(StringIO(input_str)):
            result = get_input(
                prompt=
                'Enter any string (will be stripped of leading and trailing spaces and converted to lower)',
                cleaners=strip_and_lower_cleaners)
            assert (result == 'bob jones')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(
                prompt=
                'Enter any string (will be stripped of trailing spaces and converted to upper)',
                cleaners=[rstrip_cleaner, upper_cleaner])
            assert (result == '  \t  BOB JONES')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(
                prompt='Enter your name (first word will be capitalized)',
                cleaners=[strip_cleaner, capitalize_cleaner])
            assert (result == 'Bob jones')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(
                prompt='Enter your name (all words will be capitalized)',
                cleaners=[strip_cleaner, capitalize_all_cleaner])
            assert (result == 'Bob Jones')
Example #4
0
    def test_choices(self):
        input_str_blank = """
 
            """

        input_str = """licorice
            booger
            lemon 
            """

        colors = ['red', 'green', 'blue']
        good_flavors = ['cherry', 'lime', 'lemon', 'orange']
        bad_flavors = 'licorice'
        choices_validator = ChoiceValidator(choices=colors)
        good_flavor_validator = ChoiceValidator(choices=good_flavors)
        bad_flavor_validator = ChoiceValidator(choices=bad_flavors)
        not_in_choices_validator = NoneOfValidator(
            validators=[bad_flavor_validator])
        strip_cleaner = StripCleaner()
        lower_cleaner = CapitalizationCleaner()
        strip_and_lower_cleaners = [strip_cleaner, lower_cleaner]

        with redirect_stdin(StringIO(input_str_blank)):
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=not_in_choices_validator,
                               default='cherry')
            assert (result == 'cherry')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=not_in_choices_validator,
                               default='cherry')
            assert (result == 'booger')

        with redirect_stdin(StringIO(input_str)):
            validators = [good_flavor_validator, not_in_choices_validator]
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=validators,
                               default='cherry')
            assert (result == 'lemon')
Example #5
0
    def test_choices_2(self):
        input_str_blank = """

            """

        input_str = """
                    a
                    licorice
                    bo
                    lem 
                    """

        length_3_validator = LengthValidator(min_len=3, max_len=3)
        length_5_plus_validator = LengthValidator(min_len=5)
        length_2_to_4_validator = LengthValidator(min_len=2, max_len=4)
        strip_and_lower_cleaners = [
            StripCleaner(), CapitalizationCleaner('lower')
        ]

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='Enter a three letter string',
                               cleaners=strip_and_lower_cleaners,
                               validators=[length_3_validator])
            assert (result == 'lem')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='Enter a string at least 5 letters long',
                               cleaners=strip_and_lower_cleaners,
                               validators=[length_5_plus_validator])
            assert (result == 'licorice')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='Enter a 2 to 4 letter string',
                               cleaners=strip_and_lower_cleaners,
                               validators=[length_2_to_4_validator])
            assert (result == 'bo')
Example #6
0
        'ei': {
            'first_name': 'Eric',
            'last_name': 'Idle',
            'email': '*****@*****.**',
            'password': hash('1/2aB'),
            'roles': ['arthur', 'timmy', 'stig']
        },
    }

    # Fake list of allowed roles
    roles_list = [
        'admin', 'arthur', 'praline', 'mandy', 'animator', 'anchovy', 'stig',
        'timmy', 'travel-guide'
    ]

    strip_cleaner = StripCleaner()
    default_cleaners = [StripCleaner(), CapitalizationCleaner(style='lower')]
    name_cleaners = [StripCleaner(), CapitalizationCleaner(style='all_words')]
    strong_password_validator = PasswordValidator(disallowed='[]',
                                                  min_len=5,
                                                  max_len=15,
                                                  min_lower=2,
                                                  min_puncts=2)
    email_validator = SimpleValidator(
        isemail, name='email')  # validator from validus function
    role_validator = ListValidator(elem_validators=ChoiceValidator(roles_list))
    role_prompt = 'Roles ({}, separated by commas)'.format(sorted(roles_list))
    password_confirm_fmt_str = 'password does not match'

    # Give a hint to new users...
    print(
Example #7
0
from cooked_input import get_input, get_string
from cooked_input.validators import LengthValidator, ChoiceValidator, NoneOfValidator
from cooked_input.cleaners import StripCleaner, CapitalizationCleaner
from cooked_input.convertors import YesNoConvertor

if __name__ == '__main__':
    colors = ['red', 'green', 'blue']
    good_flavors = ['cherry', 'lime', 'lemon', 'orange']
    bad_flavors = 'licorice'

    good_flavor_validator = ChoiceValidator(choices=good_flavors)
    not_in_choices_validator = NoneOfValidator(
        validators=[ChoiceValidator(choices=bad_flavors)])
    strip_and_lower_cleaners = [
        StripCleaner(), CapitalizationCleaner(style='lower')
    ]

    # simplest way
    print(get_string(prompt='Enter some text'))

    # strip and capitalization cleaners
    print(
        get_input(
            prompt=
            'Enter any string (will be stripped of trailing spaces and converted to upper)',
            cleaners=[
                StripCleaner(lstrip=False, rstrip=True),
                CapitalizationCleaner(style='upper')
            ]))