#
# -*- coding: utf-8 -*-


#-----------------------------------------------------------------------------------------------------------
#   Enable globals:
try:
    g
except NameError:
    import os.path as _p; import inspect as _i
    pathToScript = _p.pardir, 'lib', 'biocase', 'fundamentals.py'
    execfile( _p.join( _p.dirname( _i.getfile( lambda:None ) ), *pathToScript ) )


############################################################################################################
#
#   YELLOW PAGES
#
#===========================================================================================================
import                                  sys
#-----------------------------------------------------------------------------------------------------------
u                                       = g.configtool.utilities
#-----------------------------------------------------------------------------------------------------------
_feds                                   = u.feds
_getAllGraphnames                       = u.lupywrapper.search._getAllGraphnames
_inter                                  = u.kpyInterpol.inter
_search                                 = u.lupywrapper.search.search
_terms                                  = sys.argv[ 1 : ]
_wrap                                   = u.typo.wrap
_maxresults                             = u.lupywrapper.search.MAXRESULTS

############################################################################################################
#
#
#
#===========================================================================================================
def showHelp():
    print
    print "Usage: python %s query" % ( sys.argv[ 0 ], )
    print "The query phrase may consist of an arbitrary number of words."
    print
    print "*   Words preceded by a + like '+plants' are required to appear;"
    print
    print "*   words preceded by a - like '-synecology' are required not to appear;"
    print
    print "*   words surrounded by : such as ':abcd12:' define the names of "
    print "    graphs to search in; default is to search in all graphs. "
    print
    print "*   numbers surrounded by : such as ':10:' define the maximum number"
    print "    of results to be returned (default is %d). " % _maxresults
    print
    print "Ex.:"
    print "python %s plant -gathering :10:" % ( sys.argv[ 0 ], )
    print "python %s plant -gathering :abcd12: :bion:" % ( sys.argv[ 0 ], )
    print "python %s plant +synecology :abcd12:" % ( sys.argv[ 0 ], )
    print
    print "Available graphs:"
    graphnames = list( _getAllGraphnames() )
    graphnames.sort()
    gauges = ' '.join( [ ':%s:' % graphname for graphname in graphnames ] )
    print u.typo.wrap( gauges, 80 )
    print

#===========================================================================================================
def searchAndTell( phrase ):
    #-------------------------------------------------------------------------------------------------------
    groveFed    = _feds.resolve( 'Grove' )
    dsGrove     = groveFed.dsGrove
    #-------------------------------------------------------------------------------------------------------
    normalphrase, graphnames, results = _search( phrase )
    graphnames = list( graphnames )
    graphnames.sort()
    graphnames = ', '.join( graphnames )
    #-------------------------------------------------------------------------------------------------------
    print _inter( ''
        +   "You searched for '$normalphrase'\n"
        +   "in $graphnames. The query returned $len(results) results:"
        )
    for result in results:
        rating, graphname, termnr = result
        rating      = '%5.3f' % rating
        termref     = ( graphname, termnr )
        nodefact    = dsGrove.nodefactForTermref( termref )
        short, long = dsGrove.getDocumentationForTermref( termref )
        print
        print _inter( '($rating) $nodefact.ontonym' )
        if short:
            print _wrap( short )


############################################################################################################
#
#
#
#===========================================================================================================
if __name__ == '__main__':

    if not _terms:
        showHelp()
    else:
        phrase = ' '.join( _terms )
        searchAndTell( phrase )




