def make_igt_metadata(block, options): md = Metadata() mi = 1 if options['keep_headers'] and block.get('header_lines'): md.append( Meta(id='meta{}'.format(mi), type='odin-header', text='\n'.join(block.get('header_lines', [])))) mi += 1 lg_code = block.get('iso-639-3') if lg_code is None: lg_code = 'und' # undetermined # should we title-case language? # language = (block.get('language') or '').strip().title() language = (block.get('language') or '').strip() subj = MetaChild('subject', attributes={ qattrname('type', 'xsi'): 'olac:language', qattrname('code', 'olac'): lg_code.strip() }, text=language.title(), namespace=_nsmap['dc']) lang = MetaChild('language', attributes={ qattrname('type', 'xsi'): 'olac:language', qattrname('code', 'olac'): 'en' }, text='English', namespace=_nsmap['dc']) md.append(Meta(id='meta{}'.format(mi), children=[subj, lang])) return md
class TestMeta(unittest.TestCase): def setUp(self): self.m1 = Meta() self.m2 = Meta( id='meta1', type='metatype', attributes={'one': 1}, text='metatext', children=[MetaChild('child1'), MetaChild('child2')] ) def test_init(self): self.assertRaises(ValueError, Meta, id='1') # invalid id def test_id(self): self.assertIs(self.m1.id, None) self.assertEqual(self.m2.id, 'meta1') def test_type(self): self.assertIs(self.m1.type, None) self.assertEqual(self.m2.type, 'metatype') def test_attributes(self): self.assertEqual(self.m1.attributes, dict()) self.assertEqual(self.m2.attributes, {'one': 1}) def test_get_attribute(self): self.assertIs(self.m1.get_attribute('attr'), None) self.assertEqual(self.m1.get_attribute('attr', 1), 1) self.assertEqual(self.m2.get_attribute('one'), 1) self.assertIs(self.m2.get_attribute('two'), None) m = Meta(attributes={'one': 1}) md = Metadata( attributes={'two': 2}, metas=[m] ) self.assertEqual(m.get_attribute('two', inherit=True), 2) def test_text(self): self.assertIs(self.m1.text, None) self.assertEqual(self.m2.text, 'metatext') def test_children(self): self.assertEqual(self.m1.children, []) self.assertEqual(len(self.m2.children), 2) self.assertEqual(self.m2.children[0].name, 'child1') self.assertEqual(self.m2.children[1].name, 'child2')
def setUp(self): self.m1 = Meta() self.m2 = Meta( id='meta1', type='metatype', attributes={'one': 1}, text='metatext', children=[MetaChild('child1'), MetaChild('child2')] )
def test_get_attribute(self): self.assertIs(self.m1.get_attribute('attr'), None) self.assertEqual(self.m1.get_attribute('attr', 1), 1) self.assertEqual(self.m2.get_attribute('one'), 1) self.assertIs(self.m2.get_attribute('two'), None) m = Meta(attributes={'one': 1}) md = Metadata( attributes={'two': 2}, metas=[m] ) self.assertEqual(m.get_attribute('two', inherit=True), 2)
def test_get_attribute(self): assert self.m1.get_attribute('attr') is None assert self.m1.get_attribute('attr', 1) == 1 assert self.m2.get_attribute('one') == 1 assert self.m2.get_attribute('two') is None m = Meta(attributes={'one': 1}) md = Metadata( attributes={'two': 2}, metas=[m] ) assert m.get_attribute('two', inherit=True) == 2
def default_decode_meta(elem): # a meta can simply have text, which is the easy case, or it can # have nested XML. In the latter case, store the XML in very basic # MetaChild objects ns, tag = _qname_split(elem.tag) assert tag == 'meta' text = elem.text or '' meta = Meta(id=elem.get('id'), type=elem.get('type'), attributes=get_attributes(elem, ignore=('id', 'type')), text=text if text.strip() else None, children=[decode_metachild(mc) for mc in elem], namespace=ns, nsmap=elem.attrib.nsmap) elem.clear() return meta