Commits

jaraco  committed 1bdd1a3

Refactored functionality for clarity.

  • Participants
  • Parent commits 999fbba

Comments (0)

Files changed (1)

File eodsfetch/eodsfetch.py

+#!/usr/bin/env python
+
+# $ Id: $
+
 import urllib2, sys
 from ClientForm import ParseResponse
 from optparse import OptionParser
 	log = logging.getLogger( 'eodsfetch' )
 	logging.basicConfig( level = getattr( logging, options.log_level.upper() ) )
 
-def main():
-	global options
-	options, args = getArgs()
-	setupLogging()
+def getLoginPage():
 	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor)
 	urllib2.install_opener( opener )
 	log.info( 'Retrieving %s for login', options.url )
 	except IndexError:
 		log.exception( 'Did not find any forms on the login page (%s)', options.url )
 		log.info( "Check for <br/> elements, which don't follow the spec (use <br />)." )
+		sys.exit(1)
 	log.debug( 'login form follows' )
 	log.debug( str( login_form ) )
+	return login_form
+
+def submitLoginPage( login_form ):
 	login_form['userName'] = options.username
 	login_form['password'] = options.password
 	log.debug( 'Submitting login form' )
 	urllib2.urlopen(login_form.click()).read()
 	# consider checking the response above for some confirmation value (string "Main Menu" ?)
+
+def runRetrievalCommand():
 	command = urljoin( options.url, options.retrieve_command )
 	log.info( 'Assuming login was successful, retrieving %s', command )
 	result = urllib2.urlopen( command ).read()
 		output = sys.stdout
 	output.write( result )
 
+def main():
+	global options
+	options, args = getArgs()
+	setupLogging()
+	login_form = getLoginPage()
+	submitLoginPage( login_form )
+	runRetrievalCommand()
+
 if __name__ == '__main__':
 	main()