Skip to content

Commit

Permalink
Generate real metadata records
Browse files Browse the repository at this point in the history
  • Loading branch information
jirik committed Dec 23, 2019
1 parent c626c1f commit 1b8dcff
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/layman/common/metadata/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ def get_diff(p1, p2):
return f"diff={''.join(diff)}"

expected_path = 'src/layman/common/metadata/util_test_filled_template.xml'
assert filecmp.cmp(xml_path, expected_path), get_diff(xml_path, expected_path)
assert filecmp.cmp(xml_path, expected_path, shallow=False), get_diff(xml_path, expected_path)


56 changes: 40 additions & 16 deletions src/layman/layer/filesystem/metadata.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from datetime import datetime
import os
import pathlib

from flask import url_for, current_app

from . import util
from . import input_file
from . import util, input_file
from layman.common.metadata.util import fill_template
from layman.common.filesystem.uuid import get_publication_uuid_file
from .uuid import get_layer_uuid
from layman.layer.geoserver.wms import get_wms_proxy
from layman.layer.geoserver.util import get_gs_proxy_base_url
from layman.layer import LAYER_TYPE
from urllib.parse import urljoin
from xml.sax.saxutils import escape, quoteattr


DIR = __name__.split('.')[-1]
Expand Down Expand Up @@ -66,30 +73,47 @@ def get_file(username, layername):


def create_file(username, layername):
template_values = _get_template_values(username=username, layername=layername)
wms = get_wms_proxy(username)
wms_layer = wms.contents[layername]
uuid_file_path = get_publication_uuid_file(LAYER_TYPE, username, layername)
publ_datetime = datetime.fromtimestamp(os.path.getmtime(uuid_file_path))

template_values = _get_template_values(
username=username,
layername=layername,
uuid=get_layer_uuid(username, layername),
title=wms_layer.title,
abstract=wms_layer.abstract or None,
date=publ_datetime.strftime('%Y-%m-%d'),
date_type='publication',
data_identifier=url_for('rest_layer.get', username=username, layername=layername, _external=True),
data_identifier_label=layername,
extent=wms_layer.boundingBoxWGS84,
ows_url=urljoin(get_gs_proxy_base_url(), username + '/ows')
)
template_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'metadata-template.xml')
file_object = fill_template(template_path, template_values)
save_file(username, layername, file_object)


def _get_template_values(
username='browser',
layername='layer',
uuid='ca238200-8200-1a23-9399-42c9fca53542',
epsg_codes=None,
title='CORINE - Krajinný pokryv CLC 90',
abstract=None,
date='2007-05-25',
date_type='revision',
data_identifier='http://www.env.cz/data/corine/1990',
data_identifier_label='MZP-CORINE',
abstract=None,
username='browser',
layername='layer',
extent=None,
extent=None, # w, s, e, n
ows_url="http://www.env.cz/corine/data/download.zip",
epsg_codes=None,
scale_denominator=None,
dataset_language=None,
):
epsg_codes = epsg_codes or ['3857', '4326']
extent = extent or [11.87, 19.13, 48.12, 51.59]
extent = extent or [11.87, 48.12, 19.13, 51.59]

result = {
###############################################################################################################
Expand Down Expand Up @@ -133,15 +157,15 @@ def _get_template_values(

# it must be URI, but text node is optional (MZP-CORINE)
# it can point to Layman's Layer endpoint
'data_identifier': f'<gmx:Anchor xlink:href="{data_identifier}">{data_identifier_label}</gmx:Anchor>',
'data_identifier': f'<gmx:Anchor xlink:href="{data_identifier}">{escape(data_identifier_label)}</gmx:Anchor>',

'abstract': '<gmd:abstract gco:nilReason="unknown" />' if abstract is None else f"""
<gmd:abstract>
<gco:CharacterString>{abstract}</gco:CharacterString>
<gco:CharacterString>{escape(abstract)}</gco:CharacterString>
</gmd:abstract>
""",

'graphic_url': url_for('rest_layer_thumbnail.get', username=username, layername=layername, _external=True),
'graphic_url': escape(url_for('rest_layer_thumbnail.get', username=username, layername=layername, _external=True)),

'extent': """
<gmd:EX_GeographicBoundingBox>
Expand All @@ -158,13 +182,13 @@ def _get_template_values(
<gco:Decimal>{}</gco:Decimal>
</gmd:northBoundLatitude>
</gmd:EX_GeographicBoundingBox>
""".format(*extent),
""".format(extent[0], extent[2], extent[1], extent[3]),

'wms_url': ows_url,
'wms_url': escape(ows_url),

'wfs_url': ows_url,
'wfs_url': escape(ows_url),

'layer_endpoint': url_for('rest_layer.get', username=username, layername=layername, _external=True),
'layer_endpoint': escape(url_for('rest_layer.get', username=username, layername=layername, _external=True)),


###############################################################################################################
Expand Down
17 changes: 17 additions & 0 deletions src/layman/layer/rest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import time
import xml.etree.ElementTree as ET
from urllib.parse import urljoin
import filecmp
import difflib

import pytest
from flask import url_for
Expand All @@ -20,6 +22,7 @@
from layman import uuid, util as layman_util
from layman.layer import db
from layman import celery as celery_util
from .filesystem import metadata


min_geojson = """
Expand Down Expand Up @@ -346,6 +349,20 @@ def test_post_layers_shp(client):
f'{LAYER_TYPE}': num_layers_before_test + 3
})

# assert metadata file is the same as filled template except for UUID
xml_path = metadata.get_file_path(username, layername)
expected_path = 'src/layman/layer/rest_test_filled_template.xml'
diff_lines = list(difflib.unified_diff(open(xml_path).readlines(), open(expected_path).readlines()))
assert len(diff_lines) == 11, ''.join(diff_lines)
plus_lines = [l for l in diff_lines if l.startswith('+ ')]
assert len(plus_lines) == 1
minus_lines = [l for l in diff_lines if l.startswith('- ')]
assert len(minus_lines) == 1
plus_line = plus_lines[0]
assert plus_line == '+ <gco:CharacterString>m81c0debe-b2ea-4829-9b16-581083b29907</gco:CharacterString>\n'
minus_line = minus_lines[0]
assert minus_line.startswith('- <gco:CharacterString>m') and minus_line.endswith('</gco:CharacterString>\n')


@pytest.mark.usefixtures('app_context')
def test_post_layers_layer_exists(client):
Expand Down
Loading

0 comments on commit 1b8dcff

Please sign in to comment.