예제 #1
0
 def storeInLast3(self, pageTree):
     """Adds the data from the given pageTree object to the list of last 3 urls.
     
     This function should only be called when the pageTree object does not have
     a current version in the cache."""
     comicId = pageTree.getComicId(0)
     url = pageTree.getUrl(0)
     directory = cacheLoc + "predictorInfo/" + str(comicId) + "/"
     try:
         os.makedirs(directory)
         defaultPredData(comicId)
         Scheduler.predScanDir(comicId)
     except OSError:
         pass #if an error is thrown it means the directory already exists
     try:
         with open(directory + "last3Pages.txt") as f:
             urlList = f.readlines()
     except IOError: #if an error occurs the file does not yet exist
         urlList = []
     if len(urlList) >= 3:
         urlList.pop(0)
     lastTree = self.fetchCache(urlList[-1].strip())
     pageTree.assocDomains = lastTree.assocDomains
     urlList.append(url + '\n')
     with open(directory + "last3Pages.txt", 'w+') as f:
         f.writelines(urlList)
예제 #2
0
def schedule(filename):
    f = ReadFile(filename)
    scanner = Scanner(f)
    ir = IntermediateRepresentation()
    parser = Parser(scanner, ir)
    while True:
        if parser.scanner.stop_flag:
            break
        parser.parse_line()

    records, maxlive, maxVR = renameReg(ir.next, parser.maxSR + 1,
                                        parser.count - 1)
    records = list(reversed(records))

    scheduler = Scheduler(records)
    scheduler.compute_priority()
    # print(scheduler.dependency)
    ins, debug = scheduler.instruction_schedule()
    # print(debug)
    ir_collection = scheduler.IR
    for s1, s2 in ins:
        idx1 = s1[0] - 1
        idx2 = s2[0] - 1
        out1 = get_print(s1, ir_collection[idx1].ir)
        out2 = get_print(s2, ir_collection[idx2].ir)
        sys.stdout.write('[' + out1 + '; ' + out2 + ']\n')
예제 #3
0
 def test_reqError(self):
     """
     Non freshmore class have freshmore cohort class requirement
     Should result in error as requirements do not match
     """
     models.createTestDB("data1/")
     Scheduler.startAlgo()
예제 #4
0
def main():
    print("In main")
    #file to read data from
    data = Scheduler.read_data('SampleRides.csv')
    total_requests = 0
    no_new_requests = 32
    Scheduler.extract_new_requests(data,no_new_requests,total_requests)
    return 0
def lockPredData(comicId):
    """Lock a comics expected update schedule.
    
    argList should be a comic id
    Use this if a comic is going on hiatus and you want to avoid the predictor
    data losing accuracy."""
    Scheduler.lockComic(comicId, True)
    return False
def lockPredData(comicId):
    """Lock a comics expected update schedule.
    
    argList should be a comic id
    Use this if a comic is going on hiatus and you want to avoid the predictor
    data losing accuracy."""
    Scheduler.lockComic(comicId, True)
    return False
예제 #7
0
 def init_objects(self):
     if self.initiated:
         raise Exception("Objects already initiated")
     else:
         self.initiated = True
     #objects
     self.scheduler = Scheduler(self)
     
     # Activate scheduler
     activate(self.scheduler, self.scheduler.run())
예제 #8
0
def main():
    # Get the input file from the command line
    filename = sys.argv[1]
    sleepTime = int(sys.argv[2])
    batch = Scheduler()
    try:
        # Load the input line and start running the batch
        batch.load_jobs(filename)
        start(batch, sleepTime)
    except Exception as e:
        print(e)
예제 #9
0
def generate_schedule():
    if not Scheduler.running:
        try:
            Scheduler.running = True
            Scheduler.startAlgo()
            Scheduler.running = False
            response = jsonify(message='generating schedule'), 200
        except:
            response = jsonify(message='failed to generate'), 500
    else:
        response = jsonify(message='a scheduler is being generated'), 200
    return response
예제 #10
0
def proc1(lock,procname,page_table_limit,runs):
    print("pid:",os.getpid())
    print("ppid:",os.getppid())
    page_requests=[random.randint(0,page_table_limit-1) for i in range(runs)]
    lock.acquire()
    '''content=str(procname)+":"
    fp=open("Process.txt","a")
    for i in page_requests:
        content+=str(i)+","
    content+="\n"
    fp.write(content)'''
    Scheduler.initialize(page_requests)
    lock.release()
예제 #11
0
파일: Host.py 프로젝트: MIPS/overtest
    def processRun(self, processNumber):
        """
    The code to run a new worker process
    """
        # Process code
        signal.signal(signal.SIGTERM, signal.SIG_DFL)
        signal.signal(signal.SIGHUP, signal.SIG_DFL)
        self.log = LogManager("%s.%u" % (CONFIG.hostname, processNumber),
                              False)
        self.ovtDB.reconnect(quiet=True, log=self.log)

        innerexception = None
        try:
            try:
                scheduler = Scheduler.Scheduler(self, processNumber)
                scheduler.run()
            except Exception, e:
                innerexception = formatExceptionInfo()
                try:
                    self.ovtDB.FORCEROLLBACK()
                except DatabaseRetryException, e:
                    self.ovtDB.reconnect()
                self.ovtDB.setAutoCommit(True)
                self.logDB(processNumber, innerexception)
                self.log.write("%s: %s" % (time.asctime(), innerexception))
                sys.exit(3)
예제 #12
0
def run(min_metric,
        apps,
        video_desc,
        mode,
        budget=350,
        scheduler="greedy",
        verbose=False):

    s = Scheduler.Scheduler(min_metric,
                            apps,
                            video_desc,
                            app_data.model_desc,
                            verbose=verbose,
                            scheduler=scheduler)

    fnr, fpr, f1, cost, avg_rel_acc, num_frozen_list, target_fps_list = s.run(
        budget, mode=mode)
    stats = {
        "fnr": fnr,
        "fpr": fpr,
        "f1": f1,
        "cost": cost,
        "avg_rel_acc": avg_rel_acc,
        "frozen": num_frozen_list,
        "fps": target_fps_list,
    }
    stats["metric"] = 1 - stats[min_metric]
    return s, stats
예제 #13
0
	def __init__(self, specification = None, scheduler = None, stopped = False):
		if (specification != None):
			self.specification = specification
		else:
			self.specification = Specification.Specification()
		if(scheduler != None):
			self.scheduler = scheduler
		else:
			self.scheduler = Scheduler()
		ipath = os.path.join(Specification.Specification.filebase, 'imu')
		if not os.path.exists(ipath):
			os.makedirs(ipath)
		self.filebase = os.path.join(ipath,'IMU_offset.txt')
		self.metrics = {}
		self.callbacks = {}
		self.mpi = 3.14159265359
		self.radian = 180 / self.mpi
		if (IMU.isAvailable()):
			self.device = MPU6050()
			self.__initOrientations()
			self.device.readOffsets(self.filebase)
			self.config = self.device.getConfig()
			self.initRaw()
			self.initNorm()
			self.initAngles()
			self.initLowpass()
			self.initHighpass()
			self.initComplement()
			self.inittime=time.time()
			self.tottime=0
			self.scheduler.addTask('imu_watcher', self.calculate, 0.02, stopped)
예제 #14
0
def run_simulator(min_metric,
                  apps,
                  video_desc,
                  budget=350,
                  mode="mainstream",
                  dp={},
                  **kwargs):
    s = Scheduler.Scheduler(min_metric, apps, video_desc, app_data.model_desc,
                            **kwargs)

    stats = {
        "metric": s.optimize_parameters(budget, mode=mode, dp=dp),
        "rel_accs": s.get_relative_accuracies(),
    }

    # Get streamer schedule
    sched = s.make_streamer_schedule()

    # Use target_fps_str in simulator to avoid running on the hardware
    stats["fnr"], stats["fpr"], stats["f1"], stats[
        "cost"] = s.get_observed_performance(sched, s.target_fps_list)
    stats["fps"] = s.target_fps_list
    stats["frozen"] = s.num_frozen_list
    stats["avg_rel_acc"] = np.average(stats["rel_accs"])
    return s, stats
예제 #15
0
 def start(self, updatePricesCallback, updatePositionsCallback):
     self.updatePricesCallback = updatePricesCallback
     self.updatePositionsCallback = updatePositionsCallback
     self.scheduler = Scheduler.Scheduler(0.5,
                                          self.scheduleWorker,
                                          multi_thread=False)
     self.scheduler.start()
예제 #16
0
    def deploy_objects(self):
        '''Public facing API for deploying all available objects.

        Runs each job and returns a map from item to status.

        '''
        return Scheduler.run(self.deploy)
예제 #17
0
	def __init__(self, motionScheduler = None, specification = None, scheduler = None):
		if(motionScheduler == None):
			self.motionScheduler = MotionScheduler()
		else:
			self.motionScheduler = motionScheduler
		if(specification == None):
			self.specification = Specification()
		else:
			self.specification = specification
		if(scheduler == None):
			self.scheduler = Scheduler()
		else:
			self.scheduler = scheduler
		self.notifier = Notifier()
		self.sslmanager = SSLManager()
		self.server_address = (Setting.get('rpc_server_hostname', 'localhost'), Setting.get('rpc_server_port', 9000))
		try:
			self.server = SecureXMLRPCServer(self.server_address, SecureXMLRpcRequestHandler, Setting.get('rpc_server_log', False), self.sslmanager.key, self.sslmanager.certificate)
		except (socket.error):
			old = self.server_address[0]
			Setting.set('rpc_server_hostname', 'localhost')
			self.server_address = (Setting.get('rpc_server_hostname', 'localhost'), Setting.get('rpc_server_port', 9000))
			self.server = SecureXMLRPCServer(self.server_address, SecureXMLRpcRequestHandler, Setting.get('rpc_server_log', False), self.sslmanager.key, self.sslmanager.certificate)
			self.notifier.addNotice('Unable to bind to RPC Hostname: {}. Reset to localhost.'.format(old), 'warning')
		self.exposed = Exposed(self.specification, self.motionScheduler)
		self.server.register_instance(self.exposed)
		self.scheduler.addTask('srpc_server', self.serve, 0.2, stopped=(not Setting.get('rpc_autostart', False)))
def run_simulator(min_metric,
                  apps,
                  video_desc,
                  budget=350,
                  scheduler="greedy",
                  verbose=False):
    #TODO: Use args again??
    s = Scheduler.Scheduler(min_metric,
                            apps,
                            video_desc,
                            app_data.model_desc,
                            0,
                            verbose=verbose,
                            scheduler=scheduler)

    stats = {
        "metric": s.optimize_parameters(budget),
        "rel_accs": s.get_relative_accuracies(),
    }

    # Get streamer schedule
    sched = s.make_streamer_schedule()

    # Use target_fps_str in simulator to avoid running on the hardware
    stats["fnr"], stats["fpr"], stats["f1"], stats[
        "cost"] = s.get_observed_performance(sched, s.target_fps_list)
    stats["fps"] = s.target_fps_list
    stats["frozen"] = s.num_frozen_list
    stats["avg_rel_acc"] = np.average(stats["rel_accs"])
    return s, stats
예제 #19
0
    async def create_components(self, name, start_time, runtime,
                                broker_address, protocol, settings):
        self.subscriptions = []
        self.number_of_clients = []
        self.responses = {}

        file = open("{0}warning.log".format(name), 'w')
        file.truncate(0)
        file.close()
        self.warning_logger = logging.getLogger("WarningLogger")
        self.warning_logger.handlers.clear()
        self.warning_logger.addHandler(
            logging.FileHandler("{0}warning.log".format(name)))
        self.warning_logger.setLevel(logging.INFO)

        self.scheduler = Scheduler.Scheduler(self, start_time, runtime)
        self.scheduler.start_scheduler()
        self.broker_address = broker_address
        self.start_time = start_time
        self.scheduler.schedule_stop()
        self.scheduler.schedule_resource_measuring()

        if not self.check_ntp_synchronization():
            self.warning_logger.info("Time is not synchronized with ntp")
        if not self.check_ptp_synchronization():
            self.warning_logger.info("Time is not synchronized with ptp")

        self.set_adapter(protocol, name)
        if isinstance(self.adapter, MqttAdapter.MqttAdapter):
            await self.adapter.connect(broker_address, settings)
        else:
            print(broker_address)
            print(isinstance(self.adapter, CoapAdapter.CoapAdapter))
            await self.adapter.connect(broker_address)
예제 #20
0
	def __init__(self, windowSize=None):
		"""
		Initialization method. If windowSize is not defined, the default is C{Size(800,600)}.

		@param windowSize: The size of the window.
		@type windowSize: L{Size}
		"""
		if windowSize is None:
			self._windowSize = Size(800, 600)
		else:
			self._windowSize = windowSize
		self._gtkInterface = None	# set this up in setWindow()
		self._gestureDispatch = GestureDispatch()
		self._scheduler = Scheduler()
		self._actionManager = ActionManager(self._scheduler)

		self._isShowingFPS = False

		self._runningScene = None
		self._nextScene = None
		self._scenesStack = []

		self._oldFramerate = 1.0/60.0
		self._framerate = 1.0/60.0
		self._frames = 0
		self._isPaused = False
		self._dt = 0
		self._isNextDeltaTimeZero = False
		self._lastTimeStamp = 0
		self._accumDt = 0
		self._frames = 0
		self._displayedFramerate = 0
		self._isRecording = False
		self._backgroundColor = BlackColor()
예제 #21
0
 def __init__(self, args, nodeAndPortList, observer=None):
     self.scheduler = Scheduler.RealTimeScheduler()
     self.termAttr = None
     self.state = "init"
     self.nodeAndPortList = nodeAndPortList
     self.args = args
     self.observer = observer
예제 #22
0
 def initScheduler(self):
     """
     Create a Scheduler and ThreadedScheduler
     (this will run the Sceheduler class in a thread like ThreadedSensor for tobii) 
     class like in initTobiiEyeTracker method
     """
     self.scheduler = Scheduler.Scheduler()
 def __init__(self, road_control, car_amount):
     self.new_car = []
     self.road_control = road_control
     self.sa = Scheduler.Scheduler()
     self.turn_left = 0.2 * car_amount / 14000
     self.turn_right = 0.2 * car_amount / 14000
     self.through = 0.6 * car_amount / 14000
 def __init__(self, node_list, edge_list):
     #self.hist = dict()
     sched = Scheduler.Scheduler(node_list, edge_list)
     self.route_matrix = sched.calc_routes()
     for x in edge_list:  # initiate weights with default values
         x.init_weights()
     self.edge_dict = Edge.Edge.edges_list_to_dict(edge_list)
예제 #25
0
	def __init__(self, parent, gui, **options):
		super(TkResourceManager,self).__init__(parent, gui, **options)
		self.dm = TkDependencyManager(self.widget, {'package':'psutil', 'installer': 'pip', 'version': '3.3.0'}, 'Resource Manager', self.gui)
		if(not self.dm.installRequired()):
			self.gridrows = {}
			if(self.gui.scheduler != None):
				self.scheduler = self.gui.scheduler
			else:
				self.scheduler = Scheduler()
			self.resources = self.gui.getClass('Resource.Resources')(self.scheduler)
			self.tasksheight = 200 # sets the height of the tasks notebook
			self.last = { 'memory':0, 'disks':0, 'threads':0, 'processes':0, 'network':0 }
			if(Setting.get('resman_show_cpu',True)):
				self.addCpuManager()
			if(Setting.get('resman_show_tasks',True)):
				self.addTaskManager()
			if(Setting.get('resman_show_memory',True)):
				self.addMemoryManager()
			if(Setting.get('resman_show_temperature',True)):
				self.addTemperatureManager()
			if(Setting.get('resman_show_disk',True)):
				self.addDiskManager()
			self.addOptionManager()
		else:
			self.open()
			self.dm.addManager()
예제 #26
0
 def _run(self):
     """ run your fun"""
     scheduler = Scheduler.Scheduler()
     scheduler.start()
     while True:
         print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
         #print 'get Task',datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
         time.sleep(61)
예제 #27
0
def main():
    host = "localhost"
    port = 50001
    backlog = 5
    scheduler = Scheduler()
    listeningSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    listeningSocket.bind((host, port))
    listeningSocket.listen(backlog)
    listeningSocket.setblocking(0)

    server = listenOn(scheduler, listeningSocket, echoHandler)
    scheduler.add(server)
    print "***Starting Echo Server***"
    from time import sleep
    r = scheduler._run()
    for blah in r:
        sleep(0.01)
예제 #28
0
    def schedule(cls, game):
        # Only two players
        candidates = game.submission_set.filter(
            active=True, user__is_active=True).order_by("count")

        if len(candidates) < 2:
            print "Insufficient candidates"
            yield Scheduler.Task()

        for i in range(RUN_COUNT):
            for player1 in candidates:
                for player2 in candidates:
                    if player1.user == player2.user: continue

                    players = [player1, player2]

                    yield Scheduler.GameRunTask(game.cls, *players)
예제 #29
0
 def getBidAsk(self, lastMinuteData):
     while True:
         try:
             PricingFetched = self.api.pricing.get(
                 self.acct_id,
                 instruments='CAD_JPY',
                 includeUnitsAvailable=False)
             if (PricingFetched.status > 250):
                 print(PricingFetched.body)
                 self.statusError('Error getting bid-ask')
             else:
                 for price in PricingFetched.get("prices"):
                     #Checks if there was a gap in the data stream and fixes it if so
                     if (Scheduler.checkTimeDifference(
                             price.time, lastMinuteData, 121)):
                         PriceBidAsk = [
                             price.time, price.bids[0].price,
                             price.asks[0].price
                         ]
                         self.TPLog.writerow(PriceBidAsk)
                         return (PriceBidAsk)
                     elif (Scheduler.isItMidnight(price.time)
                           ):  #Around midnight, there are missing minutes
                         PriceBidAsk = [
                             price.time, price.bids[0].price,
                             price.asks[0].price
                         ]
                         self.ELog.writerow([
                             datetime.datetime.now(datetime.timezone.utc),
                             "Midnight"
                         ])
                         self.TPLog.writerow(PriceBidAsk)
                         return (PriceBidAsk)
                     else:
                         minutesBehind = Scheduler.returnTimeDifference(
                             price.time, lastMinuteData)
                         self.ELog.writerow([
                             datetime.datetime.now(datetime.timezone.utc),
                             "Error", "We are ", minutesBehind, " behind."
                         ])
                         return self.getMinuteData(minutesBehind)
         except V20Timeout:
             self.timeoutError('Forex could not get bid-ask')
         except V20ConnectionError:
             self.connectionError('Forex could not get bid-ask')
예제 #30
0
def main(path, replay_type, Bverbose, pHost = Config.proxy_host, pNSSLport = Config.proxy_nonssl_port, pSSL = Config.proxy_ssl_port):
    global verbose
    verbose = Bverbose
    check_for_ats(pHost, pNSSLport)
    Config.proxy_host = pHost
    Config.proxy_nonssl_port = pNSSLport
    Config.proxy_ssl_port = pSSL
    proxy = {"http": "http://{0}:{1}".format(Config.proxy_host, Config.proxy_nonssl_port)}
    Scheduler.LaunchWorkers(path, Config.nProcess, proxy, replay_type, Config.nThread)
예제 #31
0
    def __init__(self, logger):
        # Logger
        self._logger = logger

        # Hardware of OS
        self._hardware = Hardware(self._logger)

        # Kernel of OS
        self._kernel = KernelSO(self._logger, self._hardware, Scheduler())
예제 #32
0
 def connect(self):
     print "  connection #%s to node %s:%s" % (
         self.connId, self.node, self.port)
     if self.sd == None:
         self.sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.sd.connect((self.node, self.port))
     self.scheduler.addFdHandler(Scheduler.FunctionalFdHandler(
             self.sd, waitInputFunc = lambda: True,
             handleInputFunc = self.eventInput))
예제 #33
0
 def __init__(self, parent, gui, **options):
     super(TkNetworkManager, self).__init__(parent, gui, **options)
     self.pm = TkDependencyManager(
         self.widget, {"package": "nmap", "installer": "apt-get"}, "Network Manager", self.gui
     )
     if hasattr(self.gui, "scheduler"):
         self.scheduler = self.gui.scheduler
     else:
         self.scheduler = Scheduler()
예제 #34
0
    def force_x(self, **kwargs):
        """Set the ui.scheduler to be RandomStim
        
        At the moment, this scheduler object is shared between trial setter
        and ui objects.
        """
        self.ui.ts_obj.scheduler = Scheduler.RandomStim(
            trial_types=self.ui.ts_obj.scheduler.trial_types, **kwargs)

        return 'schedule changed'
예제 #35
0
    def createSocket(self):
        self.listenSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.listenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.listenSocket.bind(("", self.config.port))
        self.listenSocket.listen(10000)

        self.scheduler.addFdHandler(
            Scheduler.FunctionalFdHandler(
                self.listenSocket,
                waitInputFunc=lambda: True,
                handleInputFunc=self.eventClientConnection))
예제 #36
0
def main():
    
    start = time.time()
    students, course_obj_dict = getStudents(4000)
    print 'Created students'
    
    all_courses = course_obj_dict.values()

    initialThingy = Scheduler.initializeSchedule(all_courses)
    print 'initialized schedule'
    
    tentSchedule = Scheduler.scheduler(1000, initialThingy)
    print 'minimized conflicts in schedule'
    
    postSchedule = Scheduler.postProcess(tentSchedule)
    end = time.time()
    print 'output schedule for preference maximization'

    print Scheduler.conflictsBySlot

    print end-start
예제 #37
0
    def __init__(self, args, nodePortNameList, observer=None):
        self.scheduler = Scheduler.RealTimeScheduler()
        self.termAttr = None
        self.state = "init"
        self.nodePortNameList = nodePortNameList
        self.args = args
        self.observer = observer

        if self.args.mux:
            self.muxServerStart()
        else:
            self.muxServer = None
예제 #38
0
    def schedule(cls, game):
        while True:
            # Only two players
            candidates = game.submission_set.filter(active=True, user__is_active = True).order_by("count")

            if len(candidates) < 2:
                print "Insufficient candidates"
                yield Scheduler.Task()

            # Choose min candidate
            player1 = candidates[0]

            # And play against everyone else
            for player2 in candidates:
                if player1.user == player2.user: continue

                players = [player1, player2]
                yield Scheduler.GameRunTask(game.cls, *players)

                players = [player2, player1]
                yield Scheduler.GameRunTask(game.cls, *players)
예제 #39
0
def main():

    students, course_obj_dict = getStudents(500)
    print 'Created students'
    all_courses = course_obj_dict.values()

    Scheduler.initializeSchedule(all_courses)
    tentSchedule = Scheduler.scheduler(10)

    for i in range(len(tentSchedule)):
        for j in range(len(tentSchedule[i])):
            tentSchedule[i][j] = tentSchedule[i][j].course

    output_dict = {'schedule': tentSchedule}
    output_dict['students'] = map(ClassData.Student.to_dict, students)

    json_obj = json.dumps(output_dict)
    with open('schedule.txt', 'w') as output:
        output.write(json_obj)
    output.close()

    print Scheduler.conflictsBySlot
예제 #40
0
	def __init__(self, scheduler = None):
		if(scheduler != None):
			self.scheduler = scheduler
		else:
			self.scheduler = Scheduler()
		self.s = Setting()
		self.metrics = {}
		self.initCpu()
		self.initMemory()
		self.initTemps()
		self.initDisks()
		self.initProcesses()
		self.initThreads()
		self.initNetwork()
예제 #41
0
 def storeCache(self, pageTree):
     temp = self.parseDirectory(pageTree.getUrl(0))
     directory = temp[0]
     fName = temp[1]
     try:
         os.makedirs(directory)
     except OSError:
         pass #if an error is thrown it means the directory already exists
     try:
         with open(os.path.join(directory, fName)) as f:
             pass  #non race way to check to see if the file already exists
         self.revisionPush(pageTree, directory, fName)
     except IOError: #if an error occurs the file does not yet exist, which means this is a new page
         self.storeInLast3(pageTree)
         self.storeInHistoryList(directory, pageTree.getUrl(0))
         Scheduler.predUpdate(pageTree.getComicId(0))
     try:
         with open(os.path.join(directory,"pageTreeData.txt"), 'w+') as f:
             f.write(pageTree.getPageTreeData())
         with open(os.path.join(directory, fName), 'w+') as f:
             f.write(str(pageTree.getContent(0)))
         return True
     except IOError:
         return False
예제 #42
0
def main():
    students, schedule = StudentMaker.schedule_from_file()
    schedule = Scheduler.postProcess(schedule)

    final_mapping = [4, 19, 12, 17, 0, 7, 1, 13, 8, 5, 10, 2, 11, 14, 9, 15, 16, 3, 18, 6]

    mapping = []
    for i in range(ClassData.FINAL_SLOTS):
        mapping.append(i)

    #print sum(StudentScheduler.scheduleBadness(students, schedule, mapping))

    #mapp = StudentScheduler.optimizeSchedule(students, schedule, 10)

    #print sum(StudentScheduler.scheduleBadness(students, schedule, mapp))

    id = 1463
    print len(students)
예제 #43
0
 def __init__(self, scheduler=None):
     if scheduler != None:
         self.scheduler = scheduler
     else:
         self.scheduler = Scheduler()
     self.patterns = {}
     self.nodes = {}
     self.__initPatterns()
     self.nmapcachepath = os.path.join(Specification.Specification.filebase, "network")
     self.nmapcachefile = "nmap_cache.txt"
     self.nmapcommand = ["nmap", "192.168.0.0/24"]
     self.ifconfigcommand = ["ifconfig"]
     self.nics = []
     self.myip = None
     self.ifconfigraw = None
     self.nmapraw = None
     self.mapping = False
     self.notifier = Notifier()
     self.scheduler.addTask("network_mapper", self.update, 30)
예제 #44
0
    def __init__(self, specification=None, map=None, motionScheduler=None, scheduler=None):
        """
		used exclusively on the command line. Tkinter events drive the GUI.
		"""
        if scheduler != None:
            self.scheduler = scheduler
        else:
            self.scheduler = Scheduler()
        if specification != None:
            self.specification = specification
        else:
            self.specification = Specification.Specification()
        if motionScheduler == None:
            self.motionScheduler = Motion.MotionScheduler()
        else:
            self.motionScheduler = motionScheduler
        self.callbacks = {}
        self.asciimap = AsciiMap()
        self.addCallback("motion", self.standardCallback)
        self.scheduler.addTask(
            "kb_watcher", self.check, interval=0.05, stopped=(not Setting.get("kb_autostart", False))
        )
예제 #45
0
	def __init__(self, specification = None, scheduler = None):
		self.now = lambda: int(round(time.time() * 1000))
		self.queuedmotions = 0
		self.currentpos = 0
		
		if(specification == None):
			self.specification = Specification.Specification()
		else:
			self.specification = specification
		self.channelindex = { v.channel : v for k, v in self.specification.servos.items()}
		if(scheduler == None):
			self.scheduler = Scheduler()
		else:
			self.scheduler = scheduler
		self.cache = {}
		self.chaincache = {}
		self.queuemeta = []
		self.chainmeta = []
		self.queue = []
		self.queuecount = 0
		self.chaincount = 0
		self.scheduler.addTask('motion_scheduler', self.checkQueue, interval = 0.005, stopped=(not Setting.get('motion_scheduler_autostart', True)))
예제 #46
0
class TkResourceManager(TkBlock):
	def __init__(self, parent, gui, **options):
		super(TkResourceManager,self).__init__(parent, gui, **options)
		self.dm = TkDependencyManager(self.widget, {'package':'psutil', 'installer': 'pip', 'version': '3.3.0'}, 'Resource Manager', self.gui)
		if(not self.dm.installRequired()):
			self.gridrows = {}
			if(self.gui.scheduler != None):
				self.scheduler = self.gui.scheduler
			else:
				self.scheduler = Scheduler()
			self.resources = self.gui.getClass('Resource.Resources')(self.scheduler)
			self.tasksheight = 200 # sets the height of the tasks notebook
			self.last = { 'memory':0, 'disks':0, 'threads':0, 'processes':0, 'network':0 }
			if(Setting.get('resman_show_cpu',True)):
				self.addCpuManager()
			if(Setting.get('resman_show_tasks',True)):
				self.addTaskManager()
			if(Setting.get('resman_show_memory',True)):
				self.addMemoryManager()
			if(Setting.get('resman_show_temperature',True)):
				self.addTemperatureManager()
			if(Setting.get('resman_show_disk',True)):
				self.addDiskManager()
			self.addOptionManager()
		else:
			self.open()
			self.dm.addManager()
	
	#=== VIEWS ===#
	def addCpuManager(self):
		row = 0
		self.cpuwidgets = {}
		self.gridrows['cpu'] = 0
		self.widgets['cpuframe'] = Frame(self.widget,bg=self.colours['bg'])
		self.widgets['cpuframe'].grid(column=0,row=row,sticky='EW')
		self.variables['cpupercentage'] = StringVar()
		self.variables['cpupercentage'].set(self.resources.metrics['cpu_percent'].value)
		self.cpuwidgets['cpulabel'] = Tkinter.Label(self.widgets['cpuframe'],text='CPU Usage', anchor=NW, bg=self.colours['bg'], fg=self.colours['headingfg'], font=self.fonts['heading2'])
		self.cpuwidgets['cpulabel'].grid(column=0,row=self.gridrows['cpu'],sticky='EW')
		self.gridrows['cpu'] += 1
		self.cpuwidgets['graph'] = TkLineGraph(self.widgets['cpuframe'], { }, {'bg': self.colours['bg'], 'fg': self.colours['fg'], 'line': self.colours['consolefg']}, height = 50, width = 150, yrange = { 'min': 0, 'max': 100}, pointlimit=20)
		self.cpuwidgets['graph'].widget.grid(column=0,row=self.gridrows['cpu'], sticky='EW')
		self.cpuwidgets['cpudata'] = Tkinter.Label(self.widgets['cpuframe'],textvariable=self.variables['cpupercentage'], bg=self.colours['bg'], fg=self.colours['headingfg'], font=self.fonts['heading2'], width=4)
		self.cpuwidgets['cpudata'].grid(column=1,row=self.gridrows['cpu'],sticky='EW')
		self.cpuwidgets['cpupercent'] = Tkinter.Label(self.widgets['cpuframe'],text='%', bg=self.colours['bg'], fg=self.colours['headingfg'], font=self.fonts['heading2'])
		self.cpuwidgets['cpupercent'].grid(column=2,row=self.gridrows['cpu'],sticky='EW')
		self.scheduler.addTask('resman_cpu_pc', self.updateCpuPc, interval = 1)
		self.scheduler.addTask('resman_cpu_graph', self.updateCpuGraph, interval = 1)
	def updateCpuPc(self):
		try:
			history = self.resources.metrics['cpu_percent'].hotValues()
			if(len(history) > 1):
				if(history[-1].datavalue != history[-2].datavalue):
					self.variables['cpupercentage'].set(self.resources.metrics['cpu_percent'].value)
			else:
				self.variables['cpupercentage'].set(self.resources.metrics['cpu_percent'].value)
		except:
			pass
	def updateCpuGraph(self):
		try:
			self.cpuwidgets['graph'].data = { x.timestamp : x.datavalue for x in self.resources.metrics['cpu_percent'].hotValues() }
			self.cpuwidgets['graph'].update()
		except:
			pass
	def addMemoryManager(self):
		row = 0
		self.gridrows['mem'] = 0
		self.widgets['memoryframe'] = Frame(self.widget,bg=self.colours['bg'])
		self.widgets['memoryframe'].grid(column=1,row=row,sticky='EW')
		self.widgets['memlabel'] = Tkinter.Label(self.widgets['memoryframe'],text='Memory', anchor=NW, bg=self.colours['bg'], fg=self.colours['headingfg'], font=self.fonts['heading2'])
		self.widgets['memlabel'].grid(column=0,row=self.gridrows['mem'],sticky='EW')
		self.gridrows['mem'] += 1
		self.widgets['vmemlabel'] = Tkinter.Label(self.widgets['memoryframe'],text="VMem", bg=self.colours['bg'], fg=self.colours['fg'])
		self.widgets['vmemlabel'].grid(column=0,row=self.gridrows['mem'],sticky='EW')
		self.widgets['vmemdata'] = Tkinter.Label(self.widgets['memoryframe'],text=0, bg=self.colours['bg'], fg=self.colours['fg'])
		self.widgets['vmemdata'].grid(column=1,row=self.gridrows['mem'],sticky='EW')
		self.widgets['vmemgraph'] = TkBarGraph(self.widgets['memoryframe'], self.colours)
		self.widgets['vmemgraph'].widget.grid(column=2,row=self.gridrows['mem'],sticky='EW')
		self.gridrows['mem'] += 1
		self.widgets['swaplabel'] = Tkinter.Label(self.widgets['memoryframe'],text="Swap", bg=self.colours['bg'], fg=self.colours['fg'])
		self.widgets['swaplabel'].grid(column=0,row=self.gridrows['mem'],sticky='EW')
		self.widgets['swapdata'] = Tkinter.Label(self.widgets['memoryframe'],text=0, bg=self.colours['bg'], fg=self.colours['fg'])
		self.widgets['swapdata'].grid(column=1,row=self.gridrows['mem'],sticky='EW')
		self.widgets['swapgraph'] = TkBarGraph(self.widgets['memoryframe'], self.colours)
		self.widgets['swapgraph'].widget.grid(column=2,row=self.gridrows['mem'],sticky='EW')
		self.scheduler.addTask('resman_memory', self.updateMemory, interval = 10)
	def updateMemory(self):
		try:
			history = self.resources.metrics['memory'].hotValues()
			mem = self.resources.metrics['memory'].value
			if(len(history) > 1):
				if(history[-1].timestamp != self.last['memory']):
					self.last['memory'] = history[-1].timestamp
					self.widgets['vmemdata'].configure(text=mem['vmem'])
					self.widgets['vmemgraph'].update(mem['vmem'])
					self.widgets['swapdata'].configure(text=mem['smem'])
					self.widgets['swapgraph'].update(mem['smem'])
			else:
				self.widgets['vmemdata'].configure(text=mem['vmem'])
				self.widgets['vmemgraph'].update(mem['vmem'])
				self.widgets['swapdata'].configure(text=mem['smem'])
				self.widgets['swapgraph'].update(mem['smem'])
		except:
			pass
	def addTemperatureManager(self):
		row = 1
		self.gridrows['temp'] = 0
		self.widgets['tempframe'] = Frame(self.widget,bg=self.colours['bg'])
		self.widgets['tempframe'].grid(column=0,row=row, columnspan=2, sticky='EW')
		self.widgets['cpuTemplabel'] = Tkinter.Label(self.widgets['tempframe'],text='CPU Temp', anchor=E, bg=self.colours['bg'], fg=self.colours['headingfg'], height=2)
		self.widgets['cpuTemplabel'].grid(column=0,row=self.gridrows['temp'], ipadx=30,sticky='EW')
		self.widgets['cpuTempData'] = Tkinter.Label(self.widgets['tempframe'],text='TBD', anchor=W, bg=self.colours['bg'], fg=self.colours['fg'], height=2)
		self.widgets['cpuTempData'].grid(column=1,row=self.gridrows['temp'], ipadx=10, padx=10, sticky='EW')
		
		self.widgets['gpuTemplabel'] = Tkinter.Label(self.widgets['tempframe'],text='GPU Temp', anchor=E, bg=self.colours['bg'], fg=self.colours['headingfg'], height=2)
		self.widgets['gpuTemplabel'].grid(column=2,row=self.gridrows['temp'], ipadx=30,sticky='EW')
		self.widgets['gpuTempData'] = Tkinter.Label(self.widgets['tempframe'],text='TBD', anchor=W, bg=self.colours['bg'], fg=self.colours['fg'], height=2)
		self.widgets['gpuTempData'].grid(column=3,row=self.gridrows['temp'], ipadx=10, padx=10,sticky='EW')
		
		self.scheduler.addTask('resman_temp', self.updateTemps, interval = 30)
	def updateTemps(self):
		temps = self.resources.metrics['temperature'].value
		if(temps != None):
			try:
				self.widgets['cpuTempData'].configure(text=round(temps['cpu'], 1), fg=self.colours['valuefg'] )
				self.widgets['gpuTempData'].configure(text=round(temps['gpu'], 1), fg=self.colours['valuefg'] )
			except:
				pass
	def addDiskManager(self):
		row = 2	
		self.gridrows['disk'] = 0
		self.widgets['diskframe'] = Frame(self.widget,bg=self.colours['bg'])
		self.widgets['diskframe'].grid(column=0,row=row, columnspan=2, sticky='EW')
		self.widgets['disklabel'] = Tkinter.Label(self.widgets['diskframe'],text='Disk Usage', anchor=NW, bg=self.colours['bg'], fg=self.colours['headingfg'], font=self.fonts['heading2'])
		self.widgets['disklabel'].grid(column=0,row=self.gridrows['disk'],sticky='EW')
		
		self.gridrows['disk'] += 1
		
		self.widgets['diskCanvas'] = Tkinter.Canvas(self.widgets['diskframe'], borderwidth=0, highlightthickness=0, width=420, height=220)
		self.widgets['diskCanvas'].grid(column=0,row=self.gridrows['disk'], sticky='EW')
		self.widgets['diskdataframe'] = Frame(self.widgets['diskCanvas'],bg=self.colours['bg'])
		self.widgets['diskdataframe'].grid(column=0,row=0,sticky='EW')
		self.widgets['diskScroller'] = Tkinter.Scrollbar(self.widgets['diskframe'], orient=VERTICAL, command=self.widgets['diskCanvas'].yview, bg=self.colours['bg'], activebackground=self.colours['handle'], troughcolor=self.colours['trough'])
		self.widgets['diskScroller'].grid(column=1, row=self.gridrows['disk'], sticky="NS")
		self.widgets['diskCanvas'].configure(yscrollcommand=self.widgets['diskScroller'].set, bg=self.colours['bg'])
		self.widgets['diskCanvas'].create_window((0,0),window=self.widgets['diskdataframe'],anchor='nw')
		self.widgets['diskdataframe'].bind("<Configure>", self.diskInfoScroll)
		
		self.scheduler.addTask('resman_disks', self.updateDisks, interval = 30)
	def diskInfoScroll(self, event):
		self.widgets['diskCanvas'].configure(scrollregion=self.widgets['diskCanvas'].bbox(ALL), width=420, height=220)
	def updateDisks(self):
		history = self.resources.metrics['disks'].hotValues()
		if(len(history) > 1):
				if(history[-1].timestamp != self.last['disks']):
					self.last['disks'] = history[-1].timestamp
					self.__applyDiskData(history[1].datavalue)
		else:
			if(self.resources.metrics['disks'].value != None):
				try:
					self.__applyDiskData(self.resources.metrics['disks'].value)
				except:
					pass
	def __applyDiskData(self, disks):
		col = 0
		for k, v in disks.iteritems():
			try:
				self.widgets[k]['diskchart'].update({self.colours['unitblue1']: v['percent'], self.colours['unitblue2']: 100-v['percent']})
				self.widgets[k]['useddata'].configure(text=self.sizeof_fmt(v['used']))
				self.widgets[k]['freedata'].configure(text=self.sizeof_fmt(v['free']))
				self.widgets[k]['disktotaldata'].configure(text=self.sizeof_fmt(v['total']))
			except:
				self.widgets[k] = {}
				mp = v['mountpoint'].split('/')
				if(len(mp) > 2):
					mp = mp[-1]
				else:
					mp = v['mountpoint']
				self.widgets[k]['diskchart'] = TkPiChart(self.widgets['diskdataframe'], {self.colours['unitblue1']: v['percent'], self.colours['unitblue2']: 100-v['percent']}, colours=self.colours, label=mp)
				self.widgets[k]['diskchart'].widget.grid(column=col,row=self.gridrows['disk'],sticky='EW', padx=5, pady=5)
				col += 1
				self.widgets[k]['diskdata'] = Frame(self.widgets['diskdataframe'],bg=self.colours['bg'])
				self.widgets[k]['diskdata'].grid(column=col,row=self.gridrows['disk'], sticky='EW')
				
				dd = self.widgets[k]['diskdata']
				
				self.gridrows['diskdata'] = 1
				
				self.widgets[k]['devicelabel'] = Tkinter.Label(dd,text='Device', anchor=NE, bg=self.colours['bg'], fg=self.colours['fg'])
				self.widgets[k]['devicelabel'].grid(column=0,row=self.gridrows['diskdata'],sticky='EW')
				self.widgets[k]['devicedata'] = Tkinter.Label(dd,text=k, anchor=NW, bg=self.colours['bg'], fg=self.colours['lightfg'])
				self.widgets[k]['devicedata'].grid(column=1,row=self.gridrows['diskdata'],sticky='EW')
				
				self.gridrows['diskdata'] += 1
				
				self.widgets[k]['fslabel'] = Tkinter.Label(dd,text='File System', anchor=NE, bg=self.colours['bg'], fg=self.colours['fg'])
				self.widgets[k]['fslabel'].grid(column=0,row=self.gridrows['diskdata'],sticky='EW')
				self.widgets[k]['fsdata'] = Tkinter.Label(dd,text=v['fstype'], anchor=NW, bg=self.colours['bg'], fg=self.colours['lightfg'])
				self.widgets[k]['fsdata'].grid(column=1,row=self.gridrows['diskdata'],sticky='EW')
				
				self.gridrows['diskdata'] += 1
				
				self.widgets[k]['usedlabel'] = Tkinter.Label(dd,text='Used', anchor=NE, bg=self.colours['bg'], fg=self.colours['fg'])
				self.widgets[k]['usedlabel'].grid(column=0,row=self.gridrows['diskdata'],sticky='EW')
				self.widgets[k]['useddata'] = Tkinter.Label(dd,text=self.sizeof_fmt(v['used']), anchor=NW, bg=self.colours['bg'], fg=self.colours['lightfg'])
				self.widgets[k]['useddata'].grid(column=1,row=self.gridrows['diskdata'],sticky='EW')
				
				self.gridrows['diskdata'] += 1
				
				self.widgets[k]['freelabel'] = Tkinter.Label(dd,text='Free', anchor=NE, bg=self.colours['bg'], fg=self.colours['fg'])
				self.widgets[k]['freelabel'].grid(column=0,row=self.gridrows['diskdata'],sticky='EW')
				self.widgets[k]['freedata'] = Tkinter.Label(dd,text=self.sizeof_fmt(v['free']), anchor=NW, bg=self.colours['bg'], fg=self.colours['lightfg'])
				self.widgets[k]['freedata'].grid(column=1,row=self.gridrows['diskdata'],sticky='EW')
				
				self.gridrows['diskdata'] += 1
				
				self.widgets[k]['disktotallabel'] = Tkinter.Label(dd,text='Total', anchor=NE, bg=self.colours['bg'], fg=self.colours['fg'])
				self.widgets[k]['disktotallabel'].grid(column=0,row=self.gridrows['diskdata'],sticky='EW')
				self.widgets[k]['disktotaldata'] = Tkinter.Label(dd,text=self.sizeof_fmt(v['total']), anchor=NW, bg=self.colours['bg'], fg=self.colours['lightfg'])
				self.widgets[k]['disktotaldata'].grid(column=1,row=self.gridrows['diskdata'],sticky='EW')
				
				col = 0
				self.gridrows['disk'] += 1
	def addTaskManager(self):
		row = 4
		self.widgets['taskframe'] = Frame(self.widget,bg=self.colours['bg'])
		self.widgets['taskframe'].grid(column=0,row=row,columnspan=2, sticky='EW')
		self.taskwidgets = {}
		self.notestyle = ttk.Style()
		self.notestyle.configure("TNotebook", background=self.colours['bg'], borderwidth=0)
		self.notestyle.configure("TNotebook.Tab", background=self.colours['buttonbg'], foreground='#000000', borderwidth=0)
		self.gridrows['tasks'] = 0
		self.taskwidgets['frameLabel'] = Tkinter.Label(self.widgets['taskframe'],text='Tasks / Network', anchor=NW, bg=self.colours['bg'], fg=self.colours['headingfg'], font=self.fonts['heading2'])
		self.taskwidgets['frameLabel'].grid(column=0,row=self.gridrows['tasks'], columnspan=1,sticky='EW')
		self.gridrows['tasks'] += 1
		self.taskwidgets['tasksnotebook'] = ttk.Notebook(self.widgets['taskframe'], style="TNotebook")
		self.taskwidgets['tasksnotebook'].grid(column=0,row=self.gridrows['tasks'],sticky='EW')
		self.addThreadManager()
		self.addProcessManager()
		self.addTrafficManager()
		self.addConnectionManager()
		self.taskwidgets['tasksnotebook'].add(self.widgets['processframe'], text="Processes")
		self.taskwidgets['tasksnotebook'].add(self.widgets['threadframe'], text="Threads")
		self.taskwidgets['tasksnotebook'].add(self.netwidgets['trafficwrap'], text="Traffic")
		self.taskwidgets['tasksnotebook'].add(self.netwidgets['connectionwrap'], text="Connections")
	def addThreadManager(self):
		row = 0
		self.threadwidgets = {}
		self.gridrows['thread'] = 0
		self.widgets['threadframe'] = Frame(self.taskwidgets['tasksnotebook'],bg=self.colours['bg'])
		self.widgets['threadframe'].grid(column=0,row=row,sticky='EW')

		self.threadwidgets['infoCanvas'] = Tkinter.Canvas(self.widgets['threadframe'], borderwidth=0, highlightthickness=0, width=420, height=self.tasksheight)
		self.threadwidgets['infoCanvas'].grid(column=0,row=self.gridrows['thread'], sticky='EW')
		self.threadwidgets['dataframe'] = Frame(self.threadwidgets['infoCanvas'],bg=self.colours['bg'])
		self.threadwidgets['dataframe'].grid(column=0,row=0,sticky='EW')
		self.threadwidgets['infoScroller'] = Tkinter.Scrollbar(self.widgets['threadframe'], orient=VERTICAL, command=self.threadwidgets['infoCanvas'].yview, bg=self.colours['bg'], activebackground=self.colours['handle'], troughcolor=self.colours['trough'])
		self.threadwidgets['infoScroller'].grid(column=1, row=self.gridrows['thread'], sticky="NS")
		self.threadwidgets['infoCanvas'].configure(yscrollcommand=self.threadwidgets['infoScroller'].set, bg=self.colours['bg'])
		self.threadwidgets['infoCanvas'].create_window((0,0),window=self.threadwidgets['dataframe'],anchor='nw')
		self.threadwidgets['dataframe'].bind("<Configure>", self.threadInfoScroll)
		
		self.threadwidgets['subframe'] = Frame(self.threadwidgets['dataframe'],bg=self.colours['bg'])
		self.threadwidgets['subframe'].grid(column=0,row=0,columnspan=2, sticky='EW')
		
		self.threadwidgets['stretchframe'] = Frame(self.threadwidgets['subframe'],bg=self.colours['bg'], borderwidth=0, width=420)
		self.threadwidgets['stretchframe'].grid(column=0,row=0, columnspan=4, sticky='EW')
		self.threadwidgets['stretchframe'].columnconfigure(0, weight=1)
		
		self.threadwidgets['namelabel'] = Tkinter.Label(self.threadwidgets['subframe'],text='Name', bg=self.colours['bg'], fg=self.colours['fg'])
		self.threadwidgets['namelabel'].grid(column=0,row=1,sticky='EW')
		self.threadwidgets['identlabel'] = Tkinter.Label(self.threadwidgets['subframe'],text='Ident', bg=self.colours['bg'], fg=self.colours['fg'])
		self.threadwidgets['identlabel'].grid(column=1,row=1,sticky='EW')
		self.threadwidgets['daemonlabel'] = Tkinter.Label(self.threadwidgets['subframe'],text='Daemon', bg=self.colours['bg'], fg=self.colours['fg'])
		self.threadwidgets['daemonlabel'].grid(column=2,row=1,sticky='EW')
		self.threadwidgets['alivelabel'] = Tkinter.Label(self.threadwidgets['subframe'],text='Alive', bg=self.colours['bg'], fg=self.colours['fg'])
		self.threadwidgets['alivelabel'].grid(column=3,row=1,sticky='EW')

		self.threadwidgets['activelabel'] = Tkinter.Label(self.threadwidgets['dataframe'],text='Total', bg=self.colours['bg'], fg=self.colours['fg'])
		self.threadwidgets['activelabel'].grid(column=0,row=1,sticky='EW')
		self.threadwidgets['activedata'] = Tkinter.Label(self.threadwidgets['dataframe'],text='TBD', bg=self.colours['bg'], fg=self.colours['fg'])
		self.threadwidgets['activedata'].grid(column=1,row=1,sticky='EW')

		self.scheduler.addTask('resman_threads', self.updateThreads, interval = 20)
	def threadInfoScroll(self, event):
		self.threadwidgets['infoCanvas'].configure(scrollregion=self.threadwidgets['infoCanvas'].bbox(ALL), width=420, height=self.tasksheight)
	def updateThreads(self):
		history = self.resources.metrics['threads'].hotValues()
		if(len(history) > 0):
			if(history[-1].timestamp != self.last['threads']):
				try:
					self.__applyThreadData(history[-1].datavalue)
					self.last['threads'] = history[-1].timestamp
				except:
					pass
		else:
			try:
				self.__applyThreadData(self.resources.metrics['threads'].value)
			except:
				pass
	def __applyThreadData(self, threads):
		if(threads != None):
			self.gridrows['thread'] = 2
			for k, v in threads.iteritems():
				rowcolour = self.colours['rowbg']
				if(self.gridrows['thread'] % 2 == 0):
					rowcolour = self.colours['rowaltbg']
				try:
					self.threadwidgets[k]['talive'].configure(text=v['isalive'])
				except:
					self.threadwidgets[k] = {}
					self.threadwidgets[k]['name'] = Tkinter.Label(self.threadwidgets['subframe'],text=v['name'], bg=rowcolour, fg=self.colours['fg'])
					self.threadwidgets[k]['name'].grid(column=0,row=self.gridrows['thread'],sticky='EW')
					self.threadwidgets[k]['ident'] = Tkinter.Label(self.threadwidgets['subframe'],text=string.replace(k,'i',''), bg=rowcolour, fg=self.colours['fg'])
					self.threadwidgets[k]['ident'].grid(column=1,row=self.gridrows['thread'],sticky='EW')
					self.threadwidgets[k]['daemon'] = Tkinter.Label(self.threadwidgets['subframe'],text=v['isdaemon'], bg=rowcolour, fg=self.colours['fg'])
					self.threadwidgets[k]['daemon'].grid(column=2,row=self.gridrows['thread'],sticky='EW')
					self.threadwidgets[k]['talive'] = Tkinter.Label(self.threadwidgets['subframe'],text=v['isalive'], bg=rowcolour, fg=self.colours['fg'])
					self.threadwidgets[k]['talive'].grid(column=3,row=self.gridrows['thread'],sticky='EW')
				self.gridrows['thread'] += 1
			self.threadwidgets['activedata'].configure(text = str(self.resources.metrics['threadcount'].value))
	def addProcessManager(self):
		row = 1
		self.processwidgets = {}
		self.gridrows['process'] = 0
		self.widgets['processframe'] = Frame(self.taskwidgets['tasksnotebook'],bg=self.colours['bg'])
		self.widgets['processframe'].grid(column=0,row=row,sticky='EW')
		self.processwidgets['infoCanvas'] = Tkinter.Canvas(self.widgets['processframe'], borderwidth=0, highlightthickness=0, width=420, height=self.tasksheight)
		self.processwidgets['infoCanvas'].grid(column=0,row=self.gridrows['process'],sticky='EW')
		self.processwidgets['dataframe'] = Frame(self.processwidgets['infoCanvas'],bg=self.colours['bg'])
		self.processwidgets['dataframe'].grid(column=0,row=0,sticky='EW')
		self.processwidgets['infoScroller'] = Tkinter.Scrollbar(self.widgets['processframe'], orient=VERTICAL, command=self.processwidgets['infoCanvas'].yview, bg=self.colours['bg'], activebackground=self.colours['handle'], troughcolor=self.colours['trough'])
		self.processwidgets['infoScroller'].grid(column=1, row=self.gridrows['process'], sticky="NS")
		self.processwidgets['infoCanvas'].configure(yscrollcommand=self.processwidgets['infoScroller'].set, bg=self.colours['bg'])
		self.processwidgets['infoCanvas'].create_window((0,0),window=self.processwidgets['dataframe'],anchor='nw')
		self.processwidgets['dataframe'].bind("<Configure>", self.processInfoScroll)
		
		self.processwidgets['stretchframe'] = Frame(self.processwidgets['dataframe'],bg=self.colours['bg'], borderwidth=0, width=420)
		self.processwidgets['stretchframe'].grid(column=0,row=0, columnspan=2, sticky='EW')
		self.processwidgets['stretchframe'].columnconfigure(0, weight=1)
		
		self.processwidgets['namelabel'] = Tkinter.Label(self.processwidgets['dataframe'],text='Name', bg=self.colours['bg'], fg=self.colours['fg'])
		self.processwidgets['namelabel'].grid(column=0,row=1,sticky='EW')
		self.processwidgets['userlabel'] = Tkinter.Label(self.processwidgets['dataframe'],text='User', bg=self.colours['bg'], fg=self.colours['fg'])
		self.processwidgets['userlabel'].grid(column=1,row=1,sticky='EW')
		self.widgets['processframe'].grid_columnconfigure(0, weight=1)
		self.processwidgets['infoCanvas'].grid_columnconfigure(0, weight=1)
		self.processwidgets['dataframe'].grid_columnconfigure(0, weight=4)
		
		self.scheduler.addTask('resman_processes', self.updateProcesses, interval = 30)
	def processInfoScroll(self, event):
		self.processwidgets['infoCanvas'].configure(scrollregion=self.processwidgets['infoCanvas'].bbox(ALL), width=420, height=self.tasksheight)
	def updateProcesses(self):
		history = self.resources.metrics['processes'].hotValues()
		if(history):
				if(history[-1].timestamp != self.last['processes']):
					self.last['processes'] = history[-1].timestamp
					self.__applyProcessData(history[-1].datavalue)
		else:
			try:
				self.__applyProcessData(self.resources.metrics['processes'].value)
			except:
				pass
	def __applyProcessData(self, processes):
		if(processes != None):
			self.gridrows['process'] = 2
			for k, v in processes.iteritems():
				rowcolour = self.colours['rowbg']
				if(self.gridrows['process'] % 2 == 0):
					rowcolour = self.colours['rowaltbg']
				try:
						self.processwidgets[k]
				except:
					self.processwidgets[k] = {}
					self.processwidgets[k]['name'] = Tkinter.Label(self.processwidgets['dataframe'],text=v['name'], bg=rowcolour, fg=self.colours['fg'])
					self.processwidgets[k]['name'].grid(column=0,row=self.gridrows['process'],sticky='EW')
					self.processwidgets[k]['user'] = Tkinter.Label(self.processwidgets['dataframe'],text=v['username'], bg=rowcolour, fg=self.colours['fg'])
					self.processwidgets[k]['user'].grid(column=1,row=self.gridrows['process'],sticky='EW')
				self.gridrows['process'] += 1
	def addTrafficManager(self):
		self.netwidgets = {}
		self.gridrows['traffic'] = 0
		
		self.netwidgets['trafficwrap'] = Frame(self.taskwidgets['tasksnotebook'],bg=self.colours['bg'])
		self.netwidgets['trafficwrap'].grid(column=0,row=2,sticky='EW')
		
		self.netwidgets['trafficCanvas'] = Tkinter.Canvas(self.netwidgets['trafficwrap'], borderwidth=0, highlightthickness=0, width=420, height=self.tasksheight)
		self.netwidgets['trafficCanvas'].grid(column=0,row=self.gridrows['traffic'],sticky='EW')
		self.netwidgets['trafficdata'] = Frame(self.netwidgets['trafficCanvas'],bg=self.colours['bg'])
		self.netwidgets['trafficdata'].grid(column=0,row=0,sticky='EW')
		self.netwidgets['trafficScroller'] = Tkinter.Scrollbar(self.netwidgets['trafficwrap'], orient=VERTICAL, command=self.netwidgets['trafficCanvas'].yview, bg=self.colours['bg'], activebackground=self.colours['handle'], troughcolor=self.colours['trough'])
		self.netwidgets['trafficScroller'].grid(column=1, row=self.gridrows['traffic'], sticky="NS")
		self.netwidgets['trafficCanvas'].configure(yscrollcommand=self.netwidgets['trafficScroller'].set, bg=self.colours['bg'])
		self.netwidgets['trafficCanvas'].create_window((0,0),window=self.netwidgets['trafficdata'],anchor='nw')
		self.netwidgets['trafficdata'].bind("<Configure>", self.trafficInfoScroll)
		
		self.netwidgets['tstretchframe'] = Frame(self.netwidgets['trafficdata'],bg=self.colours['bg'], borderwidth=0, width=420)
		self.netwidgets['tstretchframe'].grid(column=0,row=self.gridrows['traffic'], columnspan=3, sticky='EW')
		self.netwidgets['tstretchframe'].columnconfigure(0, weight=1)
		
		self.gridrows['traffic'] += 1
		
		self.netwidgets['namelabel'] = Tkinter.Label(self.netwidgets['trafficdata'],text='Name', bg=self.colours['bg'], fg=self.colours['fg'])
		self.netwidgets['namelabel'].grid(column=0,row=self.gridrows['traffic'],sticky='EW')
		self.netwidgets['bsentlabel'] = Tkinter.Label(self.netwidgets['trafficdata'],text='Sent', bg=self.colours['bg'], fg=self.colours['fg'])
		self.netwidgets['bsentlabel'].grid(column=1,row=self.gridrows['traffic'],sticky='EW')
		self.netwidgets['breceivedlabel'] = Tkinter.Label(self.netwidgets['trafficdata'],text='Received', bg=self.colours['bg'], fg=self.colours['fg'])
		self.netwidgets['breceivedlabel'].grid(column=2,row=self.gridrows['traffic'],sticky='EW')
		self.gridrows['traffic'] += 1
		
		self.netwidgets['trafficdata'].grid_columnconfigure(0, weight=1)
	def addConnectionManager(self):
		self.connwidgets = {}
		self.gridrows['connections'] = 0
		
		self.netwidgets['connectionwrap'] = Frame(self.taskwidgets['tasksnotebook'],bg=self.colours['bg'])
		self.netwidgets['connectionwrap'].grid(column=0,row=3,sticky='EW')
		
		self.netwidgets['connectionCanvas'] = Tkinter.Canvas(self.netwidgets['connectionwrap'], borderwidth=0, highlightthickness=0, width=420, height=self.tasksheight)
		self.netwidgets['connectionCanvas'].grid(column=0,row=self.gridrows['process'],sticky='EW')
		self.netwidgets['connectiondata'] = Frame(self.netwidgets['connectionCanvas'],bg=self.colours['bg'])
		self.netwidgets['connectiondata'].grid(column=0,row=0,sticky='EW')
		self.netwidgets['connectionScroller'] = Tkinter.Scrollbar(self.netwidgets['connectionwrap'], orient=VERTICAL, command=self.netwidgets['connectionCanvas'].yview, bg=self.colours['bg'], activebackground=self.colours['handle'], troughcolor=self.colours['trough'])
		self.netwidgets['connectionScroller'].grid(column=1, row=self.gridrows['process'], sticky="NS")
		self.netwidgets['connectionCanvas'].configure(yscrollcommand=self.netwidgets['connectionScroller'].set, bg=self.colours['bg'])
		self.netwidgets['connectionCanvas'].create_window((0,0),window=self.netwidgets['connectiondata'],anchor='nw')
		self.netwidgets['connectiondata'].bind("<Configure>", self.connectionInfoScroll)
		
		self.netwidgets['cstretchframe'] = Frame(self.netwidgets['connectiondata'],bg=self.colours['bg'], borderwidth=0, width=400)
		self.netwidgets['cstretchframe'].grid(column=0,row=self.gridrows['connections'], columnspan=5, sticky='EW')
		self.netwidgets['cstretchframe'].columnconfigure(0, weight=1)
		
		self.gridrows['connections'] += 1
		
		self.netwidgets['laddlabel'] = Tkinter.Label(self.netwidgets['connectiondata'],text='Local Address', bg=self.colours['bg'], fg=self.colours['fg'])
		self.netwidgets['laddlabel'].grid(column=0,row=self.gridrows['connections'],sticky='EW')
		self.netwidgets['lportlabel'] = Tkinter.Label(self.netwidgets['connectiondata'],text='Local Port', bg=self.colours['bg'], fg=self.colours['fg'])
		self.netwidgets['lportlabel'].grid(column=1,row=self.gridrows['connections'],sticky='EW')
		self.netwidgets['raddlabel'] = Tkinter.Label(self.netwidgets['connectiondata'],text='Remote Address', bg=self.colours['bg'], fg=self.colours['fg'])
		self.netwidgets['raddlabel'].grid(column=2,row=self.gridrows['connections'],sticky='EW')
		self.netwidgets['rportlabel'] = Tkinter.Label(self.netwidgets['connectiondata'],text='Remote Port', bg=self.colours['bg'], fg=self.colours['fg'])
		self.netwidgets['rportlabel'].grid(column=3,row=self.gridrows['connections'],sticky='EW')
		self.netwidgets['statuslabel'] = Tkinter.Label(self.netwidgets['connectiondata'],text='Status', bg=self.colours['bg'], fg=self.colours['fg'])
		self.netwidgets['statuslabel'].grid(column=4,row=self.gridrows['connections'],sticky='EW')

		self.netwidgets['connectiondata'].grid_columnconfigure(0, weight=1)
		
		self.scheduler.addTask('resman_network', self.updateNetwork, interval = 30)
	def addOptionManager(self):
		self.gridrows['option'] = 0
		self.widgets['oframe'] = Frame(self.widget,bg=self.colours['bg'])
		self.widgets['oframe'].grid(column=0,row=5,columnspan=2, padx=10, pady=5, sticky='EW')
		self.netwidgets['olabel'] = Tkinter.Label(self.widgets['oframe'],text='Archive', anchor=NW, bg=self.colours['bg'], fg=self.colours['headingfg'], font=self.fonts['heading2'])
		self.netwidgets['olabel'].grid(column=0,row=self.gridrows['option'], pady=5,sticky='EW')
		self.gridrows['option'] += 1
		self.variables['archiveCpu'] = Tkinter.BooleanVar()
		self.variables['archiveCpu'].set(Setting.get('resource_archive_cpu',False))
		self.netwidgets['aCpuEntry'] = Tkinter.Checkbutton(self.widgets['oframe'], variable=self.variables['archiveCpu'], text='CPU Usage', command=self.OnToggleCpuClick, anchor=W, bg=self.colours['inputbg'], fg=self.colours['inputfg'], activebackground=self.colours['activebg'], selectcolor=self.colours['inputbg'])
		self.netwidgets['aCpuEntry'].grid(column=0,row=self.gridrows['option'], padx=0, pady=0, ipadx=10, sticky='EW')
		
		self.variables['archiveMem'] = Tkinter.BooleanVar()
		self.variables['archiveMem'].set(Setting.get('resource_archive_mem',False))
		self.netwidgets['aMemEntry'] = Tkinter.Checkbutton(self.widgets['oframe'], variable=self.variables['archiveMem'], text='Memory', command=self.OnToggleMemoryClick, anchor=W, bg=self.colours['inputbg'], fg=self.colours['inputfg'], activebackground=self.colours['activebg'], selectcolor=self.colours['inputbg'])
		self.netwidgets['aMemEntry'].grid(column=1,row=self.gridrows['option'], padx=0, pady=0, ipadx=10, sticky='EW')
		
		self.variables['archiveTemp'] = Tkinter.BooleanVar()
		self.variables['archiveTemp'].set(Setting.get('resource_archive_temp',False))
		self.netwidgets['aTempEntry'] = Tkinter.Checkbutton(self.widgets['oframe'], variable=self.variables['archiveTemp'], text='Temperature', command=self.OnToggleTempClick, anchor=W, bg=self.colours['inputbg'], fg=self.colours['inputfg'], activebackground=self.colours['activebg'], selectcolor=self.colours['inputbg'])
		self.netwidgets['aTempEntry'].grid(column=2,row=self.gridrows['option'], padx=0, pady=0, ipadx=10, sticky='EW')
		
		self.variables['archiveDisks'] = Tkinter.BooleanVar()
		self.variables['archiveDisks'].set(Setting.get('resource_archive_disks',False))
		self.netwidgets['aDisksEntry'] = Tkinter.Checkbutton(self.widgets['oframe'], variable=self.variables['archiveDisks'], text='Disks', command=self.OnToggleDisksClick, anchor=W, bg=self.colours['inputbg'], fg=self.colours['inputfg'], activebackground=self.colours['activebg'], selectcolor=self.colours['inputbg'])
		self.netwidgets['aDisksEntry'].grid(column=3,row=self.gridrows['option'],padx=0, pady=0, ipadx=10, sticky='EW')
		
		self.gridrows['option'] += 1
		self.variables['archiveProcess'] = Tkinter.BooleanVar()
		self.variables['archiveProcess'].set(Setting.get('resource_archive_processes',False))
		self.netwidgets['aProcessEntry'] = Tkinter.Checkbutton(self.widgets['oframe'], variable=self.variables['archiveProcess'], text='Processes', command=self.OnToggleProcessesClick, anchor=W, bg=self.colours['inputbg'], fg=self.colours['inputfg'], activebackground=self.colours['activebg'], selectcolor=self.colours['inputbg'])
		self.netwidgets['aProcessEntry'].grid(column=0,row=self.gridrows['option'],padx=0, pady=0, ipadx=10, sticky='EW')
		
		self.variables['archiveThread'] = Tkinter.BooleanVar()
		self.variables['archiveThread'].set(Setting.get('resource_archive_threads',False))
		self.netwidgets['aThreadEntry'] = Tkinter.Checkbutton(self.widgets['oframe'], variable=self.variables['archiveThread'], text='Threads', command=self.OnToggleThreadsClick, anchor=W, bg=self.colours['inputbg'], fg=self.colours['inputfg'], activebackground=self.colours['activebg'], selectcolor=self.colours['inputbg'])
		self.netwidgets['aThreadEntry'].grid(column=1,row=self.gridrows['option'],padx=0, pady=0, ipadx=10, sticky='EW')
		
		self.variables['archiveNet'] = Tkinter.BooleanVar()
		self.variables['archiveNet'].set(Setting.get('resource_archive_net',False))
		self.netwidgets['aNetEntry'] = Tkinter.Checkbutton(self.widgets['oframe'], variable=self.variables['archiveNet'], text='Network', command=self.OnToggleNetworkClick, anchor=W, bg=self.colours['inputbg'], fg=self.colours['inputfg'], activebackground=self.colours['activebg'], selectcolor=self.colours['inputbg'])
		self.netwidgets['aNetEntry'].grid(column=2,row=self.gridrows['option'],padx=0, pady=0, ipadx=10, sticky='EW')
	
	def trafficInfoScroll(self, event):
		self.netwidgets['trafficCanvas'].configure(scrollregion=self.netwidgets['trafficCanvas'].bbox(ALL), width=420, height=self.tasksheight)
	def connectionInfoScroll(self, event):
		self.netwidgets['connectionCanvas'].configure(scrollregion=self.netwidgets['connectionCanvas'].bbox(ALL), width=420, height=self.tasksheight)
	def updateNetwork(self):
		history = self.resources.metrics['network'].hotValues()
		if(len(history) > 1):
				if(history[-1].timestamp != self.last['network']):
					self.last['network'] = history[-1].timestamp
					self.__applyNetworkData(history[-1].datavalue)
		else:
			try:
				self.__applyNetworkData(self.resources.metrics['network'].value)
			except:
				pass
	def __applyNetworkData(self, network):
		self.gridrows['traffic'] = 2
		rowcount = 0
		for k,v in network['nics'].iteritems():
			rowcolour = self.colours['rowbg']
			if(rowcount % 2 == 0):
				rowcolour = self.colours['rowaltbg']
			rowcount += 1
			try:

				self.netwidgets[k]['bsent'].configure(text = self.sizeof_fmt(v['bytes_sent']))
				self.netwidgets[k]['breceived'].configure(text = self.sizeof_fmt(v['bytes_recv']))
			except:
				self.netwidgets[k] = {}
				self.netwidgets[k]['name'] = Tkinter.Label(self.netwidgets['trafficdata'],text=k, bg=rowcolour, fg=self.colours['fg'])
				self.netwidgets[k]['name'].grid(column=0,row=self.gridrows['traffic'],sticky='EW')
				self.netwidgets[k]['bsent'] = Tkinter.Label(self.netwidgets['trafficdata'],text=self.sizeof_fmt(v['bytes_sent']), bg=rowcolour, fg=self.colours['fg'])
				self.netwidgets[k]['bsent'].grid(column=1,row=self.gridrows['traffic'],sticky='EW')
				self.netwidgets[k]['breceived'] = Tkinter.Label(self.netwidgets['trafficdata'],text=self.sizeof_fmt(v['bytes_recv']), bg=rowcolour, fg=self.colours['fg'])
				self.netwidgets[k]['breceived'].grid(column=2,row=self.gridrows['traffic'],sticky='EW')
			self.gridrows['traffic'] += 1
		self.gridrows['connections'] = 2
		for k,v in network['conns'].iteritems():
			rowcolour = self.colours['rowbg']
			if(rowcount % 2 == 0):
				rowcolour = self.colours['rowaltbg']
			rowcount += 1
			try:
				self.connwidgets[k]['bstatus'].configure(text = v['status'])
				self.connwidgets[k]['status'] = v['status']
			except:
				self.connwidgets[k] = {}
				self.connwidgets[k]['ladd'] = Tkinter.Label(self.netwidgets['connectiondata'],text=v['laddr'][0] if len(v['laddr']) > 0 else '', bg=rowcolour, fg=self.colours['fg'])
				self.connwidgets[k]['ladd'].grid(column=0,row=self.gridrows['connections'],sticky='EW')
				self.connwidgets[k]['lport'] = Tkinter.Label(self.netwidgets['connectiondata'],text=v['laddr'][1] if len(v['laddr']) > 0 else '', bg=rowcolour, fg=self.colours['fg'])
				self.connwidgets[k]['lport'].grid(column=1,row=self.gridrows['connections'],sticky='EW')
				self.connwidgets[k]['radd'] = Tkinter.Label(self.netwidgets['connectiondata'],text=v['raddr'][0] if len(v['raddr']) > 0 else '', bg=rowcolour, fg=self.colours['fg'])
				self.connwidgets[k]['radd'].grid(column=2,row=self.gridrows['connections'],sticky='EW')
				self.connwidgets[k]['rport'] = Tkinter.Label(self.netwidgets['connectiondata'],text=v['laddr'][1] if len(v['raddr']) > 0 else '', bg=rowcolour, fg=self.colours['fg'])
				self.connwidgets[k]['rport'].grid(column=3,row=self.gridrows['connections'],sticky='EW')
				self.connwidgets[k]['bstatus'] = Tkinter.Label(self.netwidgets['connectiondata'],text=v['status'], bg=rowcolour, fg=self.colours['fg'])
				self.connwidgets[k]['bstatus'].grid(column=4,row=self.gridrows['connections'],sticky='EW')
			self.gridrows['connections'] += 1
	
	#=== ACTIONS ===#
	##== Archive ==##
	def OnToggleCpuClick(self):
		Setting.set('resource_archive_cpu', self.variables['archiveCpu'].get())
	def OnToggleMemoryClick(self):
		Setting.set('resource_archive_mem', self.variables['archiveMem'].get())
	def OnToggleTempClick(self):
		Setting.set('resource_archive_temp', self.variables['archiveTemp'].get())
	def OnToggleDisksClick(self):
		Setting.set('resource_archive_disks', self.variables['archiveDisks'].get())
	def OnToggleProcessesClick(self):
		Setting.set('resource_archive_processes', self.variables['archiveProcess'].get())
	def OnToggleThreadsClick(self):
		Setting.set('resource_archive_threads', self.variables['archiveThread'].get())
	def OnToggleNetworkClick(self):
		Setting.set('resource_archive_net', self.variables['archiveNet'].get())
	
	#=== UTILS ===#
	def sizeof_fmt(self, num):
		for x in ['bytes','KB','MB','GB','TB']:
			if num < 1024.0:
				return "%3.1f %s" % (num, x)
			num /= 1024.0
예제 #47
0
class KeyboardThread(object):
    def __init__(self, specification=None, map=None, motionScheduler=None, scheduler=None):
        """
		used exclusively on the command line. Tkinter events drive the GUI.
		"""
        if scheduler != None:
            self.scheduler = scheduler
        else:
            self.scheduler = Scheduler()
        if specification != None:
            self.specification = specification
        else:
            self.specification = Specification.Specification()
        if motionScheduler == None:
            self.motionScheduler = Motion.MotionScheduler()
        else:
            self.motionScheduler = motionScheduler
        self.callbacks = {}
        self.asciimap = AsciiMap()
        self.addCallback("motion", self.standardCallback)
        self.scheduler.addTask(
            "kb_watcher", self.check, interval=0.05, stopped=(not Setting.get("kb_autostart", False))
        )

    def check(self):
        with raw_mode(sys.stdin):
            ch = sys.stdin.read(1)
            if not ch or ch == chr(4):
                pass
            else:
                hex = "0x{0}".format("%02x" % ord(ch))
                try:
                    self.doCallback(hex=hex, ascii=self.asciimap.keyindex[hex])
                except:
                    self.doCallback(hex=hex)

    def start(self):
        self.scheduler.startTask("kb_watcher")

    def stop(self):
        self.scheduler.stopTask("kb_watcher")

    def standardCallback(self, hex, ascii):
        mappings = []
        for h, a in self.asciimap.keyindex.items():
            if h == hex:
                mappings = self.specification.getKeyMapping(h)
                break
        if len(mappings) > 0:
            for m in mappings:
                if m["action"] == "motion" and m["command"] != None:
                    for k, v in self.specification.motions.items():
                        if v["name"] == m["command"]:
                            self.motionScheduler.triggerMotion(k)
                            break
                elif m["action"] == "chain" and m["command"] != None:
                    for k, v in self.specification.chains.items():
                        if v["name"] == m["command"]:
                            self.motionScheduler.triggerChain(k)
                            break
                elif m["action"] == "relax":
                    self.motionScheduler.relax()
                elif m["action"] == "default":
                    self.motionScheduler.default()

    def printCallback(self, hex, ascii):
        if ascii != None:
            print({hex: ascii})

    def addCallback(self, index, func):
        self.callbacks.update({index: func})

    def removeCallback(self, index):
        del (self.callbacks[index])

    def doCallback(self, hex=None, ascii=None):
        for f in self.callbacks:
            self.callbacks[f](hex, ascii)
예제 #48
0
class MotionScheduler(object):
	def __init__(self, specification = None, scheduler = None):
		self.now = lambda: int(round(time.time() * 1000))
		self.queuedmotions = 0
		self.currentpos = 0
		
		if(specification == None):
			self.specification = Specification.Specification()
		else:
			self.specification = specification
		self.channelindex = { v.channel : v for k, v in self.specification.servos.items()}
		if(scheduler == None):
			self.scheduler = Scheduler()
		else:
			self.scheduler = scheduler
		self.cache = {}
		self.chaincache = {}
		self.queuemeta = []
		self.chainmeta = []
		self.queue = []
		self.queuecount = 0
		self.chaincount = 0
		self.scheduler.addTask('motion_scheduler', self.checkQueue, interval = 0.005, stopped=(not Setting.get('motion_scheduler_autostart', True)))
	def triggerMotion(self, id, slow = False):
		if(any(self.queuemeta)):
			if(self.queue[:1][0].jbIndex == id):
				return
		self.queuedmotions += 1
		self.queue.append(self.specification.motions[id])
		self.queuemeta.append({ 'queuepos' : self.queuedmotions, 'frameindex' : 0, 'slow' : slow })
		self.queuecount += 1
	def triggerChain(self, id):
		if(any(self.chainmeta)):
			self.chainmeta[0]['continue'] = True
			return
		self.chainmeta.append({ 'cid': id, 'type' : 'start', 'triggered' : [], 'looptriggered' : [], 'continue' : False})
		self.chaincount += 1
	def stopChain(self, id):
		if(len(self.chainmeta) == 0):
			return
		self.chainmeta[0]['type'] = 'stop'
	def checkQueue(self):
		if(self.queuecount > 0):
			stamp = self.now()
			qmlen = len(self.queuemeta)
			qm = self.queuemeta[:1][0]
			qd = self.queue[:1][0]
			if(qm['frameindex'] < len(qd.jsonData['keyframes'])):
				if(self.currentpos != qm['queuepos']):
					self.currentpos = qm['queuepos']
					qm['starttime'] = stamp
				mtime = stamp - qm['starttime']
				kf = qd.jsonData['keyframes'][qm['frameindex']]
				if(qm['slow']):
					target = kf['time'] * float(Setting.get('motion_slow_factor', default=5))
				else:
					target = kf['time']
				if(target <= mtime):
					qm['frameindex'] += 1
					for i in kf['instructions']:
						self.channelindex[i['channel']].angle = i['angle']
						self.channelindex[i['channel']].disabled = i['disabled']
						self.channelindex[i['channel']].setServoAngle(time=stamp)
			else:
				self.queuecount -= 1
				if(qmlen > 1):
					self.queue = self.queue[1:]
					self.queuemeta = self.queuemeta[1:]
				else:
					self.queue[:] = []
					self.queuemeta[:] = []	
		elif(self.chaincount > 0):
			cm = self.chainmeta[0]
			chain = self.specification.chains[cm['cid']]
			if(cm['type'] == 'start'):
				#print('start motion')
				if(not self.findMotion(chain, cm)):
					cm['type'] = 'loop'
			elif(cm['type'] == 'loop'):
				#print('loop motion')
				if(not self.findMotion(chain, cm) and not cm['continue']):
					cm['type'] = 'stop'
			elif(cm['type'] == 'stop'):
				#print('stop motion')
				if(not self.findMotion(chain, cm)):
					cm['type'] = 'stopped'
			elif(cm['type'] == 'stopped'):
				if(self.chaincount > 1):
					self.chainmeta[1:]
				else:
					self.chainmeta = []
				self.chaincount -= 1
	def findMotion(self, chain, meta):
		found = False
		for k, v in chain['motions'].items():
			if(not k in meta['triggered'] and meta['type'] == v['type'] and v['type'] != 'loop'):
				self.chainmeta[0]['triggered'].append(k)
				self.triggerMotion(self.specification.getMotionId(v['motion']))
				found = True
			elif(not k in meta['looptriggered'] and v['type'] == 'loop'):
				self.chainmeta[0]['looptriggered'].append(k)
				self.triggerMotion(self.specification.getMotionId(v['motion']))
				found = True
		if(not found and meta['type'] == 'loop' and meta['continue']):
			self.chainmeta[0]['looptriggered'] = []
			self.chainmeta[0]['continue'] = False
			found = True
		return found
	def relax(self):
		for s in self.channelindex.values():
			s.disabled = True
			s.setServoAngle()
	def default(self):
		for s in self.channelindex.values():
			s.reload()
			s.setServoAngle()
예제 #49
0
# ------------------------------------------ #
#      INITIALIZE OUR REWRITE RULES          #
# ------------------------------------------ #
story_rewrite_rules.append(Ambush_Rule)
story_rewrite_rules.append(Caught_Rule)
story_rewrite_rules.append(StKill_Rule)
# ------------------------------------------ #
#           GENERATE A NARRATIVE             #
# ------------------------------------------ #

main_graph.plot('output_initial')
#~ command = "dot -Tpng output_initial.dot -o Initial_Graph.png"
#~ os.system(command)

sched = Scheduler(main_graph, story_initialization_rules, story_rewrite_rules, True, None)

main_graph.generate_preconditions()
initial_preconditions = main_graph.get_preconditions()
#~ 
schedulers = []
schedulers.append(sched)

final_graphs = []
failed_to_find_stories = []
stories = []
num_stories = 0
num_scheds = 0
num_iters = 0
run = True
예제 #50
0
class Resources(object):
	def __init__(self, scheduler = None):
		if(scheduler != None):
			self.scheduler = scheduler
		else:
			self.scheduler = Scheduler()
		self.s = Setting()
		self.metrics = {}
		self.initCpu()
		self.initMemory()
		self.initTemps()
		self.initDisks()
		self.initProcesses()
		self.initThreads()
		self.initNetwork()
	def initCpu(self):
		self.metrics['cpu_percent'] = Resource('cpu_percent', 4000, Setting.get('resource_archive_cpu',False), 50)
		self.scheduler.addTask('cpu_watcher', self.cpu, interval = 0.4)
	def initMemory(self):
		self.metrics['memory'] = Resource('memory', 20000, Setting.get('resource_archive_mem',False))
		self.scheduler.addTask('memory_watcher', self.memory, interval = 10)
	def initTemps(self):
		self.metrics['temperature'] = Resource('temperature', 0, Setting.get('resource_archive_temp',False))
		self.scheduler.addTask('temperature_watcher', self.temperature, interval = 10)
	def initDisks(self):
		self.metrics['disks'] = Resource('disks', 10000, Setting.get('resource_archive_disks',False))
		self.scheduler.addTask('disk_watcher', self.disks, interval = 5)
	def initProcesses(self):
		self.metrics['processes'] = Resource('processes', 2000, Setting.get('resource_archive_processes',False))
		self.metrics['processcount'] = Resource('processcount', 20000, Setting.get('resource_archive_processes',False))
		self.scheduler.addTask('process_watcher', self.processes, interval = 10)
	def initThreads(self):
		self.metrics['threads'] = Resource('threads', 2000, Setting.get('resource_archive_threads',False))
		self.metrics['threadcount'] = Resource('threadcount', 20000, Setting.get('resource_archive_threads',False))
		self.scheduler.addTask('thread_watcher', self.threads, interval = 10)
	def initNetwork(self):
		self.metrics['networksyswide'] = Resource('network_system_wide', 10000, Setting.get('resource_archive_net',False))
		self.metrics['network'] = Resource('network', 10000, Setting.get('resource_archive_net',False))
		self.scheduler.addTask('network_watcher', self.network, interval = 10)
	def cpu(self):
		try:
			self.metrics['cpu_percent'].value = float(psutil.cpu_percent(interval=0.4))
		except:
			pass
	def memory(self):
		try:
			vmem = psutil.virtual_memory()
			smem = psutil.swap_memory()
			mem = { 'vmem': vmem.percent, 'smem': smem.percent}
			history = self.metrics['memory'].hotValues()
			if(any(history)):
				if(cmp(history[-1].datavalue,mem) != 0):
					self.metrics['memory'].value = mem
			else:
				self.metrics['memory'].value = mem
		except Exception,e:
			print(e)
예제 #51
0
class Director(object):
	"""
	The central point of the application. It handles the main run loop, L{Scene} transitions, and propagating redraw events to the Scene's children. It is also the owner of the L{GestureDispatch}, L{ActionManager}, L{Scheduler}, and the L{GTKInterface}.

	Normally only one Director should exist per application.
	"""
	def __init__(self, windowSize=None):
		"""
		Initialization method. If windowSize is not defined, the default is C{Size(800,600)}.

		@param windowSize: The size of the window.
		@type windowSize: L{Size}
		"""
		if windowSize is None:
			self._windowSize = Size(800, 600)
		else:
			self._windowSize = windowSize
		self._gtkInterface = None	# set this up in setWindow()
		self._gestureDispatch = GestureDispatch()
		self._scheduler = Scheduler()
		self._actionManager = ActionManager(self._scheduler)

		self._isShowingFPS = False

		self._runningScene = None
		self._nextScene = None
		self._scenesStack = []

		self._oldFramerate = 1.0/60.0
		self._framerate = 1.0/60.0
		self._frames = 0
		self._isPaused = False
		self._dt = 0
		self._isNextDeltaTimeZero = False
		self._lastTimeStamp = 0
		self._accumDt = 0
		self._frames = 0
		self._displayedFramerate = 0
		self._isRecording = False
		self._backgroundColor = BlackColor()

#{ Accessor methods.
	def isShowingFPS(self):
		"""
		Whether or not the Director is displaying frames per second. Default is C{False}.

		@return: Whether or not the Director is displaying frames per second (FPS).
		@rtype: L{bool}
		"""
		return self._isShowingFPS

	def setShowingFPS(self, isShowingFPS):
		"""
		Sets whether or not the Director displays frames per second.

		@param isShowingFPS: Whether or not the Director displays frames per second.
		@type isShowingFPS: C{bool}
		"""
		self._isShowingFPS = isShowingFPS

	showingFPS = property(isShowingFPS, setShowingFPS, doc="Whether or not to display the framerate.")

	def getSize(self):
		"""
		Returns the size of the main application window.

		@return: Size of the main application window.
		@rtype: L{Size}
		"""
		return self._gtkInterface.getSize()

	size = property(getSize, doc="The size of the main application window.")

	def getGestureDispatch(self):
		"""
		Returns the L{GestureDispatch} for the application, which sends out notifications of L{GestureEvent}C{s} to L{GestureListener}C{s}.

		@return: The dispatch.
		@rtype: L{GestureDispatch}
		"""
		return self._gestureDispatch

	gestureDispatch = property(getGestureDispatch, doc="The application's GestureDispatch.")

	def getActionManager(self):
		"""
		Reutrns the L{ActionManager} for the application, which manages the L{Action}C{s}.

		@return: The manager.
		@rtype: L{ActionManager}
		"""
		return self._actionManager

	actionManager = property(getActionManager, doc="The application's ActionManager.")

	def getScheduler(self):
		"""
		Returns the L{Scheduler} for the application, which manages the L{Timer}C{s}.

		@return: The scheduler.
		@rtype: L{Scheduler}
		"""
		return self._scheduler

	scheduler = property(getScheduler, doc="The application's Scheduler.")

	def getBackgroundColor(self):
		return self._backgroundColor

	def setBackgroundColor(self, color):
		"""
		Sets the color for the background of the application.

		@param color: The color of the background.
		@type color: L{Color}
		"""
		if self._gtkInterface is not None:
			self._gtkInterface.setBackgroundColor(color)
		self._backgroundColor = color

	backgroundColor = property(getBackgroundColor, setBackgroundColor, doc="The application's background color.")

	def setWindow(self, window=None):
		"""
		Sets the main window for the application. If window is C{None}, a L{GTKWindow} is generated automatically.

		@param window: The main application window.
		@type window: L{AbstractWindow} (or C{None})
		"""
		if self._gtkInterface == None:
			self._gtkInterface = GTKInterface(self, window, self._windowSize) # if window is None, defaults to GTKWindow()
			self._gtkInterface.setBackgroundColor(self._backgroundColor)
		else:
			warnings.warn("Window is already set.")

	def getGTKLayout(self):
		"""
		Returns the main gtk.Layout to which cocosCairo draws. Use this method to perform PyGTK actions such as attaching C{gtk.Widgets} to the application. If the GTKInterface has not yet been initialized, then it will return C{None}.

		@return: The main gtk.Layout.
		@rtype: L{GTKLayout} (or C{None})
		"""
		if self._gtkInterface is not None:
			return self._gtkInterface.getGTKLayout()
		else:
			return None

	gtkLayout = property(getGTKLayout, doc="The application's main gtk.Layout.")
#}


#{ Scene methods.
	def getRunningScene(self):
		"""
		Returns the L{Scene} which is currently running or C{None} if there is not a scene currently running.

		@return: The running Scene.
		@rtype: L{Scene} (or C{None})
		"""
		return self._runningScene

	runningScene = property(getRunningScene, doc="The currently-running scene.")

	def runWithScene(self, scene):
		"""
		Starts the application with a L{Scene}. This starts the main run loop.

		@param scene: The opening scene.
		@type scene: L{Scene}
		"""

		if self._runningScene:
			warnings.warn("Scene is already running. Use replaceScene or pushScene instead.")
			return

		if self._gtkInterface is None:
			self.setWindow()

		self.pushScene(scene)
		self._startAnimation()

	def replaceScene(self, scene):
		"""
		Replaces the currently running L{Scene} with a new Scene.

		@param scene: The new Scene.
		@type scene: L{Scene}
		"""
		scene.setRect(Rect(Point(0,0), self._gtkInterface.getSize()))
		scene._setDirector(self)
		index = len(self._scenesStack)-1
		self._scenesStack[index] = scene
		self._nextScene = scene

	def pushScene(self, scene):
		"""
		Pushes a new L{Scene} onto the stack of Scenes.

		@param scene: The new Scene.
		@type scene: L{Scene}
		"""
		scene.setRect(Rect(Point(0,0), self._gtkInterface.getSize()))
		scene._setDirector(self)
		self._scenesStack.append(scene)
		self._nextScene = scene

	def popScene(self):
		"""
		Pops the most recently-pushed L{Scene} off the stack of Scenes.
		"""
		scene = self._scenesStack.pop()
		count = len(self._scenesStack)
		if count == 0:
			self.end()
		else:
			self._nextScene = self._scenesStack[count-1]
#}


#{ Run methods.
	def end(self):
		"""
		Ends all animations, unregisters all L{GestureDispatch} listeners, and clears out the stack of L{Scene}C{s}. This does not end the program by itself, however.
		"""
		self._runningScene.onExit()
		self._runningScene.cleanup()
		self._scenesStack = []
		self._gestureDispatch.removeAllListeners()
		self._stopAnimation()

	def _setNextScene(self):
		"""
		Private method which handles setting the next L{Scene}. This should not normally be called manually.
		"""
		isTransitionRunning = isinstance(self._runningScene, AbstractTransition)
		isTransitionNext = isinstance(self._nextScene, AbstractTransition)
		if isTransitionNext is not True and self._runningScene is not None:
			self._runningScene.onExit()
		self._runningScene = self._nextScene
		self._nextScene = None
		if isTransitionRunning is not True:
			self._runningScene.onEnter()
			self._runningScene.onEnterFromFinishedTransition()

	def getFramerate(self):
		"""
		Returns the animation interval. Default is C{30.0} FPS.

		@return: Animation interval.
		@rtype: C{float}
		"""
		return self._framerate

	def setFramerate(self, framerate):
		"""
		Sets the animation interval, that is, the frames per second for the application. Note that this must be set before L{runWithScene} is called, otherwise this method will have no effect.

		@param framerate: The animation interval (FPS).
		@type framerate: C{float}
		"""
		self._framerate = framerate

	framerate = property(getFramerate, setFramerate, doc="The application's framerate.")

	def pause(self):
		"""
		Pauses the application.
		"""
		if self._isPaused:
			return
		self._oldFramerate = self._framerate
		self.setFramerate(1.0/4.0)
		self._isPaused = True

	def resume(self):
		"""
		Resumes the application.
		"""
		if not self._isPaused:
			return
		self.setFramerate(self._oldFramerate)
		self._isPaused = False
		self._dt = 0
#}


#{ Private methods.
	def _startAnimation(self):
		"""
		Private method that calls L{_preMainLoop} to begin the main loop.
		"""
		self._isRunning = True

		self._preMainLoop()
		self._gtkInterface.start()

	def _preMainLoop(self):
		"""
		Private method that sets up the L{_mainLoop} method to be called repeatedly.
		"""
		interval = self._framerate*1000
		interval = int(interval)
		gobject.timeout_add(interval, self._mainLoop)

	def _mainLoop(self):
		"""
		Private method which is called repeatedly to redraw the L{Node}C{s} and to update the L{Scheduler} with the time that has passed since the last loop.
		"""
		self._calculateDeltaTime()
		if not self._isPaused:
			if not self._isRecording:
				self._scheduler.tick(self._dt)
			else:
				self._scheduler.tick(self._framerate)
		if self._nextScene is not None:
			self._setNextScene()
		self._gtkInterface.redraw()	# This is not guaranteed to redraw within the same loop iteration as PyGTK accumulates redraw events before dispatching.
		if self._isShowingFPS is True:
			self._showFPS()
		if self._isRunning:
			return True
		else:
			return False

	def _calculateDeltaTime(self):
		"""
		Private method which calculates how much time has elapsed since the last loop iteration. This method should generally not be called manually.
		"""
		timestamp = time.time()	# now
		if self._isNextDeltaTimeZero:
			self._dt = 0
			self._isNextDeltaTimeZero = False
		#elif self._lastTimeStamp == 0.0:
		#	self._dt = 0
		else:
			self._dt = timestamp - self._lastTimeStamp
		self._lastTimeStamp = timestamp

	def _showFPS(self):
		self._frames += 1
		self._accumDt += self._dt
		if (self._accumDt > 0.1):
			self._displayedFramerate = self._frames/self._accumDt
			self._frames = 0
			self._accumDt = 0
		string = "%.1f" % self._displayedFramerate
		self._gtkInterface._layout.setFramerate(string)
		#self._fpsLabel.setText(string)

	def _stopAnimation(self):
		"""
		Private method which stops the main loop.
		"""
		self._isRunning = False
		self.stopRecording()
#}

#{ Recording methods.
	def takeScreenshot(self, imagePath):
		"""
		Takes a screenshot of the application. Note that PyGTK widgets will not be rendered to the image.

		@param imagePath: The name of the file to be saved.
		@type imagePath: C{string}
		"""
		self._gtkInterface._layout.takeScreenshot(imagePath)

	def startRecording(self, videoPath):
		"""
		Begins saving a sequence of image stills to be rendered to video.

		@param videoPath: The location where the video (and temporary image files) will be saved.
		@type videoPath: C{string}
		"""
		self._isRecording = True
		self._gtkInterface._layout.startRecording(videoPath)

	def stopRecording(self):
		"""
		Stops recording and, if FFmpeg is available, will automatically render the video. If there is already a movie file with the same name as the one given in L{startRecording}, that movie file will be deleted.
		"""
		self._isRecording = False
		self._gtkInterface._layout.stopRecording()
def unlockPredData(comicId):
    """Unlock a comics expected update schedule.
    
    argList should be a comic id"""
    Scheduler.lockComic(comicId, False)
    return False
예제 #53
0
def startScheduler(sharedDict):
    Scheduler.main(sharedDict)
예제 #54
0
 def checkSched():
     res = Scheduler.checkSchedule()
예제 #55
0
파일: Scenario.py 프로젝트: Obayd/CloudSim
class CloudSimScenario:
    def __init__(self, confPath):

        conf = open(confPath, 'r')

        #Single task or web mode (W or S)
        self.mode = self.nextProperty(conf)
        #Scheduling interval (0.01 - 1 seconds)
        self.sch_interval = float(self.nextProperty(conf))
        #Worker node speed (200 - 400 instructions/second)
        self.wn_speed = int(self.nextProperty(conf))
        #Worker node memory size (2 - 8 GB)
        self.wn_mem = int(self.nextProperty(conf))*1024
        #Worker node swapping cost (2 - 10 instructions/GB)
        self.wn_swap = float(self.nextProperty(conf))/1024
        #Worker quantum (0.01 - 0.5 seconds)
        self.wn_quantum = float(self.nextProperty(conf))
        #Worker node startup time (120 - 600 seconds)
        self.wn_startup = int(self.nextProperty(conf))
        #Worker node scheduler notification time (1-5 instructions)
        self.wn_notification = int(self.nextProperty(conf))
        #Worker node cost (euros/hour);
        self.wn_cost = float(self.nextProperty(conf))
        #Number of worker nodes at startup.
        self.initial_machines = int(self.nextProperty(conf))
        #Size of the sliding window for the output running average [in seconds]
        self.averageWindow = int(self.nextProperty(conf))
        #Polling interval for the output time series [in seconds]
        self.pollingInterval = int(self.nextProperty(conf))
        #Percentage of waste, amount of wasted money in the final cost that is acceptable to the client.
        self.acceptableWaste = int(self.nextProperty(conf))
        #Simulation time
        self.sim_time = int(self.nextProperty(conf))
        #Scheduling algorithm (either random or round robin)
        self.algoName = self.nextProperty(conf)
        self.schedule_algorithm = algorithms_map[self.algoName]
        #Random seed
        self.seed = int(self.nextProperty(conf))
        random.seed(self.seed)

        self.remainingTasks = 0

        self.initiated = False
        self.monitors = {}
        self.monitorPlots = {}
        self.monitorFunctions = {}

    def plotLine (self,monitor):
        SimPlot().plotLine (monitor).mainloop()

    def nextProperty(self, file):
        line = file.readline()
        while(line != '' and line.startswith("#")):
           line = file.readline()
        return line.strip('\n')

    def init_objects(self):
        if self.initiated:
            raise Exception("Objects already initiated")
        else:
            self.initiated = True
        #objects
        self.scheduler = Scheduler(self)
        
        # Activate scheduler
        activate(self.scheduler, self.scheduler.run())

    def finish_objects(self):
        if not self.initiated:
            raise Exception("Objects not initiated")
        else:
            self.initiated = False
        
        #stop scheduler
        self.scheduler.stop()

    def addMonitor (self, name):
        try: 
            self.monitors[name]
            print 'Error: Monitor name "%s" is already being used' % (name)
            sys.exit (-1)
        except KeyError:
            self.monitors[name] = Monitor (name = name)

        return self.monitors[name]

    def addMonitorPlot (self, name, monitor):
        try:
            self.monitorPlots[name]
            print 'Error: Plot function already defined for monitor %s' % (name)
            sys.exit (-1)
        except KeyError:
            monitor.ylab=name
            monitor.tlab="time"
            self.monitorPlots[name] = monitor

    def addMonitorFunction (self, name, fn):
        try:
            self.monitorFunctions[name]
            print 'Error: Monitor function already defined for monitor %s' % (name)
            sys.exit (-1)
        except KeyError:
            self.monitorFunctions [name] = fn
            
    def executeMonitorFunctions (self):
        self.printSep()
        print "- Calculating monitor functions"
        for each in self.monitorFunctions:
            print "%s\t:\t %s" % (each, self.monitorFunctions[each]())

    def executeMonitorPlots (self):
        self.printSep()
        print "- Generating graph files"
        for each in self.monitorPlots:
            fileName = each + " series - Seed " + str(self.seed) + ".ps"
            plot = SimPlot ()
            pl = plot.plotLine (self.monitorPlots[each], color="blue",width=2)
            pl.postscr(fileName)
            print "Graph file created\t:\t " + fileName

    def printSep(self):
        print "-----------------------------------------"
예제 #56
0
  - defines a method for loading a debugging configuration file that determines
    what debugging output gets displayed and what gets filtered out.
    
* **Axon.debugConfigDefaults**

  - defines a method that supplies a default debugging configuration.
"""
import Component
import Ipc
import Linkage
import Microprocess
import Postoffice
import Scheduler
import debug
import util
import AdaptiveCommsComponent
import AxonExceptions
import CoordinatingAssistantTracker
import debugConfigFile
import Box
import ThreadedComponent
import Introspector

from Axon import AxonObject, AxonType

Microprocess.microprocess.setSchedulerClass(Scheduler.scheduler)
#Microprocess.microprocess.setTrackerClass(CoordinatingAssistantTracker.coordinatingassistanttracker)
Microprocess.microprocess.setTrackerClass(None)
CoordinatingAssistantTracker.coordinatingassistanttracker()
Scheduler.scheduler() # Initialise the class.
예제 #57
0
class Network(object):
    def __init__(self, scheduler=None):
        if scheduler != None:
            self.scheduler = scheduler
        else:
            self.scheduler = Scheduler()
        self.patterns = {}
        self.nodes = {}
        self.__initPatterns()
        self.nmapcachepath = os.path.join(Specification.Specification.filebase, "network")
        self.nmapcachefile = "nmap_cache.txt"
        self.nmapcommand = ["nmap", "192.168.0.0/24"]
        self.ifconfigcommand = ["ifconfig"]
        self.nics = []
        self.myip = None
        self.ifconfigraw = None
        self.nmapraw = None
        self.mapping = False
        self.notifier = Notifier()
        self.scheduler.addTask("network_mapper", self.update, 30)

    def __initPatterns(self):
        self.patterns["ifconfig"] = {}
        self.patterns["ifconfig"]["nic"] = re.compile(r"(?P<name>[^\s]+).?")
        self.patterns["ifconfig"]["addr"] = re.compile(r"\s*inet\saddr:(?P<ip>[^\s]+).*")
        self.patterns["nmap"] = {}
        self.patterns["nmap"]["start"] = re.compile(
            r"Starting\sNmap\s(?P<version>[^\s]+)\s\( http:\/\/nmap.org \)\sat\s(?P<year>\d+)-(?P<month>\d+)-(?P<day>\d+)\s(?P<hour>\d+):(?P<minute>\d+)\s(?P<timezone>\w+)"
        )
        self.patterns["nmap"]["report"] = re.compile(r"Nmap\sscan\sreport\sfor\s(?P<ip>[^\s]+)")
        self.patterns["nmap"]["service"] = re.compile(
            r"(?P<port>[^\/]+)\/(?P<protocol>\w+)\s+(?P<state>\w+)\s+(?P<service>.+)"
        )
        self.patterns["nmap"]["mac"] = re.compile(r"MAC\sAddress:\s+(?P<mac>[^\s]+)\s+\((?P<brand>[^\)]+)\)")

    def update(self):
        if self.ifconfigraw == None and self.mapping == False:
            self.mapping = True
            self.__ifconfig()
            self.mapping = False
        if not self.__loadNmapCache():
            if self.myip != None and self.nmapraw == None and self.mapping == False:
                self.mapping = True
                self.nmapcommand[1] = self.__getBroadcast()
                self.__scanNetwork()
                self.__cacheNmap()
                self.mapping = False

    def __scanNetwork(self):
        result = False
        p = Popen(self.nmapcommand, stdout=PIPE)
        o = p.communicate()[0]
        if p.returncode == 0:
            result = True
            self.nmapraw = o
            res = self.__parseNmap()
            if res:
                self.notifier.addNotice("Nmap command complete")
        return result

    def __parseNmap(self):
        res = False
        if self.nmapraw != None:
            tmp = []
            for l in self.nmapraw.split("\n"):
                match = self.patterns["nmap"]["report"].match(l)
                if match:
                    tmp.append({"ip": match.group("ip"), "mac": "", "brand": "", "services": []})
                match = self.patterns["nmap"]["service"].match(l)
                if match:
                    tmp[len(tmp) - 1]["services"].append(
                        {
                            "port": match.group("port"),
                            "protocol": match.group("protocol"),
                            "state": match.group("state"),
                            "service": match.group("service"),
                        }
                    )
                match = self.patterns["nmap"]["mac"].match(l)
                if match:
                    tmp[len(tmp) - 1]["mac"] = match.group("mac")
                    tmp[len(tmp) - 1]["brand"] = match.group("brand")
            for t in tmp:
                self.nodes[t["ip"]] = t
            res = True
        return res

    def __parseDate(self, raw):
        date = None
        if raw != None and raw != "":
            linecount = 0
            for l in raw.split("\n"):
                match = self.patterns["nmap"]["start"].match(l)
                if match:
                    datestring = "{0}-{1}-{2} {3}:{4} {5}".format(
                        match.group("year"),
                        match.group("month"),
                        match.group("day"),
                        match.group("hour"),
                        match.group("minute"),
                        match.group("timezone"),
                    )
                    tmpdate = datetime.datetime.strptime(datestring, "%Y-%m-%d %H:%M %Z")
                    now = datetime.datetime.now()
                    if tmpdate > now - datetime.timedelta(days=1):
                        date = tmpdate
                    break
                linecount += 1
                if linecount >= 10:
                    break
        return date

    def __cacheNmap(self):
        filepath = os.path.join(self.nmapcachepath, self.nmapcachefile)
        f = open(filepath, "w")
        f.write(self.nmapraw)
        f.close()

    def __loadNmapCache(self):
        if not os.path.exists(self.nmapcachepath):
            os.makedirs(self.nmapcachepath)
            return False
        filepath = os.path.join(self.nmapcachepath, self.nmapcachefile)
        if not os.path.exists(os.path.join(self.nmapcachepath, self.nmapcachefile)):
            return False
        if len(self.nodes) > 0:
            return False
        f = open(filepath, "r")
        data = f.read()
        f.close()
        date = self.__parseDate(data)
        if date:
            self.nmapraw = data
            parsed = self.__parseNmap()
            self.notifier.addNotice("Nmap cache loaded")
            return parsed
        return False

    def __ifconfig(self):
        result = False
        p = Popen(self.ifconfigcommand, stdout=PIPE)
        o = p.communicate()[0]
        if p.returncode == 0:
            result = True
            self.ifconfigraw = o
            res = self.__parseIfconfig()
            if res:
                self.notifier.addNotice("Ifconfig command complete")
        return result

    def __parseIfconfig(self):
        if self.ifconfigraw != None:
            for l in self.ifconfigraw.split("\n"):
                match = self.patterns["ifconfig"]["nic"].match(l)
                if match:
                    self.nics.append({"name": match.group("name"), "ip": ""})
                match = self.patterns["ifconfig"]["addr"].match(l)
                if match:
                    self.nics[-1]["ip"] = match.group("ip")
            for n in self.nics:
                if n["name"] != "lo" and n["ip"] != "127.0.0.1":
                    self.myip = n["ip"]
                    break
            return True
        return False

    def __getBroadcast(self):
        ip = None
        if self.myip != None and self.myip != "127.0.0.1":
            parts = self.myip.split(".")
            if len(parts) == 4:
                ip = "{0}.{1}.{2}.{3}".format(parts[0], parts[1], parts[2], "0/24")
        return ip
예제 #58
0
# coding=utf-8
# author = ZengYueTian
# date = 2015-04-01

#
# Description:  test task schedule workflow
#

from Scheduler import *
import time


if __name__ == "__main__":

    scheduler = Scheduler()
    scheduler.clean_redis_db()
    scheduler.reset_task_to_submit()

    while True:
        time.sleep(1)
        scheduler.get_robot_status()

        time.sleep(1)
        scheduler.get_task_status()

        time.sleep(1)
        scheduler.sync_submitted_task()

        time.sleep(1)
        scheduler.schedule_task()