-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSW harvester OutputSchema config support #258 #259
Open
ccancellieri
wants to merge
9
commits into
ckan:master
Choose a base branch
from
ccancellieri:csw-output-schema
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e2349d9
CSW harvester OutputSchema config support #258
ccancellieri 33d9b70
fix special character formatting
ccancellieri 855ab7d
preload config to read namespace if necessary. Generalize metadata na…
ccancellieri bf2d1d6
lets reload config at each run (the config can be different for each …
ccancellieri fbd5d1c
improve checks and comments
ccancellieri 9d36378
namespace from getcapabilities also for getrecords
ccancellieri 4fb17cc
documentation, pep8 and improve are requested
ccancellieri f91f516
fallback to default schema
ccancellieri b82eb89
fallback to default schema
ccancellieri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,14 +70,40 @@ class CswService(OwsService): | |
def __init__(self, endpoint=None): | ||
super(CswService, self).__init__(endpoint) | ||
self.sortby = SortBy([SortProperty('dc:identifier')]) | ||
# check capabilities | ||
_cap = self.getcapabilities(endpoint)['response'] | ||
self.capabilities = etree.ElementTree(etree.fromstring(_cap)) | ||
self.output_schemas = { | ||
'GetRecords': self._get_output_schemas('GetRecords'), | ||
'GetRecordById': self._get_output_schemas('GetRecordById'), | ||
} | ||
|
||
def _get_output_schemas(self, operation): | ||
_cap_ns = self.capabilities.getroot().nsmap | ||
_ows_ns = _cap_ns.get('ows') | ||
if not _ows_ns: | ||
raise CswError('Bad getcapabilities response: OWS namespace not found ' + str(_cap_ns)) | ||
_op = self.capabilities.find("//{{{}}}Operation[@name='{}']".format(_ows_ns, operation)) | ||
_schemas = _op.find("{{{}}}Parameter[@name='outputSchema']".format(_ows_ns)) | ||
_values = map(lambda v: v.text, _schemas.findall("{{{}}}Value".format(_ows_ns))) | ||
output_schemas = {} | ||
for key, value in _schemas.nsmap.items(): | ||
if value in _values: | ||
output_schemas.update({key : value}) | ||
return output_schemas | ||
|
||
def getrecords(self, qtype=None, keywords=[], | ||
typenames="csw:Record", esn="brief", | ||
skip=0, count=10, outputschema="gmd", **kw): | ||
from owslib.csw import namespaces | ||
|
||
constraints = [] | ||
csw = self._ows(**kw) | ||
|
||
# check target csw server capabilities for requested output schema | ||
output_schemas = self.output_schemas['GetRecords'] | ||
if not output_schemas.get(outputschema): | ||
raise CswError('Output schema \'{}\' not supported by target server: '.format(output_schemas)) | ||
|
||
if qtype is not None: | ||
constraints.append(PropertyIsEqualTo("dc:type", qtype)) | ||
|
||
|
@@ -87,7 +113,7 @@ def getrecords(self, qtype=None, keywords=[], | |
"esn": esn, | ||
"startposition": skip, | ||
"maxrecords": count, | ||
"outputschema": namespaces[outputschema], | ||
"outputschema": output_schemas[outputschema], | ||
"sortby": self.sortby | ||
} | ||
log.info('Making CSW request: getrecords2 %r', kwa) | ||
|
@@ -102,10 +128,15 @@ def getrecords(self, qtype=None, keywords=[], | |
def getidentifiers(self, qtype=None, typenames="csw:Record", esn="brief", | ||
keywords=[], limit=None, page=10, outputschema="gmd", | ||
startposition=0, cql=None, **kw): | ||
from owslib.csw import namespaces | ||
|
||
constraints = [] | ||
csw = self._ows(**kw) | ||
|
||
# check target csw server capabilities for requested output schema | ||
output_schemas = self.output_schemas['GetRecords'] | ||
if not output_schemas.get(outputschema): | ||
raise CswError('Output schema \'{}\' not supported by target server: '.format(output_schemas)) | ||
|
||
if qtype is not None: | ||
constraints.append(PropertyIsEqualTo("dc:type", qtype)) | ||
|
||
|
@@ -115,7 +146,7 @@ def getidentifiers(self, qtype=None, typenames="csw:Record", esn="brief", | |
"esn": esn, | ||
"startposition": startposition, | ||
"maxrecords": page, | ||
"outputschema": namespaces[outputschema], | ||
"outputschema": output_schemas[outputschema], | ||
"cql": cql, | ||
"sortby": self.sortby | ||
} | ||
|
@@ -129,7 +160,6 @@ def getidentifiers(self, qtype=None, typenames="csw:Record", esn="brief", | |
err = 'Error getting identifiers: %r' % \ | ||
csw.exceptionreport.exceptions | ||
#log.error(err) | ||
raise CswError(err) | ||
|
||
if matches == 0: | ||
matches = csw.results['matches'] | ||
|
@@ -154,11 +184,17 @@ def getidentifiers(self, qtype=None, typenames="csw:Record", esn="brief", | |
kwa["startposition"] = startposition | ||
|
||
def getrecordbyid(self, ids=[], esn="full", outputschema="gmd", **kw): | ||
from owslib.csw import namespaces | ||
|
||
csw = self._ows(**kw) | ||
|
||
# fetch target csw server capabilities for requested output schema | ||
output_schemas=output_schemas = self.output_schemas['GetRecordById'] | ||
if not output_schemas.get(outputschema): | ||
raise CswError('Output schema \'{}\' not supported by target server: '.format(output_schemas)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably here I should be more tolerant Logging ERROR and returning. |
||
|
||
kwa = { | ||
"esn": esn, | ||
"outputschema": namespaces[outputschema], | ||
"outputschema": output_schemas[outputschema], | ||
} | ||
# Ordinary Python version's don't support the metadata argument | ||
log.info('Making CSW request: getrecordbyid %r %r', ids, kwa) | ||
|
@@ -168,14 +204,17 @@ def getrecordbyid(self, ids=[], esn="full", outputschema="gmd", **kw): | |
csw.exceptionreport.exceptions | ||
#log.error(err) | ||
raise CswError(err) | ||
if not csw.records: | ||
elif csw.records: | ||
record = self._xmd(list(csw.records.values())[0]) | ||
elif csw.response: | ||
record = self._xmd(etree.fromstring(csw.response)) | ||
else: | ||
return | ||
record = self._xmd(list(csw.records.values())[0]) | ||
|
||
## strip off the enclosing results container, we only want the metadata | ||
#md = csw._exml.find("/gmd:MD_Metadata")#, namespaces=namespaces) | ||
# Ordinary Python version's don't support the metadata argument | ||
md = csw._exml.find("/{http://www.isotc211.org/2005/gmd}MD_Metadata") | ||
# '/{schema}*' expression should be safe enough and is able to match the | ||
# desired schema followed by both MD_Metadata or MI_Metadata (iso19115[-2]) | ||
md = csw._exml.find("/{{{schema}}}*".format(schema=output_schemas[outputschema])) | ||
mdtree = etree.ElementTree(md) | ||
try: | ||
record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=str) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you document the new
output_schema
option and its default value in here so others are aware of it?https://github.com/ckan/ckanext-spatial/blob/master/doc/harvesters.rst
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added fallback to default in case the server is not supporting iso19139 -> 19115 transformation
the fallback will log and switch back to default asking for iso19139 -> iso19139.