def update(self,dt): super(Machinery, self).update(dt) if self.active: if not self.recently_active: self.recently_active = True self.refresh_image() #self.set_active() self.operating_time_since_last_maintenance += dt if random.random() < (dt/util.seconds(1,'month'))*self.operating_time_since_last_maintenance/(100*self.wear*self.maintenance_interval): self.broken = True else: if self.recently_active: self.recently_active = False #self.set_active() self.refresh_image() if self.broken: if not self.maint_task or self.maint_task.name != ''.join(['Repair ',self.name]): self.maint_task = Task(''.join(['Repair ',self.name]), owner = self, timeout=None, task_duration = util.seconds(4,'hours'), severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.maint_task) if self.operating_time_since_last_maintenance >= self.wear*self.maintenance_interval and ( not self.maint_task or self.maint_task.task_ended() ): self.maint_task = Task(''.join(['Maintain ',self.name]), owner = self, timeout=None, task_duration = util.seconds(1,'hours'), severity='LOW', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) #print self.maint_task.timeout,self.maint_task.task_duration self.installed.station.tasks.add_task(self.maint_task)
def __init__(self, name): cpu = self.cpu create_commands = ( 'pacman-key --init', 'pacman-key --populate msys2', 'pacman --noconfirm -Sy --force --asdeps pacman-mirrors', 'pacman --noconfirm -Sy tar {}'.format( ' '.join(self.packages(name))), 'rm -rf /var/cache/pacman/pkg', 'python -m pip install pip==10.0.1 --upgrade', 'pip install wheel', 'mv {}/{}/bin/{{{{mingw32-,}}}}make.exe'.format(msys(cpu), mingw(cpu)), 'tar -jcf msys2.tar.bz2 --hard-dereference {}'.format(msys(cpu)), ) env = MsysBase.by_name(cpu) h = hashlib.sha1(env.hexdigest.encode()) h.update(';'.join(create_commands).encode()) self.hexdigest = h.hexdigest() Task.__init__( self, task_env=env, description='msys2 image: {} {}'.format(name, cpu), index=self.index, expireIn='26 weeks', command=create_commands, artifact='msys2.tar.bz2', )
def test_add_tasks_to_tasklist(self): tasklist = TaskList(self.tl, "task-inserter") tasklist.insert() self.failIf(not tasklist.exists()) tl_id = tasklist.get_id() task = Task(self.task, tl_id) task.insert()
def test_add_tasks_to_tasklist(self): #print ("adding tasks") tasklist = TaskList("task-inserter") tasklist.create() self.failIf(not tasklist.exists()) tl_id = tasklist.get_id() task = Task(self.task, tl_id) task.create()
def __init__(self, os_and_version): (os, version) = os_and_version.split('.', 1) env = TaskEnvironment.by_name('{}.build'.format(os)) kwargs = {} if len(version) == 40: # Assume it's a sha1 pretty_version = 'r{}'.format(version) artifact_version = 'unknown' expire = '2 weeks' else: pretty_version = 'v{}'.format(version) artifact_version = version expire = '26 weeks' desc = 'hg {}'.format(pretty_version) if os == 'linux': artifact = 'mercurial-{}-cp27-none-linux_x86_64.whl' else: desc = '{} {} {}'.format(desc, env.os, env.cpu) if os.startswith('osx'): artifact = ('mercurial-{{}}-cp27-cp27m-macosx_{}_intel.whl' .format(os[3:])) kwargs.setdefault('env', {})['MACOSX_DEPLOYMENT_TARGET'] = \ '10.10' else: artifact = 'mercurial-{}-cp27-cp27m-mingw.whl' pre_command = [] if len(version) == 40: source = './hg' pre_command.extend( self.install('{}.{}'.format(os, MERCURIAL_VERSION))) pre_command.extend([ 'hg clone https://www.mercurial-scm.org/repo/hg -r {}' .format(version), 'rm -rf hg/.hg', ]) # 2.6.2 is the first version available on pypi elif parse_version('2.6.2') <= parse_version(version): source = 'mercurial=={}' else: source = 'https://mercurial-scm.org/release/mercurial-{}.tar.gz' Task.__init__( self, task_env=env, description=desc, index='{}.hg.{}'.format(env.hexdigest, pretty_version), expireIn=expire, command=pre_command + [ 'python -m pip wheel -v --build-option -b --build-option' ' $PWD/wheel -w $ARTIFACTS {}'.format(source.format(version)), ], artifact=artifact.format(artifact_version), **kwargs )
def update(self,dt): super(Machinery, self).update(dt) if self.broken: if not self.maint_task or self.maint_task.name != ''.join(['Repair ',self.name]): self.maint_task = Task(''.join(['Repair ',self.name]), owner = self, timeout=util.seconds(1,'months'), task_duration = util.seconds(4,'hours'), severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.maint_task) if self.maint_timer < 0 and ( not self.maint_task or self.maint_task.task_ended() ): self.maint_task = Task(''.join(['Maintain ',self.name]), owner = self, timeout=util.seconds(1,'months'), task_duration = util.seconds(1,'hours'), severity='LOW', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) #print self.maint_task.timeout,self.maint_task.task_duration self.installed.station.tasks.add_task(self.maint_task) self.maint_timer -= dt
def __init__(self, msg=None): Task.__init__(self, msg) self.map_tasks = {} self.reduce_tasks = {} self.im = self.intermediate self.im.task_id = msg self.im.reducers = self.reducers self.maptask = MapWrapper(self.map('MapTask'), self.im, self) self.reducetask = ReduceWrapper(self.reduce('ReduceTask'), self.im, self) for src in self.datasources.itervalues(): src.connect()
def install_task(self,station): if self.installed or not station: return self.task = TaskSequence(name = ''.join(['Install Equipment']), severity = "LOW", logger=self.logger) self.task.station = station self.task.add_task(Task(name = ''.join(['Pick Up']), owner = self, timeout=86400, task_duration = 60, severity='LOW', fetch_location_method=Searcher(self,station,check_storage=True).search,station=station)) self.task.add_task(Task(name = ''.join(['Install']), owner = self, timeout=86400, task_duration = 600, severity='LOW', fetch_location_method=Searcher(EquipmentFilter(target=self.type, subtype=self.name, comparison_type="Equipment Slot"),station).search,station=station)) station.tasks.add_task(self.task)
def __init__(self, msg=None): Task.__init__(self, msg) self.__lock = Lock() self.map_tasks = {} self.reduce_tasks = {} self.im = self.intermediate self.im.task_id = msg self.im.reducers = self.reducers self.maptask = MapWrapper(self.map("MapTask"), self.im, self) self.reducetask = ReduceWrapper(self.reduce("ReduceTask"), self.im, self) for src in self.datasources.itervalues(): src.open()
def main(config, model_type=None, controller_type=None, struct_type=None, visualizer_type=None, load_path=None, save_path=None): config = copy(config) if model_type is not None: config["model_type"] = model_type if controller_type is not None: config["controller_type"] = controller_type if struct_type is not None: config["struct_type"] = struct_type if load_path is not None: config["load_path"] = load_path if save_path is not None: config["save_path"] = save_path task = Task.from_config_dict(config) metrics = task.run_experiment() if visualizer_type is not None: visualizer = visualizer_type(task) visualizer.visualize_generic_example() return metrics
def undock_module(self, item=[None,None]): if not item[0]: return False if not self.installed: return False if self.docking_task and not self.docking_task.task_ended(): return False #TODO check for fuel, engines, etc on item self.docking_item = item if self.docking_item[0] and not self.docking_item[1]: self.docking_item[1] = self.docking_item[0].get_neighbor_dock(self.installed.station) #TODO this WILL crash, implement when it does #check for hatches closed, start tasks if necessary dock = self.docking_item[0].equipment[self.docking_item[1]][3] if dock.docked: self.docking_item[0].disconnect(self.docking_item[1], dock.partner) return False #undock stations self.installed.station.undock_station(self.docking_item[0].station) #start motion new_loc, new_orient = self.installed.station.get_safe_distance_orient() new_loc *= 2 self.docking_path = FlightPath(self.docking_item[0], self.docking_item[1], new_loc, new_orient,self.docking_duration, FlightType='UNDOCK') self.docking_task = Task("Undock module", owner = self, timeout=None, task_duration = self.docking_duration, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.docking_task) return True
def dock(self, target, partner=None, instant = False): self.docked=target self.in_vaccuum = False self.partner=partner if instant: self.open_() else: if not self.installed.station: return self.task = Task(''.join(['Open Hatch']), owner = self, timeout=None, task_duration = 300, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.task)
def __init__(self, cpu): assert cpu in CPUS _create_command = ( 'curl -L http://repo.msys2.org/distrib/{cpu}' '/msys2-base-{cpu}-{version}.tar.xz | xz -cd | bzip2 -c' ' > $ARTIFACTS/msys2.tar.bz2'.format( cpu=msys_cpu(cpu), version=MSYS_VERSION) ) h = hashlib.sha1(_create_command.encode()) self.hexdigest = h.hexdigest() self.cpu = cpu Task.__init__( self, task_env=DockerImage.by_name('base'), description='msys2 image: base {}'.format(cpu), index=self.index, expireIn='26 weeks', command=[_create_command], artifact='msys2.tar.bz2', )
def __init__(self, name): super(DockerImageTask, self).__init__(name) kwargs = {} if isinstance(self.base, DockerImage): kwargs['dependencies'] = [self.base] Task.__init__(self, task_env=self, description='docker image: {}'.format(name), index=self.index, expireIn='26 weeks', image='python:2.7', dind=True, command=Task.checkout() + [ 'pip install requests-unixsocket zstandard==0.8.1', 'python2.7 repo/CI/docker.py build {}'.format(name), 'python2.7 repo/CI/docker.py save {}' ' > $ARTIFACTS/image.tar.zst'.format(name), ], artifact='image.tar.zst', **kwargs)
def api_task_enqueue(): try: token = request.json["token"] except KeyError: return abort(400) name = db.check_api_token(token) if name: task = Task(request.json["website_id"], request.json["url"], request.json["priority"], request.json["callback_type"], json.dumps(request.json["callback_args"])) logger.info("API force enqueue by " + name + "\n(" + str(task.to_json()) + ")") taskManager.queue_task(task) return "" else: return abort(403)
def test_added_task_has_id_set(): """Make sure the task_id field is set by tasks.add().""" # GIVEN an initialized tasks db # AND a new task is added new_task = Task('sit in chair', owner='me', done=True) task_id = tasks.add(new_task) # WHEN task is retrieved task_from_db = tasks.get(task_id) # THEN task_id matches id field assert task_from_db.id == task_id
def admin_queue_empty_websites(): if "username" in session: for website_id in get_empty_websites(): website = db.get_website_by_id(website_id) task = Task(website.id, website.url, 1) taskManager.queue_task(task) flash("Dispatched empty websites", "success") return redirect("/dashboard") else: abort(403)
def test_added_task_has_id_set(): """Убедимся, что поле task_id установлено tasks.add().""" # GIVEN an initialized tasks db # AND a new task is added new_task = Task('sit in chair', owner='me', done=True) task_id = tasks.add(new_task) # WHEN task is retrieved task_from_db = tasks.get(task_id) # THEN task_id matches id field assert task_from_db.id == task_id
def check_token(self, TOKEN_path): valid = False try: TOKEN = read_json(TOKEN_path) authentication = Task(self.loop) if token_seems_valid(TOKEN): hdrs = copy.deepcopy(headers) hdrs.update( {'Authorization': 'Bearer {}'.format(TOKEN['id_token'])}) auth = authentication.do_the_task( self.AUTHORISE_URL, hdrs, json.dumps({"token": TOKEN['id_token']})) if 'status_code' in auth[0] and auth[0]['status_code'] == 200: valid = True print( colored("Token authentication successful ", color='green')) self.TOKEN = TOKEN headers.update({ 'Authorization': 'Bearer {}'.format(self.TOKEN['id_token']) }) else: print( colored("Token authentication Failed, status code:{}". format(auth[0]['status_code']), color='red'), colored("\nbut, new Token request will be called", color='blue')) valid = False else: valid = False except: print(colored("Token Not found!", color='red'), colored("but, new request will be called", color='blue')) finally: return valid
def admin_rescan_website(website_id): require_role("admin") website = db.get_website_by_id(website_id) if website: priority = request.args.get("priority") if "priority" in request.args else 1 task = Task(website_id, website.url, priority) taskManager.queue_task(task) flash("Enqueued rescan task", "success") else: flash("Website does not exist", "danger") return redirect("/website/" + str(website_id))
def test_add_task_has_id_set(): """make sure task_is filed is set""" # GIVEN initialize db # auto run initialized_tasks_db # And new task id new_task = Task('sit in chair', owner='me', done=True) task_id = tasks.add(new_task) # WHEN task retrieve task_from_db = tasks.get(task_id) # THEN task_id match assert task_from_db.id == task_id
def test_add_taks_has_id_set(): """Make sure the task_id field is set by tasks.add().""" # given an intialized tasks db # Add a new task is added new_task = Task('sit in chair', owner='me', done=True) task_id = tasks.add(new_task) # when task is retrived task_from_db = tasks.get(task_id) # then task_id matches id field assert task_from_db.id == task_id
def tasks_mult_per_owner(): """Several owners with several tasks each""" return ( Task('make a cookie', 'Brian', True), Task("use a emoji", 'Katie', False), Task('move to Berlin', 'Michelle', False), Task('create', 'Michelle', False), Task('Inspier', 'Michelle', False), Task('Encourage', 'Michelle', False), Task('Fix what Brian did', 'Michelle', False), )
def handle_update(self, driver, update): """ Handle a task update. If we were resync-ing then check if we have completed the sync. :param driver: :param update: """ # Extract the task classname from the task ID and update the task. classname = Task.classname_from_task_id(update.task_id.value) # Lookup the existing task, if there is one. Only update if the task # ID matches the one in our cache. task = self.tasks.get(classname) if not task or task.task_id != update.task_id.value: if update.state == mesos_pb2.TASK_RUNNING: _log.debug("Killing unrecognized task: %s" % update.task_id.value) driver.killTask(update.task_id) else: _log.debug("Ignoring update from unrecognized stopped task.") return _log.debug("TASK_UPDATE - %s: %s on Slave %s", mesos_pb2.TaskState.Name(update.state), task, update.slave_id.value[-7:]) # Update the task. task.update(update) # We expect restart tasks to fail, but not others. if task.failed() and not task.restarts: _log.error("\t%s is in unexpected state %s with message '%s'", task, mesos_pb2.TaskState.Name(update.state), update.message) _log.debug("\tData: %s", repr(str(update.data))) _log.debug("\tSent by: %s", mesos_pb2.TaskStatus.Source.Name(update.source)) _log.debug("\tReason: %s", mesos_pb2.TaskStatus.Reason.Name(update.reason)) _log.debug("\tMessage: %s", update.message) _log.debug("\tHealthy: %s", update.healthy) # Persist the updated tasks to the datastore. zk.store_tasks(self.agent_id, self.tasks) # If we were resyncing then check if all of our tasks are now syncd. if not self.agent_syncd and all(task.clean for task in self.tasks.values()): _log.debug("Agent is now sync'd - perform full reconcile to tidy " "up old tasks") self.agent_syncd = True driver.reconcileTasks([])
def tasks_mult_per_owner(): return ( Task('Make a cookie', 'Raphael'), Task('Use an emoji', 'Raphael'), Task('Move to Berlin', 'Raphael'), Task('Create', 'Micheal'), Task('Inspire', 'Micheal'), Task('Encourage', 'Micheal'), )
def load(self, jobrecord): ''' Populate a job's state from a job record ''' self.state.load(jobrecord) if self.state.tasks: task_objects = [] for task in self.state.tasks: task_obj = Task(parent_id=self.state.id) # Add values from new task object to those from the jobrecord task_obj.state._state = dict(task_obj.state._state.items() + task.items()) task_objects.append(task_obj) self.state.tasks = task_objects return self
def undock(self, instant = False): if not self.open: self.docked = None self.in_vaccuum = True self.partner = None return True if self.task and not self.task.task_ended(): return False if instant: self.close_() else: #TODO check and add a task for disconnecting the pipes self.task = Task(''.join(['Close Hatch']), owner = self, timeout=86400, task_duration = 300, severity='LOW', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.task) return self.undock(instant)
def fetch_task(self): try: payload = {"token": config.API_TOKEN} r = requests.post(config.SERVER_URL + "/task/get", data=payload) if r.status_code == 200: text = r.text logger.info("Fetched task from server : " + text) task_json = json.loads(text) return Task(task_json["website_id"], task_json["url"]) return None except Exception as e: raise e
def __init__(self, name): super(DockerImageTask, self).__init__(name) kwargs = {} if isinstance(self.base, DockerImage): kwargs['dependencies'] = [self.base] Task.__init__( self, task_env=self, description='docker image: {}'.format(name), index=self.index, expireIn='26 weeks', image='python:3.7', dind=True, command=Task.checkout() + [ 'pip install requests-unixsocket zstandard==0.8.1', 'python repo/CI/docker.py build {}' .format(name), 'python repo/CI/docker.py save {}' ' > $ARTIFACTS/image.tar.zst'.format(name), ], artifact='image.tar.zst', **kwargs )
def test_added_task_has_id_set(tasks_db): """Make sure the task_id field is set by tasks.add().""" # GIVEN an initialized tasks db # AND a new task is added new_task = Task('sit in chair', owner='me', done=True) task_id = tasks.add(new_task) # WHEN task is retrieved task_from_db = tasks.get(task_id) # THEN task_id matches id field assert task_from_db.id == task_id # AND contents are equivalent (except for id) # the [:-1] syntax returns a list with all but the last element assert task_from_db[:-1] == new_task[:-1]
def setup(self): LOG.info(LOG_TAG + "proxy connected to Lego server") super(LegoHandler, self).setup() self.stop = threading.Event() self.is_first_frame = True self.commited_bitmap = np.zeros((1, 1), np.int) # basically nothing self.temp_bitmap = {'start_time': None, 'bitmap': None, 'count': 0} self.task = Task.Task(bitmaps) self.counter = { 'confident': 0, 'not_confident': 0, 'same_as_prev': 0, 'diff_from_prev': 0, }
class Machinery(Equipment): #ancestor class for things that need regular maintenance def __init__(self): if not hasattr(self,'imgfile'): self.imgfile = "images/placeholder_machinery.tif" super(Machinery, self).__init__() self.idle_draw = 0.001 #kW self.maint_timer = random.randrange(util.seconds(6,'months'), util.seconds(2,'years') ) self.maint_task = None self.wear = 1.0 self.broken = False def refresh_image(self): super(Machinery, self).refresh_image() self.sprite.add_layer('Machinery',util.load_image("images/machinery_40x40.png")) def update(self,dt): super(Machinery, self).update(dt) if self.broken: if not self.maint_task or self.maint_task.name != ''.join(['Repair ',self.name]): self.maint_task = Task(''.join(['Repair ',self.name]), owner = self, timeout=util.seconds(1,'months'), task_duration = util.seconds(4,'hours'), severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.maint_task) if self.maint_timer < 0 and ( not self.maint_task or self.maint_task.task_ended() ): self.maint_task = Task(''.join(['Maintain ',self.name]), owner = self, timeout=util.seconds(1,'months'), task_duration = util.seconds(1,'hours'), severity='LOW', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) #print self.maint_task.timeout,self.maint_task.task_duration self.installed.station.tasks.add_task(self.maint_task) self.maint_timer -= dt def task_finished(self,task): #TODO add supply usage super(Machinery, self).task_finished(task) if not task: return if task.name == ''.join(['Maintain ',self.name]) and task.target == self: self.maint_task = None self.wear += (1 - self.wear) / 2 self.maint_timer = random.randrange(int (util.seconds(1,'months') * self.wear), int( util.seconds(6,'months') * self.wear ) ) elif task.name == ''.join(['Repair ',self.name]) and task.target == self: self.broken = False self.wear += (1 - self.wear) / 2 def task_failed(self,task): super(Machinery, self).task_failed(task) if not task: return if task.name == ''.join(['Maintain ',self.name]) and task.target == self: self.wear -= 0.05 if random.random() > self.wear: self.broken = True
def __init__(self, text, subproc, task=None, afterCB=None, *args, **kw): from tasks import Task self._subproc = subproc self._aborted = False if not isinstance(text, basestring): text = ' '.join(text) if task is None: self._ownTask = True self._task = Task(text, self.cancelCB, self.statusCB) self._text = "" else: self._ownTask = False task.addCallbacks(self.cancelCB, self.statusCB) self._task = task self._text = text self._after = afterCB self.returncode = None
def api_task_enqueue(): try: token = request.json["token"] except KeyError: return abort(400) name = db.check_api_token(token) if name: task = Task(request.json["website_id"], request.json["url"], request.json["priority"], request.json["callback_type"], json.dumps(request.json["callback_args"])) taskManager.queue_task(task) return "" else: return abort(403)
def define_tasks(self, filtered_run_info=None): if filtered_run_info is None: filtered_run_info = [] for sample in filtered_run_info: read_format = sample['library_layout'] if read_format == Task.SINGLE: job_list = self.jobs_single elif read_format == Task.PAIRED: job_list = self.jobs_paired else: raise ValueError("read_format not in [{}, {}]".format( Task.SINGLE, Task.PAIRED)) self.tasks.append( Task(sample_id=sample['sample'], jobs=job_list, run_ids=sample['runs'], read_format=read_format, download_paths=sample["download_paths"]))
def readFile(path): """Reads a file from disk and parses it into a task list.""" tasksFile = open(path, 'r') tasklist = Tasklist(os.path.splitext(os.path.basename(path))[0]) parentStack = [] #Initialize the stack of parent tasks task = None for line in tasksFile: deepness = 1 line = line.rstrip() #Remove the trailing whitespaces (newlines, mainly) while line.startswith("\t"): #starts with tab deepness += 1 #Add a deepness level to the current line line = line[1:] #Remove the leading tab if line.startswith("- ") or line.endswith(':'): #It's a task or a project (both are saved as tasks) if task is not None: #There's a previous task tasklist.append(task) #Commit previous task to the list if (getLast(parentStack) is None and deepness >= 1) or deepness > getLast(parentStack)[DEEP]: parentStack.append({TASK:task, DEEP:deepness}) #This'll be parent to the next ones else: if deepness < getLast(parentStack)[DEEP]: parentStack.pop() #Up one level #Get the new task's parent from the stack parent = getLast(parentStack) if parent is not None: parent = parent[TASK] #Create the task object if line.startswith("- "): #It's a regular task taskParts = parseTask(line[2:]) task = Task(taskParts[TITLE], '', parent) task.addAttributeList(taskParts[TAGS]) else: #It's a project task = Task(line[:-1], '', parent) #Remove ':' from the task title task.addAttribute(properties.TASKPAPER_ISPROJECT_ATTR, '') #Mark it as a project else: if task is not None: task.notes += line #Put last task in the list if task is not None: tasklist.append(task) return tasklist
def test_add_first_attempt(tmpdir): # GIVEN an empty db db_path = tmpdir.join('.tasks_db.json') db = TasksDB(db_path) assert 0 == db.count() # WHEN I add something new_task = Task('do something') new_id = db.add(new_task) # THEN count() return 1 # AND list_tasks() will return new task # AND list_tasks() will return 1 item # AND get() will return new task # (choice of side effects, but this is overkill) assert 1 == db.count() assert new_task == db.list_tasks()[0] assert 1 == len(db.list_tasks()) assert new_task == db.get(new_id)
def test_member_access(): t = Task('get cheese', 'james') assert t.summary == 'get cheese' assert t.owner == 'james' assert (t.done, t.id) == (False, None)
def dock_module(self,item=[None,None],target=[None, None]): if not item[0]: return False if not self.installed: return False if self.docking_task and not self.docking_task.task_ended(): return False #TODO check for fuel, engines, etc on item self.docking_item = item self.docking_target = target if not self.docking_target[0]: mods = self.installed.station.modules self.docking_target[0] = random.choice( [ mods[m] for m in mods if mods[m].get_random_dock() ] ) if not self.docking_target[1]: self.docking_target[1] = self.docking_target[0].get_random_dock() if self.docking_item[0] and not self.docking_item[1]: self.docking_item[1] = self.docking_item[0].get_random_dock(side_port_allowed=False) self.docking_path = FlightPath(self.docking_target[0], self.docking_target[1], self.docking_item[0], self.docking_item[1],self.docking_duration, FlightType='DOCK') self.docking_task = Task("Dock module", owner = self, timeout=None, task_duration = self.docking_duration, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.docking_task) return True
def update(self,dt): super(MissionComputer, self).update(dt) if not self.installed or not self.mission: return if not self.objective: self.mission = None return if self.objective.completed and (not self.task or self.task.task_ended()): #print self.objective.name self.logger.info(''.join(['Mission objective completed: ',self.objective.name,' Updating mission...'])) self.task = Task(''.join(['Update Mission']), owner = self, timeout=None, task_duration = 30, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.task) self.objective_timer = 30 self.objective_tick -= dt if self.objective_tick < 0: self.objective_tick = self.objective_timer if not self.objective.completed: self.objective.carry_out(station=self.installed.station, scenario=self.scenario) self.objective_timer += 30 else: self.objective_timer = 30
def api_get_task(): token = request.form.get("token") name = db.check_api_token(token) if name: task = db.pop_task(name) if task: print("Assigning task " + str(task.website_id) + " to " + name) else: print("No queued tasks, creating new rescan task") website_id = db.get_oldest_website_id() website = db.get_website_by_id(website_id) task = Task(website_id, website.url) db.put_task(task) task = db.pop_task(name) return Response(str(task), mimetype="application/json") else: return abort(403)
def __init__(self, worker_count=2, worker_names=None): if worker_names == None: worker_names = [ 'worker_{}'.format(i + 1) for i in xrange(worker_count) ] # worker properties self.worker_count = worker_count self.worker_names = worker_names self.worker_map = dict([(name, i) for i, name in enumerate(self.worker_names)]) # task variables self.tasks = [] # contains self.max_task_states = [] self.max_task_requirements = [] self.task_map = {} # init dependencies between task completions self._dependency_map = {} self._nullblock = Task(name='null', timeblocks=[Active(1)])
def try_enqueue(url): url = os.path.join(url, "") url = od_util.get_top_directory(url) if not od_util.is_valid_url(url): return "<strong>Error:</strong> Invalid url. Make sure to include the appropriate scheme.", "warning" website = db.get_website_by_url(url) if website: return "Website already exists", "danger" website = db.website_exists(url) if website: return "A parent directory of this url has already been posted", "danger" if db.is_blacklisted(url): return "<strong>Error:</strong> " \ "Sorry, this website has been blacklisted. If you think " \ "this is an error, please <a href='/contribute'>contact me</a>.", "danger" if not od_util.is_od(url): return "<strong>Error:</strong>" \ "The anti-spam algorithm determined that the submitted url is not " \ "an open directory or the server is not responding. If you think " \ "this is an error, please <a href='/contribute'>contact me</a>.", "danger" website_id = db.insert_website( Website( url, str(request.remote_addr + "_" + request.headers.get("X-Forwarded-For", "")), request.user_agent)) task = Task(website_id, url, priority=1) taskManager.queue_task(task) return "The website has been added to the queue", "success"
def test_member_access(): t = Task('buy milk', 'brian') assert t.summary == 'buy milk' assert t.owner == 'brian' assert (t.done, t.id) == (False, None)
def test_defaults(): t1 = Task() t2 = Task(None, None, False, None) assert t1 == t2
class DockingRing(Equipment): def __init__(self): if not hasattr(self,'imgfile'): self.imgfile = "images/closed_hatch.tif" self.open = False self.player_usable = True #toggle to allow player to "turn off" hatches super(DockingRing, self).__init__() self.docked = None #a pointer to the module we've docked to self.in_vaccuum = True self.partner = None #a pointer to the docking equipment partner def toggle_player_usable(self): self.player_usable = not self.player_usable self.refresh_image() def refresh_image(self): super(DockingRing, self).refresh_image() if self.sprite is None: return if self.open: self.sprite.add_layer('DockingRing',util.load_image("images/open_hatch.png")) else: self.sprite.add_layer('DockingRing',util.load_image("images/closed_hatch.png")) #import pyglet #img1=pyglet.image.AnimationFrame(util.load_image("images/blank_40x40.png"),0.5 if not self.player_usable else None) #img2=pyglet.image.AnimationFrame(util.load_image("images/half_red.png"),0.5) #animation = pyglet.image.Animation([img1,img2]) if self.player_usable: self.sprite.add_layer('Forbidden',util.load_image("images/blank_40x40.png")) else: self.sprite.add_layer('Forbidden',util.load_image("images/half_red.png")) self.sprite.layer['Equipment'].visible=False #these two need to generate tasks for unpowered rings def open_(self): if self.open: return if not self.docked: return False, "What are you, nuts?!" self.open = True self.refresh_image() if self.partner and not self.partner.open: self.installed.station.paths.add_edge(self.installed.get_node(self),self.docked.get_node(self.partner),weight=1) self.partner.installed.station.paths.add_edge(self.installed.get_node(self),self.docked.get_node(self.partner),weight=1) def close_(self): if not self.open: return self.open=False self.refresh_image() if self.partner and not self.partner.open: self.installed.station.paths.remove_edge(self.installed.get_node(self),self.docked.get_node(self.partner)) self.partner.installed.station.paths.remove_edge(self.installed.get_node(self),self.docked.get_node(self.partner)) def dock(self, target, partner=None, instant = False): self.docked=target self.in_vaccuum = False self.partner=partner if instant: self.open_() else: if not self.installed.station: return self.task = Task(''.join(['Open Hatch']), owner = self, timeout=None, task_duration = 300, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.task) def undock(self, instant = False): if not self.open: self.docked = None self.in_vaccuum = True self.partner = None return True if self.task and not self.task.task_ended(): return False if instant: self.close_() else: #TODO check and add a task for disconnecting the pipes self.task = Task(''.join(['Close Hatch']), owner = self, timeout=86400, task_duration = 300, severity='LOW', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.task) return self.undock(instant) def task_finished(self,task): super(DockingRing, self).task_finished(task) if task.name == 'Close Hatch': #TODO check for someone in the other module if self.partner: self.partner.close_() if self.open: self.close_() elif task.name == 'Open Hatch': if not self.open: self.open_() if self.partner: self.partner.open_() #TODO add a task to connect pipes, open other side def task_work_report(self,task,dt): if task.name == 'Close Hatch' and not self.open: self.task.flag('COMPLETED') elif task.name == 'Open Hatch' and self.open: self.task.flag('COMPLETED')
class Equipment(object): def __init__(self, installed=None, logger=None, name='UnspecifiedEquipment'): self.id=util.register(self) self.components = [] #list of items used to build this equipment self.installed=None #pointer to module if installed, none if loose self.mass=100 self.task=None self.power_usage = 0 #in kilowatts self.powered = False self.idle_draw = 0 #in kilowatts self.in_vaccuum = False #if True, requires EVA to service self.volume = 1.3 #m^3 self.broken = False self.name = name self.type = 'Misc' self.satisfies = dict() #what qualities can this equipment provide? self.logger = logging.getLogger(logger.name + '.' + self.name) if logger else util.generic_logger self.loggername = self.logger.name self.visible = True self.local_coords = 0.75*np.array([random.uniform(-1,1),random.uniform(-1,1),0]) self.sprite=None #basic health stats and such go here, as well as hooking into the task system #if not hasattr(self,'imgfile'): self.imgfile = "images/placeholder_equipment.tif" self.refresh_image() def __getstate__(self): d = dict(self.__dict__) del d['logger'] del d['sprite'] #if 'docked' in d: d.pop('docked') return d def __setstate__(self, d): self.__dict__.update(d) self.logger = logging.getLogger(self.loggername) if self.loggername else util.generic_logger self.sprite=None self.refresh_image() def refresh_image(self): if not gv.config['GRAPHICS']: return if gv.config['GRAPHICS'] == 'pyglet': import graphics_pyglet if self.sprite: self.sprite.delete() self.sprite = graphics_pyglet.LayeredSprite(name=self.name) self.sprite.add_layer('Equipment',util.make_solid_image(40,40,(100,100,100,255))) self.sprite.owner = self def update(self,dt): if self.task and self.task.task_ended(): self.task = None self.powered = self.idle_draw > 0 and self.draw_power(self.idle_draw,dt) > 0 def install(self,home,loc=None): if self.installed: return None # "Can't install the same thing twice!" self.installed=home if loc: home.equipment[loc][3] = self if home.station: self.logger = logging.getLogger(home.station.logger.name +'.' + home.short_id+ '.' + self.name) self.logger.info(''.join([self.name," installed in ", home.short_id,' at ',str(loc)])) return self def get_name(self): if not self.installed: return None for e in self.installed.equipment.keys(): if self.installed.equipment[e][3] == self: return e return None def refresh_station(self): if self.installed and self.installed.station: self.logger = logging.getLogger(self.installed.station.logger.name +'.' + self.installed.short_id+ '.' + self.name) elif self.station: self.logger = logging.getLogger(self.station.logger.name +'.' + self.name) self.task = None #a new station means our tasks are no longer relevant def uninstall(self): if not self.installed: return None # "Can't install the same thing twice!" worked = self.installed.uninstall_equipment(self) self.installed=None self.refresh_image() return worked def draw_power(self,kilowattage,dt): #kilowatts if self.installed and (not hasattr(self,'broken') or not self.broken): #it's installed, not broken or can't break #self.installed.station.resources.resources['Electricity'].available -= kilowattage*dt/3600 #TODO add equivalent heat into module return self.installed.station.resources.resources['Electricity'].draw(kilowattage*dt) return 0 def task_finished(self,task): if task: if task.name == "Install" and not self.installed: home, loc = task.station.get_module_from_loc(task.location) , task.location.split('|')[1] if home.equipment[loc][3]: #this slot already has something installed! task.drop() return if task.assigned_to.held == self: task.assigned_to.held = None else: print "Object not held!" self.install(home,loc) self.refresh_image() elif task.name == "Uninstall" and self.installed: self.uninstall() elif task.name == 'Pick Up': if self.installed: assert self.uninstall(), 'Unknown error after uninstallation' module = task.station.get_module_from_loc(task.location) if module.stowage.remove(self) is None: self.logger.warning('This equipment not found in expected location. Dropping pickup task.') task.flag('CLOSED') return task.assigned_to.held=self self.refresh_image() elif task.name == 'Put Down': module = task.station.get_module_from_loc(task.location) module.stowage.add(self) task.assigned_to.held=None self.refresh_image() def task_failed(self,task): pass def task_dropped(self,task): if task.assigned_to.held == self: task.assigned_to.drop_held() def install_task(self,station): if self.installed or not station: return self.task = TaskSequence(name = ''.join(['Install Equipment']), severity = "LOW", logger=self.logger) self.task.station = station self.task.add_task(Task(name = ''.join(['Pick Up']), owner = self, timeout=86400, task_duration = 60, severity='LOW', fetch_location_method=Searcher(self,station,check_storage=True).search,station=station)) self.task.add_task(Task(name = ''.join(['Install']), owner = self, timeout=86400, task_duration = 600, severity='LOW', fetch_location_method=Searcher(EquipmentFilter(target=self.type, subtype=self.name, comparison_type="Equipment Slot"),station).search,station=station)) station.tasks.add_task(self.task) def uninstall_task(self): if not self.installed: return self.task = Task(name = ''.join(['Uninstall']), owner = self, timeout=None, task_duration = 300, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.task)
def __init__(self, os_and_variant): os, variant = (os_and_variant.split('.', 2) + [''])[:2] if variant == 'asan' and os == 'osx10_10': os = 'osx10_11' env = TaskEnvironment.by_name('{}.build'.format(os)) artifact = 'git-cinnabar-helper' if os.startswith('mingw'): artifact += '.exe' artifacts = [artifact] def prefix(p, s): return p + s if s else s make_flags = [] hash = None head = None desc_variant = variant extra_commands = [] if variant == 'asan': if os.startswith('osx'): opt = '-O2' else: opt = '-Og' make_flags.append('LDFLAGS=-static-libasan') make_flags.append( 'CFLAGS="{} -g -fsanitize=address -fno-omit-frame-pointer"' .format(opt)) elif variant == 'coverage': make_flags.append('CFLAGS="-coverage"') artifacts += ['coverage.tar.xz'] extra_commands = [ 'mv repo/git-core/{{cinnabar,connect,hg}}*.gcno repo/helper', '(cd repo && tar -Jcf $ARTIFACTS/coverage.tar.xz' ' helper/{{cinnabar,connect,hg}}*.gcno)', ] elif variant == 'old' or variant.startswith('old:'): if len(variant) > 3: head = variant[4:] else: head = old_helper_head() hash = helper_hash(head) variant = '' elif variant: raise Exception('Unknown variant: {}'.format(variant)) if os == 'linux': make_flags.append('CURL_COMPAT=1') elif not os.startswith('osx'): make_flags.append('USE_LIBPCRE1=YesPlease') make_flags.append('USE_LIBPCRE2=') make_flags.append('CFLAGS+=-DCURLOPT_PROXY_CAINFO=246') hash = hash or helper_hash() Task.__init__( self, task_env=env, description='helper {} {}{}'.format( env.os, env.cpu, prefix(' ', desc_variant)), index='helper.{}.{}.{}{}'.format( hash, env.os, env.cpu, prefix('.', variant)), expireIn='26 weeks', command=Task.checkout(commit=head) + [ 'make -C repo helper -j $({}) prefix=/usr{} V=1'.format( nproc(env), prefix(' ', ' '.join(make_flags))), 'mv repo/{} $ARTIFACTS/'.format(artifact), ] + extra_commands, artifacts=artifacts, )
class DockingComputer(Computer, Rack): def __init__(self): if not hasattr(self,'imgfile'): self.imgfile = "images/docking_computer.tif" super(DockingComputer, self).__init__() self.idle_draw = 0.200 #kW self.docking_mode = 'DOCK' self.docking_item = None #The thing doing the docking or undocking (THEM) self.docking_target = None #The (module,dock) where it will be docking to (US) self.docking_path = None #The path object that will interpolate its journey self.docking_task = None self.docking_duration = util.seconds(2,'minutes') self.name = "Docking console" def refresh_image(self): super(DockingComputer, self).refresh_image() if self.sprite is None: return self.sprite.add_layer('DockingComputer',util.load_image("images/smalldockingsymbol_40x40.png")) def dock_module(self,item=[None,None],target=[None, None]): if not item[0]: return False if not self.installed: return False if self.docking_task and not self.docking_task.task_ended(): return False #TODO check for fuel, engines, etc on item self.docking_item = item self.docking_target = target if not self.docking_target[0]: mods = self.installed.station.modules self.docking_target[0] = random.choice( [ mods[m] for m in mods if mods[m].get_random_dock() ] ) if not self.docking_target[1]: self.docking_target[1] = self.docking_target[0].get_random_dock() if self.docking_item[0] and not self.docking_item[1]: self.docking_item[1] = self.docking_item[0].get_random_dock(side_port_allowed=False) self.docking_path = FlightPath(self.docking_target[0], self.docking_target[1], self.docking_item[0], self.docking_item[1],self.docking_duration, FlightType='DOCK') self.docking_task = Task("Dock module", owner = self, timeout=None, task_duration = self.docking_duration, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.docking_task) return True def undock_module(self, item=[None,None]): if not item[0]: return False if not self.installed: return False if self.docking_task and not self.docking_task.task_ended(): return False #TODO check for fuel, engines, etc on item self.docking_item = item if self.docking_item[0] and not self.docking_item[1]: self.docking_item[1] = self.docking_item[0].get_neighbor_dock(self.installed.station) #TODO this WILL crash, implement when it does #check for hatches closed, start tasks if necessary dock = self.docking_item[0].equipment[self.docking_item[1]][3] if dock.docked: self.docking_item[0].disconnect(self.docking_item[1], dock.partner) return False #undock stations self.installed.station.undock_station(self.docking_item[0].station) #start motion new_loc, new_orient = self.installed.station.get_safe_distance_orient() new_loc *= 2 self.docking_path = FlightPath(self.docking_item[0], self.docking_item[1], new_loc, new_orient,self.docking_duration, FlightType='UNDOCK') self.docking_task = Task("Undock module", owner = self, timeout=None, task_duration = self.docking_duration, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.docking_task) return True def task_finished(self,task): super(DockingComputer, self).task_finished(task) if not task or not self.installed: return if task.name == "Dock module": self.docking_item[0].location, self.docking_item[0].orientation = self.docking_path.get_time_point(task.task_duration) self.docking_item[0].station.percolate_location(self.docking_item[0]) self.docking_item[0].refresh_image() self.installed.station.dock_module(self.docking_target[0], self.docking_target[1], self.docking_item[0], self.docking_item[1]) self.docking_item[0].station.position='Docked' if task.name == "Undock module": self.docking_item[0].station.position='Approach' def task_work_report(self,task,dt): if task.name.startswith('Dock module') or task.name.startswith('Undock module'): self.docking_item[0].location, self.docking_item[0].orientation = self.docking_path.get_time_point(task.task_duration - task.task_duration_remaining) self.docking_item[0].station.percolate_location(self.docking_item[0]) self.docking_item[0].refresh_image()
def test_defaults(): """Using no parameters should invoke defaults""" t1 = Task() t2 = Task(None, None, False, None) assert t1 == t2
def uninstall_task(self): if not self.installed: return self.task = Task(name = ''.join(['Uninstall']), owner = self, timeout=None, task_duration = 300, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.task)
class MissionComputer(Computer, Rack): def __init__(self, scenario=None): super(MissionComputer, self).__init__() self.idle_draw = 1.000 #kW self.scenario=scenario self.mission = None self.objective = None self.objective_timer = 30 self.objective_tick = 30 self.name = "Mission console" def refresh_image(self): super(MissionComputer, self).refresh_image() if self.sprite is None: return self.sprite.add_layer('DockingComputer',util.load_image("images/smallmissionsymbol_40x40.png")) def update(self,dt): super(MissionComputer, self).update(dt) if not self.installed or not self.mission: return if not self.objective: self.mission = None return if self.objective.completed and (not self.task or self.task.task_ended()): #print self.objective.name self.logger.info(''.join(['Mission objective completed: ',self.objective.name,' Updating mission...'])) self.task = Task(''.join(['Update Mission']), owner = self, timeout=None, task_duration = 30, severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.task) self.objective_timer = 30 self.objective_tick -= dt if self.objective_tick < 0: self.objective_tick = self.objective_timer if not self.objective.completed: self.objective.carry_out(station=self.installed.station, scenario=self.scenario) self.objective_timer += 30 else: self.objective_timer = 30 def generate_mission(self, selection='New Module', target_id = '', module_id = ''): new_miss = mission.Mission() new_miss.load_mission( selection=selection, target_id=target_id, module_id=module_id) self.new_mission(new_miss) def new_mission(self,mission): if not mission or (self.task and not self.task.task_ended()) or not self.installed: return self.task = Task(''.join(['Log Mission']), owner = self, timeout=None, task_duration = 30, severity='HIGH', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.task.mission=mission self.installed.station.tasks.add_task(self.task) def task_finished(self,task): super(MissionComputer, self).task_finished(task) if not task or not self.installed: return if task.name == "Log Mission": self.mission = self.task.mission self.objective = self.mission.current_objective() elif task.name == "Update Mission": self.objective = self.mission.current_objective() if not self.objective: return self.objective.carry_out(station=self.installed.station, scenario=self.scenario) self.logger.info(''.join(['Mission updated. Current objective: ',self.objective.name])) self.update(0)
def tasks_just_a_few(): """ All summaries and owners are unique. """ return (Task('Write some code', 'Brian', True), Task("Code review Brian's code", 'Katie', False), Task('Fix what Brian did', 'Michelle', False))
def __init__(self, os_and_version): (os, version) = os_and_version.split('.', 1) if os.startswith('osx'): build_image = TaskEnvironment.by_name('osx10_10.build') else: build_image = DockerImage.by_name('build') if os == 'linux' or os.startswith('osx'): h = hashlib.sha1(build_image.hexdigest.encode()) h.update(b'v2') if os == 'linux': description = 'git v{}'.format(version) else: env = build_image description = 'git v{} {} {}'.format(version, env.os, env.cpu) Task.__init__( self, task_env=build_image, description=description, index='{}.git.v{}'.format(h.hexdigest(), version), expireIn='26 weeks', command=Task.checkout( 'git://git.kernel.org/pub/scm/git/git.git', 'v{}'.format(version) ) + [ 'make -C repo -j$({}) install prefix=/ NO_GETTEXT=1' ' NO_OPENSSL=1 NO_TCLTK=1 DESTDIR=$PWD/git'.format( nproc(build_image)), 'tar -Jcf $ARTIFACTS/git-{}.tar.xz git' .format(version), ], artifact='git-{}.tar.xz'.format(version), ) else: env = TaskEnvironment.by_name('{}.build'.format(os)) raw_version = version if 'windows' not in version: version = { version: version + '.windows.1', '2.17.1': '2.17.1.windows.2', }.get(version) if version.endswith('.windows.1'): min_ver = version[:-len('.windows.1')] else: min_ver = version.replace('windows.', '') Task.__init__( self, task_env=build_image, description='git v{} {} {}'.format(version, env.os, env.cpu), index='{}.git.v{}'.format(os, raw_version), expireIn='26 weeks', command=[ 'curl -L https://github.com/git-for-windows/git/releases/' 'download/v{}/MinGit-{}-{}-bit.zip' ' -o git.zip'.format(version, min_ver, msys.bits(env.cpu)), 'unzip -d git git.zip', 'curl -L https://github.com/git-for-windows/git/releases/' 'download/v{}/Git-{}-{}-bit.tar.bz2 | ' 'tar -C git -jx {}/libexec/git-core/git-http-backend.exe' .format(version, min_ver, msys.bits(env.cpu), msys.mingw(env.cpu).lower()), 'tar -jcf $ARTIFACTS/git-{}.tar.bz2 git'.format( raw_version), ], artifact='git-{}.tar.bz2'.format(raw_version), )
def test_replace(): """replace() should change passed if fields.""" t_before = Task('finish book', 'brian', False) t_after = t_before._replace(id=10, done=True) t_expected = Task('finish book', 'brian', True, 10) assert t_after == t_expected
def new_mission(self,mission): if not mission or (self.task and not self.task.task_ended()) or not self.installed: return self.task = Task(''.join(['Log Mission']), owner = self, timeout=None, task_duration = 30, severity='HIGH', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.task.mission=mission self.installed.station.tasks.add_task(self.task)
def test_member_access(): """Check.field functionality of namedtuple""" t = Task('buy milk', 'brian') assert t.summary == 'buy milk' assert t.owner == 'brian' assert (t.done, t.id) == (False, None)
class Machinery(Equipment): #ancestor class for things that need regular maintenance def __init__(self): self.active = False super(Machinery, self).__init__() self.idle_draw = 0.001 #kW #self.maint_timer = random.randrange(util.seconds(6,'months'), util.seconds(2,'years') ) self.maint_task = None self.operating_time_since_last_maintenance = 0 self.maintenance_interval = util.seconds(7,'days') self.wear = 1.0 self.broken = False self.recently_active=False self.type = 'MACHINERY' def refresh_image(self): super(Machinery, self).refresh_image() if self.sprite is None: return import pyglet img1=pyglet.image.AnimationFrame(util.load_image("images/machinery_40x40.png"),0.5 if self.active else None) img2=pyglet.image.AnimationFrame(util.load_image("images/machinery_40x40_1.png"),0.5) animation = pyglet.image.Animation([img1,img2]) self.sprite.add_layer('Machinery',animation) def set_active(self): #TODO figure out why the below line doesn't work self.sprite.layer['Machinery'].image.frames[0].duration=0.5 if self.recently_active else None def update(self,dt): super(Machinery, self).update(dt) if self.active: if not self.recently_active: self.recently_active = True self.refresh_image() #self.set_active() self.operating_time_since_last_maintenance += dt if random.random() < (dt/util.seconds(1,'month'))*self.operating_time_since_last_maintenance/(100*self.wear*self.maintenance_interval): self.broken = True else: if self.recently_active: self.recently_active = False #self.set_active() self.refresh_image() if self.broken: if not self.maint_task or self.maint_task.name != ''.join(['Repair ',self.name]): self.maint_task = Task(''.join(['Repair ',self.name]), owner = self, timeout=None, task_duration = util.seconds(4,'hours'), severity='MODERATE', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) self.installed.station.tasks.add_task(self.maint_task) if self.operating_time_since_last_maintenance >= self.wear*self.maintenance_interval and ( not self.maint_task or self.maint_task.task_ended() ): self.maint_task = Task(''.join(['Maintain ',self.name]), owner = self, timeout=None, task_duration = util.seconds(1,'hours'), severity='LOW', fetch_location_method=Searcher(self,self.installed.station).search,logger=self.logger) #print self.maint_task.timeout,self.maint_task.task_duration self.installed.station.tasks.add_task(self.maint_task) def task_finished(self,task): #TODO add supply usage super(Machinery, self).task_finished(task) if not task: return if task.name == ''.join(['Maintain ',self.name]) and task.target == self: self.maint_task = None self.wear -= self.operating_time_since_last_maintenance/(self.wear*self.maintenance_interval) - 1 print self.operating_time_since_last_maintenance, self.wear, self.maintenance_interval self.wear = max(self.wear,0) self.operating_time_since_last_maintenance = 0 elif task.name == ''.join(['Repair ',self.name]) and task.target == self: self.maint_task = None self.broken = False self.wear += (1 - self.wear) / 2