def get_kbd_values(kbname, searchwith=""): """Return a list of values by searching a dynamic kb. @param kbname: name of the knowledge base @param searchwith: a term to search with """ import search_engine #first check that the kb in question is dynamic kbid = bibknowledge_dblayer.get_kb_id(kbname) if not kbid: return [] kbtype = bibknowledge_dblayer.get_kb_type(kbid) if not kbtype: return [] if kbtype != 'd': return [] #get the configuration so that we see what the field is confdict = bibknowledge_dblayer.get_kb_dyn_config(kbid) if not confdict: return [] if not confdict.has_key('field'): return [] field = confdict['field'] expression = confdict['expression'] collection = "" if confdict.has_key('collection'): collection = confdict['collection'] reclist = [] #return this if searchwith and expression: if (expression.count('%') > 0): expression = expression.replace("%", searchwith) reclist = search_engine.perform_request_search(p=expression, cc=collection) else: #no %.. just make a combination expression = expression + " and "+searchwith reclist = search_engine.perform_request_search(p=expression, cc=collection) else: #either no expr or no searchwith.. but never mind about searchwith if expression: #in this case: only expression reclist = search_engine.perform_request_search(p=expression, cc=collection) else: #make a fake expression so that only records that have this field #will be returned fake_exp = "/.*/" if searchwith: fake_exp = searchwith reclist = search_engine.perform_request_search(f=field, p=fake_exp, cc=collection) if reclist: fieldvaluelist = search_engine.get_most_popular_field_values(reclist, field) val_list = [] for f in fieldvaluelist: (val, dummy) = f val_list.append(val) return val_list return [] #in case nothing worked
def get_kb_id(kb_name): """ Gets the id by name @param kb_name knowledge base name """ return bibknowledge_dblayer.get_kb_id(kb_name)
def get_kbd_values(kbname, searchwith=""): """ To be used by bibedit. Returns a list of values based on a dynamic kb. @param kbname: name of the knowledge base @param searchwith: a term to search with """ import search_engine #first check that the kb in question is dynamic kbid = bibknowledge_dblayer.get_kb_id(kbname) if not kbid: return [] kbtype = bibknowledge_dblayer.get_kb_type(kbid) if not kbtype: return [] if kbtype != 'd': return [] #get the configuration so that we see what the field is confdict = bibknowledge_dblayer.get_kb_dyn_config(kbid) if not confdict: return [] if not confdict.has_key('field'): return [] field = confdict['field'] expression = confdict['expression'] collection = "" if confdict.has_key('collection'): collection = confdict['collection'] reclist = [] #return this #see if searchwith is a quoted expression if searchwith: if not searchwith.startswith("'"): searchwith = "'"+searchwith if not searchwith.endswith("'"): searchwith = searchwith+"'" if searchwith and expression: if (expression.count('%') > 0) or (expression.endswith(":*")): expression = expression.replace("%", searchwith) expression = expression.replace(":*", ':'+searchwith) else: #no %.. just make a combination expression = expression + "and "+searchwith reclist = search_engine.perform_request_search(p=expression, cc=collection) else: #either no expr or no searchwith.. but never mind about searchwith if expression: reclist = search_engine.perform_request_search(p=expression, cc=collection) else: #make a fake expression so that only records that have this field #will be returned fake_exp = "/.*/" if searchwith: fake_exp = searchwith reclist = search_engine.perform_request_search(f=field, p=fake_exp, cc=collection) if reclist: fieldvaluelist = search_engine.get_most_popular_field_values(reclist, field) val_list = [] for f in fieldvaluelist: (val, dummy) = f #support "starts with", #indicated by the * at the end of the searchstring if searchwith and (len(searchwith) > 2) and (searchwith[-2] == '*'): if (val.startswith(searchwith[1:-3])): val_list.append(val) else: val_list.append(val) return val_list return [] #in case nothing worked
def get_kbd_values(kbname, searchwith=""): """ To be used by bibedit. Returns a list of values based on a dynamic kb. @param kbname: name of the knowledge base @param searchwith: a term to search with """ import search_engine #first check that the kb in question is dynamic kbid = bibknowledge_dblayer.get_kb_id(kbname) if not kbid: return [] kbtype = bibknowledge_dblayer.get_kb_type(kbid) if not kbtype: return [] if kbtype != 'd': return [] #get the configuration so that we see what the field is confdict = bibknowledge_dblayer.get_kb_dyn_config(kbid) if not confdict: return [] if not confdict.has_key('field'): return [] field = confdict['field'] expression = confdict['expression'] collection = "" if confdict.has_key('collection'): collection = confdict['collection'] reclist = [] #return this #see if searchwith is a quoted expression if searchwith: if not searchwith.startswith("'"): searchwith = "'" + searchwith if not searchwith.endswith("'"): searchwith = searchwith + "'" if searchwith and expression: if (expression.count('%') > 0) or (expression.endswith(":*")): expression = expression.replace("%", searchwith) expression = expression.replace(":*", ':' + searchwith) else: #no %.. just make a combination expression = expression + "and " + searchwith reclist = search_engine.perform_request_search(p=expression, cc=collection) else: #either no expr or no searchwith.. but never mind about searchwith if expression: reclist = search_engine.perform_request_search(p=expression, cc=collection) else: #make a fake expression so that only records that have this field #will be returned fake_exp = "/.*/" if searchwith: fake_exp = searchwith reclist = search_engine.perform_request_search(f=field, p=fake_exp, cc=collection) if reclist: fieldvaluelist = search_engine.get_most_popular_field_values( reclist, field) val_list = [] for f in fieldvaluelist: (val, dummy) = f #support "starts with", #indicated by the * at the end of the searchstring if searchwith and (len(searchwith) > 2) and (searchwith[-2] == '*'): if (val.startswith(searchwith[1:-3])): val_list.append(val) else: val_list.append(val) return val_list return [] #in case nothing worked