def load_data_from_dump(endos, dump, eligible_nations): """Build the endorsement graph using data from the data dump. Args: endos (networkx.DiGraph): Endorsement graph. dump (file object): Data dump file. eligible_nations (set): Used to confirm an endorsement is valid. """ is_in_region = False for evt, elem in ET.iterparse(dump): if elem.tag == 'NATION': nation = utils.canonical(elem.find('NAME').text) if nation in eligible_nations: is_in_region = True endos_text = elem.find('ENDORSEMENTS').text if endos_text is not None: endos_text = utils.canonical(endos_text) if endos_text is None: endos.add_node(nation) elif "," not in endos_text: add_endo(endos_text, nation, endos, eligible_nations) elif "," in endos_text: for endo in endos_text.split(","): add_endo(endo, nation, endos, eligible_nations) elif is_in_region: break elem.clear() logger.info('Loaded endorsement data from data dump')
def get_eligible_nations(dump, region_name): """Get eligible nations to confirm validity of endorsements. Read NationStates's data dump endorsement issue for the rationale behind this. Args: dump (file object): Data dump file. region_name (str): Region. Returns: set: Eligible nations. """ eligible_nations = set() for evt, elem in ET.iterparse(dump): if elem.tag == 'NATION': if (elem.find('REGION').text == region_name and elem.find('UNSTATUS').text.find('WA') != -1): nation_name = utils.canonical(elem.find('NAME').text) eligible_nations.add(nation_name) elem.clear() return eligible_nations
def load_data_from_api(events, endos, precision_mode=False): """Update the endorsement graph with data from the happenings API. Args: events (list): Relevant happenings. endos (networkx.DiGraph): Endorsement graph. """ for event in reversed(events): event_text = utils.canonical(event['TEXT'].replace('@@', '')[:-1]) try: if "endorsed" in event_text: endo = event_text.split(" endorsed ") endos.add_edge(endo[0], endo[1]) logger.debug('Added endorsement between "%s" and "%s"', endo[0], endo[1]) elif "withdrew its endorsement from" in event_text: endo = event_text.split(" withdrew its endorsement from ") endos.remove_edge(endo[0], endo[1]) logger.debug('Removed endorsement between "%s" and "%s"', endo[0], endo[1]) elif "was admitted to the world assembly" in event_text: nation = event_text.split( " was admitted to the world assembly")[0] endos.add_node(nation) logger.debug('Added nation "%s"', nation) except nx.NetworkXError as e: if precision_mode: raise exceptions.IllegalEndorsement logger.debug('Illegal endorsement on event "%s"', event_text) logger.info('Loaded endorsement data from NS API')
def get(self, config): """Automatically collect pin for private requests if password is provided.""" if 'password' in config['meguca']['auth']: ns_api = NSApi(config['meguca']['auth']['user_agent'], config['meguca']['auth']['password']) ns_api.get_nation( utils.canonical(config['meguca']['auth']['host_nation']), 'ping') else: ns_api = NSApi(config['meguca']['auth']['user_agent']) return ns_api
def run(self, ns_api): required_endorsed = self.plg_config['criteria'][ 'required_to_endorse_nations'] endorsees_list = [] for nation in required_endorsed: endorsees = set( ns_api.get_nation(utils.canonical(nation), 'endorsements')['ENDORSEMENTS'].split(',')) endorsees_list.append(endorsees) guardians = list(set.intersection(*endorsees_list)) result = {'guardians': guardians} logger.debug('Guardian stats generated: %r', result) return result
def test_canonicalize_on_pretty_nation_name(self): assert utils.canonical('Test_Topia') == 'test topia'