def check_changes(config, changes_path, elections_path, ids_path): ''' Checks that the configuration set ''' # get changes and tree data changes = get_changes_config(changes_path) tree = get_changes_tree(changes) node_changes = get_node_changes(tree, changes) election_ids = get_list(tree, list_all) parse_parity_config(config) # get all the ids other_ids = [] if ids_path is not None: with open(ids_path, mode='r', encoding="utf-8", errors='strict') as f: for line in f: line = line.strip() other_ids.append(line) # add to the tree the id as a root leave if not present if line not in election_ids: tree[line] = dict() else: print("WARNING: ids_path not supplied") # join both list of ids, removing duplicates, and sorting the list election_ids = list(set(election_ids + other_ids)) election_ids.sort() # check first global_prechanges for election_id in election_ids: election_config = get_election_config(elections_path, election_id) for change, kwargs in config['global_prechanges']: check_change_applied(change, kwargs, election_id, election_config) # check the remaining changes for election_id in election_ids: election_changes, ancestors = get_changes_chain_for_election_id( election_id, config, tree, node_changes) calculated_election = apply_elections_changes( config, elections_path, ancestors, election_id, election_changes) check_diff_changes(elections_path, election_id, calculated_election) # check that all final elections have been tagged with the sex if "agora_results_config_parity" in config: cfg = config["agora_results_config_parity"] names = dict([ (i['answer_text'], i['is_woman']) for i in cfg['parity_list'] if int(i['election_id']) == int(election_id)]) if len(names) == 0: print("election %d not in the parity list") continue for question in calculated_election['questions']: for answer in question['answers']: if answer['text'] not in names: print("Answer '%s' from election '%d' not in parity list" % ( answer['text'], int(election_id)))
def download_elections(config, changes_path, elections_path, ids_path): ''' Download the configuration for the elections listed in the changes ''' changes = get_changes_config(changes_path) tree = get_changes_tree(changes) election_ids = get_list(tree, list_all) other_ids = [] if ids_path is not None: with open(ids_path, mode='r', encoding="utf-8", errors='strict') as f: for line in f: line = line.strip() other_ids.append(line) else: print("WARNING: ids_path not supplied") # join both list of ids, removing duplicates election_ids = list(set(election_ids + other_ids)) # sort list, just to be ordered (not really needed) election_ids.sort() headers = {'content-type': 'application/json'} base_url = config['agora_elections_base_url'] # retrieve and save each election config for election_id in election_ids: url = '%s/election/%s' % (base_url, election_id.strip()) r = requests.get(url, headers=headers) if r.status_code != 200: print(r.status_code, r.text) raise Exception('Invalid status code: %d for election_id = %s' % (r.status_code, election_id)) epath = os.path.join(elections_path, "%s.config.json" % election_id) with open(epath, mode='w', encoding="utf-8", errors='strict') as f: f.write(r.text)
def write_agora_results_files(config, changes_path, elections_path, ids_path): ''' Checks that the configuration set ''' # get changes and tree data changes = get_changes_config(changes_path) tree = get_changes_tree(changes) node_changes = get_node_changes(tree, changes) election_ids = get_list(tree, list_all) final_ids = get_list(tree, list_leaves) parse_parity_config(config) # get all the ids other_ids = [] if ids_path is not None: with open(ids_path, mode='r', encoding="utf-8", errors='strict') as f: for line in f: line = line.strip() other_ids.append(line) # add to the tree the id as a root leave if not present if line not in election_ids: tree[line] = dict() else: print("WARNING: ids_path not supplied") # join both list of ids, removing duplicates, and sorting the list final_ids = list(set(final_ids + other_ids)) final_ids.sort() election_ids = list(set(election_ids + other_ids)) election_ids.sort() # calculate a configuration for each election, including middle steps, so # that later on the answers between election can be tracked and collected # by hash hashed_election_configs = dict() for election_id in election_ids: election_changes, ancestors = get_changes_chain_for_election_id( election_id, config, tree, node_changes) election_config = get_election_config(elections_path, ancestors[0]) apply_changes_hashing( election_id, election_config, election_changes, ancestors, elections_path) hashed_election_configs[election_id] = dict( config=election_config, ancestors=ancestors ) # iterate and process the results config for all final elections for election_id in final_ids: election_config = hashed_election_configs[election_id]['config'] ancestors = hashed_election_configs[election_id]['ancestors'] results_config = copy.deepcopy(config['agora_results_config']) # apply first global_prechanges for change, kwargs in config['global_prechanges']: apply_results_config_prechanges( change, kwargs, ancestors, election_id, election_config, results_config) post_process_results_config( config, elections_path, ancestors, election_id, results_config, election_config, hashed_election_configs) epath = os.path.join(elections_path, "%s.config.results.json" % election_id) with open(epath, mode='w', encoding="utf-8", errors='strict') as f: f.write(serialize(results_config))