Package biblio :: Package webquery :: Package scripts :: Module common
[hide private]
[frames] | no frames]

Source Code for Module biblio.webquery.scripts.common

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  """ 
 4  Function shared between the scripts. 
 5   
 6  """ 
 7   
 8  __docformat__ = 'restructuredtext en' 
 9   
10   
11  ### IMPORTS ### 
12   
13  from biblio.webquery.basewebquery import * 
14  from config import * 
15   
16  try: 
17     from biblio.webquery import __version__ as script_version 
18  except: 
19     script_version = 'unknown' 
20   
21  __all__ = [ 
22     'script_version', 
23     'add_shared_options', 
24     'check_shared_options', 
25     'construct_webquery', 
26  ] 
27   
28   
29  ### CONSTANTS & DEFINES ### 
30   
31  ### IMPLEMENTATION ### 
32   
33 -def add_shared_options (optparser):
34 optparser.add_option ('--debug', 35 dest="debug", 36 action='store_true', 37 help='For errors, issue a full traceback instead of just a message.', 38 ) 39 40 optparser.add_option ('--service', '-s', 41 dest='webservice', 42 help="The webservice to query. Choices are %s. The default is %s." % ( 43 ', '.join (['%s (%s)' % (s['id'], s['title']) for s in WEBSERVICES]), 44 DEFAULT_WEBSERVICE['id'] 45 ) , 46 metavar='SERVICE', 47 choices=WEBSERVICE_LOOKUP.keys(), 48 default=DEFAULT_WEBSERVICE['id'], 49 ) 50 51 optparser.add_option ('--key', '-k', 52 dest="service_key", 53 help='''The access key for the webservice, if one is required.''', 54 metavar='KEY', 55 default=None, 56 )
57 58
59 -def check_shared_options (options, optparser):
60 serv = WEBSERVICE_LOOKUP.get (options.webservice, None) 61 if (not serv): 62 optparser.error ("Unrecognised webservice '%s'" % options.webservice) 63 if (issubclass (serv['ctor'], BaseKeyedWebQuery)): 64 if (not options.service_key): 65 optparser.error ("%s webservice requires access key" % serv['title']) 66 else: 67 if (options.service_key): 68 optparser.error ("%s webservice does not require access key" % 69 serv['title'])
70 71
72 -def construct_webquery (service, key):
73 serv_cls = WEBSERVICE_LOOKUP[service]['ctor'] 74 if (issubclass (serv_cls, BaseKeyedWebQuery)): 75 return serv_cls (key=key, timeout=5.0, limits=None) 76 else: 77 return serv_cls (timeout=5.0, limits=None)
78 79 80 81 ### TEST & DEBUG ### 82 83 ### MAIN ### 84 85 if __name__ == '__main__': 86 main() 87 88 89 ### END ###################################################################### 90