()
return parameters
Learning mode.
Learning mode.
Learning mode .
def ask_mode():
    """Learning mode.
    """

    print dedent("""\
    -----------------------------------------------------
                   WELCOME TO LEARNING MODE

                You will be guided through the
               possibilities by answering a few
                         questions.
    -----------------------------------------------------\
    """)

    questions = {
        'source'   : '[1/5] Which data source do you want to work with? ',
        'all_keys' : '[2/5] Consider all data for this source [Yn]? ',
        'from_keys': '[   ] Which keys should we consider (separated with " ")? ',
        'search'   : '[3/5] What kind of search? ',
        'field'    : '[4/5] On which field? ',
        'value'    : '[   ] Which value to look for? ',
        'point'    : '[4/5] From which point (key or geocode)? ',
        'radius'   : '[   ] Which radius for the search (kms)? ',
        'limit'    : '[   ] Which limit for the search (number of results)? ',
        'display'  : '[5/5] Which display? ',
        'execute'  : '[   ] Execute the command [Yn]? ',
    }

    # 1. Choose base
    base = ask_till_ok(questions['source'], sorted(S_MANAGER), prefill='ori_por')

    # 2. Choose from keys
    all_keys = ask_till_ok(questions['all_keys'], boolean=True, default=True)

    if all_keys:
        from_keys = None
    else:
        from_keys = ask_input(questions['from_keys']).strip().split()

    # 3. Choose search type
    print dedent("""
    (*) none     : no search done, all data used
    (*) exact    : make an exact search on a specific field
    (*) fuzzy    : make a fuzzy search on a specific field
    (*) phonetic : make a phonetic search on a specific field
    (*) near     : make a geographical search from a point with a radius
    (*) closest  : make a geographical search from a point with a number of results
    """)
    search = ask_till_ok(questions['search'], ['none', 'exact', 'fuzzy', 'phonetic', 'near', 'closest'], show=False)

    if search.strip().lower() in ('none',):
        search = None

    # 4. Search parameters
    field, value, radius, limit = None, None, None, None

    if search in ['exact', 'fuzzy', 'phonetic']:
        field = ask_till_ok(questions['field'], sorted(S_MANAGER.get(base)['headers']), prefill='name')
        value = ask_till_ok(questions['value'],
                            is_ok = lambda r: r,
                            fail_message='-/!\- Cannot be empty')

    elif search in ['near']:
        value = ask_till_ok(questions['point'],
                            is_ok = lambda r: r,
                            fail_message='-/!\- Cannot be empty')
        radius = ask_input(questions['radius'], prefill='50').strip()

    elif search in ['closest']:
        value = ask_till_ok(questions['point'],
                            is_ok = lambda r: r,
                            fail_message='-/!\- Cannot be empty')
        limit = ask_input(questions['limit'], prefill='5').strip()

    # 5. Display
    print dedent("""
    (*) terminal : fancy display in the terminal
    (*) quiet    : csv-like display
    (*) map      : map display
    (*) graph    : graph display
    """)
    display = ask_till_ok(questions['display'], ['terminal', 'quiet', 'map', 'graph'], prefill='terminal', show=False)

    # 6. Conclusion
    parameters = {
        'base'      : base,
        'from_keys' : from_keys,
        'search'    : search,
        'field'     : field,
        'radius'    : radius,
        'limit'     : limit,
        'value'     : value,
        'display'   : display
    }

    print
    print '-----------------------------------------------------'
    print
    print '              Congrats! You choosed:                 '
    print
    for k, v in parameters.iteritems():
        if v is not None:
            print '(*) %-10s => "%s"' % (str(k), str(v))
        #else:
        #    print '(*) %-10s => None' % str(k)

    # One liner
    print
    print '            Equivalent one-liner command             '
    print '        with long options and short options          '
    print

    # Long options
    base_part         = '--base %s' % base
    from_keys_part    = '' if from_keys is None else ' '.join(from_keys)
    search_part       = ('--%s "%s"' % (search, value)) if search is not None else ''
    display_part     = ('--%s' % display) if display != 'terminal' else ''

    if search in ['exact', 'fuzzy', 'phonetic']:
        search_field_part = '--%s-field %s' % (search, field)
    elif search in ['near']:
        search_field_part = '--%s-limit %s' % (search, radius)
    elif search in ['closest']:
        search_field_part = '--%s-limit %s' % (search, limit)
    else:
        search_field_part = ''

    command = ' '.join(e for e in [SCRIPT_NAME,
                                   from_keys_part,
                                   base_part,
                                   search_field_part,
                                   search_part,
                                   display_part] if e)
    print command


    # Short options
    base_part         = '-b %s' % base
    from_keys_part    = '' if from_keys is None else ' '.join(from_keys)
    search_part       = ('-%s "%s"' % (search[0], value)) if search is not None else ''
    display_part      = ('-%s' % display[0]) if display != 'terminal' else ''

    if search in ['exact', 'fuzzy', 'phonetic']:
        search_field_part = '-%s %s' % (search[0].upper(), field)
    elif search in ['near']:
        search_field_part = '-%s %s' % (search[0].upper(), radius)
    elif search in ['closest']:
        search_field_part = '-%s %s' % (search[0].upper(), limit)
    else:
        search_field_part = ''

    command = ' '.join(e for e in [SCRIPT_NAME,
                                   from_keys_part,
                                   base_part,
                                   search_field_part,
                                   search_part,
                                   display_part] if e)
    print command
    print '-----------------------------------------------------'
    print

    execute = ask_till_ok(questions['execute'], boolean=True, default=True)
    if execute:
        os.system(command)

    return parameters
def ask_mode ( ) : print dedent ( """\
    -----------------------------------------------------
                   WELCOME TO LEARNING MODE

                You will be guided through the
               possibilities by answering a few
                         questions.
    -----------------------------------------------------\
    """ ) questions = { 'source' : '[1/5] Which data source do you want to work with? ' , 'all_keys' : '[2/5] Consider all data for this source [Yn]? ' , 'from_keys' : '[   ] Which keys should we consider (separated with " ")? ' , 'search' : '[3/5] What kind of search? ' , 'field' : '[4/5] On which field? ' , 'value' : '[   ] Which value to look for? ' , 'point' : '[4/5] From which point (key or geocode)? ' , 'radius' : '[   ] Which radius for the search (kms)? ' , 'limit' : '[   ] Which limit for the search (number of results)? ' , 'display' : '[5/5] Which display? ' , 'execute' : '[   ] Execute the command [Yn]? ' , } # 1. Choose base base = ask_till_ok ( questions [ 'source' ] , sorted ( S_MANAGER ) , prefill = 'ori_por' ) # 2. Choose from keys all_keys = ask_till_ok ( questions [ 'all_keys' ] , boolean = True , default = True ) if all_keys : from_keys = None else : from_keys = ask_input ( questions [ 'from_keys' ] ) . strip ( ) . split ( ) # 3. Choose search type print dedent ( """
    (*) none     : no search done, all data used
    (*) exact    : make an exact search on a specific field
    (*) fuzzy    : make a fuzzy search on a specific field
    (*) phonetic : make a phonetic search on a specific field
    (*) near     : make a geographical search from a point with a radius
    (*) closest  : make a geographical search from a point with a number of results
    """ ) search = ask_till_ok ( questions [ 'search' ] , [ 'none' , 'exact' , 'fuzzy' , 'phonetic' , 'near' , 'closest' ] , show = False ) if search . strip ( ) . lower ( ) in ( 'none' , ) : search = None # 4. Search parameters field , value , radius , limit = None , None , None , None if search in [ 'exact' , 'fuzzy' , 'phonetic' ] : field = ask_till_ok ( questions [ 'field' ] , sorted ( S_MANAGER . get ( base ) [ 'headers' ] ) , prefill = 'name' ) value = ask_till_ok ( questions [ 'value' ] , is_ok = lambda r : r , fail_message = '-/!\- Cannot be empty' ) elif search in [ 'near' ] : value = ask_till_ok ( questions [ 'point' ] , is_ok = lambda r : r , fail_message = '-/!\- Cannot be empty' ) radius = ask_input ( questions [ 'radius' ] , prefill = '50' ) . strip ( ) elif search in [ 'closest' ] : value = ask_till_ok ( questions [ 'point' ] , is_ok = lambda r : r , fail_message = '-/!\- Cannot be empty' ) limit = ask_input ( questions [ 'limit' ] , prefill = '5' ) . strip ( ) # 5. Display print dedent ( """
    (*) terminal : fancy display in the terminal
    (*) quiet    : csv-like display
    (*) map      : map display
    (*) graph    : graph display
    """ ) display = ask_till_ok ( questions [ 'display' ] , [ 'terminal' , 'quiet' , 'map' , 'graph' ] , prefill = 'terminal' , show = False ) # 6. Conclusion parameters = { 'base' : base , 'from_keys' : from_keys , 'search' : search , 'field' : field , 'radius' : radius , 'limit' : limit , 'value' : value , 'display' : display } print print '-----------------------------------------------------' print print '              Congrats! You choosed:                 ' print for k , v in parameters . iteritems ( ) : if v is not None : print '(*) %-10s => "%s"' % ( str ( k ) , str ( v ) ) #else: #    print '(*) %-10s => None' % str(k) # One liner print print '            Equivalent one-liner command             ' print '        with long options and short options          ' print # Long options base_part = '--base %s' % base from_keys_part = '' if from_keys is None else ' ' . join ( from_keys ) search_part = ( '--%s "%s"' % ( search , value ) ) if search is not None else '' display_part = ( '--%s' % display ) if display != 'terminal' else '' if search in [ 'exact' , 'fuzzy' , 'phonetic' ] : search_field_part = '--%s-field %s' % ( search , field ) elif search in [ 'near' ] : search_field_part = '--%s-limit %s' % ( search , radius ) elif search in [ 'closest' ] : search_field_part = '--%s-limit %s' % ( search , limit ) else : search_field_part = '' command = ' ' . join ( e for e in [ SCRIPT_NAME , from_keys_part , base_part , search_field_part , search_part , display_part ] if e ) print command # Short options base_part = '-b %s' % base from_keys_part = '' if from_keys is None else ' ' . join ( from_keys ) search_part = ( '-%s "%s"' % ( search [ 0 ] , value ) ) if search is not None else '' display_part = ( '-%s' % display [ 0 ] ) if display != 'terminal' else '' if search in [ 'exact' , 'fuzzy' , 'phonetic' ] : search_field_part = '-%s %s' % ( search [ 0 ] . upper ( ) , field ) elif search in [ 'near' ] : search_field_part = '-%s %s' % ( search [ 0 ] . upper ( ) , radius ) elif search in [ 'closest' ] : search_field_part = '-%s %s' % ( search [ 0 ] . upper ( ) , limit ) else : search_field_part = '' command = ' ' . join ( e for e in [ SCRIPT_NAME , from_keys_part , base_part , search_field_part , search_part , display_part ] if e ) print command print '-----------------------------------------------------' print execute = ask_till_ok ( questions [ 'execute' ] , boolean = True , default = True ) if execute : os . system ( command ) return parameters