def kb_export(req, kbname="", format="kbr", searchkey="", searchvalue="", searchtype="s", limit=None, ln=CFG_SITE_LANG): """ Exports the given kb so that it is listed in stdout (the browser). @param req the request @param kbname knowledge base name @param expression evaluate this for the returned lines @param format 'kba' for authority file, 'kbr' for leftside-rightside, json for json-formatted dictionaries @param searchkey include only lines that match this on the left side @param searchvalue include only lines that match this on the right side @param searchtype s = substring match, e = exact match @param limit how many results to return. None means all @param ln language """ ln = wash_language(ln) _ = gettext_set_language(ln) navtrail_previous_links = ''' > <a class="navtrail" href="%s/kb?ln=%s">%s</a>''' % (CFG_SITE_SECURE_URL, ln, _("Manage Knowledge Bases")) if not kbname: return page(title=_("Knowledge base name missing"), body = """Required parameter kbname is missing.""", language=ln, navtrail = navtrail_previous_links, lastupdated=__lastupdated__, req=req) #in order to make 'wget' downloads easy we do not require authorization #first check the type of the KB kbtype = None kbinfo = None kbid = None kbinfos = bibknowledge.get_kbs_info("", kbname) if kbinfos: kbinfo = kbinfos[0] kbtype = kbinfo['kbtype'] kbid = kbinfo['id'] else: return page(title=_("Unknown knowledge base"), body = _("There is no knowledge base with that name."), language=ln, navtrail = navtrail_previous_links, lastupdated=__lastupdated__, req=req) if not kbtype or kbtype == 'w': if format and format == "ejson": req.content_type = 'application/json' return bibknowledge.get_kb_mappings_embedded_json(kbname, searchkey, \ searchvalue, searchtype, limit) elif format and format[0] == 'j': # as JSON formatted string req.content_type = 'application/json' return bibknowledge.get_kb_mappings_json(kbname, searchkey, \ searchvalue, searchtype, limit) # left side / right side KB mappings = bibknowledge.get_kb_mappings(kbname, searchkey, \ searchvalue, searchtype) if format == 'right' or format == 'kba': # as authority sequence seq = [m['value'] for m in mappings] seq = uniq(sorted(seq)) for s in seq: req.write(s+"\n"); return else: # as regularly formatted left-right mapping for m in mappings: req.write(m['key'] + '---' + m['value'] + '\n') return elif kbtype == 'd': # dynamic kb, another interface for perform_request_search if format and format[0] == 'j': req.content_type = "application/json" return bibknowledge.get_kbd_values_json(kbname, searchvalue) else: # print it as a list of values for hit in bibknowledge.get_kbd_values(kbname, searchvalue): req.write(hit + '\n') req.write('\n') return elif kbtype == 't': #taxonomy: output the file kbfilename = CFG_WEBDIR+"/kbfiles/"+str(kbid)+".rdf" try: f = open(kbfilename, 'r') for line in f: req.write(line) f.close() except: req.write("Reading the file "+kbfilename+" failed.") else: # This situation should never happen raise ValueError, "Unsupported KB Type: %s" % kbtype
def kb_export(req, kbname="", format="kbr", searchkey="", searchvalue="", searchtype="s", limit=None, ln=CFG_SITE_LANG): """ Exports the given kb so that it is listed in stdout (the browser). @param req the request @param kbname knowledge base name @param expression evaluate this for the returned lines @param format 'kba' for authority file, 'kbr' for leftside-rightside, json for json-formatted dictionaries @param searchkey include only lines that match this on the left side @param searchvalue include only lines that match this on the right side @param searchtype s = substring match, e = exact match @param limit how many results to return. None means all @param ln language """ ln = wash_language(ln) _ = gettext_set_language(ln) navtrail_previous_links = ''' > <a class="navtrail" href="%s/kb?ln=%s">%s</a>''' % ( CFG_SITE_SECURE_URL, ln, _("Manage Knowledge Bases")) if not kbname: return page(title=_("Knowledge base name missing"), body="""Required parameter kbname is missing.""", language=ln, navtrail=navtrail_previous_links, lastupdated=__lastupdated__, req=req) #in order to make 'wget' downloads easy we do not require authorization #first check the type of the KB kbtype = None kbinfo = None kbid = None kbinfos = bibknowledge.get_kbs_info("", kbname) if kbinfos: kbinfo = kbinfos[0] kbtype = kbinfo['kbtype'] kbid = kbinfo['id'] else: return page(title=_("Unknown knowledge base"), body=_("There is no knowledge base with that name."), language=ln, navtrail=navtrail_previous_links, lastupdated=__lastupdated__, req=req) if kbtype == None or kbtype == 'w': if format and format == "ejson": req.content_type = 'application/json' return bibknowledge.get_kb_mappings_embedded_json(kbname, searchkey, \ searchvalue, searchtype, limit) elif format and format[0] == 'j': # as JSON formatted string req.content_type = 'application/json' return bibknowledge.get_kb_mappings_json(kbname, searchkey, \ searchvalue, searchtype, limit) # left side / right side KB mappings = bibknowledge.get_kb_mappings(kbname, searchkey, \ searchvalue, searchtype) if format == 'right' or format == 'kba': # as authority sequence seq = [m['value'] for m in mappings] seq = uniq(sorted(seq)) for s in seq: req.write(s + "\n") return else: # as regularly formatted left-right mapping for m in mappings: req.write(m['key'] + '---' + m['value'] + '\n') return elif kbtype == 'd': # dynamic kb, another interface for perform_request_search if format and format[0] == 'j': req.content_type = "application/json" return bibknowledge.get_kbd_values_json(kbname, searchvalue) else: # print it as a list of values for hit in bibknowledge.get_kbd_values(kbname, searchvalue): req.write(hit + '\n') req.write('\n') return elif kbtype == 't': #taxonomy: output the file kbfilename = CFG_WEBDIR + "/kbfiles/" + str(kbid) + ".rdf" try: f = open(kbfilename, 'r') for line in f: req.write(line) f.close() except: req.write("Reading the file " + kbfilename + " failed.") else: # This situation should never happen raise ValueError, "Unsupported KB Type: %s" % kbtype
def test_EJOURNALS_get_as_json(self): """bibknowledge - get key-value mappings as json list""" api_returns = get_kb_mappings_json('EJOURNALS', 'AAS', 'AAS', 's') expected = '[{"value": "AAS Photo Bull.", "label": "AAS Photo Bull."}]' self.assertEqual(expected, api_returns)