| 1 |
""" |
| 2 |
Helper functions for biblinks management. |
| 3 |
|
| 4 |
You will usually use these from within an postCreation (or perhaps afterMeta, |
| 5 |
if you would like to update them independently of the content) script. |
| 6 |
""" |
| 7 |
|
| 8 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 9 |
#c |
| 10 |
#c This program is free software, covered by the GNU GPL. See the |
| 11 |
#c COPYING file in the source distribution. |
| 12 |
|
| 13 |
|
| 14 |
from gavo import base |
| 15 |
from gavo import rsc |
| 16 |
from gavo import svcs |
| 17 |
|
| 18 |
|
| 19 |
# use this to prefix ivoids in dataset-ref-s |
| 20 |
IVO_SCHEMA_RESOLVER = "http://reg.g-vo.org/LP/" |
| 21 |
|
| 22 |
|
| 23 |
def defineLinks(connection, rd, links, bibFormat=None, linkSource=None): |
| 24 |
"""Inserts links into the system's bibliography links table. |
| 25 |
|
| 26 |
It does this through connection (which must be a writable admin connection); |
| 27 |
rd is the RD of the table managing the datasets in question (or the |
| 28 |
RD of the service pointed to), bibFormat can be "doi" if the bibliographic |
| 29 |
identifiers are not bibcodes, and linkSource is a user-controlled |
| 30 |
string to be used with incremental imports. |
| 31 |
|
| 32 |
links, finally, is a sequence of triples:: |
| 33 |
|
| 34 |
(bib-ref, relationship, dataset-ref) |
| 35 |
|
| 36 |
or quadruples:: |
| 37 |
|
| 38 |
(bib-ref, relationship, biblinks-service-ref, nlinks) |
| 39 |
|
| 40 |
See the reference documentation for what to put where. Note that |
| 41 |
dataset-ref-s with an ivo schema will be mangled to point to a landing |
| 42 |
page. |
| 43 |
""" |
| 44 |
sourceRD = rd.sourceId |
| 45 |
|
| 46 |
links_table = rsc.TableForDef( |
| 47 |
base.resolveCrossId("//biblinks#biblinks"), connection=connection) |
| 48 |
feeder = links_table.getFeeder() |
| 49 |
|
| 50 |
with feeder: |
| 51 |
for linkDef in links: |
| 52 |
|
| 53 |
if len(linkDef)==3: |
| 54 |
bibRef, relationship, datasetRef = linkDef |
| 55 |
nlinks = None |
| 56 |
elif len(linkDef)==4: |
| 57 |
bibRef, relationship, datasetRef, nlinks = linkDef |
| 58 |
else: |
| 59 |
raise TypeError(f"Uninterpretable link def: {linkDef}") |
| 60 |
|
| 61 |
# change ivoids to a landing page. |
| 62 |
if datasetRef.startswith("ivo://"): |
| 63 |
datasetRef = IVO_SCHEMA_RESOLVER+datasetRef[6:] |
| 64 |
|
| 65 |
feeder.add({ |
| 66 |
"bib_ref": bibRef, |
| 67 |
"relationship": relationship, |
| 68 |
"dataset_ref": datasetRef, |
| 69 |
"bib_format": bibFormat, |
| 70 |
"sourcerd": sourceRD, |
| 71 |
"link_source": linkSource, |
| 72 |
"cardinality": nlinks}) |
| 73 |
|
| 74 |
|
| 75 |
def clearLinks(connection, rd, linkSource=None): |
| 76 |
"""drops bibliography links from the system biblink table. |
| 77 |
|
| 78 |
connection, rd, and linkSource are as in defineLinks. |
| 79 |
|
| 80 |
This function should usually be called first in scripts defining |
| 81 |
bibliography links to clear the results of any previous import. |
| 82 |
""" |
| 83 |
sourceRD = rd.sourceId |
| 84 |
query = "DELETE FROM {} WHERE sourcerd=%(sourceRD)s".format( |
| 85 |
base.resolveCrossId("//biblinks#biblinks").getQName()) |
| 86 |
if linkSource is not None: |
| 87 |
query += " AND link_source=%(linkSource)s" |
| 88 |
|
| 89 |
connection.execute(query, locals()) |
| 90 |
|
| 91 |
|
| 92 |
class BiblinksCore(svcs.Core): |
| 93 |
"""A core retrieving biblinks-harvest records from dc.biblinks. |
| 94 |
|
| 95 |
Probably is only place in which this makes sense is in //biblinks#links; |
| 96 |
consider making this a pythonCore there. |
| 97 |
|
| 98 |
(since 2.8.2) |
| 99 |
""" |
| 100 |
name_ = "biblinksCore" |
| 101 |
|
| 102 |
def __init__(self, parent, **kwargs): |
| 103 |
kwargs["inputTable"] = base.makeStruct(svcs.InputTD) |
| 104 |
kwargs["outputTable"] = svcs.OutputTableDef.fromTableDef( |
| 105 |
base.resolveCrossId("//biblinks#core-output")) |
| 106 |
super().__init__(parent, **kwargs) |
| 107 |
|
| 108 |
def run(self, service, inputData, queryMeta): |
| 109 |
with base.getTableConn() as conn: |
| 110 |
return rsc.TableForDef(self.outputTable, |
| 111 |
rows=list(conn.queryToDicts("select * from dc.biblinks"))) |