Пример #1
0
def perform_request_knowledge_bases_management(ln=CFG_SITE_LANG,
                                               search="",
                                               descriptiontoo=""):
    """
    Return 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.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)
Пример #2
0
def perform_request_knowledge_bases_management(ln=CFG_SITE_LANG, search="",
                                               descriptiontoo=""):
    """
    Return 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.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)
Пример #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 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
Пример #4
0
 def test_kbs_info(self):
     """bibknowledge - get_kbs_info returns EJOURNALS info"""
     from invenio.modules.knowledge.api import get_kbs_info
     myinfolist = get_kbs_info("", "EJOURNALS")
     myinfo = myinfolist[0]
     self.assertEqual(myinfo["name"],"EJOURNALS")
Пример #5
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