3 from optparse import OptionParser, IndentedHelpFormatter
4 from ConfigParser import SafeConfigParser
11 __docformat__ = "restructredtext en"
13 CONFIG_SYSTEM = '/etc/hts_frontend/hts_frontend.conf'
14 CONFIG_USER = os.path.expanduser('~/.hts_frontend.conf')
16 #Disable or enable commandline arg parsing; disabled by default.
17 DISABLE_CMDLINE = True
19 class FlowCellNotFound(Exception): pass
20 class WebError404(Exception): pass
24 Used when command line parsing is disabled; default
28 self.output_filepath = None
30 self.genome_dir = None
32 class PreformattedDescriptionFormatter(IndentedHelpFormatter):
34 #def format_description(self, description):
37 # return description + "\n"
41 def format_epilog(self, epilog):
43 It was removing my preformated epilog, so this should override
44 that behavior! Muhahaha!
47 return "\n" + epilog + "\n"
52 def constructOptionParser():
54 returns a pre-setup optparser
56 global DISABLE_CMDLINE
61 parser = OptionParser(formatter=PreformattedDescriptionFormatter())
63 parser.set_description('Retrieves eland config file from hts_frontend web frontend.')
68 * %s (User specific; overrides system)
69 * command line overrides all config file options
74 base_host_url=http://somewhere.domain:port
75 """ % (CONFIG_SYSTEM, CONFIG_USER)
77 #Special formatter for allowing preformatted description.
78 ##parser.format_epilog(PreformattedDescriptionFormatter())
80 parser.add_option("-u", "--url",
81 action="store", type="string", dest="url")
83 parser.add_option("-o", "--output",
84 action="store", type="string", dest="output_filepath")
86 parser.add_option("-f", "--flowcell",
87 action="store", type="string", dest="flowcell")
89 parser.add_option("-g", "--genome_dir",
90 action="store", type="string", dest="genome_dir")
92 #parser.set_default("url", "default")
96 def constructConfigParser():
98 returns a pre-setup config parser
100 parser = SafeConfigParser()
101 parser.read([CONFIG_SYSTEM, CONFIG_USER])
102 if not parser.has_section('config_file_server'):
103 parser.add_section('config_file_server')
104 if not parser.has_section('local_setup'):
105 parser.add_section('local_setup')
110 def getCombinedOptions():
112 Returns optparse options after it has be updated with ConfigParser
113 config files and merged with parsed commandline options.
115 cl_parser = constructOptionParser()
116 conf_parser = constructConfigParser()
118 if cl_parser is None:
119 options = DummyOptions()
121 options, args = cl_parser.parse_args()
123 if options.url is None:
124 if conf_parser.has_option('config_file_server', 'base_host_url'):
125 options.url = conf_parser.get('config_file_server', 'base_host_url')
127 if options.genome_dir is None:
128 if conf_parser.has_option('local_setup', 'genome_dir'):
129 options.genome_dir = conf_parser.get('local_setup', 'genome_dir')
131 print 'USING OPTIONS:'
132 print ' URL:', options.url
133 print ' OUT:', options.output_filepath
134 print ' FC:', options.flowcell
135 print 'GDIR:', options.genome_dir
141 def saveConfigFile(flowcell, base_host_url, output_filepath):
143 retrieves the flowcell eland config file, give the base_host_url
144 (i.e. http://sub.domain.edu:port)
146 url = base_host_url + '/eland_config/%s/' % (flowcell)
148 f = open(output_filepath, 'w')
151 web = urllib2.urlopen(url)
152 except urllib2.URLError, e:
153 errmsg = 'URLError: %s' % (e.reason,)
154 logging.error(errmsg)
155 logging.error('opened %s' % (url,))
156 raise IOError(errmsg)
158 #except IOError, msg:
159 # if str(msg).find("Connection refused") >= 0:
160 # print 'Error: Connection refused for: %s' % (url)
163 # elif str(msg).find("Name or service not known") >= 0:
164 # print 'Error: Invalid domain or ip address for: %s' % (url)
172 if data.find('Hmm, config file for') >= 0:
173 msg = "Flowcell (%s) not found in DB; full url(%s)" % (flowcell, url)
174 raise FlowCellNotFound, msg
176 if data.find('404 - Not Found') >= 0:
177 msg = "404 - Not Found: Flowcell (%s); base_host_url (%s);\n full url(%s)\n " \
178 "Did you get right port #?" % (flowcell, base_host_url, url)
179 raise FlowCellNotFound, msg
184 logging.info('Wrote config file to %s' % (output_filepath,))