def _process_meta(self, elem): """ Process the <meta> element of an LGR XML file. """ metadata = Metadata(self.rfc7940_checks) reference_manager = ReferenceManager() MAPPER = { DATE_TAG: lambda d: metadata.set_date(d, force=self.force_mode), VALIDITY_START_TAG: lambda d: metadata.set_validity_start(d, force=self.force_mode), VALIDITY_END_TAG: lambda d: metadata.set_validity_end(d, force=self.force_mode), UNICODE_VERSION_TAG: lambda d: metadata.set_unicode_version(d, force=self.force_mode), } unicode_version_tag_found = False for child in elem: tag = child.tag logger.debug("Got '%s' element", tag) if tag in MAPPER: MAPPER[tag](child.text) if tag == UNICODE_VERSION_TAG: unicode_version_tag_found = True elif tag == VERSION_TAG: metadata.version = Version(child.text, child.get('comment', None)) elif tag == LANGUAGE_TAG: metadata.add_language(child.text, force=self.force_mode) elif tag == SCOPE_TAG: metadata.scopes.append( Scope(child.text, child.get('type', None))) elif tag == DESCRIPTION_TAG: # Seems to be an issue with CDATA/iterparse: https://bugs.launchpad.net/lxml/+bug/1788449 # For now, manually replace CRLF with LF metadata.description = Description( child.text.replace('\r\n', '\n'), child.get('type', None)) elif tag == REFERENCES_TAG: for reference in child: value = reference.text # Don't convert it to an int since ref_id may be a string ref_id = reference.get('id') comment = reference.get('comment', None) reference_manager.add_reference(value, comment=comment, ref_id=ref_id) # Since we have processed <reference> elements here, let's clean-up child.clear() else: logger.warning("Unhandled '%s' element in <meta> section", tag) self.rfc7940_checks.error('parse_xml') child.clear() self.rfc7940_checks.add_test_result('explicit_unicode_version', unicode_version_tag_found) self._lgr = LGR(name=self.filename, metadata=metadata, reference_manager=reference_manager, unicode_database=self._unicode_database)
def union_reference_manager(first, second): """ Union two reference managers. :param first: The first object to union. :param second: The other object to union. :return: A new reference manager object. """ first_ref = {r['value'] for r in first.values()} second_ref = {r['value'] for r in second.values()} output = ReferenceManager() for ref in set.union(first_ref, second_ref): output.add_reference(ref) return output
def setUp(self): self.manager = ReferenceManager()
class TestReferenceManager(unittest.TestCase): def setUp(self): self.manager = ReferenceManager() def test_add_reference(self): ref_id = self.manager.add_reference('The Unicode Standard 1.1', comment='A pretty old version') self.assertEqual('0', ref_id) self.assertIn('0', self.manager) reference = self.manager[ref_id] self.assertIsInstance(reference, dict) self.assertEqual('The Unicode Standard 1.1', reference['value']) self.assertEqual('A pretty old version', reference['comment']) def test_add_existing_reference(self): ref_id = self.manager.add_reference('The Unicode Standard 1.1', comment='A pretty old version') self.assertEqual(ref_id, '0') self.assertRaises(ReferenceAlreadyExists, self.manager.add_reference, 'The Unicode Standard 6.3', ref_id=ref_id) def test_add_existing_reference_other_type(self): ref_id = self.manager.add_reference('The Unicode Standard 1.1', comment='A pretty old version') self.assertEqual(ref_id, '0') self.assertRaises(ReferenceAlreadyExists, self.manager.add_reference, 'The Unicode Standard 6.3', ref_id=0) def test_del_existing_reference(self): ref_id = self.manager.add_reference('The Unicode Standard 1.1', comment='A pretty old version') self.manager.del_reference(ref_id) self.assertEqual(0, len(self.manager)) def test_del_inexisting_reference(self): ref_id = self.manager.add_reference('The Unicode Standard 1.1', comment='A pretty old version') self.assertEqual(ref_id, '0') self.assertRaises(ReferenceNotDefined, self.manager.del_reference, 1) self.assertRaises(ReferenceNotDefined, self.manager.del_reference, '1') def test_update_reference(self): ref_id = self.manager.add_reference('The Unicode Standard 1.1') self.manager.update_reference(ref_id, value='The Unicode Standard 1.2') self.assertEqual(self.manager[ref_id]['value'], 'The Unicode Standard 1.2') self.assertNotIn('comment', self.manager[ref_id]) self.manager.update_reference(ref_id, comment='A recent version') self.assertEqual(self.manager[ref_id]['value'], 'The Unicode Standard 1.2') self.assertEqual(self.manager[ref_id]['comment'], 'A recent version') def test_update_inexsting_reference(self): ref_id = self.manager.add_reference('The Unicode Standard 1.1', comment='A pretty old version') self.assertEqual(ref_id, '0') self.assertRaises(ReferenceNotDefined, self.manager.update_reference, 1, **{'value': 'The Unicode Standard 1.2'})