def started(self, component): # x = yield self.call(task(factorial, 10)) Timer(1, Event.create("foo"), persist=True).register(self) self.fire(task(download_web_page, 'http://www.slickdeals.net')) # async self.fire(task(download_web_page, 'http://www.google.com')) # async self.fire(task(download_web_page, 'http://www.yahoo.com')) # async
def decorated(itself, event, *args, **kwargs): evt = task(func, itself, event, *args, **kwargs) ret = yield itself.call(evt, self.pool) result = ret.value if isinstance(result, Exception): raise result yield result
def test_args(worker): x = worker.fire(task(add, 1, 2)) assert pytest.wait_for(x, "result") assert x.result assert x.value == 3
def google(self, source, target, args): """Perform a google search and return the first result. Syntax: GOOGLE <search> """ if not args: yield "No search terms specified." q = args value = yield self.call( task( get_url, self.url, params={"v": "1.0", "q": q} ), "workerthreads" ) response = value.value response.raise_for_status() data = loads(response.content)["responseData"] yield "Total results: {0:s}".format( data["cursor"]["estimatedResultCount"] ) hits = data["results"] yield 'Top {0:d} hits:'.format(len(hits)) for i, hit in enumerate(hits): yield " {0:d}. {1:s}".format((i + 1), hit["unescapedUrl"]) yield "For more results, see: {0:s}".format( data["cursor"]["moreResultsUrl"] )
def test(worker): x = worker.fire(task(f)) assert pytest.wait_for(x, "result") assert x.result assert x.value == 1000000
def upload_event(self, event): ustart = time_of_now() print(time_of_now(), "Sensing+Uploading started") ###log.log(45, "Before upload BYTES\t" + str(get_tx_bytes())) yield self.call(task(upload_a_packet), self._worker) ###log.log(45, "After upload BYTES\t" + str(get_tx_bytes())) print(time_of_now(), "Sensing+Uploading IS COMPLETED. of time ", ustart)
def upload_event(self, *args, **kwargs): ## args[0] is the reading queue queue = args[0] print(EventReport("UploadEvent", (str(args) + ", " + str(kwargs)))) #lprint(EventReport("UploadEvent", "started")) CircuitsApp.shareState = 0 yield self.call(task(upload_a_bundle(queue))) print("Upload successful.") CircuitsApp.shareState = 1 lprint(EventReport("UploadEvent", "ENDED")) CircuitsApp.last_uploaded = getSentByte() - CircuitsApp.startbyte lprint( EventReport("UploadedSoFar", str(convert_size(CircuitsApp.last_uploaded))), 45) M = c["interval"]["M"] t = time.time() - CircuitsApp.starttime if (M == t): return #~ if(M - t < c["interval"]["upload"]): #~ CircuitsApp.timers["upload"] = Timer((M - t), UploadEvent(args[0]), persist=False).register(self) #~ else: if not CircuitsApp.isEnd: CircuitsApp.timers["upload"] = Timer(c["interval"]["upload"], UploadEvent(args[0]), persist=False).register(self) rate = CircuitsApp.upload_rate u = CircuitsApp.last_uploaded CircuitsApp.upload_rate = min((c["params"]["D"] - u) * 1.0 / (M - t), c["upload"]["max_rate"])
def sample(self): if self.sample_pending: logging.warn("%s already has a sample pending.", self.get_dev_id()) else: logging.debug("%s Spawning sample task", self.sn) self.queued_time = time.time() self.sample_pending = True self.fire(task(self._sample), "sample_worker")
def _scan_filesystem(self, path): self.log('Scanning path:', path) new_volume = objectmodels['volume']({'uuid': std_uuid()}) new_volume.name = path.split('/')[-1] new_volume.path = path new_volume.save() self.volumes[new_volume.uuid] = new_volume self.fireEvent(task(filewalk, path, self), 'filemanagerworkers')
def started(self, component): self.fire(task(factorial, 3)) # async self.fire(task(factorial, 5)) # async self.fire(task(factorial, 7)) # async self.fire(task(factorial, 10)) # async self.fire(task(factorial, 11)) # async self.fire(task(factorial, 11)) # async self.fire(task(factorial, 12)) # async self.fire(task(factorial, 14)) # async Timer(1, Event.create("foo"), persist=True).register(self)
def test_args(manager, watcher, worker): e = task(add, 1, 2) e.success = True x = worker.fire(e) assert watcher.wait("task_success") assert x.value == 3
def test_failure(manager, watcher, worker): e = task(err) e.failure = True x = worker.fire(e) assert watcher.wait("task_failure") assert isinstance(x.value[1], Exception)
def test_success(manager, watcher, worker): e = task(foo) e.success = True x = worker.fire(e) assert watcher.wait("task_success") assert x.value == 1000000
def get_frames(self): self.log('Getting frames') try: # frames, durations, log = \ data = self.filename, self.ignore_timings, self.scale self.fireEvent(task(load_image, data), "gifimport_" + self.dataname) except Exception as e: self.log("[GIF_WORKERS]", e, type(e), exc=True)
def connect(self, sock, *args): host, port = args[:2] self.pending[sock] = True self.fire(reply(sock, Message(u"NOTICE", u"*", u"*** Looking up your hostname..."))) e = task(check_host, sock) e.complete = True e.complete_channels = ("server",) self.fire(e, "threadpool")
def auth(self, source, target, args): """Authorize a Remote Plugin Syntax: AUTH <plugin> <password> """ if not args: yield "No plugin specified." return tokens = args.split(" ", 2) plugin = first(tokens) password = second(tokens) data = self.parent.data.rplugins config = self.parent.config["rplugins"] if password != config["password"]: yield "Authorization failed." return if plugin in data["pending"]: url = data["pending"][plugin] del data["pending"][plugin] data["allowed"][plugin] = True value = yield self.call( task( verify_plugin, url, config["path"], data["allowed"], ), "workerprocesses" ) allowed, plugin = value.value if allowed: msg = log( "Remote Plugin {0:s} ({1:s}) successfully authorized.", url, plugin ) yield msg else: del data["allowed"][plugin] msg = log( "Remote Plugin {0:s} ({1:s}) failed authorization.", url, plugin ) yield msg else: yield log("Remote Plugin {0:s} not found.", plugin)
def status(self, source, target, args): """Report Web Status Syntax: STATUS """ try: url = self.parent.server.http.base yield self.call(task(check_url, url), "workerthreads") yield "Web: Online" except Exception as error: yield "Web: Offline ({0:s})".format(error)
def _shell_action(self, event, *args, **kwargs): """The @handler() annotation without an event name makes this a default handler - for all events on this component's queue. This will be called with some "internal" events from Circuits, so you must declare the method with the generic parameters (event, *args, **kwargs), and ignore any messages that are not from the Actions module. """ if not isinstance(event, ActionMessage): # Some event we are not interested in return # Based on the action name, # find the commandline template for this action action_name = event.name action_template = self.options.get(action_name, self.options.get("command")) # Disposition can vary too disposition_args = self.options.get(action_name + "_result_disposition", self.options.get("result_disposition", "new_attachment")) # The disposition arguments can use template features # Add a few convenience properties event.message["properties"] = event.message.get("properties") or {} event.message["action_name"] = action_name event.message["properties"]["_message_headers"] = event.hdr() disposition_args = template_functions.render(disposition_args, event.message) # Construct the disposition result_disposition = Disposition(self.rest_client(), disposition_args) # Run the action based on the template and data; # the result is returned as a string. evt = task(_shell_run, action_template, event.message) LOG.info("shell: %s", action_name) ret = yield self.call(evt) result = ret.value if isinstance(result, list): # results from circuits tasks come back wrapped in a list result = result[0] if isinstance(result, Exception): raise result if result is None: LOG.debug("No result.") yield "No result." else: # Process the result according to the chosen disposition LOG.debug("Result: %s", result) result_disposition.call(event, result) yield "Found result"
def connect(self, sock, *args): host, port = args[:2] self.pending[sock] = True self.fire( reply(sock, Message(u"NOTICE", u"*", u"*** Looking up your hostname..."))) e = task(check_host, sock) e.complete = True e.complete_channels = ("server", ) self.fire(e, "threadpool")
def decorated(itself, event, *args, **kwargs): """the decorated function""" LOG.debug("decorated") function_parameters = event.message.get("inputs", {}) def _the_task(event, *args, **kwargs): return func(itself, event, *args, **kwargs) def _call_the_task(evt, **kwds): # On the worker thread, call the function, and handle a single or generator result. LOG.debug("%s: _call_the_task", threading.currentThread().name) result_list = [] task_result_or_gen = _the_task(evt, *args, **kwds) if not isinstance(task_result_or_gen, GeneratorType): task_result_or_gen = [task_result_or_gen] for val in task_result_or_gen: if isinstance(val, StatusMessage): # Fire the wrapped status message event to notify resilient LOG.info("[%s] StatusMessage: %s", evt.name, val) itself.fire( StatusMessageEvent(parent=evt, message=val.text)) elif isinstance(val, FunctionResult): # Collect the result for return LOG.debug("[%s] FunctionResult: %s", evt.name, val) val.name = evt.name result_list.append(val) elif isinstance(val, Event): # Some other event, just fire it LOG.debug(val) itself.fire(val) elif isinstance(val, FunctionError_): LOG.error("[%s] FunctionError: %s", evt.name, val) itself.fire( FunctionErrorEvent(parent=evt, message=str(val))) evt.success = False return # Don't wait for more results! elif isinstance(val, Exception): raise val else: # Whatever this is, add it to the results LOG.debug(val) result_list.append(val) return result_list the_task = task(_call_the_task, event, **function_parameters) ret = yield itself.call(the_task, "functionworker") xxx = ret.value # Return value is the result_list that was yielded from the wrapped function yield xxx
def get_frames(self): self.log('Getting frames') if self.config['filename'] in (None, ""): self.log('No filename, cannot load gif') return try: # frames, durations, log = \ scale = (self.config['scale']['height'], self.config['scale']['width']) data = self.config['filename'], self.config[ 'ignore_timings'], scale self.fireEvent(task(load_image, data), "gifimport_" + self.uniquename) self.log('Worker started', lvl=verbose) except Exception as e: self.log("[GIF_WORKERS]", e, type(e), exc=True)
def inbound_app_decorator(itself, event, *args, **kwargs): """ The decorated method :param itself: The method to decorate :type itself: resilient_circuits.ResilientComponent :param event: The Event with the StompFrame and the Message read off the Message Destination :type event: resilient_circuits.action_message.InboundMessage """ def _invoke_inbound_app(evt, **kwds): """ The code to call when a method with the decorator `@inbound_app(<inbound_destination_api_name>)` is invoked. Returns result_list when method with the decorator `@inbound_app(<inbound_destination_api_name>)` is finished processing. A method that has this handler should yield a str when done - E.g: `yield "Processing Complete!"` :param evt: The Event with the StompFrame and the Message read off the Message Destination :type ia: resilient_circuits.action_message.FunctionMessage """ result_list = [] LOG.debug("Running _invoke_inbound_app in Thread: %s", threading.currentThread().name) # Get the required attribute from the message message = evt.message inbound_action = message.get("action", "Unknown") # Invoke the actual Function ia_results = ia(itself, evt.message, inbound_action) for r in ia_results: LOG.debug(r) result_list.append(r) return result_list invoke_inbound_app = task(_invoke_inbound_app, event) ia_result = yield itself.call(invoke_inbound_app, channels="functionworker") yield ia_result.value
def _query_event(self, query_definition, action_event): """ Handler that kicks off queries and updates """ numeric_log_level = LOG.getEffectiveLevel() loglevel = self.options.get("loglevel") if loglevel: try: numeric_log_level = getattr(logging, loglevel) except AttributeError: pass except TypeError: pass LOG.debug("Log level %s", numeric_log_level) yield self.call( task(search_and_update, self.run_search, self.rest_client(), self.options, query_definition, action_event.message, action_event.context, numeric_log_level, QueryRunner.datatable_locks))
def _call_async_downloader(self, search_link, download=False): ref = self.future_links[search_link] if download: ref.send_youtube = True if ref.is_complete: self._fire_youtube(search_link) return elif ref.is_downloading: return ref.set_downloading() self.fire(task(self.query_object.get_youtube_and_related_links, search_link), '*') # If our cache is less than PRE_SEND_COUNT, we need to build these up. count_left = self.PRE_SEND_COUNT - self._cached_count if count_left > 0: available = [key for key in self.future_links.keys() if self.future_links[key].is_uninitialized] number_to_fire = min(len(available), count_left) if number_to_fire: self._call_async_downloader(available[0])
def add(self, source, target, args): """Add a Remote Plugin Syntax: ADD <url> """ if not args: yield "No URL specified." url = first(args.split(" ", 1)) data = self.parent.data.rplugins config = self.parent.config["rplugins"] if url in data["enabled"]: yield log("Remote Plugin {0:s} already loaded!", url) else: value = yield self.call( task( verify_plugin, url, config["path"], data["allowed"], ), "workerprocesses" ) allowed, plugin = value.value if allowed: msg = log( "Remote Plugin {0:s} ({1:s}) is already authorized.", url, plugin ) yield msg else: data["pending"][plugin] = url msg = log( "Remote Plugin {0:s} ({1:s}) pending authorization.", url, plugin ) yield msg
def _on_request(self, event, client): event.stop() environ = { 'wsgi.errors': StringIO(), 'wsgi.run_once': False, 'wsgi.multithread': self.multithread, 'wsgi.multiprocess': self.multiprocess, 'PATH_INFO': client.path_segments.get('path_info'), } wsgi = WSGIClient(client.request, None, client.socket, client.server, environ) if self.multiprocess or self.multithread: yield self.call(task(wsgi, self.application)) else: wsgi(self.application) if wsgi.exc_info: try: reraise(wsgi.exc_info[0], wsgi.exc_info[1], wsgi.exc_info[2]) finally: wsgi.exc_info = None client.response = wsgi.response self.fire(RE(client), client.server.channel)
def tilecache(self, event, *args, **kwargs): """Checks and caches a requested tile to disk, then delivers it to client""" request, response = event.args[:2] try: filename, url = self._splitURL(request.path) except UrlError: return # Do we have the tile already? if os.path.isfile(filename): hfoslog("[TC] Tile exists in cache") # Don't set cookies for static content response.cookie.clear() try: yield serve_file(request, response, filename) finally: event.stop() else: # We will have to get it first. hfoslog("[TC] Tile not cached yet. Tile data: ", filename, url) if url in self._tilelist: hfoslog("[TC] Getting a tile for the second time?!", lvl=error) else: self._tilelist += url try: tile, log = yield self.call(task(get_tile, url), "tcworkers") if log: hfoslog("[TC] Thread error: ", log) except Exception as e: hfoslog("[TC]", e, type(e)) tile = None tilepath = os.path.dirname(filename) if tile: try: os.makedirs(tilepath) except OSError as e: if e.errno != errno.EEXIST: hfoslog("[TC] Couldn't create path: %s (%s)" % (e, type(e))) hfoslog("[TC] Caching tile.", lvl=verbose) try: with open(filename, "wb") as tilefile: try: tilefile.write(bytes(tile)) except Exception as e: hfoslog("[TC] Writing error: %s" % str([type(e), e])) except Exception as e: hfoslog("[TC] Open error: %s" % str([type(e), e])) return finally: event.stop() try: hfoslog("[TC] Delivering tile.", lvl=verbose) yield serve_file(request, response, filename) except Exception as e: hfoslog("[TC] Couldn't deliver tile: ", e, lvl=error) event.stop() hfoslog("[TC] Tile stored and delivered.", lvl=verbose) else: hfoslog("[TC] Got no tile, serving defaulttile: %s" % url) if self.defaulttile: try: yield serve_file(request, response, self.defaulttile) finally: event.stop() else: yield
def process_data(self, block): self.log.debug("New block to process: %s", block) self.fire(task(self._do_task, block), self.get_worker_channel()) # get inside the event handling framework
def test(manager, watcher, worker): x = manager.fire(task(f)) assert watcher.wait("task_complete") assert x.result assert x.value == 1000000
def test_args(manager, watcher, worker): x = manager.fire(task(add, 1, 2)) assert watcher.wait("task_complete") assert x.result assert x.value == 3
def hello_world(self, component): yield self.call(task(show_window_gtk))
def tilecache(self, event, *args, **kwargs): """Checks and caches a requested tile to disk, then delivers it to client""" request, response = event.args[:2] try: filename, url = self._splitURL(request.path) except UrlError: return # Do we have the tile already? if os.path.isfile(filename): hfoslog("Tile exists in cache", emitter="MTS") # Don't set cookies for static content response.cookie.clear() try: yield serve_file(request, response, filename) finally: event.stop() else: # We will have to get it first. hfoslog("Tile not cached yet. Tile data: ", filename, url, emitter="MTS") if url in self._tilelist: hfoslog("Getting a tile for the second time?!", lvl=error, emitter="MTS") else: self._tilelist += url try: tile, log = yield self.call(task(get_tile, url), "tcworkers") if log: hfoslog("Thread error: ", log, emitter="MTS") except Exception as e: hfoslog("[MTS]", e, type(e)) tile = None tilepath = os.path.dirname(filename) if tile: try: os.makedirs(tilepath) except OSError as e: if e.errno != errno.EEXIST: hfoslog("Couldn't create path: %s (%s)" % (e, type(e)), exc=True, emitter="MTS") hfoslog("Caching tile.", lvl=verbose, emitter="MTS") try: with open(filename, "wb") as tilefile: try: tilefile.write(bytes(tile)) except Exception as e: hfoslog("Writing error: %s" % str([type(e), e]), emitter="MTS") except Exception as e: hfoslog("Open error: %s" % str([type(e), e]), emitter="MTS") return finally: event.stop() try: hfoslog("Delivering tile.", lvl=verbose, emitter="MTS") yield serve_file(request, response, filename) except Exception as e: hfoslog("Couldn't deliver tile: ", e, lvl=error, emitter="MTS") event.stop() hfoslog("Tile stored and delivered.", lvl=verbose, emitter="MTS") else: hfoslog("Got no tile, serving defaulttile: %s" % url, emitter="MTS") if self.defaulttile: try: yield serve_file(request, response, self.defaulttile) finally: event.stop() else: yield
def get_tile_url(self, url, queue, layer, client): self.log('url:', url, queue, self.cancelled) if queue in self.cancelled: self.log('Not downloading tile due to cancellation') return filename, real_url = self._split_cache_url(url, 'tilecache') if filename is None or real_url is None: self.log('Could not get tile:', url) return try: tile, log = yield self.call(task(get_tile, real_url), "tclworkers") if log != "": self.log("Thread error: ", log, lvl=error) except Exception as e: self.log("[MTS]", e, type(e)) tile = None tile_path = os.path.dirname(filename) if tile: try: os.makedirs(tile_path) except OSError as e: if e.errno != errno.EEXIST: self.log("Couldn't create path: %s (%s)" % (e, type(e))) self.log("Caching tile.", lvl=verbose) try: with open(filename, "wb") as tile_file: try: tile_file.write(bytes(tile)) except Exception as e: self.log("Writing error: %s" % str([type(e), e])) except Exception as e: self.log("Open error: %s" % str([type(e), e])) return self.log("Tile stored.", lvl=verbose) self.requests[queue]['lists'][layer].remove(url) self.requests[queue]['completed'] += 1 completed = self.requests[queue]['completed'] if completed % 10 == 0: self.log('Sending progress report:', completed, '/', self.requests[queue]['tiles']) progress_report = { 'component': 'isomer.map.maptileservice', 'action': 'offline_loader_progress', 'data': { 'queue': queue, 'completed': completed } } self.fire(send(client, progress_report)) else: self.log("Got no tile: %s" % real_url)
def index(self, event, *args, **kwargs): """Checks and caches a requested tile to disk, then delivers it to client""" request, response = event.args[:2] cookie = request.cookie.get('isomer-client', request.cookie.get('isomer-dev', None)) if cookie is None: self.log('Client not authorized by cookie.', lvl=warn) return # self.log(request.path, lvl=verbose) try: filename, url = self._split_cache_url(request.path, 'tilecache') except UrlError: return # self.log('CACHE QUERY:', filename, url) # Do we have the tile already? if os.path.isfile(filename): self.log("Tile exists in cache", lvl=verbose) # Don't set cookies for static content response.cookie.clear() try: yield serve_file(request, response, filename) finally: event.stop() else: # We will have to get it first. self.log("Tile not cached yet. Tile data: ", filename, url, lvl=verbose) if url in self._tiles: self.log("Getting a tile for the second time?!", lvl=error) else: self._tiles += url try: tile, log = yield self.call(task(get_tile, url), "tcworkers") if log: self.log("Thread error: ", log, lvl=error) except Exception as e: self.log("[MTS]", e, type(e)) tile = None tile_path = os.path.dirname(filename) if tile: try: os.makedirs(tile_path) except OSError as e: if e.errno != errno.EEXIST: self.log("Couldn't create path: %s (%s)" % (e, type(e)), lvl=error) self.log("Caching tile.", lvl=verbose) try: with open(filename, "wb") as tile_file: try: tile_file.write(bytes(tile)) except Exception as e: self.log("Writing error: %s" % str([type(e), e]), lvl=error) except Exception as e: self.log("Open error on %s - %s" % (filename, str([type(e), e])), lvl=error) return finally: event.stop() try: self.log("Delivering tile.", lvl=verbose) yield serve_file(request, response, filename) except Exception as e: self.log("Couldn't deliver tile: ", e, lvl=error) event.stop() self.log("Tile stored and delivered.", lvl=verbose) else: self.log("Got no tile, serving default tile: %s" % url) if self.default_tile: try: yield serve_file(request, response, self.default_tile) except Exception as e: self.log('Cannot deliver default tile:', e, type(e), exc=True, lvl=error) finally: event.stop() else: yield
def discover(self): logging.info("Discovering ModbusTCP devices.") for ip in self._get_ip_scan_list(): self.fire(task(self._discover, ip), "discovery_worker")
def started(self, component): Timer(1, Event.create("foo"), persist=True).register(self) x = yield self.call(task(factorial, 10)) print("{0:d}".format(x.value)) self.stop()
def app_function_decorator(itself, event, *args, **kwargs): """ The decorated function :param itself: The function to decorate :type itself: resilient_circuits.ResilientComponent :param event: The Event with the StompFrame and the Message read off the Message Destination :type event: resilient_circuits.action_message.FunctionMessage """ function_inputs = event.message.get("inputs", {}) def _invoke_app_function(evt, **kwds): """ The code to call when a function with the decorator `@app_function(api_name)` is invoked. Returns result_list when function with the decorator `@app_function(api_name)` is finished processing. A method that has this handler should yield a StatusMessage or a FunctionResult - When a StatusMessage is yield'ed a StatusMessageEvent is fired with the text of the StatusMessage - When a FunctionResult is yield'ed it calls resilient-lib.ResultPayload.done() with the parameters of FunctionResult being passed to it and appends the result to result_list. E.g: `yield FunctionResult({"key":"value"})` `yield FunctionResult({"key": "value"}, success=False, reason="Bad call")` :param evt: The Event with the StompFrame and the Message read off the Message Destination :type fn: resilient_circuits.action_message.FunctionMessage """ LOG.debug("Running _invoke_app_function in Thread: %s", threading.currentThread().name) result_list = [] # Validate the fn_inputs in the Message fn_inputs = validate_fields([], kwds) LOG.info("[%s] Validated function inputs", evt.name) LOG.debug("[%s] fn_inputs: %s", evt.name, fn_inputs) rp = ResultPayload( itself.PACKAGE_NAME, version=constants.APP_FUNCTION_PAYLOAD_VERSION, **fn_inputs) fn_inputs_tuple = namedtuple( "fn_inputs", fn_inputs.keys())(*fn_inputs.values()) # Set evt.message in local thread storage itself.set_fn_msg(evt.message) # Invoke the actual Function fn_results = fn(itself, fn_inputs_tuple) for r in fn_results: if isinstance(r, StatusMessage): LOG.info("[%s] StatusMessage: %s", evt.name, r) itself.fire( StatusMessageEvent(parent=evt, message=r.text)) elif isinstance(r, FunctionResult): r.name = evt.name r.value = rp.done(content=r.value, success=r.success, reason=r.reason) LOG.info("[%s] Returning results", r.name) result_list.append(r) elif isinstance(r, Exception): raise r else: # Whatever this is, add it to the results LOG.debug(r) result_list.append(r) return result_list invoke_app_function = task(_invoke_app_function, event, **function_inputs) fn_result = yield itself.call(invoke_app_function, channels="functionworker") yield fn_result.value
def privmsg(self, event, source, target, raw): server = self.parent.servers[event.channels[0]] message = strip(raw, True) if source[0] not in ('spaceone', ) and target == server.nick: self.fire( PRIVMSG('spaceone', '%s wrote: %r' % ( source[0], message, )), server.channel) destination = source[0] if target == server.nick else target dest = destination if source[0] in self.ignore: return if source[0] in self._get_master( server, True) and source[0] not in self._get_master(server): self.fire(PRIVMSG('NickServ', 'STATUS %s' % (source[0], )), server.channel) messages = message for trigger in self.get_trigger(source, server): if messages.startswith(trigger): messages = messages[len(trigger):] break else: return stdout = None while messages: message, separator, messages = self.pop_next(messages) if not message: break from_stdin, to_stdout = None, None if separator in ('<', '<<'): from_stdin, separator, messages = self.pop_next(messages) elif separator in ('>', '>>'): to_stdout, separator, messages = self.pop_next(messages) command, args = (message.split(None, 1) + [''])[:2] # command = command.encode('utf-8') try: if from_stdin and from_stdin not in ('/dev/stdin'): stdout = from_stdin if self.commands.get(command) and self.commands[ command].get_default('threaded'): def execute(*args, **kwargs): try: return self.execute(*args, **kwargs) except: return sys.exc_info() cmd = task(execute, server, command, args, source, target, dest, stdin=stdout) else: cmd = Event.create('execute', server, command, args, source, target, dest, stdin=stdout) value = (yield self.call(cmd)).value if len(value) == 3: six.reraise(value[0], value[1], value[2]) dest, stdout = value if to_stdout and to_stdout not in ('/dev/stdout', ): if not to_stdout.startswith('/') and to_stdout not in ( 'spaceone', ) and source[0] not in ('spaceone', ): raise ArgumentInfos( 'Cannot redirect to other users/channels! login or redirect to /foo.' ) elif not to_stdout.startswith('/') and source[0] in ( 'spaceone', ): if dest == destination: dest = to_stdout elif to_stdout in ('spaceone', 'livinskull', 'tehron', 'dloser', '#wechall', '#wechalladmin', '#spaceone'): if dest == destination: dest = to_stdout else: stdout = [] if separator == '||': break elif separator in ('&&', ';', '&'): self.respond(dest, stdout, server) dest = destination stdout = [] except ArgumentInfos as exc: stdout = str(exc).splitlines() if not separator or separator == '&&': break except ArgumentParserError as exc: stdout = ("Error: %s" % (exc)).splitlines() if not separator or separator == '&&': break except BaseException as exc: print(traceback.format_exc()) stdout = ("Error: %s" % (exc)).splitlines() for line in self.wrap( traceback.format_exception(*sys.exc_info())): self.fire( PRIVMSG('spaceone', repr(line).lstrip('u').strip('\'"')), server.channel) break self.respond(dest, stdout, server)
def handle_executable(self, req, res, script, *args): if args: req.environ["SCRIPT_NAME"] = "/".join(req.selector.split("/")[:-(len(args))]) res.stream = True args = " ".join((str(script),) + args) self.fire(task(execute, req, res, args, cwd=str(script.parent)), "workers")
def takepicture(self): if self.recording: self.log('Taking a pic') self.fireEvent(task(take_picture, self.cam), 'camera')