<filename>jade/magglass.py
import multiprocessing, os, psutil, time
from . import magglasslib
from . import magglassfile

class Magglass:
    def __init__(self):
        self._files = []
        self._yara = False
        self._acl = False
        self._meta = False
        self._patternList = []
        self.setThreads(1)
        self._callback = null

    def setCallback(self,inFun):
        self._callback = inFun

    def setThreads(self,inNum):
        if inNum == 0:
            inNum = 1
        if inNum > multiprocessing.cpu_count():
            inNum = multiprocessing.cpu_count()
        self._threadCount = inNum

    def setFiles(self,inFileList):
        self._files = inFileList

    def setYara(self,inVal):
        self._yara = inVal

    def setACL(self,inVal):
        self._acl = inVal

    def setMeta(self,inVal):
        self._meta = inVal

    def setPatterns(self,inPatternList):
                self._patternList = inPatternList

    def evaluate(self):
        workers = []
        results = []
        endVal = len(self._files)
        pool = multiprocessing.Pool(processes=self._threadCount, initializer=init, initargs=(self._yara, self._acl,self._meta,self._patternList))
        for arg in self._files:
            workers.append(pool.apply_async(threadFunction, (arg,)))
        for progress,worker in enumerate(workers):
            self._callback(int(float(progress)/float(endVal)*100))
            worker.wait()
            results.append(worker.get())
        pool.close()
        pool.join()
        return results

def threadFunction(inPath):
    buff = {"name": inPath, "yara": [], "pattern": [], "acl": [], "meta": []}
    if runMeta:
        buff["meta"] += magglassfile.getMeta(inPath)
    if runYara:
        buff["yara"] += magglasslib.yara_run(inPath)
    if runPattern:
        buff["pattern"] += magglasslib.pattern_run(inPath)
    if runACL:
        buff["acl"] += magglasslib.acl_run(inPath)
    return buff

def init(inYara,inACL,inMeta,inPatternList):
    global runYara, runPattern, runACL, runMeta
    runYara = inYara
    runACL = inACL
    runMeta = inMeta
    if runYara:
        magglasslib.yara_init()
    if inPatternList:
        runPattern = True
        magglasslib.pattern_init(inPatternList)
    else:
        runPattern = False

def null(inVal):
    pass

<filename>jade/magglass.py
import multiprocessing, os, psutil, time
from . import magglasslib
from . import magglassfile

class Magglass:
    def __init__(self):
        self._files = []
        self._yara = False
        self._acl = False
        self._meta = False
        self._patternList = []
        self.setThreads(1)
        self._callback = null

    def setCallback(self,inFun):
        self._callback = inFun

    def setThreads(self,inNum):
        if inNum == 0:
            inNum = 1
        if inNum > multiprocessing.cpu_count():
            inNum = multiprocessing.cpu_count()
        self._threadCount = inNum

    def setFiles(self,inFileList):
        self._files = inFileList

    def setYara(self,inVal):
        self._yara = inVal

    def setACL(self,inVal):
        self._acl = inVal

    def setMeta(self,inVal):
        self._meta = inVal

    def setPatterns(self,inPatternList):
                self._patternList = inPatternList

    def evaluate(self):
        workers = []
        results = []
        endVal = len(self._files)
        pool = multiprocessing.Pool(processes=self._threadCount, initializer=init, initargs=(self._yara, self._acl,self._meta,self._patternList))
        for arg in self._files:
            workers.append(pool.apply_async(threadFunction, (arg,)))
        for progress,worker in enumerate(workers):
            self._callback(int(float(progress)/float(endVal)*100))
            worker.wait()
            results.append(worker.get())
        pool.close()
        pool.join()
        return results

def threadFunction(inPath):
    buff = {"name": inPath, "yara": [], "pattern": [], "acl": [], "meta": []}
    if runMeta:
        buff["meta"] += magglassfile.getMeta(inPath)
    if runYara:
        buff["yara"] += magglasslib.yara_run(inPath)
    if runPattern:
        buff["pattern"] += magglasslib.pattern_run(inPath)
    if runACL:
        buff["acl"] += magglasslib.acl_run(inPath)
    return buff

def init(inYara,inACL,inMeta,inPatternList):
    global runYara, runPattern, runACL, runMeta
    runYara = inYara
    runACL = inACL
    runMeta = inMeta
    if runYara:
        magglasslib.yara_init()
    if inPatternList:
        runPattern = True
        magglasslib.pattern_init(inPatternList)
    else:
        runPattern = False

def null(inVal):
    pass
