Beispiel #1
0
    def test_tostring(self):
        nrml = etree.Element('nrml',
                             {'xmlns': 'http://openquake.org/xmlns/nrml/0.4'})
        em = etree.SubElement(
            nrml, 'exposureModel', {
                'id': "my_exposure_model_for_population",
                'category': "population",
                'taxonomySource': "fake population datasource"
            })

        descr = etree.SubElement(em, 'description')
        descr.text = 'Sample population'
        etree.SubElement(em, 'assets')
        self.assertEqual(
            tostring(nrml), b'''\
<nrml
xmlns="http://openquake.org/xmlns/nrml/0.4"
>
    <exposureModel
    category="population"
    id="my_exposure_model_for_population"
    taxonomySource="fake population datasource"
    >
        <description>
            Sample population
        </description>
        <assets />
    </exposureModel>
</nrml>
''')
    def test_tostring(self):
        nrml = etree.Element(
            'nrml', {'xmlns': 'http://openquake.org/xmlns/nrml/0.4'})
        em = etree.SubElement(
            nrml, 'exposureModel',
            {'id': "my_exposure_model_for_population",
             'category': "population",
             'taxonomySource': "fake population datasource"})

        descr = etree.SubElement(em, 'description')
        descr.text = 'Sample population'
        etree.SubElement(em, 'assets')
        self.assertEqual(tostring(nrml), b'''\
<nrml
xmlns="http://openquake.org/xmlns/nrml/0.4"
>
    <exposureModel
    category="population"
    id="my_exposure_model_for_population"
    taxonomySource="fake population datasource"
    >
        <description>
            Sample population
        </description>
        <assets />
    </exposureModel>
</nrml>
''')
Beispiel #3
0
def assert_xml_equal(a, b):
    """
    Compare two XML artifacts for equality.

    :param a, b:
        Paths to XML files, or a file-like object containing the XML
        contents.
    """
    path_a = get_path(a)
    path_b = get_path(b)
    content_a = tostring(parse(a).getroot(), nsmap=PARSE_NS_MAP)
    content_b = tostring(parse(b).getroot(), nsmap=PARSE_NS_MAP)
    if content_a != content_b:
        # uncomment this to see the differences
        # import warnings; warnings.warn(str(content_a), stacklevel=2)
        raise AssertionError('The files %s and %s are different!' %
                             (path_a, path_b))
Beispiel #4
0
def assert_xml_equal(a, b):
    """
    Compare two XML artifacts for equality.

    :param a, b:
        Paths to XML files, or a file-like object containing the XML
        contents.
    """
    path_a = get_path(a)
    path_b = get_path(b)
    content_a = tostring(parse(a).getroot(), nsmap=PARSE_NS_MAP)
    content_b = tostring(parse(b).getroot(), nsmap=PARSE_NS_MAP)
    if content_a != content_b:
        # uncomment this to see the differences
        # import warnings; warnings.warn(str(content_a), stacklevel=2)
        raise AssertionError('The files %s and %s are different!' %
                             (path_a, path_b))
Beispiel #5
0
        _handle_occupancy(anode, cursor, asset)
        _handle_tags(anode, cursor, asset)
    return nrml


def exposure_to_nrml(model_id):
    """
    Return a NRML XML tree for the exposure model with the specified id
    """
    with connections['geddb'].cursor() as cursor:
        cursor.execute(MODEL_QUERY, [model_id])
        model_dict = dictfetchone(cursor)
        if model_dict is None:
            return None
        return _build_tree(model_id, model_dict, cursor)


if __name__ == '__main__':
    if len(sys.argv) == 1:
        sys.stderr.write('Usage {0} <exposure model id>\n'.format(sys.argv[0]))
        exit(1)

    for xmodel_id in sys.argv[1:]:
        xnrml = exposure_to_nrml(xmodel_id)
        if xnrml is None:
            exit('Exposure model {0} not found'.format(xmodel_id))

        verbose_message("Exporting {0}\n".format(xmodel_id))
        sys.stdout.write('<?xml version="1.0" encoding="UTF-8"?>\n')
        sys.stdout.write(tostring(xnrml))