Example #1
0
def UrlInHtmlMode(anUrl):
    urlMode = lib_util.GetModeFromUrl(anUrl)
    # sys.stderr.write("UrlInHtmlMode anUrl=%s urlMode=%s\n"%(anUrl,urlMode))
    if urlMode:
        return anUrl
    else:
        return lib_util.AnyUriModed(anUrl, "html")
def _add_survol_node(grph, host_survol, url_survol_clean):
    logging.debug("AddSurvolNode hostSurvol=%s", host_survol)
    survol_host_node = lib_uris.gUriGen.HostnameUri(host_survol)

    curr_disp_mode = lib_util.GuessDisplayMode()

    # Several possibilities:
    # - Open a new HTML page with this URL. Or SVG, passed on the current mode.
    # - If we are in D3 mode, this should return a JSON object from the other agent.
    if curr_disp_mode == "json":
        server_box = lib_uris.OtherAgentBox(host_survol)

        # This is the URL of the remote host, on the remote agent.
        node_remote_host = server_box.HostnameUri(host_survol)
        grph.add((survol_host_node, lib_common.MakeProp("Survol host"), node_remote_host))

        node_survol_url = lib_common.NodeUrl(url_survol_clean)
        grph.add((survol_host_node, lib_common.MakeProp("Survol agent"), node_survol_url))
    else:
        url_survol_moded = lib_util.AnyUriModed(url_survol_clean, curr_disp_mode)

        node_survol_url = lib_common.NodeUrl(url_survol_moded)

        # Should check the URL to be sure it is valid.
        grph.add((survol_host_node, lib_common.MakeProp("Survol agent"), node_survol_url))

    return node_survol_url
def AddSurvolNode(grph, hostSurvol, urlSurvolClean):
    sys.stderr.write("AddSurvolNode hostSurvol=%s\n" % (hostSurvol))
    survolHostNode = lib_common.gUriGen.HostnameUri(hostSurvol)

    currDispMode = lib_util.GuessDisplayMode()

    # Several possibilities:
    # - Open a new HTML page with this URL. Or SVG, passed on the current mode.
    # - If we are in D3 mode, this should return a JSON object from the other agent.
    if currDispMode == "json":

        if lib_util.IsLocalAddress(hostSurvol):
            machName_or_None = None
            serverBox = lib_common.gUriGen
        else:
            machName_or_None = hostSurvol
            serverBox = lib_common.OtherAgentBox(urlSurvolClean)

        # This is the URL of the remote host, on the remote agent.
        nodeRemoteHost = serverBox.HostnameUri(hostSurvol)
        grph.add((survolHostNode, lib_common.MakeProp("Survol host"),
                  nodeRemoteHost))

        nodeSurvolUrl = lib_common.NodeUrl(urlSurvolClean)
        grph.add((survolHostNode, lib_common.MakeProp("Survol agent"),
                  nodeSurvolUrl))

    else:
        urlSurvolModed = lib_util.AnyUriModed(urlSurvolClean, currDispMode)

        nodeSurvolUrl = lib_common.NodeUrl(urlSurvolModed)

        # Should check the URL to be sure it is valid.

        # sys.stderr.write("AddSurvolNode urlSurvolModed=%s\n"%(urlSurvolModed))
        grph.add((survolHostNode, lib_common.MakeProp("Survol agent"),
                  nodeSurvolUrl))

    return nodeSurvolUrl
Example #4
0
def DispClassObjects(dictSubjPropObj):
    listPropsTdDoubleColSpan = [
        pc.property_information, pc.property_rdf_data_nolist2,
        pc.property_rdf_data_nolist1
    ]

    WrtAsUtf('<table class="class_objects">')

    # The subjects must be sorted by their title.
    lstTuplesSubjects = []
    for aSubj in dictSubjPropObj:
        subj_str = str(aSubj)
        (subj_title, entity_graphic_class,
         entity_id) = lib_naming.ParseEntityUri(subj_str)
        lstTuplesSubjects.append(
            (aSubj, subj_str, subj_title, entity_graphic_class, entity_id))

    # Sorted by the title of the subject, which is the third value of the tuple.
    lib_util.natural_sort_list(lstTuplesSubjects, key=lambda tup: tup[2])

    # Now it iterates on the sorted list.
    # This reuses all the intermediate values.
    for aSubj, subj_str, subj_title, entity_graphic_class, entity_id in lstTuplesSubjects:
        dictPred = dictSubjPropObj[aSubj]

        arrayGraphParams = lib_patterns.TypeToGraphParams(entity_graphic_class)
        # "Graphic_shape","Graphic_colorfill","Graphic_colorbg","Graphic_border","Graphic_is_rounded"
        colorClass = arrayGraphParams[1]

        # Total number of lines.
        cntPreds = 0
        for aPred in dictPred:
            lstObjs = dictPred[aPred]
            cntPreds += len(lstObjs)

        mustWriteColOneSubj = True

        subj_str_with_mode = UrlInHtmlMode(subj_str)

        # The predicates, i.e. the properties associated a subject with an object,
        # must be alphabetically sorted.
        for aPred in lib_util.natural_sorted(dictPred):
            lstObjs = dictPred[aPred]

            predStr = lib_exports.AntiPredicateUri(str(aPred))
            cntObjs = len(lstObjs)
            mustWriteColOnePred = True

            # The objects must be sorted by title.
            lstTuplesObjs = []
            for anObj in lstObjs:
                obj_str = str(anObj)
                obj_str = DesHex(obj_str)
                obj_title = lib_naming.ParseEntityUri(obj_str)[0]
                lstTuplesObjs.append((anObj, obj_str, obj_title))

            # Sorted by the title of the object, which is the third value of the tuple.
            lib_util.natural_sort_list(lstTuplesObjs, key=lambda tup: tup[2])

            for anObj, obj_str, obj_title in lstTuplesObjs:

                WrtAsUtf('<tr bgcolor="%s">' % colorClass)

                if mustWriteColOneSubj:
                    WrtAsUtf(
                        '<td valign="top" rowspan="%s"><a href="%s">%s</a></td>'
                        % (str(cntPreds), subj_str_with_mode, subj_title))
                    mustWriteColOneSubj = False

                if mustWriteColOnePred:
                    if aPred not in listPropsTdDoubleColSpan:
                        WrtAsUtf('<td valign="top" rowspan="%s">%s</td>' %
                                 (str(cntObjs), predStr))
                    mustWriteColOnePred = False

                if aPred in listPropsTdDoubleColSpan:
                    colSpan = 2
                else:
                    colSpan = 1

                dispMimeUrls = True

                WrtAsUtf('<td colspan="%d">' % (colSpan))
                if dispMimeUrls:
                    if lib_kbase.IsLink(anObj):
                        objStrClean = lib_util.UrlNoAmp(obj_str)
                        mimeType = lib_mime.GetMimeTypeFromUrl(objStrClean)
                        if mimeType:
                            if mimeType.startswith("image/"):
                                WrtAsUtf(
                                    """<a href="%s"><img src="%s" alt="%s" height="42" width="42"></a>"""
                                    % (obj_str, obj_str, obj_title))
                            else:
                                WrtAsUtf("""<a href="%s">%s</a>""" %
                                         (obj_str, obj_title))
                        else:
                            url_with_mode = lib_util.AnyUriModed(
                                obj_str, "html")
                            WrtAsUtf("""<a href="%s">%s</a>""" %
                                     (url_with_mode, obj_title))
                    else:
                        WrtAsUtf('%s' % (obj_str))
                else:
                    if lib_kbase.IsLink(anObj):
                        url_with_mode = UrlInHtmlMode(obj_str)
                        WrtAsUtf('<a href="%s">%s</a>' %
                                 (url_with_mode, obj_title))
                    else:
                        WrtAsUtf('%s' % (obj_str))

                WrtAsUtf("</td>")

                WrtAsUtf("</tr>")

    WrtAsUtf(" </table>")
Example #5
0
def _display_class_objects_no_jinja(dict_subj_prop_obj):
    # The subjects must be sorted by their title.
    tuples_subjects_list = []
    for a_subj in dict_subj_prop_obj:
        subj_str = str(a_subj)
        (subj_title, entity_graphic_class,
         entity_id) = lib_naming.ParseEntityUri(subj_str)
        if subj_title:
            # The intention is to detect a specific test case with accented characters.
            if subj_title[0] == 'Y' and subj_title.find("Boulogne"):
                sys.stderr.write(
                    "_display_class_objects_no_jinja subj_str=%s\n" % subj_str)
                sys.stderr.write(
                    "_display_class_objects_no_jinja subj_title=%s\n" %
                    subj_title)
                continue
        else:
            sys.stderr.write("NO TITLE FOR %s\n" % subj_str)
        tuples_subjects_list.append(
            (a_subj, subj_str, subj_title, entity_graphic_class, entity_id))

    # Sorted by the title of the subject, which is the third value of the tuple.
    lib_util.natural_sort_list(tuples_subjects_list, key=lambda tup: tup[2])

    # Apparently, a problem is that "%" gets transformed into an hexadecimal number, preventing decoding.
    def _custom_decode_hex(the_str):
        the_str = lib_util.survol_unescape(the_str)
        return the_str.replace("%25", "%").replace("%2F", "/").replace(
            "%5C", "\\").replace("%3A", ":")

    # Now it iterates on the sorted list.
    # This reuses all the intermediate values.
    for a_subj, subj_str, subj_title, entity_graphic_class, entity_id in tuples_subjects_list:
        # FIXME: This is a specific test to catch a specific condition...
        if a_subj.find("Boulogne") >= 0 or subj_str.find(
                "Boulogne") >= 0 or subj_title.find("Boulogne") >= 0:
            sys.stderr.write("a_subj=%s\n" % a_subj)
            sys.stderr.write("subj_str=%s\n" % subj_str)
            sys.stderr.write("subj_title=%s\n" % subj_title)
            continue

        dict_pred = dict_subj_prop_obj[a_subj]

        # Total number of lines.
        cnt_preds = 0
        for a_pred in dict_pred:
            lst_objs = dict_pred[a_pred]
            cnt_preds += len(lst_objs)

        must_write_col_one_subj = True

        subj_str_with_mode = _url_in_html_mode(subj_str)

        # The predicates, i.e. the properties associated a subject with an object,
        # must be alphabetically sorted.
        for a_pred in lib_util.natural_sorted(dict_pred):
            lst_objs = dict_pred[a_pred]

            pred_str = lib_exports.AntiPredicateUri(str(a_pred))
            cnt_objs = len(lst_objs)
            must_write_col_one_pred = True

            # The objects must be sorted by title.
            lst_tuples_objs = []
            for an_obj in lst_objs:
                obj_str = str(an_obj)
                obj_str = _custom_decode_hex(obj_str)
                obj_title = lib_naming.ParseEntityUri(obj_str)[0]
                lst_tuples_objs.append((an_obj, obj_str, obj_title))

            # Sorted by the title of the object, which is the third value of the tuple.
            lib_util.natural_sort_list(lst_tuples_objs, key=lambda tup: tup[2])

            for an_obj, obj_str, obj_title in lst_tuples_objs:
                # FIXME: This is a specific test to catch a specific condition...
                if an_obj.find("Boulogne") >= 0 or obj_str.find(
                        "Boulogne") >= 0 or obj_title.find("Boulogne") >= 0:
                    sys.stderr.write("an_obj=%s\n" % an_obj)
                    sys.stderr.write("obj_str=%s\n" % obj_str)
                    sys.stderr.write("obj_title=%s\n" % obj_title)
                    continue

                yield ('<tr>')

                if must_write_col_one_subj:
                    yield (
                        '<td valign="top" rowspan="%s"><a href="%s">%s</a></td>'
                        % (str(cnt_preds), subj_str_with_mode, subj_title))
                    must_write_col_one_subj = False

                if must_write_col_one_pred:
                    if a_pred not in _list_props_td_double_col_span:
                        yield ('<td valign="top" rowspan="%s">%s</td>' %
                               (str(cnt_objs), pred_str))
                    must_write_col_one_pred = False

                if a_pred in _list_props_td_double_col_span:
                    col_span = 2
                else:
                    col_span = 1

                disp_mime_urls = True

                yield ('<td colspan="%d">' % (col_span))
                if disp_mime_urls:
                    if lib_kbase.IsLink(an_obj):
                        objStrClean = lib_util.UrlNoAmp(obj_str)
                        mimeType = lib_mime.GetMimeTypeFromUrl(objStrClean)
                        if mimeType:
                            if mimeType.startswith("image/"):
                                yield (
                                    """<a href="%s"><img src="%s" alt="%s" height="42" width="42"></a>"""
                                    % (obj_str, obj_str, obj_title))
                            else:
                                yield ("""<a href="%s">%s</a>""" %
                                       (obj_str, obj_title))
                        else:
                            url_with_mode = lib_util.AnyUriModed(
                                obj_str, "html")
                            yield ("""<a href="%s">%s</a>""" %
                                   (url_with_mode, obj_title))
                    else:
                        yield ('%s' % (obj_str))
                else:
                    if lib_kbase.IsLink(an_obj):
                        url_with_mode = _url_in_html_mode(obj_str)
                        yield ('<a href="%s">%s</a>' %
                               (url_with_mode, obj_title))
                    else:
                        yield ('%s' % (obj_str))

                yield ("</td>")
                yield ("</tr>")
Example #6
0
def ParseEntityUri(uriWithMode, longDisplay=True, force_entity_ip_addr=None):
    #sys.stderr.write("ParseEntityUri uriWithMode=%s\n"%uriWithMode)

    # Maybe there is a host name before the entity type. It can contain letters, numbers,
    # hyphens, dots etc... but no ":" or "@".
    # THIS CANNOT WORK WITH IPV6 ADDRESSES...
    # WE MAY USE SCP SYNTAX: scp -6 osis@\[2001:db8:0:1\]:/home/osis/test.file ./test.file

    # In the URI, we might have the CGI parameter "&mode=json". It must be removed otherwise
    # it could be taken in entity_id, and the result of EntityToLabel() would be wrong.
    uriWithModeClean = lib_util.UrlNoAmp(uriWithMode)
    uri = lib_util.AnyUriModed(uriWithModeClean, "")
    uriMode = lib_util.GetModeFromUrl(uriWithModeClean)

    uprs = lib_util.survol_urlparse(uri)

    filScript = os.path.basename(uprs.path)
    # sys.stderr.write("ParseEntityUri filScript=%s\n"%filScript)

    # This works for the scripts:
    # entity.py            xid=namespace/type:idGetNamespaceType
    # objtypes_wbem.py     Just extracts the namespace, as it prefixes the type: xid=namespace/type:id

    # See variable lib_util.xidCgiDelimiter="?xid="
    if uprs.query.startswith("xid="):
        # TODO: Maybe the chain contains HTML codes and therefore cannot be parsed.
        # Ex: "xid=%40%2F%3Aoracle_package." == "xid=@/:oracle_package."
        (entity_type, entity_id,
         entity_host) = lib_util.ParseXid(uprs.query[4:])

        entity_graphic_class = entity_type

        entity_label = CalcLabel(entity_host, entity_type, entity_id,
                                 force_entity_ip_addr, filScript)

        # TODO: Consider external_url_to_title, similar logic with different results.
        if longDisplay:
            entity_label = KnownScriptToTitle(filScript, uriMode, entity_host,
                                              entity_label)

    # Maybe an internal script, but not entity.py
    # It has a special entity type as a display parameter
    elif uri.startswith(lib_util.uriRoot):
        # This is a bit of a special case which allows to display something if we know only
        # the type of the entity but its id is undefined. Instead of displaying nothing,
        # this attempts to display all available entities of this given type.
        # source_top/enumerate_process.py etc... Not "." because this has a special role in Python.
        mtch_enumerate = re.match(r"^.*/enumerate_([a-z0-9A-Z_]*)\.py$", uri)
        if mtch_enumerate:
            entity_graphic_class = mtch_enumerate.group(1)
            entity_id = ""
            # TODO: Change this label, not very nice.
            # This indicates that a specific script can list all objects of a given entity type.
            entity_label = entity_graphic_class + " enumeration"
        else:
            entity_graphic_class = "provider_script"
            entity_id = ""

            entity_label = KnownScriptToTitle(filScript, uriMode)

    elif uri.split(':')[0] in ["ftp", "http", "https", "urn", "mail"]:
        # Standard URLs. Example: lib_common.NodeUrl( "http://www.google.com" )
        entity_graphic_class = ""
        entity_id = ""
        # Display the complete URL, otherwise it is not clickable.
        entity_label = uriWithMode  # uri # uri.split('/')[2]

    else:
        entity_graphic_class = ""
        entity_id = "PLAINTEXTONLY"
        entity_label = UriToTitle(uprs)
        # TODO: " " are replaced by "%20". Why ? So change back.
        entity_label = entity_label.replace("%20", " ")

    return (entity_label, entity_graphic_class, entity_id)