Package gavo :: Package user :: Module common
[frames] | no frames]

Source Code for Module gavo.user.common

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