Exemplo n.º 1
0
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
Exemplo n.º 2
0
    def setUp(self):
        self.mc1 = MetaChild('childname')

        self.mc2 = MetaChild(
            'childname',
            attributes={'id': 'mc2', 'type': 'childtype', 'one': 1},
            text='childtext',
            children=[MetaChild('grandchild1'), MetaChild('grandchild2')]
        )
Exemplo n.º 3
0
    def test_get_attribute(self):
        self.assertIs(self.mc1.get_attribute('id'), None)
        self.assertIs(self.mc1.get_attribute('attr'), None)
        self.assertEqual(self.mc1.get_attribute('attr', 1), 1)

        self.assertEqual(self.mc2.get_attribute('id'), 'mc2')
        self.assertEqual(self.mc2.get_attribute('type'), 'childtype')
        self.assertEqual(self.mc2.get_attribute('one'), 1)
        self.assertIs(self.mc2.get_attribute('two'), None)

        mc = MetaChild('childname', attributes={'one': 1})
        m = Meta(children=[mc])
        md = Metadata(
            attributes={'two': 2},
            metas=[m]
        )
        self.assertEqual(mc.get_attribute('two', inherit=True), 2)
Exemplo n.º 4
0
    def test_get_attribute(self):
        assert self.mc1.get_attribute('id') is None
        assert self.mc1.get_attribute('attr') is None
        assert self.mc1.get_attribute('attr', 1) == 1

        assert self.mc2.get_attribute('id') == 'mc2'
        assert self.mc2.get_attribute('type') == 'childtype'
        assert self.mc2.get_attribute('one') == 1
        assert self.mc2.get_attribute('two') is None

        mc = MetaChild('childname', attributes={'one': 1})
        m = Meta(children=[mc])
        md = Metadata(
            attributes={'two': 2},
            metas=[m]
        )
        assert mc.get_attribute('two', inherit=True) == 2
Exemplo n.º 5
0
def default_decode_metachild(elem):
    ns, tag = _qname_split(elem.tag)
    text = elem.text or ''
    mc = MetaChild(tag,
                   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 mc
Exemplo n.º 6
0
class TestMetaChild(unittest.TestCase):

    def setUp(self):
        self.mc1 = MetaChild('childname')

        self.mc2 = MetaChild(
            'childname',
            attributes={'id': 'mc2', 'type': 'childtype', 'one': 1},
            text='childtext',
            children=[MetaChild('grandchild1'), MetaChild('grandchild2')]
        )

    def test_init(self):
        # name (i.e. tag in XML) is mandatory
        self.assertRaises(TypeError, MetaChild)
        # invalid names
        self.assertRaises(ValueError, MetaChild, '1')
        self.assertRaises(ValueError, MetaChild, 'a:1')
        # id and type not allowed as parameters (they can be attributes)
        self.assertRaises(TypeError, MetaChild, 'mc0', id='mc1')
        self.assertRaises(TypeError, MetaChild, 'mc0', type='childtype')

    def test_name(self):
        self.assertEqual(self.mc1.name, 'childname')

        self.assertEqual(self.mc2.name, 'childname')

    def test_attributes(self):
        self.assertEqual(self.mc1.attributes, dict())

        self.assertEqual(self.mc2.attributes,
                         {'id': 'mc2', 'type': 'childtype', 'one': 1})

    def test_get_attribute(self):
        self.assertIs(self.mc1.get_attribute('id'), None)
        self.assertIs(self.mc1.get_attribute('attr'), None)
        self.assertEqual(self.mc1.get_attribute('attr', 1), 1)

        self.assertEqual(self.mc2.get_attribute('id'), 'mc2')
        self.assertEqual(self.mc2.get_attribute('type'), 'childtype')
        self.assertEqual(self.mc2.get_attribute('one'), 1)
        self.assertIs(self.mc2.get_attribute('two'), None)

        mc = MetaChild('childname', attributes={'one': 1})
        m = Meta(children=[mc])
        md = Metadata(
            attributes={'two': 2},
            metas=[m]
        )
        self.assertEqual(mc.get_attribute('two', inherit=True), 2)

    def test_text(self):
        self.assertIs(self.mc1.text, None)

        self.assertEqual(self.mc2.text, 'childtext')

    def test_children(self):
        self.assertEqual(self.mc1.children, [])

        self.assertEqual(len(self.mc2.children), 2)
        self.assertEqual(self.mc2.children[0].name, 'grandchild1')
        self.assertEqual(self.mc2.children[1].name, 'grandchild2')