3 from optparse import OptionParser, IndentedHelpFormatter
4 from ConfigParser import SafeConfigParser
10 CONFIG_SYSTEM = '/etc/ga_frontend/ga_frontend.conf'
11 CONFIG_USER = os.path.expanduser('~/.ga_frontend.conf')
13 class PreformattedDescriptionFormatter(IndentedHelpFormatter):
15 #def format_description(self, description):
18 # return description + "\n"
22 def format_epilog(self, epilog):
24 It was removing my preformated epilog, so this should override
25 that behavior! Muhahaha!
28 return "\n" + epilog + "\n"
33 def constructOptionParser():
35 returns a pre-setup optparser
37 parser = OptionParser(formatter=PreformattedDescriptionFormatter())
39 parser.set_description('Retrieves eland config file from ga_frontend web frontend.')
44 * %s (User specific; overrides system)
45 * command line overrides all config file options
50 base_host_url=http://somewhere.domain:port
51 """ % (CONFIG_SYSTEM, CONFIG_USER)
53 #Special formatter for allowing preformatted description.
54 ##parser.format_epilog(PreformattedDescriptionFormatter())
56 parser.add_option("-u", "--url",
57 action="store", type="string", dest="url")
59 parser.add_option("-o", "--output",
60 action="store", type="string", dest="output_filepath")
62 parser.add_option("-f", "--flowcell",
63 action="store", type="string", dest="flowcell")
65 #parser.set_default("url", "default")
69 def constructConfigParser():
71 returns a pre-setup config parser
73 parser = SafeConfigParser()
74 parser.read([CONFIG_SYSTEM, CONFIG_USER])
75 if not parser.has_section('server_info'):
76 parser.add_section('server_info')
81 def getCombinedOptions():
83 Returns optparse options after it has be updated with ConfigParser
84 config files and merged with parsed commandline options.
86 cl_parser = constructOptionParser()
87 conf_parser = constructConfigParser()
89 options, args = cl_parser.parse_args()
91 if options.url is None:
92 if conf_parser.has_option('server_info', 'base_host_url'):
93 options.url = conf_parser.get('server_info', 'base_host_url')
95 print 'USING OPTIONS:'
96 print ' URL:', options.url
97 print ' OUT:', options.output_filepath
98 print ' FC:', options.flowcell
104 def saveConfigFile(flowcell, base_host_url, output_filepath):
106 retrieves the flowcell eland config file, give the base_host_url
107 (i.e. http://sub.domain.edu:port)
109 url = base_host_url + '/eland_config/config/%s/' % (flowcell)
111 f = open(output_filepath, 'w')
113 web = urllib.urlopen(url)
115 if str(msg).find("Connection refused") >= 0:
116 print 'Error: Connection refused for: %s' % (url)
119 elif str(msg).find("Name or service not known") >= 0:
120 print 'Error: Invalid domain or ip address for: %s' % (url)
129 print 'Wrote config file to %s' % (output_filepath)
131 if __name__ == '__main__':
132 #Display help if no args are presented
133 if len(sys.argv) == 1:
134 sys.argv.append('-h')
136 options = getCombinedOptions()
137 msg_list = ['ERROR MESSAGES:']
138 if options.output_filepath is None:
139 msg_list.append(" Output filepath argument required. -o <filepath> or --output=<filepath>")
141 if options.flowcell is None:
142 msg_list.append(" Flow cell argument required. -f <flowcell> or --flowcell=<flowcell>")
144 if options.url is None:
145 msg_list.append(" URL argument required (-u <url> or --url=<url>), or entry\n" \
146 " in /etc/elandifier/elandifer.conf or ~/.elandifier.conf")
148 if len(msg_list) > 1:
149 print '\n'.join(msg_list)
152 saveConfigFile(options.flowcell, options.url, options.output_filepath)