tool. This is a number between 0(quiet) and 3(extremely verbose). This is manly for debugging purposes. --format=<format>, determines the output format for the dataset. Currently the tool supports only DSPL (Dataset publishing Language). arguments: filename, the filename to write the output to. """ % { 'regions': "\n ".join ([regions_dict.get_name(r[0]) for r in regions_dict.all()]), 'pollutants': "\n ".join ([ "%(formula)s (%(name)s)" % { 'formula': pollutants_dict.get_formula(p[0]), 'name': pollutants_dict.get_name(p[0]), } for p in pollutants_dict.all() ]), } class OptionsManager(object): """Provides options management. """ long_options = [ "help", "keep", "local", "verbosity=", "from=", "to=",
def __call__(self, args): opts, args = getopt.getopt(args, "", self.long_options) for o, a in opts: if o == "--year": year = int(a) if year < DEFAULT_FROM_YEAR: raise getopt.GetoptError( "No data available before %d" % DEFAULT_FROM_YEAR) self.from_year = year logger.debug("Setting starting year to %d", year) self.to_year = year logger.debug("Setting ending year to %d", year) elif o == "--from": from_year = int(a) if from_year < DEFAULT_FROM_YEAR: raise getopt.GetoptError( "No data available before %d" % DEFAULT_FROM_YEAR) self.from_year = from_year logger.debug("Setting starting year to %d", from_year) elif o == "--to": to_year = int(a) if DEFAULT_TO_YEAR < to_year: raise getopt.GetoptError( "No data available after %d" % DEFAULT_TO_YEAR) self.to_year = to_year logger.debug("Setting ending year to %d", to_year) elif o == "--region": region_name = regions_dict.get_name(a) region_code = regions_dict.get_pk(a) self.regions.append(region_code) logger.debug("Adding region '%s'", region_name) elif o == '--pollutant': pollutant_formula = pollutants_dict.get_formula(a) pollutant_name = pollutants_dict.get_name(a) pollutant_code = pollutants_dict.get_pk(a) self.pollutants.append(pollutant_code) logger.debug("Adding pollutant '%s' (%s)", pollutant_formula, pollutant_name) elif o == "--verbosity": level = int(a) self.verbosity = level if level == 0: logger.setLevel(logging.ERROR) elif level == 1: logger.setLevel(logging.WARNING) elif level == 2: logger.setLevel(logging.INFO) elif level == 3: logger.setLevel(logging.DEBUG) else: assert False, "Unsupported verbosity level" logger.debug("Setting verbosity level to %s", ["ERROR", "WARNING", "INFO", "DEBUG"][level]) elif o == "--keep": self.keep = True if self.local: raise getopt.GetoptError( "--local and --keep are not supported together") elif o == "--local": self.local = True if self.keep: raise getopt.GetoptError( "--local and --keep are not supported together") elif o == "--help": print usage sys.exit() else: assert False, "unhandled option"
p[0] for p in pollutants_dict.all()] if not opts_mgr.regions: opts_mgr.regions = [ r[0] for r in regions_dict.all()] # main body if __name__ == "__main__": data_mgr = DataManager() # Phase 1. Fetch data total_rows = 0 for pollutant_code in opts_mgr.pollutants: pollutant_formula = pollutants_dict.get_formula(pollutant_code) pollutant_name = pollutants_dict.get_name(pollutant_code) for region_code in opts_mgr.regions: region_name = regions_dict.get_name(region_code) for year in range(opts_mgr.from_year, 1 + opts_mgr.to_year): if not opts_mgr.local: # fetch remote archive logger.info( "Trying to fetch data for year %d, pollutant '%s' (%s), " "region '%s'", year, pollutant_formula, pollutant_name, region_name) archive = query(region_code, pollutant_code, year)