Package gavo :: Package registry :: Module servicelist
[frames] | no frames]

Source Code for Module gavo.registry.servicelist

  1  """ 
  2  The (DC-internal) service list: querying, adding records, etc. 
  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  from gavo import base 
 12  from gavo import utils 
 13  from gavo import rsc 
 14  from gavo.registry import common 
 15   
 16   
17 -def getSetsForResource(restup):
18 """returns the list of set names the resource described by restup 19 belongs to. 20 """ 21 with base.getTableConn() as conn: 22 return [str(r[0]) for r in 23 conn.query("SELECT DISTINCT setName FROM dc.sets WHERE" 24 " sourceRD=%(sourceRD)s AND resId=%(resId)s AND NOT deleted", 25 restup)]
26 27
28 -def getSets():
29 """returns a sequence of dicts giving setName and and a list of 30 services belonging to that set. 31 """ 32 tableDef = common.getServicesRD().getById("sets") 33 setMembers = {} 34 35 for rec in tableDef.doSimpleQuery(): 36 setMembers.setdefault(rec["setName"], []).append( 37 (rec["sourceRD"], rec["resId"])) 38 39 return [{"setName": key, "services": value} 40 for key, value in setMembers.iteritems()]
41 42
43 -def queryServicesList(whereClause="", pars={}, tableName="resources_join"):
44 """returns a list of services based on selection criteria in 45 whereClause. 46 47 The table queried is the resources_join view, and you'll get back all 48 fields defined there. 49 """ 50 td = common.getServicesRD().getById(tableName) 51 return td.doSimpleQuery(fragments=whereClause, params=pars)
52 53
54 -def querySubjectsList(setName=None):
55 """returns a list of local services chunked by subjects. 56 57 This is mainly for the root page (see web.root). Query the 58 cache using the __system__/services key to clear the cache on services 59 """ 60 setName = setName or 'local' 61 svcsForSubjs = {} 62 td = common.getServicesRD().getById("subjects_join") 63 for row in td.doSimpleQuery( 64 fragments="setName=%(setName)s AND subject IS NOT NULL", 65 params={"setName": setName}): 66 svcsForSubjs.setdefault(row["subject"], []).append(row) 67 68 for s in svcsForSubjs.values(): 69 s.sort(key=lambda a: a["title"]) 70 71 res = [{"subject": subject, "chunk": s} 72 for subject, s in svcsForSubjs.iteritems()] 73 res.sort(lambda a,b: cmp(a["subject"], b["subject"])) 74 return res
75 76
77 -def getChunkedServiceList(setName=None):
78 """returns a list of local services chunked by title char. 79 80 This is mainly for the root page (see web.root). Query the 81 cache using the __system__/services key to clear the cache on services 82 reload. 83 """ 84 setName = setName or 'local' 85 return utils.chunk( 86 sorted(queryServicesList("setName=%(setName)s and not deleted", 87 {"setName": setName}), 88 key=lambda s: s.get("title").lower()), 89 lambda srec: srec.get("title", ".")[0].upper())
90 91
92 -def cleanServiceTablesFor(rd, connection):
93 """removes/invalidates all entries originating from rd from the service 94 tables. 95 """ 96 # this is a bit of a hack: We're running services#tables' newSource 97 # skript without then importing anything new. 98 tables = rsc.Data.create( 99 common.getServicesRD().getById("tables"), 100 connection=connection) 101 tables.runScripts("newSource", sourceToken=rd)
102 103
104 -def basename(tableName):
105 if "." in tableName: 106 return tableName.split(".")[-1] 107 else: 108 return tableName
109 110
111 -def getTableDef(tableName):
112 """returns a tableDef instance for the schema-qualified tableName. 113 114 If no such table is known to the system, a NotFoundError is raised. 115 """ 116 with base.AdhocQuerier(base.getTableConn) as q: 117 res = list(q.query("SELECT tableName, sourceRD FROM dc.tablemeta WHERE" 118 " LOWER(tableName)=LOWER(%(tableName)s)", {"tableName": tableName})) 119 if len(res)!=1: 120 raise base.NotFoundError(tableName, what="table", 121 within="data center table listing.", hint="The table is missing from" 122 " the dc.tablemeta table. This gets filled at gavoimp time.") 123 tableName, rdId = res[0] 124 return base.caches.getRD(rdId).getById(basename(tableName))
125