← Back to team overview

team4alfanous team mailing list archive

[Branch ~team4alfanous/alfanous/alfanous-git] Rev 261: Many changes on console interface

 

------------------------------------------------------------
revno: 261
git commit: 07771e9aa05e42949f008a920508a4d9435c660f
committer: Assem Chelli <assem.ch@xxxxxxxxx>
timestamp: Wed 2012-06-13 13:53:22 +0100
message:
  Many changes on console interface
   - use argparse lib instead of optparse (deprecated)
   - use the new output system
modified:
  src/alfanous/console.py


--
lp:alfanous
https://code.launchpad.net/~team4alfanous/alfanous/alfanous-git

Your team Alfanous team is subscribed to branch lp:alfanous.
To unsubscribe from this branch go to https://code.launchpad.net/~team4alfanous/alfanous/alfanous-git/+edit-subscription
=== modified file 'src/alfanous/console.py'
--- src/alfanous/console.py	2012-06-01 02:54:15 +0000
+++ src/alfanous/console.py	2012-06-13 12:53:22 +0000
@@ -17,59 +17,105 @@
 '''
 Created on 24 févr. 2010
 
-
 @author: Assem Chelli
 @contact: assem.ch [at] gmail.com
 @license: AGPL
 
+TODO  get default values from Alfanous.output
+TODO  show RAW|JSON
+
 '''
 
-
-#from  alfanous.Misc import *
-from  alfanous.main import QuranicSearchEngine
-
-
-import sys
-from optparse import OptionParser
-
-def main():
-    #initial options
-    parser = OptionParser( 
-                              description = ' a search engine in the Holy Quran',
-                              prog = 'alfanous-cmd',
-                              version = '%prog 0.1',
-                              usage = sys.argv[0] + " [options] \"QUERY\""
-                        )
-
-
-    parser.add_option( "-s", type = "string", dest = "search", nargs = 0, default = None, help = "search in the quran" )
-    parser.add_option( "-g", type = "string", dest = "suggest", nargs = 0, default = None, help = "suggest the missing words" )
-    parser.add_option( "-t", type = "int", dest = "OPTION", nargs = 1, default = 12, help = "just a test" )
-
-
-    #execute command
-    ( options, args ) = parser.parse_args()
-
-    if len( args ) >= 1:
-            QSE = QuranicSearchEngine( "/usr/share/alfanous-indexes/main/" )
-            if options.search != None:
-                print "\n#search#"
-                res, terms = QSE.search_all( args[0], sortedby = "score" )
-                html = u"Results(time=%f,number=%d):\n" % ( res.runtime, len( res ) )
-                for r in res:
-                        html += "(" + r["sura"] + "," + unicode( r["aya_id"] ) + "):"
-                        html += r["aya_"] + "\n"
-                print html
-
-            if options.suggest != None:
-                print "#suggestions#"
-                for key, value in QSE.suggest_all( args[0] ).items():
-                        print key, ":", ",".join( value )
-    else:
-        print parser.print_help()
-
-
-if  __name__ == "__main__":
-    main()
+import sys, json
+from argparse import ArgumentParser
+from alfanous.outputs import Raw
+
+RAWoutput = Raw( QSE_index = "../../indexes/main/", TSE_index = "../../indexes/extend/", WSE_index = "../../indexes/word/", Recitations_list_file = "../../resources/configs/recitations.js", Translations_list_file = "../../resources/configs/translations.js", Information_file = "../../resources/configs/information.js", Hints_file = "../../resources/configs/hints.js", Stats_file = "../../resources/configs/stats.js" )
+
+INFORMATION = RAWoutput.do( {"action":"show", "query":"information" } )["show"]["information"]
+DEFAULTS = RAWoutput.do( {"action":"show", "query":"defaults" } )["show"]["defaults"]
+DOMAINS = RAWoutput.do( {"action":"show", "query":"domains" } )["show"]["domains"]
+HELPMESSAGES = RAWoutput.do( {"action":"show", "query":"help_messages" } )["show"]["help_messages"]
+
+## a function to decide what is True and what is False
+TRUE_FALSE = lambda x:False if x.lower() in ["no", "none", "null", "0", "-1", "", "false"] else True;
+
+def DEFREEZE_XRANGE( d ):
+    """ TODO reversing the operation of freezing xranges done by module alfanous.output """
+    pass
+
+
+arg_parser = ArgumentParser( 
+                              description = ' Alfanous - advanced search engine in  Holy Quran',
+                              prog = 'alfanous-console',
+                              version = '%(prog)s ' + INFORMATION["version"],
+                              usage = sys.argv[0] + " [flags]"
+                            )
+
+# add arguments
+arg_parser.add_argument( "-a", "--action", dest = "action", type = str , choices = DOMAINS["action"], default = "search", help = HELPMESSAGES["action"] )
+arg_parser.add_argument( "-q", "--query", dest = "query", type = str  , help = HELPMESSAGES["query"] )
+arg_parser.add_argument( "-o", "--offset", dest = "offset", type = int, choices = DOMAINS["offset"], help = HELPMESSAGES["offset"] )
+arg_parser.add_argument( "-r", "--range", dest = "range", type = int, choices = DOMAINS["range"], help = HELPMESSAGES["range"] )
+arg_parser.add_argument( "-s", "--sortedby", dest = "sortedby", type = str, choices = DOMAINS["sortedby"], help = HELPMESSAGES["sortedby"] )
+arg_parser.add_argument( "--page", dest = "page", type = int, default = 1, choices = DOMAINS["page"], help = HELPMESSAGES["page"] )
+arg_parser.add_argument( "--perpage", dest = "perpage", type = int, choices = DOMAINS["perpage"], help = HELPMESSAGES["perpage"] )
+arg_parser.add_argument( "--recitation", dest = "recitation", type = str, choices = DOMAINS["recitation"], help = HELPMESSAGES["recitation"] )
+arg_parser.add_argument( "--translation", dest = "translation", type = str, choices = DOMAINS["translation"], help = HELPMESSAGES["translation"] )
+arg_parser.add_argument( "--highlight", dest = "highlight", type = str, choices = DOMAINS["highlight"], help = HELPMESSAGES["highlight"] )
+arg_parser.add_argument( "--script", dest = "script", type = str, choices = DOMAINS["script"] , help = HELPMESSAGES["script"] )
+arg_parser.add_argument( "--vocalized", dest = "vocalized", type = bool, choices = DOMAINS["vocalized"], help = HELPMESSAGES["vocalized"] )
+arg_parser.add_argument( "--prev_aya", dest = "prev_aya", type = bool, choices = DOMAINS["prev_aya"], help = HELPMESSAGES["prev_aya"] )
+arg_parser.add_argument( "--next_aya", dest = "next_aya", type = bool, choices = DOMAINS["next_aya"], help = HELPMESSAGES["next_aya"] )
+arg_parser.add_argument( "--sura_info", dest = "sura_info", type = bool, choices = DOMAINS["sura_info"], help = HELPMESSAGES["sura_info"] )
+arg_parser.add_argument( "--word_info", dest = "word_info", type = bool, choices = DOMAINS["word_info"], help = HELPMESSAGES["word_info"] )
+arg_parser.add_argument( "--aya_position_info", dest = "aya_position_info", type = bool, choices = DOMAINS["aya_position_info"], help = HELPMESSAGES["aya_position_info"] )
+arg_parser.add_argument( "--aya_theme_info", dest = "aya_theme_info", type = bool, choices = DOMAINS["aya_theme_info"], help = HELPMESSAGES["aya_theme_info"] )
+arg_parser.add_argument( "--aya_stat_info", dest = "aya_stat_info", type = bool, choices = DOMAINS["aya_stat_info"], help = HELPMESSAGES["aya_stat_info"] )
+arg_parser.add_argument( "--aya_sajda_info", dest = "aya_sajda_info", type = bool, choices = DOMAINS["aya_sajda_info"], help = HELPMESSAGES["aya_sajda_info"] )
+arg_parser.add_argument( "--annotation_aya", dest = "annotation_aya", type = bool, choices = DOMAINS["annotation_aya"], help = HELPMESSAGES["annotation_aya"] )
+arg_parser.add_argument( "--annotation_word", dest = "annotation_word", type = bool, choices = DOMAINS["annotation_word"], help = HELPMESSAGES["annotation_word"] )
+arg_parser.add_argument( "--fuzzy", dest = "fuzzy", type = bool, choices = DOMAINS["fuzzy"], help = HELPMESSAGES["fuzzy"] )
+arg_parser.add_argument( "--ident", dest = "ident", type = str , default = "unknown" , help = HELPMESSAGES["ident"] )
+arg_parser.add_argument( "--platform", dest = "platform", type = str, choices = DOMAINS["platform"], help = HELPMESSAGES["platform"] )
+arg_parser.add_argument( "--domain", dest = "domain", type = str, help = HELPMESSAGES["domain"] )
+#execute command
+args = arg_parser.parse_args()
+
+
+if args.action and args.query:
+    flags = {}
+    if args.action: flags["action"] = args.action
+    if args.query: flags["query"] = args.query
+    if args.ident: flags["ident"] = args.ident
+    if args.platform: flags["platform"] = args.platform
+    if args.domain: flags["domain"] = args.domain
+    if args.sortedby: flags["sortedby"] = args.sortedby
+    if args.page: flags["page"] = args.page
+    if args.perpage: flags["perpage"] = args.perpage
+    if args.offset: flags["offset"] = args.offset
+    if args.range:  flags["range"] = args.range
+    if args.recitation: flags["recitation"] = args.recitation
+    if args.translation: flags["translation"] = args.translation
+    if args.highlight: flags["highlight"] = args.highlight
+    if args.script: flags["script"] = args.script
+    if args.vocalized: flags["vocalized"] = TRUE_FALSE( args.vocalized )
+    if args.prev_aya: flags["prev_aya"] = TRUE_FALSE( args.prev_aya )
+    if args.next_aya: flags["next_aya"] = TRUE_FALSE( args.next_aya )
+    if args.sura_info: flags["sura_info"] = TRUE_FALSE( args.sura_info )
+    if args.word_info: flags["word_info"] = TRUE_FALSE( args.word_info )
+    if args.aya_position_info: flags["aya_position_info"] = TRUE_FALSE( args.aya_position_info )
+    if args.aya_theme_info: flags["aya_theme_info"] = TRUE_FALSE( args.aya_theme_info )
+    if args.aya_stat_info: flags["aya_stat_info"] = TRUE_FALSE( args.aya_stat_info )
+    if args.aya_sajda_info: flags["aya_sajda_info"] = TRUE_FALSE( args.aya_sajda_info )
+    if args.annotation_aya: flags["annotation_aya"] = TRUE_FALSE( args.annotation_aya )
+    if args.annotation_word: flags["annotation_word"] = TRUE_FALSE( args.annotation_word )
+    if args.fuzzy: flags["fuzzy"] = TRUE_FALSE( args.fuzzy )
+
+    print json.dumps( RAWoutput.do( flags ) )
+else:
+    print RAWoutput._information["console_note"]
+
+