CNX_ENV_VAR = "XCA_CNX" def connect(uri): """Create a connection to the database at the specified URI. Raises ConnectionError if any problems are encountered.""" try: sqlhub.processConnection = connectionForURI(uri) except KeyError: raise ConnectionError(uri + " is not a valid database URI.") except AssertionError, error: requested_driver = uri.split(":")[0] raise ConnectionError("No database driver exists for " + requested_driver) #Check that communication with the given database is possible try: Distances.get(1) except OperationalError, error: raise ConnectionError(error) except SQLObjectNotFound: pass def default_server(): """Get the default server defined in the environment variable XCA_CNX.""" try: return environment[CNX_ENV_VAR] except KeyError: return None def gender_callback(option, option_string, value, parser): """Process gender information.""" try:
index = count() try: connect(options.server) gender = arguments[next(index)] except IndexError: option_parser.error("") except ConnectionError, error: option_parser.error(error) if options.conference is not None: options.conference = Conferences.get(options.conference) options.filter = list(Schools.selectBy(conference=options.conference)) else: options.filter = option_parser.parse_schools(options.filter) if options.dist_limit is None: if gender == "M": options.dist_limit = Distances.get(1).mens_distance else: options.dist_limit = Distances.get(1).womens_distance return options, (gender,) @main_function(parse_arguments) def main(options, (gender,)): results = assemble_results(gender, options.dist_limit, options.filter, options.year, options.previous_years) results.score() if options.exclude_scoreless: results.purge_scoreless_runners() if options.show_races: for row in RaceDumper(results.races, gender): print row print
except SQLObjectNotFound, error: try: venue = int(venue) except ValueError: try: name, city, state = venue.split(", ") except ValueError, error: option_parser.error("Could not extract a venue from \"" + venue + "\": " + error) venue = Venues(name=name, city=city, state=state, elevation=None) else: option_parser.error(error) except ValueError, error: option_parser.error(error) if options.mens_distance is None: options.mens_distance = Distances.get(1).mens_distance if options.womens_distance is None: options.womens_distance = Distances.get(1).womens_distance return options, (race_name, date, venue) @main_function(parse_arguments) def main(options, (name, date, venue)): """Reads race results from a YAML file taken from stdin and saves them to the database based on the command-line arguments.""" try: results = load(stdin.read()) except KeyboardInterrupt: return 1 except ScannerError, error: print >> stderr, error return 1