| 1 |
# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 |
""" |
| 3 |
This file contains routines to verify the correctness of UCD strings. |
| 4 |
|
| 5 |
MD, 2021: I'm forking this here to develop it a bit. The main changes |
| 6 |
ought to go back to astropy when this has stabilised. |
| 7 |
""" |
| 8 |
|
| 9 |
|
| 10 |
# STDLIB |
| 11 |
import re |
| 12 |
|
| 13 |
# LOCAL |
| 14 |
# from astropy.utils import data |
| 15 |
|
| 16 |
|
| 17 |
from gavo import base |
| 18 |
|
| 19 |
|
| 20 |
__all__ = ['parse_ucd', 'check_ucd'] |
| 21 |
|
| 22 |
|
| 23 |
class UCDWords: |
| 24 |
""" |
| 25 |
Manages a list of acceptable UCD words. |
| 26 |
|
| 27 |
Works by reading in a data file exactly as provided by IVOA. This |
| 28 |
file resides in data/ucd1p-words.txt. |
| 29 |
""" |
| 30 |
|
| 31 |
def __init__(self): |
| 32 |
self._primary = set() |
| 33 |
self._secondary = set() |
| 34 |
self._descriptions = {} |
| 35 |
self._capitalization = {} |
| 36 |
|
| 37 |
# astropy |
| 38 |
# with data.get_pkg_data_fileobj( |
| 39 |
# "data/ucd1p-words.txt", encoding='ascii') as fd: |
| 40 |
# gavo |
| 41 |
with base.openDistFile("data/ucd-words.txt") as fd: |
| 42 |
for line in fd.readlines(): |
| 43 |
if line.startswith("#"): |
| 44 |
continue |
| 45 |
type, name, descr = [ |
| 46 |
x.strip() for x in line.split('|')] |
| 47 |
name_lower = name.lower() |
| 48 |
if type in 'QPEVC': |
| 49 |
self._primary.add(name_lower) |
| 50 |
if type in 'QSEVC': |
| 51 |
self._secondary.add(name_lower) |
| 52 |
self._descriptions[name_lower] = descr |
| 53 |
self._capitalization[name_lower] = name |
| 54 |
|
| 55 |
def is_primary(self, name): |
| 56 |
""" |
| 57 |
Returns True if *name* is a valid primary name. |
| 58 |
""" |
| 59 |
return name.lower() in self._primary |
| 60 |
|
| 61 |
def is_secondary(self, name): |
| 62 |
""" |
| 63 |
Returns True if *name* is a valid secondary name. |
| 64 |
""" |
| 65 |
return name.lower() in self._secondary |
| 66 |
|
| 67 |
def get_description(self, name): |
| 68 |
""" |
| 69 |
Returns the official English description of the given UCD |
| 70 |
*name*. |
| 71 |
""" |
| 72 |
return self._descriptions[name.lower()] |
| 73 |
|
| 74 |
def normalize_capitalization(self, name): |
| 75 |
""" |
| 76 |
Returns the standard capitalization form of the given name. |
| 77 |
""" |
| 78 |
return self._capitalization[name.lower()] |
| 79 |
|
| 80 |
|
| 81 |
_ucd_singleton = None |
| 82 |
|
| 83 |
|
| 84 |
def parse_ucd(ucd, check_controlled_vocabulary=True, has_colon=False): |
| 85 |
""" |
| 86 |
Parse the UCD into its component parts. |
| 87 |
|
| 88 |
Parameters |
| 89 |
---------- |
| 90 |
ucd : str |
| 91 |
The UCD string |
| 92 |
|
| 93 |
check_controlled_vocabulary : bool, optional |
| 94 |
If `True`, then each word in the UCD will be verified against |
| 95 |
the UCD1+ controlled vocabulary, (as required by the VOTable |
| 96 |
specification version 1.2), otherwise not. |
| 97 |
|
| 98 |
has_colon : bool, optional |
| 99 |
If `True`, the UCD may contain a colon (as defined in earlier |
| 100 |
versions of the standard). |
| 101 |
|
| 102 |
Returns |
| 103 |
------- |
| 104 |
parts : list |
| 105 |
The result is a list of tuples of the form: |
| 106 |
|
| 107 |
(*namespace*, *word*) |
| 108 |
|
| 109 |
If no namespace was explicitly specified, *namespace* will be |
| 110 |
returned as ``'ivoa'`` (i.e., the default namespace). |
| 111 |
|
| 112 |
Raises |
| 113 |
------ |
| 114 |
ValueError : *ucd* is invalid |
| 115 |
""" |
| 116 |
global _ucd_singleton |
| 117 |
if _ucd_singleton is None: |
| 118 |
_ucd_singleton = UCDWords() |
| 119 |
|
| 120 |
if has_colon: |
| 121 |
m = re.search(r'[^A-Za-z0-9_.:;\-]', ucd) |
| 122 |
else: |
| 123 |
m = re.search(r'[^A-Za-z0-9_.;\-]', ucd) |
| 124 |
if m is not None: |
| 125 |
raise ValueError(f"UCD has invalid character '{m.group(0)}' in '{ucd}'") |
| 126 |
|
| 127 |
word_component_re = r'[A-Za-z0-9][A-Za-z0-9\-_]*' |
| 128 |
word_re = fr'{word_component_re}(\.{word_component_re})*' |
| 129 |
|
| 130 |
parts = ucd.split(';') |
| 131 |
words = [] |
| 132 |
for i, word in enumerate(parts): |
| 133 |
colon_count = word.count(':') |
| 134 |
if colon_count == 1: |
| 135 |
ns, word = word.split(':', 1) |
| 136 |
if not re.match(word_component_re, ns): |
| 137 |
raise ValueError(f"Invalid namespace '{ns}'") |
| 138 |
ns = ns.lower() |
| 139 |
elif colon_count > 1: |
| 140 |
raise ValueError(f"Too many colons in '{word}'") |
| 141 |
else: |
| 142 |
ns = 'ivoa' |
| 143 |
|
| 144 |
if not re.match(word_re, word): |
| 145 |
raise ValueError(f"Invalid word '{word}'") |
| 146 |
|
| 147 |
if ns == 'ivoa' and check_controlled_vocabulary: |
| 148 |
if i == 0: |
| 149 |
if not _ucd_singleton.is_primary(word): |
| 150 |
if _ucd_singleton.is_secondary(word): |
| 151 |
raise ValueError( |
| 152 |
f"Secondary word '{word}' is not valid as a primary word") |
| 153 |
else: |
| 154 |
raise ValueError(f"Unknown word '{word}'") |
| 155 |
else: |
| 156 |
if not _ucd_singleton.is_secondary(word): |
| 157 |
if _ucd_singleton.is_primary(word): |
| 158 |
raise ValueError( |
| 159 |
f"Primary word '{word}' is not valid as a secondary word") |
| 160 |
else: |
| 161 |
raise ValueError(f"Unknown word '{word}'") |
| 162 |
|
| 163 |
try: |
| 164 |
normalized_word = _ucd_singleton.normalize_capitalization(word) |
| 165 |
except KeyError: |
| 166 |
normalized_word = word |
| 167 |
words.append((ns, normalized_word)) |
| 168 |
|
| 169 |
return words |
| 170 |
|
| 171 |
|
| 172 |
def check_ucd(ucd, check_controlled_vocabulary=False, has_colon=False): |
| 173 |
""" |
| 174 |
Returns False if *ucd* is not a valid `unified content descriptor`_. |
| 175 |
|
| 176 |
Parameters |
| 177 |
---------- |
| 178 |
ucd : str |
| 179 |
The UCD string |
| 180 |
|
| 181 |
check_controlled_vocabulary : bool, optional |
| 182 |
If `True`, then each word in the UCD will be verified against |
| 183 |
the UCD1+ controlled vocabulary, (as required by the VOTable |
| 184 |
specification version 1.2), otherwise not. |
| 185 |
|
| 186 |
has_colon : bool, optional |
| 187 |
If `True`, the UCD may contain a colon (as defined in earlier |
| 188 |
versions of the standard). |
| 189 |
|
| 190 |
Returns |
| 191 |
------- |
| 192 |
valid : bool |
| 193 |
""" |
| 194 |
if ucd is None: |
| 195 |
return True |
| 196 |
|
| 197 |
try: |
| 198 |
parse_ucd(ucd, |
| 199 |
check_controlled_vocabulary=check_controlled_vocabulary, |
| 200 |
has_colon=has_colon) |
| 201 |
except ValueError: |
| 202 |
return False |
| 203 |
return True |