예제 #1
0
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 = ''' &gt; <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_get_kbd_values(self):
     """bibknowledge - search a "find x by y" kb"""
     vals = get_kbd_values(self.dyn_kbname, "Rodentia")
     self.assertEqual(1, len(vals))
     self.assertEqual(vals[0], 'Charles Darwin')
예제 #3
0
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 = ''' &gt; <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_get_kbd_values(self):
     """bibknowledge - search a "find x by y" kb"""
     vals = get_kbd_values(self.dyn_kbname, "Rodentia")
     self.assertEqual(1, len(vals))
     self.assertEqual(vals[0], 'Charles Darwin')
예제 #5
0
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 = ''' &gt; <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.")
예제 #6
0
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 = ''' &gt; <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.")