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

Source Code for Module gavo.registry.common

  1  """ 
  2  Common code and definitions for registry support. 
  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.utils import stanxml 
 14   
 15   
 16  SERVICELIST_ID = "__system__/services" 
 17   
 18   
 19  METADATA_PREFIXES = [ 
 20  # (prefix, schema-location, namespace) 
 21          ("oai_dc", stanxml.schemaURL("OAI-PMH.xsd"), 
 22                  "http://www.openarchives.org/OAI/2.0/oai_dc/"), 
 23          ("ivo_vor", stanxml.schemaURL("VOResource-v1.0.xsd"), 
 24                  "http://www.ivoa.net/xml/RegistryInterface/v1.0"), 
 25  ] 
 26   
 27   
28 -class OAIError(base.Error):
29 """is one of the standard OAI errors. 30 """
31
32 -class BadArgument(OAIError): pass
33 -class BadResumptionToken(OAIError): pass
34 -class BadVerb(OAIError): pass
35 -class CannotDisseminateFormat(OAIError): pass
36 -class IdDoesNotExist(OAIError): pass
37 -class NoMetadataFormats(OAIError): pass
38 -class NoSetHierarchy(OAIError): pass
39 -class NoRecordsMatch(OAIError): pass
40 41
42 -def getServicesRD():
43 return base.caches.getRD(SERVICELIST_ID)
44 45
46 -def getRegistryService():
47 return getServicesRD().getById("registry")
48 49
50 -def getResType(resob):
51 res = base.getMetaText(resob, "resType", default=None) 52 if res is None: 53 res = resob.resType 54 return res
55 56
57 -def getDependencies(rdId, connection=None):
58 """returns a list of RD ids that are need for the generation of RRs 59 from rdId. 60 """ 61 if connection is None: 62 with base.getTableConn() as conn: 63 return getDependencies(rdId, conn) 64 65 return [r[0] for r in 66 connection.query(getServicesRD().getById("res_dependencies") 67 .getSimpleQuery(["prereq"], "rd=%(rd)s"), {"rd": rdId})]
68 69
70 -class DateUpdatedMixin(object):
71 """A mixin providing computers for dateUpdated and datetimeUpdated. 72 73 The trouble is that we need this in various formats. Classes 74 mixing this in may give a dateUpdated attribute (a datetime.datetime) 75 that is used to compute both meta elements. 76 77 If any of them is overridden manually, the other is computed from 78 the one given. 79 """
80 - def __getDatetimeMeta(self, key, format):
81 dt = getattr(self, "dateUpdated", None) 82 if dt is None: 83 raise base.NoMetaKey(key, carrier=self) 84 return dt.strftime(format)
85
86 - def _meta_dateUpdated(self):
87 if "datetimeUpdated" in self.meta_: 88 return str(self.meta_["datetimeUpdated"])[:8] 89 return self.__getDatetimeMeta("dateUpdated", "%Y-%m-%d")
90
91 - def _meta_datetimeUpdated(self):
92 if "dateUpdated" in self.meta_: 93 return str(self.meta_["dateUpdated"])+"T00:00:00" 94 return self.__getDatetimeMeta("dateUpdated", utils.isoTimestampFmtNoTZ)
95 96 97 __all__ = ["SERVICELIST_ID", "METADATA_PREFIXES", 98 99 "getResType", "getServicesRD", "getRegistryService", "getDependencies", 100 101 "DateUpdatedMixin", 102 103 "OAIError", "BadArgument", "BadResumptionToken", "BadVerb", 104 "CannotDisseminateFormat", "IdDoesNotExist", 105 "NoMetadataFormats", "NoSetHierarchy", 106 "NoRecordsMatch", 107 ] 108