| 1 |
""" |
| 2 |
Common functionality for the DC user interface. |
| 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 sys |
| 12 |
|
| 13 |
|
| 14 |
def getMatchingFunction(funcSelector, functions, parser): |
| 15 |
"""returns the module name and a function name within the module for |
| 16 |
the function selector funcSelector. |
| 17 |
|
| 18 |
The function will exit if funcSelector is not a unique prefix within |
| 19 |
functions. |
| 20 |
""" |
| 21 |
matches = [] |
| 22 |
for key, res in functions: |
| 23 |
if key.startswith(funcSelector): |
| 24 |
matches.append(res) |
| 25 |
if len(matches)==1: |
| 26 |
return matches[0] |
| 27 |
if matches: |
| 28 |
sys.stderr.write("Multiple matches for function %s.\n\n"%funcSelector) |
| 29 |
else: |
| 30 |
sys.stderr.write("No match for function %s.\n\n"%funcSelector) |
| 31 |
parser.print_help(file=sys.stderr) |
| 32 |
sys.exit(1) |
| 33 |
|
| 34 |
|
| 35 |
def _getAutoDDIds(rd): |
| 36 |
"""helps getPertainingDDs |
| 37 |
""" |
| 38 |
res = [] |
| 39 |
for dd in rd.dds: |
| 40 |
if dd.auto: |
| 41 |
res.append(dd) |
| 42 |
return res |
| 43 |
|
| 44 |
|
| 45 |
def _getSelectedDDIds(rd, selectedIds): |
| 46 |
"""helps getPertainingDDs |
| 47 |
""" |
| 48 |
res = [] |
| 49 |
ddDict = dict((dd.id, dd) for dd in rd.dds) |
| 50 |
for ddId in selectedIds: |
| 51 |
if ddId not in ddDict: |
| 52 |
from gavo import base |
| 53 |
raise base.ReportableError( |
| 54 |
"The DD '%s' you are trying to import is not defined within" |
| 55 |
" the RD '%s'."%(ddId, rd.sourceId), |
| 56 |
hint="Data elements available in %s include %s"%(rd.sourceId, |
| 57 |
", ".join(ddDict) or '(None)')) |
| 58 |
res.append(ddDict[ddId]) |
| 59 |
return res |
| 60 |
|
| 61 |
|
| 62 |
def getPertainingDDs(rd, selectedIds): |
| 63 |
"""returns a list of dds on which imp or drop should operate. |
| 64 |
|
| 65 |
By default, that's the "auto" dds of rd. If ddIds is not empty, |
| 66 |
it is validated that all ids mentioned actually exist. |
| 67 |
|
| 68 |
Finally, if no DDs are selected but DDs are available, an error is raised. |
| 69 |
""" |
| 70 |
if selectedIds: |
| 71 |
dds = _getSelectedDDIds(rd, selectedIds) |
| 72 |
else: |
| 73 |
dds = _getAutoDDIds(rd) |
| 74 |
if not dds: |
| 75 |
if not rd.dds: |
| 76 |
from gavo import base |
| 77 |
base.ui.notifyWarning("There is no data element" |
| 78 |
" in the RD %s; is that all right?"%rd.sourceId) |
| 79 |
else: |
| 80 |
from gavo import base |
| 81 |
raise base.ReportableError( |
| 82 |
"Neither automatic not manual data selected from RD %s "%rd.sourceId, |
| 83 |
hint="All data elements have auto=False. You have to" |
| 84 |
" explicitly name one or more data to import (names" |
| 85 |
" available: %s)"%(", ".join(dd.id or "(anon)" for dd in rd.dds))) |
| 86 |
return dds |
| 87 |
|
| 88 |
|
| 89 |
|