def perform_request_knowledge_bases_management(ln=CFG_SITE_LANG, search="", descriptiontoo=""): """ Returns the main page for knowledge bases management. @param ln language @param search search for this string in kb's @param descriptiontoo search in descriptions too @return the main page for knowledge bases management """ kbs = bibknowledge.get_kbs_info() #if search is nonempty, filter out kb's that do not have the #the string that we search newkbs = [] if search: for kb in kbs: skip = 0 #do-we-need-to-scan-more control kbname = kb['name'] #get description if needed if descriptiontoo and kb['description'].count(search) > 0: #add and skip newkbs.append(kb) skip = 1 #likewise: check if name matches if descriptiontoo and kbname.count(search) > 0: #add and skip newkbs.append(kb) skip = 1 #get mappings mappings = bibknowledge_dblayer.get_kb_mappings(kbname) for mapping in mappings: if skip == 0: key = mapping['key'] value = mapping['value'] if key.count(search) > 0 or value.count(search) > 0: #add this in newkbs newkbs.append(kb) #skip the rest, we know there's ok stuff in this kb skip = 1 kbs = newkbs return bibknowledge_templates.tmpl_admin_kbs_management(ln, kbs, search)
def perform_request_knowledge_bases_management(ln=CFG_SITE_LANG, search="", descriptiontoo=""): """ Returns the main page for knowledge bases management. @param ln language @param search search for this string in kb's @param descriptiontoo search in descriptions too @return the main page for knowledge bases management """ kbs = bibknowledge.get_kbs_info() #if search is nonempty, filter out kb's that do not have the #the string that we search newkbs = [] if search: for kb in kbs: skip = 0 #do-we-need-to-scan-more control kbname = kb['name'] #get description if needed if descriptiontoo and kb['description'].count(search) > 0: #add and skip newkbs.append(kb) skip = 1 #likewise: check if name matches if descriptiontoo and kbname.count(search) > 0: #add and skip newkbs.append(kb) skip = 1 #get mappings mappings = bibknowledge_dblayer.get_kb_mappings(kbname) for mapping in mappings: if skip == 0: key = mapping['key'] value = mapping['value'] if key.count(search)> 0 or value.count(search)> 0: #add this in newkbs newkbs.append(kb) #skip the rest, we know there's ok stuff in this kb skip = 1 kbs = newkbs return bibknowledge_templates.tmpl_admin_kbs_management(ln, kbs, search)
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 test_kbs_info(self): """bibknowledge - get_kbs_info returns EJOURNALS info""" myinfolist = get_kbs_info("", "EJOURNALS") myinfo = myinfolist[0] self.assertEqual(myinfo["name"],"EJOURNALS")
def test_kbs_info(self): """bibknowledge - get_kbs_info returns EJOURNALS info""" myinfolist = get_kbs_info("", "EJOURNALS") myinfo = myinfolist[0] self.assertEqual(myinfo["name"], "EJOURNALS")
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 kb_export(req, kbname="", format="kbr", searchkey="", searchvalue="", searchtype="s", 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 format kba for an authority file, kbr for a leftside-rightside kb @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 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_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': #get the kb and print it mappings = bibknowledge.get_kb_mappings(kbname, searchkey, \ searchvalue, searchtype) if format == 'jquery': ret = [] for m in mappings: label = m['value'] or m['key'] value = m['key'] or m['value'] ret.append({'label': label, 'value': value}) req.content_type = 'application/json' return json.dumps(ret) if not mappings: body = "There is no knowledge base named "+kbname+" or it is empty", return page(title=_("No such knowledge base"), body=body, language=ln, navtrail=navtrail_previous_links, lastupdated=__lastupdated__, req=req) else: #there were mappings seq = [] #sequence: right sides need to made unique for m in mappings: mkey = m['key'] mvalue = m['value'] if format == "right" or format == "kba": seq.append(mvalue) else: req.write(mkey+"---"+mvalue+"\n") #make unique seq and print it useq = uniq(seq) for s in useq: req.write(s+"\n") if kbtype == 'd': #dynamic type: call export #ok, let's search.. res = bibknowledge.get_kbd_values(kbname, searchvalue) if not res: req.write("\n") #in order to say something for r in res: if not searchvalue: req.write(r+"\n") #output all if searchvalue and r.count(searchvalue) > 0: req.write(r+"\n") #output matching lines if 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.")
def kb_export(req, kbname="", format="kbr", searchkey="", searchvalue="", searchtype="s", 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 format kba for an authority file, kbr for a leftside-rightside kb @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 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_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': #get the kb and print it mappings = bibknowledge.get_kb_mappings(kbname, searchkey, \ searchvalue, searchtype) if format == 'jquery': ret = [] for m in mappings: label = m['value'] or m['key'] value = m['key'] or m['value'] ret.append({'label': label, 'value': value}) req.content_type = 'application/json' return json.dumps(ret) if not mappings: body = "There is no knowledge base named "+kbname+" or it is empty", return page(title=_("No such knowledge base"), body=body, language=ln, navtrail=navtrail_previous_links, lastupdated=__lastupdated__, req=req) else: #there were mappings seq = [] #sequence: right sides need to made unique for m in mappings: mkey = m['key'] mvalue = m['value'] if format == "right" or format == "kba": seq.append(mvalue) else: req.write(mkey+"---"+mvalue+"\n") #make unique seq and print it useq = uniq(seq) for s in useq: req.write(s+"\n") if kbtype == 'd': #dynamic type: call export #ok, let's search.. res = bibknowledge.get_kbd_values(kbname, searchvalue) if not res: req.write("\n") #in order to say something for r in res: if not searchvalue: req.write(r+"\n") #output all if searchvalue and r.count(searchvalue) > 0: req.write(r+"\n") #output matching lines if 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.")