def load_context(input_dir, build_time): """ Read the input data, and return the context to use for Jinja2. Args: input_dir: the directory containing the input data, as a Path object. build_time: a datetime object representing the current build time, e.g. datetime.datetime.now(). Returns a dict with keys: areas_by_id: build_time: election: languages: result_stat_types_by_id: result_styles_by_id: translations: voting_groups_by_id: """ context = dict(build_time=build_time) path = input_dir / 'election.json' data = utils.read_json(path) cls_info = dict(context=context) root_loader = RootLoader(input_dir=input_dir) # This load_object() call returns a ModelRoot object, but we don't need # or use that object. Instead, the context is the entry way we provide # for access to the election data from the top level. load_object(root_loader, data, cls_info=cls_info, context=context) return context
def main(): try: path = sys.argv[1] except IndexError: raise RuntimeError('path not provided') data = utils.read_json(path) serialized = json.dumps(data, **DEFAULT_JSON_DUMPS_ARGS) print(serialized)
def load_input(data, path): """ Data Loader: The data loader will read arbitrary json or yaml formatted data from the named file and update/replace data to be processed with templates. The parsed content must be a dictionary (set of named values). """ path = Path(path) suffix = path.suffix if suffix == '.json': newdata = utils.read_json(path) else: raise RuntimeError(f'unsupported suffix {suffix!r} for input path: {path}') if not isinstance(newdata, dict): _log.error(f'Invalid data in file {path}'); return data.update(newdata) _log.info(f'loaded data from {path}')
def load_contest_status(election): """ Load contest results statuses from the contest status file. Args: election: an Election object. """ input_dir = election.input_dir path = input_dir / CONTEST_STATUS_PATH contests_data = utils.read_json(path) process_attrs = dict(reporting_time=parse_date_time, total_precincts=parse_int, precincts_reporting=parse_int, rcv_rounds=parse_int) _set_attributes(contests_data, objects_by_id=election.contests_by_id, id_key='_id', process_attrs=process_attrs)
def load_context(input_dir, build_time): """ Read the input data, and return the context to use for Jinja2. Args: input_dir: the directory containing the input data, as a Path object. build_time: a datetime object representing the current build time, e.g. datetime.datetime.now(). Returns a dict with keys: areas_by_id: build_time: election: languages: result_stat_types_by_id: result_styles_by_id: translations: voting_groups_by_id: """ context = dict(build_time=build_time) path = input_dir / 'election.json' data = utils.read_json(path) # Inject the Election and Contest data loader methods defined # in this module. setattr(datamodel.Contest, 'load_results_details', load_results_details) setattr(datamodel.Election, 'load_all_results_details', load_all_results_details) setattr(datamodel.Election, 'load_contest_status', load_contest_status) cls_info = dict(context=context) root_loader = RootLoader(input_dir=input_dir) load_object(root_loader, data, cls_info=cls_info, context=context) return context
def load_results(election): """ Load contest results statuses from the results file. Args: election: an Election object. """ election._results_loaded = True input_results_dir = election.input_results_dir path = input_results_dir / RESULTS_FILE_NAME if not path.exists(): election.have_results = False return results_data = utils.read_json(path) if isinstance(results_data, list): # Convert from results_format 0.1 contests_data = results_data results_data = dict( _results_format="0.1", turnout=contests_data.pop(0), contests=contests_data, ) # TODO: this should be converted to the object model style. process_election_attrs = dict( _reporting_time=parse_date_time, _results_id=parse_as_is, _results_title=parse_i18n, ) process_contest_attrs = dict( reporting_time=parse_date_time, result_stats=process_contest_result_stats, choices=process_choice_results, total_precincts=parse_int, precincts_reporting=parse_int, rcv_rounds=parse_int, no_voter_precincts=parse_as_is, success=parse_bool, ) _set_obj_attributes(results_data, process_election_attrs, election, True) data = results_data.get('turnout', None) if data: _set_obj_attributes(data, process_contest_attrs, election.turnout) election.have_results = True for data in results_data.get("contests", []): _id = data.get('_id', '') if _id == 'TURNOUT': # Reset results array _set_obj_attributes(data, process_contest_attrs, election.turnout) else: _set_attributes_by_id(data, process_attrs=process_contest_attrs, objects_by_id=election.contests_by_id, id_key='_id') contest = election.contests_by_id[_id]