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 'URL:', options.url
96 print 'OUT:', options.output_filepath
97 print ' FC:', options.flowcell
102 def saveConfigFile(flowcell, base_host_url, output_filepath):
104 retrieves the flowcell eland config file, give the base_host_url
105 (i.e. http://sub.domain.edu:port)
107 url = base_host_url + '/elandifier/config/%s/' % (flowcell)
109 f = open(output_filepath, 'w')
110 web = urllib.urlopen(url)
115 if __name__ == '__main__':
116 #Display help if no args are presented
117 if len(sys.argv) == 1:
118 sys.argv.append('-h')
120 options = getCombinedOptions()
121 msg_list = ['ERROR MESSAGES:']
122 if options.output_filepath is None:
123 msg_list.append(" Output filepath argument required. -o <filepath> or --output=<filepath>")
125 if options.flowcell is None:
126 msg_list.append(" Flow cell argument required. -f <flowcell> or --flowcell=<flowcell>")
128 if options.url is None:
129 msg_list.append(" URL argument required (-u <url> or --url=<url>), or entry\n" \
130 " in /etc/elandifier/elandifer.conf or ~/.elandifier.conf")
132 if len(msg_list) > 1:
133 print '\n'.join(msg_list)
136 saveConfigFile(options.flowcell, options.url, options.output_filepath)