1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
|
"""
A simple client of OAI-http.
This includes both some high-level functions and rudimentary parsers
that can serve as bases for more specialized parsers.
"""
#c Copyright 2008-2020, the GAVO project
#c
#c This program is free software, covered by the GNU GPL. See the
#c COPYING file in the source distribution.
import io
import hashlib
import os
import pickle
import re
import urllib.request, urllib.parse, urllib.error
from xml import sax
from xml.sax import saxutils
from gavo import base
from gavo import svcs
from gavo import utils
class FailedQuery(Exception):
def __init__(self, msg, code="?", value="?"):
Exception.__init__(self, msg)
self.code, self.value = code, value
class NoRecordsMatch(Exception):
pass
class PrefixIsTaken(Exception):
pass
# Canonical prefixes, i.e., essentially fixed prefixes for certain
# namespaces. This is all an ugly nightmare, but this is what you
# get for having namespace prefixes in attributes.
class CanonicalPrefixes(object):
"""a self-persisting dictionary of the prefixes we use in our
OAI interface.
CanonicalPrefixes objects are constructed with the name of a
pickle file containing a list of (prefix, uri) pairs.
This reproduces some code from stanxml.NSRegistry, but we want that
stuff as instance method here, not as class method.
"""
def __init__(self, pickleName):
self.pickleName = pickleName
self._registry = {}
self._reverseRegistry = {}
self._loadData()
def registerPrefix(self, prefix, ns, save=True):
if prefix in self._registry:
if ns!=self._registry[prefix]:
raise PrefixIsTaken(prefix)
return
self._registry[prefix] = ns
if ns in self._reverseRegistry and self._reverseRegistry[ns]!=prefix:
raise ValueError("Namespace %s already has prefix %s, will"
" not clobber with %s"%(ns, self._reverseRegistry[ns], prefix))
self._reverseRegistry[ns] = prefix
if save:
self._saveData()
def registerPrefixOrMakeUp(self, prefix, ns):
"""registers prefix for ns or, if prefix is already taken, makes
up a new prefix for the namespace URI ns.
"""
try:
self.registerPrefix(prefix, ns)
except PrefixIsTaken:
origPrefix, uniquer = prefix, 0
while True:
try:
prefix = origPrefix+str(uniquer)
self.registerPrefix(prefix, ns)
except PrefixIsTaken:
uniquer += 1
else:
break
def getPrefixForNS(self, ns):
try:
return self._reverseRegistry[ns]
except KeyError:
raise svcs.NotFoundError(ns, "XML namespace",
"registry of XML namespaces.")
def haveNS(self, ns):
return ns in self._reverseRegistry
def getNSForPrefix(self, prefix):
try:
return self._registry[prefix]
except KeyError:
raise base.NotFoundError(prefix, "XML namespace prefix",
"registry of prefixes.")
def iterNS(self):
return iter(self._registry.items())
def _fillFromPairs(self, pairs):
"""fills the instance from a list of prefix, uri pairs.
Pairs is what is stored in the pickle.
"""
for prefix, uri in pairs:
self.registerPrefix(prefix, uri, save=False)
def _bootstrap(self):
"""sets up our canonical prefixes from DaCHS' (stanxml) namespace
registry.
"""
from gavo import api #noflake: hope most prefixes are registred after that
from gavo.utils import stanxml
self._fillFromPairs(iter(stanxml.NSRegistry._registry.items()))
self._saveData()
def _loadData(self):
try:
with open(self.pickleName, "rb") as f:
self._fillFromPairs(pickle.load(f))
except IOError: # most likely, the file does not exist yet
base.ui.notifyWarning("Starting new canonical prefixes")
self._bootstrap()
def _saveData(self):
toPersist = list(sorted(self._registry.items()))
try:
with open(self.pickleName+".tmp", "wb") as f:
pickle.dump(toPersist, f)
os.rename(self.pickleName+".tmp", self.pickleName)
except IOError as msg:
base.ui.notifyWarning("Could not persist canonical prefixes: %s"%
msg)
def getCanonicalPrefixes():
return CanonicalPrefixes(os.path.join(base.getConfig("cacheDir"),
"rrOaiPrefixes.pickle"))
class OAIErrorMixin(object):
def _end_error(self, name, attrs, content):
if attrs["code"]=="noRecordsMatch":
raise NoRecordsMatch()
raise FailedQuery("Registry bailed with code %s, value %s"%(
attrs["code"], content), attrs["code"], content)
class IdParser(utils.StartEndHandler, OAIErrorMixin):
"""A parser for simple OAI-PMH headers.
Records end up as a list of dictionaries in the recs attribute.
"""
resumptionToken = None
def __init__(self, initRecs=None):
utils.StartEndHandler.__init__(self)
if initRecs is None:
self.recs = []
else:
self.recs = initRecs
def getResult(self):
return self.recs
def _end_identifier(self, name, attrs, content):
self.recs[-1]["id"] = content.strip()
def _end_datestamp(self, name, attrs, content):
try:
self.recs[-1]["date"] = utils.parseISODT(content)
except ValueError: # don't fail just because of a broken date
self.recs[-1]["date"] = None
def _start_header(self, name, attrs):
self.recs.append({})
def _end_resumptionToken(self, name, attrs, content):
if content.strip():
self.resumptionToken = content
class RecordParser(IdParser, OAIErrorMixin):
"""A simple parser for ivo_vor records.
This only pulls out a number of the most salient items; more will
probably follow as needed.
"""
def _end_title(self, name, attrs, content):
if self.getParentTag()=="Resource":
self.recs[-1][name] = content
def _end_email(self, name, attrs, content):
if self.getParentTag()=="contact":
self.recs[-1]["contact.email"] = content
def _end_name(self, name, attrs, content):
if self.getParentTag()=="creator":
self.recs[-1].setdefault(name, []).append(content)
def _end_subject(self, name, attrs, content):
self.recs[-1].setdefault(name, []).append(content)
def _handleContentChild(self, name, attrs, content):
if self.getParentTag()=="content":
self.recs[-1][name] = content
_end_description = _end_source = _end_referenceURL = \
_handleContentChild
def _end_datestamp(self, name, attrs, content):
# nuke IdParser implementation, we take our date from ri:Resource
pass
def _startResource(self, name, attrs):
self.recs.append({})
def _end_Resource(self, name, attrs, content):
self.recs[-1]["date"] = utils.parseISODT(attrs["updated"])
def _end_accessURL(self, name, attrs, content):
self.recs[-1].setdefault(name, []).append(content)
class OAIRecordsParser(sax.ContentHandler, OAIErrorMixin):
"""a SAX ContentHandler generating tuples of some record-level metadata
and pre-formatted XML of simple implementation of the OAI interface.
canonicalPrefixes is a CanonicalPrefixesInstance built from
res/canonicalPrefixes.pickle
Note that we *require* that records actually carry ivo_vor metadata.
Note that this is nothing people should need in normal operation.
GAVO Heidelberg needs this for infrastructure services
(including OAI on RegTAP, but we needed it beyond that).
"""
# attribute names the values of which should be disambiguated to
# reduce the likelihood of clashes when ids are reused between documents.
# (see _normalizeAttrs)
_referringAttributeNames = set(["id", "ref",
"coord_system_id"])
resumptionToken = None
def __init__(self, canonicalPrefixes=None):
self.canonicalPrefixes = canonicalPrefixes or getCanonicalPrefixes()
sax.ContentHandler.__init__(self)
self.buffer = None
self.writer = None
self.rowdicts = []
self.prefixMap = {}
self.prefixesToTranslate = {}
def startPrefixMapping(self, prefix, uri):
self.prefixMap.setdefault(prefix, []).append(uri)
# Here, we make sure we find a globally unique prefix for every
# namespace URI. canonicalPrefixes makes sure this unique prefix
# is persistent and later available to the OAI interface
if not self.canonicalPrefixes.haveNS(uri):
self.canonicalPrefixes.registerPrefixOrMakeUp(prefix, uri)
canonPrefix = self.canonicalPrefixes.getPrefixForNS(uri)
if prefix!=canonPrefix or prefix in self.prefixesToTranslate:
self.prefixesToTranslate.setdefault(prefix, []).append(canonPrefix)
def endPrefixMapping(self, prefix):
self.prefixMap[prefix].pop()
if prefix in self.prefixesToTranslate:
self.prefixesToTranslate[prefix].pop()
if not self.prefixesToTranslate[prefix]:
del self.prefixesToTranslate[prefix]
def startElementNS(self, namePair, ignored, attrs):
ns, name = namePair
if ns is not None:
name = self.canonicalPrefixes.getPrefixForNS(ns)+":"+name
if attrs:
attrs = self._normalizeAttrs(attrs)
if name in self.startHandlers:
self.startHandlers[name](self, name, attrs)
if self.writer:
self.writer.startElement(name, attrs)
self._lastChars = []
def endElementNS(self, namePair, name):
ns, name = namePair
if ns is not None:
name = self.canonicalPrefixes.getPrefixForNS(ns)+":"+name
if self.writer:
self.writer.endElement(name)
if name in self.endHandlers:
self.endHandlers[name](self, name)
def characters(self, stuff):
if self.writer:
self.writer.characters(stuff)
# Hack, see _getLastContent
self._lastChars.append(stuff)
def normalizeNamespace(self, name):
"""fixes the namespace prefix of name if necessary.
name must be a qualified name, i.e., contain exactly one colon.
"normalize" here means make sure the prefix matches our canonical prefix
and change it to the canonical one if necessary.
"""
prefix, base = name.split(":")
if prefix not in self.prefixesToTranslate:
return name
return self.prefixesToTranslate[prefix][-1]+":"+base
def _normalizeAttrs(self, attrs):
"""fixes attribute name and attribute value namespaces if necessary.
It also always checks for xsi:type and fixes namespaced attribute
values as necessary.
See also normalizeNamespace.
"""
newAttrs = {}
for ns, name in list(attrs.keys()):
value = attrs[(ns, name)]
if ns is None:
newName = name
else:
newName = self.canonicalPrefixes.getPrefixForNS(ns)+":"+name
if newName=="xsi:type":
if ":" in value:
value = self.normalizeNamespace(value)
# to uniqueify id/ref-pairs, prepend an md5-digest of the ivoid
# to selected ids. This isn't guaranteed to always work, but
# if someone is devious enough to cause collisions here, they
# deserve no better.
if newName in self._referringAttributeNames:
value = value+hashlib.md5(self.ivoid.encode("utf-8")).hexdigest()
newAttrs[newName] = value
return newAttrs
def _getLastContent(self):
"""returns the entire character content since the last XML event.
"""
return "".join(self._lastChars)
def notifyError(self, err):
self._errorOccurred = True
def shipout(self, role, record):
# see _end_identifier for an explanation of the following condition
if self.ivoid is None:
return
# see our docstring on why we need the following
if not self.metadataSeen:
return
if self._errorOccurred:
return
# _start_header sets _isDeleted
if self._isDeleted:
return
self.rowdicts.append((role, record))
def _start_oai_header(self, name, attrs):
self._isDeleted = attrs.get("status", "").lower()=="deleted"
def _start_oai_record(self, name, attrs):
self._errorOccurred = False
self.curXML = io.BytesIO()
self.writer = saxutils.XMLGenerator(self.curXML, "utf-8")
self.writer.startDocument()
self.ivoid, self.updated = None, None
self.metadataSeen = False
self.oaiSets = set()
def _start_ri_Resource(self, anme, attrs):
self.metadataSeen = True
def _end_oai_record(self, name):
if self.writer is not None:
self.writer.endDocument()
# yeah, we decode the serialized result right away; it's easier
# to store character streams in the DB the way I'm doing things.
oaixml = self.curXML.getvalue().decode("utf-8")
# unfortunately, XMLGenerator insists on adding an XML declaration,
# which I can't have here. I remove it manually
if oaixml.startswith("<?xml"):
oaixml = oaixml[oaixml.index("?>")+2:]
self.shipout("oairecs", {
"ivoid": self.ivoid,
"updated": self.updated,
"oaixml": oaixml})
self.writer = None
self.curXML = None
def _end_oai_setSpec(self, name):
self.oaiSets.add(self._getLastContent())
def _end_oai_identifier(self, name):
self.ivoid = self._getLastContent().lower().strip()
def _end_oai_resumptionToken(self, name):
self.resumptionToken = self._getLastContent()
def _start_oai_error(self, name, attrs):
self._errorAttrs = attrs
def _end_oai_error(self, name):
self._end_error(name, self._errorAttrs, self._getLastContent())
def getResult(self):
return self.rowdicts
startHandlers = {
"oai:record": _start_oai_record,
"oai:header": _start_oai_header,
"ri:Resource": _start_ri_Resource,
"oai:error": _start_oai_error,
}
endHandlers = {
"oai:record": _end_oai_record,
"oai:setSpec": _end_oai_setSpec,
"oai:resumptionToken": _end_oai_resumptionToken,
"oai:identifier": _end_oai_identifier,
"oai:error": _end_oai_error,
}
class ServerProperties(object):
"""A container for what an OAI-PMH server gives in response to
identify.
"""
repositoryName = None
baseURL = None
protocolVersion = None
adminEmails = ()
earliestDatestamp = None
deletedRecord = None
granularity = None
repositoryName = None
compressions = ()
def __init__(self):
self.adminEmails = []
self.compressions = []
self.descriptions = []
def set(self, name, value):
setattr(self, name, value)
def add(self, name, value):
getattr(self, name).append(value)
class IdentifyParser(utils.StartEndHandler, OAIErrorMixin):
"""A parser for the result of the identify operation.
The result (an instance of ServerProperties) is in the serverProperties
attribute.
"""
resumptionToken = None
serverProperties = None
def getResult(self):
if not self.serverProperties:
raise FailedQuery("Identify request did not contain an Identify"
" element")
return self.serverProperties
def _start_Identify(self, name, attrs):
self.serverProperties = ServerProperties()
def _endListThing(self, name, attrs, content):
self.serverProperties.add(name+"s", content.strip())
_end_adminEmail = _end_compression \
= _endListThing
def _endStringThing(self, name, attrs, content):
self.serverProperties.set(name, content.strip())
_end_repositoryName = _end_baseURL = _end_protocolVersion \
= _end_granularity = _end_deletedRecord = _end_earliestDatestamp \
= _end_repositoryName = _endStringThing
class OAIQuery(object):
"""A container for queries to OAI interfaces.
Construct it with the oai endpoint and the OAI verb, plus some optional
query attributes. If you want to retain or access the raw responses
of the server, pass a contentCallback function -- it will be called
with a byte string containing the payload of the server response if
it was parsed successfully. Error responses cannot be obtained in
this way.
The OAIQuery is constructed with OAI-PMH parameters (verb, startDate,
endDate, set, metadataPrefix; see the OAI-PMH docs for what they mean,
only verb is mandatory). In addition, you can pass granularity,
which is the granularity
"""
startDate = None
endDate = None
set = None
registry = None
metadataPrefix = None
# maxRecords is mainly used in test_oai; that's why there's no
# constructor parameter for it
maxRecords = None
# a timeout on HTTP operations
timeout = 100
def __init__(self, registry, verb, startDate=None, endDate=None, set=None,
metadataPrefix="ivo_vor", identifier=None, contentCallback=None,
granularity=None):
self.registry = registry
self.verb, self.set = verb, set
self.startDate, self.endDate = startDate, endDate
self.identifier = identifier
self.metadataPrefix = metadataPrefix
self.contentCallback = contentCallback
self.granularity = granularity
if not self.granularity:
self.granularity = "YYYY-MM-DD"
def getKWs(self, **moreArgs):
"""returns a dictionary containing query keywords for OAI interfaces
from what's specified on the command line.
"""
kws = {"verb": self.verb}
if self.metadataPrefix:
kws["metadataPrefix"] = self.metadataPrefix
kws.update(moreArgs)
if self.granularity=='YY-MM-DD':
dateFormat = "%Y-%m-%d"
else:
dateFormat = "%Y-%m-%dT%H:%M:%SZ"
if self.startDate:
kws["from"] = self.startDate.strftime(dateFormat)
if self.endDate:
kws["until"] = self.endDate.strftime(dateFormat)
if self.set:
kws["set"] = self.set
if self.maxRecords:
kws["maxRecords"] = str(self.maxRecords)
if self.identifier:
kws["identifier"] = self.identifier
if "resumptionToken" in kws:
kws = {"resumptionToken": kws["resumptionToken"],
"verb": kws["verb"]}
return kws
def doHTTP(self, **moreArgs):
"""returns the result of parsing the current query plus
moreArgs to the current registry.
The result is returned as a string.
"""
srcURL = self.registry.rstrip("?"
)+"?"+self._getOpQS(**self.getKWs(**moreArgs))
base.ui.notifyInfo("OAI query %s"%srcURL)
f = utils.urlopenRemote(srcURL, timeout=self.timeout)
res = f.read()
f.close()
return res
def _getOpQS(self, **args):
"""returns a properly quoted HTTP query part from its (keyword) arguments.
"""
# we don't use urllib.urlencode to not encode empty values like a=&b=val
qString = "&".join("%s=%s"%(k, urllib.parse.quote(v))
for k, v in args.items() if v)
return "%s"%(qString)
def talkOAI(self, parserClass):
"""processes an OAI dialogue for verb using the IdParser-derived
parserClass.
"""
res = self.doHTTP(verb=self.verb)
if not res.strip():
# empty reply is not admissable XML here
raise FailedQuery("Empty HTTP response")
handler = parserClass()
try:
xmlReader = sax.make_parser()
xmlReader.setFeature(sax.handler.feature_namespaces, True)
xmlReader.setContentHandler(handler)
xmlReader.parse(io.BytesIO(res))
if self.contentCallback:
self.contentCallback(res)
except NoRecordsMatch:
return []
oaiResult = handler.getResult()
while handler.resumptionToken is not None:
resumptionToken = handler.resumptionToken
handler = parserClass(oaiResult)
try:
res = self.doHTTP(verb=self.verb,
resumptionToken=resumptionToken)
sax.parseString(res, handler)
if self.contentCallback:
self.contentCallback(res)
except NoRecordsMatch:
break
return oaiResult
def getIdentifiers(registry, startDate=None, endDate=None, set=None,
granularity=None):
"""returns a list of "short" records for what's in the registry specified
by args.
"""
q = OAIQuery(registry, verb="ListIdentifiers", startDate=startDate,
endDate=endDate, set=set)
return q.talkOAI(IdParser)
def getRecords(registry, startDate=None, endDate=None, set=None,
granularity=None):
"""returns a list of "long" records for what's in the registry specified
by args.
parser should be a subclass of RecordParser; otherwise, you'll miss
resumption and possibly other features.
"""
q = OAIQuery(registry, verb="ListRecords", startDate=startDate,
endDate=endDate, set=set, granularity=granularity)
return q.talkOAI(RecordParser)
def _addCanonicalNSDecls(xmlLiteral):
"""adds XML namespace declarations for namespace prefixes we
suspect in xmlLiteral.
This is an ugly hack based on REs necessary because in the OAIRecordsParser
we discard the namespace declarations. It won't work with CDATA
sections, and it'll make a hash of things if namespace declarations are
already present. However, for the use case of making the mutilated
resource records coming out of the OAIRecordsParser valid, it will just
do.
Without an XML schema and a full parse (which of course is impossible
without the necessary declarations), this is, really, not possible. But
the whole idea of canonical namespace prefixes is a mess, and so we
hack along; in particular, we accept any string of the form \w+: within
what looks like an XML tag as a namespace. Oh my.
"""
prefixesUsed = set()
for elementContent in re.finditer("<[^>]+>", xmlLiteral):
prefixesUsed |= set(re.findall("([a-zA-Z_]\w*):[a-zA-Z_]",
elementContent.group()))
cp = getCanonicalPrefixes()
nsDecls = " ".join('xmlns:%s=%s'%(
pref, utils.escapeAttrVal(cp.getNSForPrefix(pref)))
for pref in prefixesUsed)
return re.sub("<([\w:-]+)", r"<\1 "+nsDecls, xmlLiteral, 1)
def getRecord(registry, identifier):
"""returns the XML form of an OAI-PMH record for identifier from
the OAI-PMH endpoint at URL registry.
This uses the OAIRecordsParser which enforces canonical prefixes,
and the function will add their declarations as necessary. This also means
that evil registry records could be broken by us.
"""
q = OAIQuery(registry, verb="GetRecord", identifier=identifier)
res = q.talkOAI(OAIRecordsParser)
dest, row = res[0]
assert dest=='oairecs'
return _addCanonicalNSDecls(row["oaixml"])
def parseRecord(recordXML):
"""returns some main properties from an XML-encoded VOResource record.
recordXML can be an OAI-PMH response or just a naked record. If multiple
records are contained in recordXML, only the first will be returned.
What's coming back is a dictionary as produced by RecordParser.
"""
handler = RecordParser()
sax.parseString(recordXML, handler)
return handler.recs[0]
def getServerProperties(registry):
"""returns a ServerProperties instance for registry.
In particular, you can retrieve the granularity argument that
actually matches the registry from the result's granularity attribute.
"""
q = OAIQuery(registry, verb="Identify", metadataPrefix=None)
return q.talkOAI(IdentifyParser)
|