| 1 |
""" |
| 2 |
A shallow interface to IVOA vocabularies. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import json |
| 12 |
import functools |
| 13 |
import os |
| 14 |
import time |
| 15 |
from urllib import request |
| 16 |
|
| 17 |
from twisted.web.template import tags as T |
| 18 |
|
| 19 |
from gavo import base |
| 20 |
from gavo import utils |
| 21 |
from gavo.base import meta |
| 22 |
|
| 23 |
from gavo.utils.dachstypes import cast, Dict, StanElement, Vocabulary |
| 24 |
|
| 25 |
IVOA_VOCABULARY_ROOT = "http://www.ivoa.net/rdf/" |
| 26 |
|
| 27 |
|
| 28 |
def download_file( |
| 29 |
url: str, |
| 30 |
cache: str, |
| 31 |
show_progress: bool, |
| 32 |
http_headers: Dict[str, str]) -> str: |
| 33 |
"""a partial implementation of astropy.utils.data.download_file |
| 34 |
to be used until we can rely on astropy 4. |
| 35 |
|
| 36 |
We actually ignore cache and show_progress... |
| 37 |
""" |
| 38 |
cache_name = base.getConfig("cacheDir") / utils.defuseFileName(url) |
| 39 |
|
| 40 |
if not os.path.exists(cache_name) or cache=="update": |
| 41 |
voc_req = request.Request(url, |
| 42 |
headers=http_headers) |
| 43 |
with request.urlopen(voc_req) as src: |
| 44 |
payload = src.read() |
| 45 |
# only write when we have all the data; we don't want to |
| 46 |
# clobber a cache that's still halfway good. |
| 47 |
with open(cache_name, "wb") as dest: |
| 48 |
dest.write(payload) |
| 49 |
|
| 50 |
return cache_name |
| 51 |
|
| 52 |
|
| 53 |
@functools.lru_cache(30) |
| 54 |
def get_vocabulary( |
| 55 |
voc_name: str, |
| 56 |
force_update: bool = False) -> Vocabulary: |
| 57 |
"""returns an IVOA vocabulary in its desise form. |
| 58 |
|
| 59 |
See Vocabularies in the VO 2 to see what is inside of this. |
| 60 |
|
| 61 |
This will use a cache to avoid repeated updates, but it |
| 62 |
will attempt to re-download if the cached copy is older than 6 months. |
| 63 |
|
| 64 |
Use force_update to expire the cache immediately |
| 65 |
""" |
| 66 |
# insert astropy.utils.data below once oldstable has astropy 4. |
| 67 |
try: |
| 68 |
src_name = download_file( |
| 69 |
IVOA_VOCABULARY_ROOT+voc_name, |
| 70 |
cache=cast(str, "update" if force_update else True), |
| 71 |
show_progress=False, |
| 72 |
http_headers={"accept": "application/x-desise+json"}) |
| 73 |
except Exception as msg: |
| 74 |
raise base.ui.logOldExc(base.ReportableError( |
| 75 |
"No such vocabulary: %s"%voc_name, |
| 76 |
hint="Could not retrieve the vocabulary; failure: %s"% |
| 77 |
str(msg))) |
| 78 |
|
| 79 |
if time.time()-os.path.getmtime(src_name)>3600*60*150: |
| 80 |
# attempt a re-retrieval, but ignore failure |
| 81 |
try: |
| 82 |
src_name = download_file( |
| 83 |
IVOA_VOCABULARY_ROOT+voc_name, |
| 84 |
cache="update", show_progress=False, |
| 85 |
http_headers={"accept": "application/x-desise+json"}) |
| 86 |
except Exception as msg: |
| 87 |
base.ui.notifyWarning("Updating cache for the vocabulary" |
| 88 |
" {} failed: {}".format(voc_name, msg)) |
| 89 |
|
| 90 |
with open(src_name, "r", encoding="utf-8") as f: |
| 91 |
return json.load(f) |
| 92 |
|
| 93 |
|
| 94 |
def get_label(voc: Vocabulary, term: str) -> str: |
| 95 |
"""returns the label of term if it's in the desise vocabulary voc, |
| 96 |
term capitalised otherwise. |
| 97 |
""" |
| 98 |
if term in voc["terms"]: |
| 99 |
return voc["terms"][term]["label"] |
| 100 |
else: |
| 101 |
return term.capitalize() |
| 102 |
|
| 103 |
|
| 104 |
@meta.forKeys("subject") |
| 105 |
class SubjectMeta(meta.MetaValue): |
| 106 |
"""A MetaValue translating UAT terms to their labels in HTML. |
| 107 |
|
| 108 |
This follows the assumption that HTML is what humans look at, anything |
| 109 |
else is either computers or nerds. |
| 110 |
""" |
| 111 |
def _getContentAsHTML(self, content: str, block: bool = False |
| 112 |
) -> StanElement: |
| 113 |
try: |
| 114 |
uat = get_vocabulary("uat")["terms"] |
| 115 |
if content in uat: |
| 116 |
return T.a(href="http://www.ivoa.net/rdf/uat#"+content)[ |
| 117 |
uat[content]["label"]] # type: ignore # TODO: figure out why mypy gets the twisted type wrong |
| 118 |
except (base.ReportableError, KeyError): |
| 119 |
# something went wrong fetching the UAT; just hand through stuff. |
| 120 |
pass |
| 121 |
|
| 122 |
return content |
| 123 |
|
| 124 |
if __name__=="__main__": # pragma: no cover |
| 125 |
print(get_vocabulary("datalink/core")) |
| 126 |
|
| 127 |
# vi:et:sw=4:sta |