Ejemplo n.º 1
0
def getInstances(cache, className, filterNotExists=None):
    """
    Get the instances of a class.
    """
    INFO("    Get instances of %s" %className)

    if filterNotExists is None:
        filterNotExistsLine = ""
    else:
        filterNotExistsLine = "FILTER NOT EXISTS { %s }" %filterNotExists

    results = QUERY("""
        SELECT DISTINCT ?instance ?label ?comment ?counter ?rdfClass
        WHERE {
            ?instance rdf:type/rdfs:subClassOf* %s .
            OPTIONAL { ?instance rdfs:label ?label } .
            OPTIONAL { ?instance rdfs:comment ?comment } .
            OPTIONAL { ?instance ontoscript:counter ?counter } .
            OPTIONAL { ?instance a/(rdfs:subClassOf*) ?rdfClass . FILTER (!isBlank(?rdfClass)) } .
            %s
        }
        """ %(className, filterNotExistsLine))

    d = {}
    for uri, label, comment, counter, rdfClass in results:
        qname = URI_TO_QNAME(uri)

        if not d.has_key(qname):
            d[qname] = Node(
                        qname           = qname,
                        uri             = uri.toPython(),
                        cache           = cache)

        if label is not None:
            d[qname]["label"] = label.toPython()
        if comment is not None:
            d[qname]["comment"] = comment.toPython()
        if counter is not None:
            d[qname]["counter"] = int(counter.toPython())
        if rdfClass is not None:
            d[qname].registerClass(URI_TO_QNAME(rdfClass.toPython()))

    keysStr = ""
    for key in d.keys():
        keysStr += (key + " ")

    INFO("     --> " + keysStr)

    for qname, node in d.items():

        node.registerKnownViews()

        if not cache.has_key(qname):
            DEBUG("Caching %s" %qname)
            cache[qname] = node

    # return a list of QNames
    ret = [] # list of qnames
    resultNodes =  sorted(d.values(), key=lambda x: x["counter"])
    for resultNode in resultNodes:
        ret.append(resultNode['qname'])
    return ret
Ejemplo n.º 2
0
def fillFields(node, mandatories={}, optionals={}):
    """
    Fill some mandatory and/or optional fields of a node.
    """
    subject = node['qname']

    INFO("    Fill these fields of %s: mandatories=%s, optionals=%s" %(subject, mandatories.keys(), optionals.keys()))

    selectLine = ""
    wherePart = ""

    for key,value in mandatories.items():
        selectLine += " ?%s" %key
        wherePart += "%s %s ?%s .\n" %(subject, value, key)

    for key,value in optionals.items():
        selectLine += " ?%s" %key
        wherePart += "OPTIONAL { %s %s ?%s } .\n" %(subject, value, key)

    query = """
        SELECT DISTINCT %s
        WHERE {
            %s
        }
        """ %(selectLine, wherePart)

    results = QUERY(query)

    if len(results) == 0:
        raise Exception("No results for query:\n%s" %query)


    infoStr = "     --> mandatories ["

    for result in results:
        # the mandatories
        for i in xrange(len(mandatories)):
            key = mandatories.keys()[i]
            try:
                if IS_URI(result[i]):
                    node.cache[subject][key] = URI_TO_QNAME(result[i].toPython())
                else:
                    node.cache[subject][key] = result[i].toPython()
            except:
                node.cache[subject][key] = None

            if i > 0: infoStr += ","
            infoStr += str(node.cache[subject][key])

        infoStr += "], optionals ["

        for i in xrange(len(optionals)):
            key = optionals.keys()[i]
            try:
                j = len(mandatories) + i
                if IS_URI(result[j]):
                    node.cache[subject][key] = URI_TO_QNAME(result[j].toPython())
                else:
                    node.cache[subject][key] = result[j].toPython()
            except:
                node.cache[subject][key] = None

            if i > 0: infoStr += ","
            infoStr += str(node.cache[subject][key])

        infoStr += "]"

    INFO(infoStr)
Ejemplo n.º 3
0
    sanitizer = LogSanitizer()
    sanitizer.sanitize_files(args)
    sanitizer.save_secrets(args.out, __version__)

    if args.time:
        sanitizer.timings()


#===============================================================================

# LINE = "Failed opening persistent JSON file C:\Program Files\SentinelOne\Sentinel Agent 4.3.2.86\config\LocationEngineState.json with error 2"
LINE = "Starting loop sentinel::ResourceMonitor::{ctor}::<lambda_3>::operator ()"


def test():
    sanitizer = LogSanitizer()
    output = sanitizer.sanitize_line(LINE)
    print(output)


#===============================================================================

if __name__ == "__main__":
    try:
        # test()
        main()
    except Exception as e:
        print_exc()

    INFO("Done.")
Ejemplo n.º 4
0
def getRelated(cache, subject, property, restriction=None, remove=None,  sortedByNumber=False, filterNotExists=None):
    """
    Get the related individuals of an individual.
    """
    INFO("    Get related %s of %s" %(property, subject))

    extraVariables = ""

    if restriction is None:
        restrictionLine = ""
    else:
        restrictionLine = "\n            ?result rdf:type/rdfs:subClassOf* %s ." %restriction

    if remove is None:
        removeLine = ""
    else:
        removeLine = "\n            FILTER NOT EXISTS { %s %s ?result }" %(subject, remove)

    if sortedByNumber:
        numberLine = "\n            OPTIONAL { ?result (cont:isItemOf|(^sys:hasItem))/cont:hasNumber ?number }"
        extraVariables += "?number"
    else:
        numberLine = ""

    if filterNotExists is None:
        filterNotExistsLine = ""
    else:
        filterNotExistsLine = "FILTER NOT EXISTS { %s }" %filterNotExists

    results = QUERY("""
        SELECT DISTINCT ?result ?label ?comment ?counter ?rdfClass %s
        WHERE {
            %s %s ?result . %s%s%s
            OPTIONAL { ?result rdfs:label ?label } .
            OPTIONAL { ?result rdfs:comment ?comment } .
            OPTIONAL { ?result ontoscript:counter ?counter } .
            OPTIONAL { ?result a/(rdfs:subClassOf*) ?rdfClass . FILTER (!isBlank(?rdfClass)) } .
            %s
        }
        """ %(extraVariables, subject,  property, restrictionLine, removeLine, numberLine, filterNotExistsLine))

    d = {}
    for result in results:
        resultQName = URI_TO_QNAME(result[0])

        if resultQName not in d.keys():
            d[resultQName] = Node(uri             = result[0].toPython(),
                                  qname           = resultQName,
                                  cache           = cache)

        if result[1] is not None:
            d[resultQName]["label"] = result[1].toPython()
        if result[2] is not None:
            d[resultQName]["comment"] = result[2].toPython()
        if result[3] is not None:
            d[resultQName]["counter"] = int(result[3].toPython())
        if result[4] is not None:
            d[resultQName].registerClass(URI_TO_QNAME(result[4].toPython()))

        if sortedByNumber:
            if result[5] is not None:
                d[resultQName]["number"] = int(result[5].toPython())
            else:
                d[resultQName]["number"] = None

    keysStr = ""
    for key in d.keys():
        keysStr += (key + " ")

    INFO("     --> " + keysStr)

    for resultQName, resultNode in d.items():
        resultNode.registerKnownViews()
        if not cache.has_key(resultQName):
            cache[resultQName] = d[resultQName]

    # return a list of QNames
    ret = [] # list of qnames

    # first sort by 'counter' key:
    resultNodes =  sorted(d.values(), key=lambda x: x['counter'])  # entries with None will be put first in the sorted list

    # then, if necessary, sort by number:
    if sortedByNumber:
        resultNodes =  sorted(resultNodes, key=lambda x: x['number'])

    for resultNode in resultNodes:
        ret.append(resultNode['qname'])

    return ret
Ejemplo n.º 5
0
def create_folder(path, erase=False, quiet=False):
    """Create folder at @path.
    - @erase - erase existing folder
    - @quiet - don't ask user about particular actions
    - if @quiet is False, new folder with name @path[i]
      will be created
    - @erase has more priority than @quiet
    """
    # >:( a lot of returns - not good style

    DEBUG("Creating '%s' folder", path)

    try:
        os.makedirs(path)

    except OSError as ex:

        # we can't support other errors, except 'Folder already exists'
        if ex.errno != 17:
            CRITICAL("Can't create folder %s", path)
            EXCEPTION("")
            emergency_exit()

    else:
        DEBUG("Folder '%s' created", path)
        return path

    # Looks like folder already exists
    # lets try to erase it or create new
    # at different path

    ERROR("Can't create '%s' folder", path)
    if erase:
        try:
            erase_dir(path)

        except Exception:
            CRITICAL("Folder '%s' can't be erased")

        else:
            INFO("Folder erased: '{}'".format(path))
            return path

    # Well, erase == False or folder can't be erased
    if not quiet:
        answ = ''

        while not answ:
            answ = raw_input(("Type (E) to erase existing folder, "
                              "type (Q) to exit the script "
                              "or enter new folder name: ")).lower()

            if answ == "e":
                return create_folder(path, erase=True, quiet=quiet)

            elif answ == "q":
                script_exit()

            elif answ:
                return create_folder(answ, erase=False, quiet=quiet)

    else:
        return create_folder(find_unic_path(path), erase=erase, quiet=quiet)