1
2
3 """
4 Function shared between the scripts.
5
6 """
7
8 __docformat__ = 'restructuredtext en'
9
10
11
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
30
31
32
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
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
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
82
83
84
85 if __name__ == '__main__':
86 main()
87
88
89
90