def state_file(state, format='csv', outputdir=None, datefilter=None, election_type=None, level=None): """ Writes election and candidate data, along with a manifest to structured files. Arguments: * state: Required. Postal code for a state. For example, "md". * format: Format of output files. This can be "csv" or "json". Defaults to "csv". * outputdir: Directory where output files will be written. Defaults to "openelections/us/bakery" * datefilter: Date specified in "YYYY" or "YYYY-MM-DD" used to filter elections before they are baked. * election_type: Election type. For example, general, primary, etc. * level: Reporting level of the election results. For example, "state", "county", "precinct", etc. Value must be one of the options specified in openelex.models.Result.REPORTING_LEVEL_CHOICES. """ # TODO: Decide if datefilter should be required due to performance # considerations. # TODO: Implement filtering by office, district and party after the # the data is standardized # TODO: Filtering by election type and level timestamp = datetime.now() filter_kwargs = {} if election_type: filter_kwargs['election_type'] = election_type if level: filter_kwargs['reporting_level'] = level baker = Baker(state=state, datefilter=datefilter, **filter_kwargs) baker.collect_items() \ .write(format, outputdir=outputdir, timestamp=timestamp) \ .write_manifest(outputdir=outputdir, timestamp=timestamp)
def test_default_outputdir(self): baker = Baker(state='md') path = os.path.join(os.path.join('openelex', 'us', 'bakery')) outputdir = baker.default_outputdir() self.assertTrue(outputdir.endswith(path))
def test_manifest_filename(self): baker = Baker(state='md') ts = datetime(2014, 2, 11, 10, 56, 15) filename = baker.manifest_filename(timestamp=ts, state='md') self.assertEqual(filename, 'md_20140211T105615_manifest.txt')
def test_filename(self): baker = Baker(state='md') ts = datetime(2014, 2, 11, 10, 56, 15) fmt = 'json' filename = baker.filename(fmt=fmt, timestamp=ts, state='md') self.assertEqual(filename, 'md_20140211T105615.json')
def test_write_unsupported_format(self): baker = Baker(state='md') self.assertRaises(UnsupportedFormatError, baker.write, 'xml')