class ThreddsConnection(object): def __init__(self, app, random_state, timeout=300.0, cached=False, **fs_args): """ Create an expiring connection to the THREDDS server. The connection will drop after 5 minutes of non-use. Any subsequent attempt to use the connection will initiate a new one. Access to the connection is RLock'd to ensure only one connection is alive at a time. Parameters: timeout : int, seconds The length of time in seconds to hold open a connection. Remaining keyword args are passed to the connection's constructor. """ self.app = app self.random_state = random_state self.cached = cached self._fs = {} self._fs_lock = RLock() self._fs_args = fs_args self._timer = None self.timeout = float(timeout) def cached(): doc = "If True, use cached data." def fget(self): return self._cached def fset(self, value): self.datasources = { 'hindcast': HINDCAST_CACHE_DATA_URI if value else HINDCAST_DATA_URI, 'forecast': FORECAST_CACHE_DATA_URI if value else FORECAST_DATA_URI, } self._cached = value return locals() cached = property(**cached()) def _forget(self): self.app.logger.info("Closing THREDDS connection") if self._timer: self._timer.cancel() self._timer = None self._fs = {} def _reset_timer(self): self.app.logger.info("Resetting THREDDS connection timer") if self._timer: self._timer.cancel() self._timer = Timer(self.timeout, self._forget) self._timer.start() hindcast_fs = property(fget=partial(fget, datasource='hindcast'), fdel=fdel) forecast_fs = property(fget=partial(fget, datasource='forecast'), fdel=fdel)
def __init__(self, name) : rospy.on_shutdown(self._on_node_shutdown) self.current_node = "Unknown" self.nogo_pre_active = False self.nogo_active = False self.nogos=[] self.stop_services=["/enable_motors", "/emergency_stop", "/task_executor/set_execution_status"] self.activate_services=["/enable_motors", "/reset_motorstop", "/task_executor/set_execution_status"] #Waiting for Topological Map self._map_received=False rospy.Subscriber('/topological_map', TopologicalMap, self.MapCallback) rospy.loginfo("Waiting for Topological map ...") while not self._map_received : pass rospy.loginfo(" ...done") self.update_service_list() print self.av_stop_services, self.av_activate_services #Subscribing to Localisation Topics rospy.loginfo("Subscribing to Localisation Topics") rospy.Subscriber('/current_node', String, self.currentNodeCallback) rospy.loginfo(" ...done") self.nogos = self.get_no_go_nodes() print self.nogos self._killall_timers=False t = Timer(1.0, self.timer_callback) t.start() rospy.loginfo("All Done ...") rospy.spin()
def program_next_poll(self, interval, method, args, kwargs): t = Timer(interval=interval, function=self.poller, kwargs={'interval': interval, 'method': method, 'args': args, 'kwargs': kwargs}) self.current_timers.append(t) # save the timer to be able to kill it t.setName('Poller thread for %s' % type(method.__self__).__name__) t.setDaemon(True) # so it is not locking on exit t.start()
def test_sqs_longpoll(self): c = SQSConnection() queue_name = "test_sqs_longpoll_%s" % int(time.time()) queue = c.create_queue(queue_name) self.addCleanup(c.delete_queue, queue, True) messages = [] # The basic idea is to spawn a timer thread that will put something # on the queue in 5 seconds and verify that our long polling client # sees the message after waiting for approximately that long. def send_message(): messages.append(queue.write(queue.new_message("this is a test message"))) t = Timer(5.0, send_message) t.start() self.addCleanup(t.join) start = time.time() response = queue.read(wait_time_seconds=10) end = time.time() t.join() self.assertEqual(response.id, messages[0].id) self.assertEqual(response.get_body(), messages[0].get_body()) # The timer thread should send the message in 5 seconds, so # we're giving +- .5 seconds for the total time the queue # was blocked on the read call. self.assertTrue(4.5 <= (end - start) <= 5.5)
class Countdown(object): """ Countdown timer starts immediatly on init from `start_value` and counts down to zero, then counts up with negated time. Only displays minutes and seconds. No pause or reset available. Each "tick" of the clock is passed to the callback function as a string. """ def __init__(self, start_value, callback): self._finished = False self.start_value = start_value self.callback = callback self.start_time = now() self._update() def _update(self): self._set_time(now() - self.start_time) if not self._finished: self._timer = Timer(1, self._update) self._timer.start() def _set_time(self, value): neg = '' if self.start_value > value: value = self.start_value - value elif self.start_value < value: value = value - self.start_value neg = '-' else: value = 0 mm, ss = divmod(value, 60) self.callback("{}{:02d}:{:02d}".format(neg, mm, ss)) def stop(self): self._timer.cancel() self._finished = True
def run(data, d=None): global timer print(data) if data == "ring": if int(d) == 1: if timer: timer.cancel() timer = Timer(60, genugdavon) timer.start() check = Thread(target=check_locks) check.start() putfile('/sys/class/gpio/gpio11/value', '0') playsound("/root/ring-bb.wav") else: playsound("/root/ring-fis.wav") if data == "open": # if not locked_oben: playsound("/root/open.wav") if data == "summ": if timer: timer.cancel() # if not locked_oben: putfile('/sys/class/gpio/gpio11/value', '1') playsound("/root/summ.wav")
def onStatusUpdate(self, cmd): cmd.updateTime = time.time() # append the command's status to the rendernode's history if isFinalStatus(cmd.status): # only if we don't already have a command for this task if hasattr(cmd.renderNode, 'tasksHistory') and cmd.task.id not in cmd.renderNode.tasksHistory: cmd.renderNode.tasksHistory.append(cmd.task.id) cmd.renderNode.history.append(cmd.status) if cmd.status is CMD_DONE: # TOFIX: handle CANCEL status and update end time when cancelling a # job so that it can be properly cleaned cmd.endTime = cmd.updateTime cmd.computeAvgTimeByFrame() cmd.attempt += 1 # autoretry elif cmd.status is CMD_ERROR: cmd.attempt += 1 LOGGER.debug("Mark command %d for auto retry in %ds (%d/%d)" % (cmd.id, singletonconfig.get('CORE', 'DELAY_BEFORE_AUTORETRY'), cmd.attempt, cmd.task.maxAttempt)) if cmd.attempt < cmd.task.maxAttempt: t = Timer(singletonconfig.get('CORE', 'DELAY_BEFORE_AUTORETRY'), self.autoretry, [cmd]) t.start() elif cmd.status is CMD_ASSIGNED: cmd.startTime = cmd.updateTime elif cmd.status < CMD_ASSIGNED: cmd.startTime = None
def run(dir, main="tmp"): try: ##print("DEBUG:", [ ## config.JAVA_CMD, ## "-Djava.security.manager", ## "-Djava.security.policy=whiley.policy", ## "-cp",config.WYJC_JAR + ":" + dir, ## main ## ]) # run the JVM proc = subprocess.Popen([ config.JAVA_CMD, "-Djava.security.manager", "-Djava.security.policy=whiley.policy", "-cp", config.WYJC_JAR + ":" + dir, main ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False) # Configure Timeout kill_proc = lambda p: p.kill() timer = Timer(20, kill_proc, [proc]) timer.start() # Run process (out, err) = proc.communicate() timer.cancel() # Check what happened if proc.returncode >= 0: return out else: return "Timeout: Your program ran for too long!" except Exception as ex: # error, so return that return "Run Error: " + str(ex)
def setInterval(func, sec): def func_wrapper(): setInterval(func, sec) func() t = Timer(sec, func_wrapper) t.start() return t
class InfiniteTimer(): """A Timer class that does not stop, unless you want it to.""" def __init__(self, seconds, target): self._should_continue = False self.is_running = False self.seconds = seconds self.target = target self.thread = None def _handle_target(self): self.is_running = True self.target() self.is_running = False self._start_timer() def _start_timer(self): if self._should_continue: # Code could have been running when cancel was called. self.thread = Timer(self.seconds, self._handle_target) self.thread.start() def start(self): if not self._should_continue and not self.is_running: self._should_continue = True self._start_timer() else: print("Timer already started or running, please wait if you're restarting.") def cancel(self): if self.thread is not None: # Just in case thread is running and cancel fails. self._should_continue = False self.thread.cancel() else: print("Timer never started or failed to initialize.")
class RepeatTimer: """Timer that repeats itself `interval` seconds /after/ each function call returns.""" def __init__(self, interval, fn, args=None, kwargs=None): self.cancelled = False args = args or () kwargs = kwargs or {} def repeater(): if self.cancelled: return fn(*args, **kwargs) self.timer = Timer(interval, repeater) self.timer.start() self.timer = Timer(interval, repeater) def start(self): logging.debug('starting timer') self.timer.start() def cancel(self): logging.debug('cancelling timer') # `timer.cancel()` won't cancel properly if within repeater call, so use internal check self.cancelled = True
def setupClient(self, topic, msg_type, wait_duration=10): """ Tries to set up an action client for calling it later. @type topic: string @param topic: The topic of the action to call. @type msg_type: msg type @param msg_type: The type of messages of this action client. @type wait_duration: int @param wait_duration: Defines how long to wait for the given client if it is not available right now. """ if topic not in ProxyActionClient._clients: client = actionlib.SimpleActionClient(topic, msg_type) t = Timer(1, self._print_wait_warning, [topic]) t.start() available = client.wait_for_server(rospy.Duration.from_sec(wait_duration)) warning_sent = False try: t.cancel() except Exception as ve: # already printed the warning warning_sent = True if not available: Logger.logerr("Action client %s timed out!" % topic) else: ProxyActionClient._clients[topic] = client if warning_sent: Logger.loginfo("Finally found action client %s..." % (topic))
def scheduleFlight(res, flight, blocking=False, scheduler=None): flight_time = time_module.mktime(flight.legs[0].depart.dt_utc.utctimetuple()) - time_module.timezone seconds_before = config["CHECKIN_WINDOW"] + 24*60*60 # how many seconds before the flight time do we check in flight.sched_time = flight_time - seconds_before flight.sched_time_formatted = DateTimeToString(flight.legs[0].depart.dt_utc.replace(tzinfo=utc) - timedelta(seconds=seconds_before)) flight.seconds = flight.sched_time - time_module.time() # Retrieve timezone and apply it because datetimes are stored as naive (no timezone information) tz = airport_timezone_map[flight.legs[0].depart.airport] flight.sched_time_local_formatted = DateTimeToString(flight.legs[0].depart.dt_utc.replace(tzinfo=utc).astimezone(tz) - timedelta(seconds=seconds_before)) db.Session.commit() dlog("Flight time: %s" % flight.legs[0].depart.dt_formatted) if config["CELERY"]: from tasks import check_in_flight result = check_in_flight.apply_async([res.id, flight.id], countdown=flight.seconds) flight.task_uuid = result.id db.Session.commit() elif not blocking: result = "Scheduling check in for flight at", flight.legs[0].depart.dt_formatted, "(local), ", flight.legs[0].depart.dt_utc_formatted, "(UTC) in", int(flight.seconds/60/60), "hrs", int(flight.seconds/60%60), "mins from now..." t = Timer(flight.seconds, TryCheckinFlight, (res.id, flight.id, None, 1)) t.daemon = True t.start() # DEBUG # if flight == res.flights[0]: # Timer(5, TryCheckinFlight, (res, flight, None, 1)).start() else: scheduler.enterabs(flight.sched_time, 1, TryCheckinFlight, (res.id, flight.id, scheduler, 1)) result = "Scheduling check in for flight at", flight.legs[0].depart.dt_formatted, "(local), ", flight.legs[0].depart.dt_utc_formatted, "(UTC)" print result dlog('Checkin scheduled at (UTC): %s' % flight.sched_time_formatted) dlog('Checkin scheduled at (local): %s' % flight.sched_time_local_formatted) dlog('Flights scheduled. Waiting...') return result
class Handler(): """Queues filesystem events for broadcast""" def __init__(self): """Create an event handler""" ## Timer used to prevent notification floods self._timer = None ## The files that have changed since the last broadcast self._files = set() ## Lock to protect files and timer from threading issues self._lock = Lock() def broadcast(self): """Broadcasts a list of all files touched since last broadcast""" with self._lock: cola.notifier().broadcast(signals.update_status, self._files) self._files = set() self._timer = None def handle(self, path): """Queues up filesystem events for broadcast""" with self._lock: self._files.add(path) if self._timer is None: self._timer = Timer(0.333, self.broadcast) self._timer.start()
class InternalServer(HTTPServer): def kill_me(self): self.shutdown() logging.info("The server's life has come to an end, pid: {}".format(os.getpid())) def reset_selfdestruct_timer(self): if self.self_destruct_timer: self.self_destruct_timer.cancel() self.self_destruct_timer = Timer(LIFESPAN, self.kill_me) self.self_destruct_timer.start() def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate=bind_and_activate) self.self_destruct_timer = None self.clients = 1 self.reset_selfdestruct_timer() def suicide(self): self.clients -= 1 if self.clients == 0: if self.self_destruct_timer is not None: self.self_destruct_timer.cancel() quick_and_painless_timer = Timer(0.1, self.kill_me) quick_and_painless_timer.start()
def _connected(self, link_uri): """ This callback is called form the Crazyflie API when a Crazyflie has been connected and the TOCs have been downloaded.""" print('Connected to %s' % link_uri) # The definition of the logconfig can be made before connecting self._lg_stab = LogConfig(name='Stabilizer', period_in_ms=10) self._lg_stab.add_variable('stabilizer.roll', 'float') self._lg_stab.add_variable('stabilizer.pitch', 'float') self._lg_stab.add_variable('stabilizer.yaw', 'float') # Adding the configuration cannot be done until a Crazyflie is # connected, since we need to check that the variables we # would like to log are in the TOC. try: self._cf.log.add_config(self._lg_stab) # This callback will receive the data self._lg_stab.data_received_cb.add_callback(self._stab_log_data) # This callback will be called on errors self._lg_stab.error_cb.add_callback(self._stab_log_error) # Start the logging self._lg_stab.start() except KeyError as e: print('Could not start log configuration,' '{} not found in TOC'.format(str(e))) except AttributeError: print('Could not add Stabilizer log config, bad configuration.') # Start a timer to disconnect in 10s t = Timer(5, self._cf.close_link) t.start()
def request_handler(self): timeout = self.opcd.get('power_save_timeout') req = PowerReq() rep = PowerRep() timer = None while True: rep.status = OK try: req_data = self.ctrl_socket.recv() except: sleep(1) continue try: req.ParseFromString(req_data) except: rep.status = E_SYNTAX else: try: timer.cancel() except: pass if req.cmd == STAND_POWER: timer = Timer(timeout, self.power_off) timer.start() else: if self.critical: # flying command is not allowed if battery was critical: rep.status = E_POWER else: self.gpio_mosfet.set_gpio(self.power_pin, True) self.ctrl_socket.send(rep.SerializeToString())
class IOServer(threading.Thread): # 6 7 8 9 # l d u r uPin = 15 # u <-> d lPin = 27 # l <-r rPin = 14 dPin = 18 def __init__(self): threading.Thread.__init__(self) # tprint(self.currentState) GPIO.setmode(GPIO.BCM) setupGPIO(self.uPin, self.upButtonHandler) setupGPIO(self.lPin, self.leftButtonHandler) setupGPIO(self.rPin, self.rightButtonHandler) setupGPIO(self.dPin, self.downButtonHandler) self.quit = False self.lcd = get_lcdcontroller() self.timer = Timer(0.5, self.timeoutHandler) def run(self): tprint("start ioserver") self.timer.start() lcd = self.lcd while True: c = getch() if (c == 'w'): lcd.up() elif (c == 'a'): lcd.left() elif (c == 's'): lcd.down() elif (c == 'd'): lcd.right() elif (c == ' '): lcd.center() elif (c == 'u'): lcd.updateDisplay() elif (c == 'q'): self.quit = True break else: tprint("Unknown Command") def upButtonHandler(self, pin = None): self.lcd.up() def downButtonHandler(self, pin = None): self.lcd.down() def leftButtonHandler(self, pin = None): self.lcd.left() def rightButtonHandler(self, pin = None): self.lcd.right() def timeoutHandler(self): self.lcd.updateDisplay() if not self.quit: self.timer = Timer(0.5, self.timeoutHandler) self.timer.start()
class OpeningState: def __init__(self, door_model): self.door_model = door_model def door_position_changed(self, new_position): if new_position == DOOR_POSITION_OPEN: self.door_model.set_new_state("Open") elif new_position == DOOR_POSITION_CLOSED: self.door_model.set_new_state("Closed") def enter(self): logger.debug("State 'opening' entered") self.timer = Timer(self.door_model.transit_time, self._transit_timeout) self.timer.start() def exit(self): logger.debug("State 'opening' exited") if self.timer: self.timer.cancel() self.timer = False def _transit_timeout(self): logger.info("Transit timeout reached") self.timer = False self.door_model.set_new_state("Intermediate")
def run(self): while self.is_running: try: super(SerializeThread, self).run() except Exception, e: log.error("Exception in thread %s: %s", self.name, e) delay = TIMER_DELAYS[self.name] if delay > MAX_DELAY: num_attempts = int(math.log((delay/10), 2)); log.error("Giving up re-spawning serialization " "worker after %d seconds (%d attempts).", delay, num_attempts ) notify_serialization_failure(None, subject="Notice - Bungeni Serialization Workers", body="""Unable to restart serialization worker. Please check the Bungeni logs.""" ) else: log.info("Attempting to respawn serialization " "consumer in %d seconds", delay ) next_delay = delay * 2 timer = Timer(delay, init_thread, [], {"delay": next_delay }) timer.daemon = True timer.start() self.is_running = False del TIMER_DELAYS[self.name]
def clean_avisos(): global avisos_n logger.info("avisos_n = 0") avisos_n = 0 global hilo hilo = Timer(config['clean_time'], clean_avisos) hilo.start()
def run(self, app, type): # Invoke wren and run the test. test_arg = self.path if type == "api test": # Just pass the suite name to API tests. test_arg = basename(splitext(test_arg)[0]) proc = Popen([app, test_arg], stdin=PIPE, stdout=PIPE, stderr=PIPE) # If a test takes longer than two seconds, kill it. # # This is mainly useful for running the tests while stress testing the GC, # which can make a few pathological tests much slower. timed_out = [False] def kill_process(p): timed_out[0] = True p.kill() timer = Timer(2, kill_process, [proc]) try: timer.start() out, err = proc.communicate(self.input_bytes) if timed_out[0]: self.fail("Timed out.") else: self.validate(type == "example", proc.returncode, out, err) finally: timer.cancel()
def set_dimmer(self, value_id, value): """ The command 0x26 (COMMAND_CLASS_SWITCH_MULTILEVEL) of this node. Set switch to value (using value value_id). :param value_id: The value to retrieve state :type value_id: int :param value: The level : a value between 0-99 or 255. 255 set the level to the last value. \ 0 turn the dimmer off :type value: int """ logging.debug("set_dimmer Level:%s" % (value,)) if value_id in self.get_dimmers(): if 99 < value < 255: value = 99 elif value < 0 : value = 0 self.values[value_id].data = value #Dimmers doesn't return the good level. #Add a Timer to refresh the value if value == 0: timer1 = Timer(1, self.values[value_id].refresh) timer1.start() timer2 = Timer(2, self.values[value_id].refresh) timer2.start() return True return False
def dataout(): global timer1 timer1 = Timer(1.0 / data_freq, dataout) timer1.start() lock.acquire() print "{:+.3f}".format(data.popleft()) lock.release()
class TimerMixIn(object): def __init__(self, *argl, **argd): super(TimerMixIn,self).__init__(*argl,**argd) self.timer = None self.timerSuccess = True def startTimer(self, secs): if self.timer is not None: self.cancelTimer() self.timer = Timer(secs, self.__handleTimerDone) self.timerSuccess = False self.timer.start() def cancelTimer(self): if self.timer is not None and self.timer: self.timer.cancel() self.timer = None self.timerSuccess = False def timerRunning(self): return self.timer is not None def timerWasCancelled(self): return not self.timerSuccess def __handleTimerDone(self): self.scheduler.wakeThread(self) self.timer = None self.timerSuccess = True
def create_session(self): log.info("Enabling for MAC %s" % (self.mac)) sys_cmd = "iptables -I internet 1 -t mangle -m mac --mac-source %s -j RETURN" % self.mac log.info(sys_cmd) log.info(os.popen(sys_cmd).read()) callback = Timer(self.length, self.destroy_session) callback.start()
def main(): def uonline(): ol = online() games_info = ol.get_games_info() tl = [] for info in games_info: t = Thread(target=ol.update_games_online, args=[info]) tl.append(t) for i in range(len(tl)): tl[i].start() Tu = Timer(300.0, uonline) Tu.start() Tu = Timer(300.0, uonline) Tu.start() def conline(): ol = online() games_info = ol.get_games_info() for info in games_info: ol.check_online(info) tl = [] t = Thread(target=ol.check_online, args=[info]) tl.append(t) for i in range(len(tl)): tl[i].start() Tc = Timer(3.0, conline) Tc.start() Tc = Timer(3.0, conline) Tc.start()
def view_image(self, im, auto_close=True): # use a manager to open so will auto close on quit self.open_view(im) if auto_close: minutes = 2 t = Timer(60 * minutes, im.close_ui) t.start()
def defer(self, t, run_on_cancel, func, *args): event = Timer(t, self.run_action, kwargs={'func': func, 'args': args}) event.name = '%s deferring %s' % (event.name, func.__name__) event.start() with worker_lock: self.events[event.ident] = Event(event, run_on_cancel) return event.ident
def initial(): #UDP socket works print 'Waiting For Peers.... :)' sock_udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock_udp.bind((SERVER_IP,SERVER_PORT)) peer_data = '' t = Timer (TIMER_IDLE_PEER, timer_function) t.start() exp_seq=1 old_buffer='' #Request from Peer while True: recv_buffer,addr = sock_udp.recvfrom(4096) if recv_buffer == 'list': sock_udp.sendto(str(peer_list), addr) else: sock_udp.sendto(recv_buffer,addr) if old_buffer != recv_buffer: old_buffer = recv_buffer recv_buffer_split = recv_buffer.split() recv_buffer_len = len(recv_buffer_split) num_pkts = recv_buffer_split.pop(recv_buffer_len-1) seq_pkt = recv_buffer_split.pop(recv_buffer_len-2) exp_seq=exp_seq+1 peer_data = peer_data + ''.join((str(x)+' ') for x in recv_buffer_split) if num_pkts == seq_pkt: thread = Thread(target = process_packet, args = (peer_data,sock_udp,addr)) thread.start() #TEST THREADING!!!!!!!!!!!!!!!!!!!! #thread.join() peer_data = '' exp_seq=1
def testExecute(self): # Start a timer for copying the file pyTimer = Timer(5, self.copyFile) pyTimer.start() self.run() pyTimer.cancel()
class Button: def __init__(self, hass, name, config, component_state): self.hass = hass self.name = name self.timer = None self.hold_call = None self.click_call = None data = config.get(name) if not data: # Button not configured self.log_debug("Not setup") return self.log_debug("Data %s" % data) self.hold_call = self.create_behaviour_call(hass, data.get('hold'), component_state) if self.hold_call: self.hold_interval = data.get('hold').get('interval') if not self.hold_interval: self.hold_interval = 0.1 # Default to 100ms self.click_call = self.create_behaviour_call(hass, data.get('click'), component_state) if self.hold_call: self.log_debug('Setup hold') if self.click_call: self.log_debug('Setup click') # self.light_id = 'light.living_room_floor' # self._reverse_brightness = False # self.timer = None def create_behaviour_call(self, hass, data, component_state): if not data: return # Not configured entity_id = data.get('entity_id') # if not entity_id: # self.log_debug('No entity_id set on button') # return None behavior = data.get('behavior') if not behavior: self.log_debug('No behavior set on button') return None behavior_service_call = service_call_mapper[behavior] if not behavior_service_call: self.log_debug('Invalid behavior %s specified' % behavior) return None def call(): resolved_entity_id = entity_id if resolved_entity_id == 'all_entities': entities = component_state['entities'] if len(entities) == 0: print('no entities specified, could not perform bevavior') return # Perform behavior on all entities for e in component_state['entities']: behavior_service_call(hass, e, data.get('behavior_data'), component_state) return elif resolved_entity_id == 'selected_entity': selected_entity_index = component_state['selected_entity_index'] entities = component_state['entities'] if selected_entity_index == -1: print('no entity selected, could not perform behavior') return elif selected_entity_index == len(entities): # Perform behavior on all entities for e in component_state['entities']: behavior_service_call(hass, e, data.get('behavior_data'), component_state) return resolved_entity_id = entities[selected_entity_index] behavior_data = data.get('behavior_data') if not behavior_data: behavior_data = {} behavior_service_call(hass, resolved_entity_id, behavior_data, component_state) return call def handle_key_event(self, event_code): self.log_debug('event_code: %d' % event_code) key_event = event_code % 1000 if key_event == 1: self.handle_button_hold() elif key_event == 2: self.handle_button_short_click() elif key_event == 3: self.handle_button_long_click() else: _LOGGER.info('Button %s: Unhandled key event %d from event code %d' % name, key_event, event_code) def handle_button_hold(self): self.log_debug('hold 1') # self.brightness = 0 if not self.hold_call: return # Nothing to do self.log_debug('hold 2') if self.hold_stop(): return self.log_debug('hold 3') # self.timer = Timer(DIM_UPDATE_INTERVAL_MS, self.dim_update, args=[], kwargs={}) # self.timer = Timer(self.hold_interval, self.hold_call, args=[], kwargs={}) # self.timer.start() self.hold_update() def handle_button_short_click(self): self.log_debug('short click') if self.hold_stop(): return # TODO: don't do click directly but wait for a small delay to wait for double click if self.click_call != None: self.click_call() def handle_button_long_click(self): self.log_debug('long click') if self.hold_stop(): return def hold_update(self): self.hold_call() self.timer = Timer(self.hold_interval, self.hold_update, args=[], kwargs={}) self.timer.start() # def set_brightness(self, value): # self.log_debug('Brightness %d' % value) # self.brightness = value # service_data = { # 'entity_id': self.light_id, # 'brightness_pct': self.brightness # } # self.log_debug('before call %s' % service_data) # self.hass.services.call('light', 'turn_on', service_data, False) # self.log_debug('after call') # def get_brightness(self): # return self.brightness def hold_stop(self): if self.timer == None: return False self.log_debug('hold stop') if self.timer.isAlive: self.timer.cancel() self.timer = None return True def dim_update(self): self.timer = Timer(hold.interval, self.hold_update, args=[], kwargs={}) self.timer.start() def log_debug(self, msg): print('Button %s: %s' % (self.name, msg))
class HitBTCConnector(WebSocketConnectorThread): """Class to pre-process HitBTC data, before putting it on the internal queue. Data on the queue is available as a 3-item-tuple by default: symbol, data_type, data Whereas data can either be the extracted ``params`` data from a streamed notification object, or a tuple of ``(request, response)``, where ``request`` is the original payload sent by the client and ``response`` the related response object from the server. You can disable extraction and handling by passing 'raw=True' on instantiation. Note that this will also turn off recording of sent requests, as well all logging activity. """ def __init__(self, url=None, raw=None, stdout_only=False, silent=False, **conn_ops): """Initialize a HitBTCConnector instance.""" url = url or 'wss://api.hitbtc.com/api/2/ws' super(HitBTCConnector, self).__init__(url, **conn_ops) self.books = defaultdict(dict) self.requests = {} self.raw = raw self.logged_in = False self.silent = silent self.stdout_only = stdout_only def put(self, item, block=False, timeout=None): """Place the given item on the internal q.""" if not self.stdout_only: self.q.put(item, block, timeout) def echo(self, msg): """Print message to stdout if ``silent`` isn't True.""" if not self.silent: print(msg) def _start_timers(self): """Reset and start timers for API connection.""" self._stop_timers() # Automatically reconnect if we didnt receive data self.connection_timer = Timer(self.connection_timeout, self._connection_timed_out) self.connection_timer.start() def _stop_timers(self): """Stop connection timer.""" if self.connection_timer: self.connection_timer.cancel() def _on_message(self, ws, message): """Handle and pass received data to the appropriate handlers.""" if not self.raw: decoded_message = json.loads(message) if 'jsonrpc' in decoded_message: if 'result' in decoded_message or 'error' in decoded_message: self._handle_response(decoded_message) else: try: method = decoded_message['method'] symbol = decoded_message['params'].pop('symbol') params = decoded_message.pop('params') except Exception as e: self.log.exception(e) self.log.error(decoded_message) return self._handle_stream(method, symbol, params) else: self.put(message) def _handle_response(self, response): """ Handle JSONRPC response objects. Acts as a pre-sorting function and determines whether or not the response is an error message, or a response to a succesful request. """ try: i_d = response['id'] except KeyError as e: self.log.exception(e) self.log.error("An expected Response ID was not found in %s", response) raise try: request = self.requests.pop(i_d) except KeyError as e: log.exception(e) log.error("Could not find Request relating to Response object %s", response) raise if 'result' in response: self._handle_request_response(request, response) elif 'error' in response: self._handle_error(request, response) def _handle_request_response(self, request, response): """ Handle responses to succesful requests. Logs messages and prints them to screen. Finally, we'll put the response and its corresponding request on the internal queue for retrieval by the client. """ method = request['method'] try: msg = response_types[method] except KeyError as e: log.exception(e) log.error("Response's method %s is unknown to the client! %s", method) return print(request) if method.startswith('subscribe'): if 'symbol' in request['params']: formatted_msg = msg.format(symbol=request['params']['symbol']) else: formatted_msg = msg self.log.info(formatted_msg) self.echo(formatted_msg) else: text = "Sucessfully processed %s request:\n" % method if method.startswith('get'): # loop over item in response['result'] for: # getSymbols, getTrades, getTradingBalance, getOrders for item in response['result']: text += msg.format(item) self.log.info(text) self.echo(text) else: # Format messages for these using response['result'] directly # (place, cancel, replace, getSymbol, getCurrency) text += msg.format(**response['result']) self.log.info(text) self.echo(text) self.log.debug("Request: %r, Response: %r", request, response) self.put(('Response', 'Success', (request, response))) def _handle_error(self, request, response): """ Handle Error messages. Logs the corresponding requests and the error code and error messages, and prints them to the screen. Finally, we'll put the response and its corresponding request on the internal queue for retrieval by the client. """ err_message = "{code} - {message} - {description}!".format( **response['error']) err_message += " Related Request: %r" % request self.log.error(err_message) self.echo(err_message) self.put(('Response', 'Failure', (request, response))) def _handle_stream(self, method, symbol, params): """Handle streamed data.""" self.put((method, symbol, params)) def send(self, method, custom_id=None, **params): """ Send the given Payload to the API via the websocket connection. :param method: JSONRPC method to call :param custom_id: custom ID to identify response messages relating to this request :param kwargs: payload parameters as key=value pairs """ if not self._is_connected: self.echo("Cannot Send payload - Connection not established!") return payload = { 'method': method, 'params': params, 'id': custom_id or int(10000 * time.time()) } if not self.raw: self.requests[payload['id']] = payload self.log.debug("Sending: %s", payload) self.conn.send(json.dumps(payload)) def authenticate(self, key, secret, basic=False, custom_nonce=None): """Login to the HitBTC Websocket API using the given public and secret API keys.""" if basic: algo = 'BASIC' skey = secret payload = {'sKey': skey} else: algo = 'HS256' nonce = custom_nonce or str(round(time.time() * 1000)) raw_sig = (key + nonce).encode(encoding='UTF-8') signature = hmac.new(secret, raw_sig, hashlib.sha256).hexdigest() payload = {'nonce': nonce, 'signature': signature} payload['algo'] = algo payload['pKey'] = key self.send('login', **payload)
test = TestCase.create_sharing2_test_case() tester.run_test(test, iterations) elif test_file: tester = Tester(output_file) test_params = TestParams.load_test(test_file) test = TestCase.create_test_case(test_params, tester.rand_gen) tester.run_test(test, iterations) else: # I'm the child process :D test = pickle.loads("".join(sys.stdin.readlines())) watchdog = Timer(interval=test.timeout, function=Tester.timer_fn, args=(test.crt_iteration, test.num_iterations)) watchdog.start() supervisor = Supervisor(test) supervisor.register_banned_thread(watchdog) supervisor.register_banned_thread() return_code = supervisor.run_testcase() watchdog.cancel() sys.stdout.flush() sys.stderr.flush() sys.exit(return_code) if __name__ == "__main__": main()
def setup_process_timeout(proc, timeout, failure_callback=None): """ Setup a timeout on a process. After `timeout` seconds, the process is killed by `signal_at_timeout` signal. :param proc: The subprocess.Process object representing the process to attach the watcher to. :param timeout: The timeout the process is allowed to run for, in seconds. This timeout is counted down some moments after calling setup_process_timeout(), and due to OS scheduling, might not be exact. :param failure_callback: An optional function which is called when the process is killed by the timeout. This is called with no arguments passed. :return: A function is returned which should be called when the client code (usually via subprocess.Process.wait() or subprocess.Process.communicate())) figures out that the called process has terminated. (See below for what this called function returns.) """ # The watch dict encapsulated the captured variables for the timeout watch. watch = { 'pid': proc.pid, 'timer': None, # Will be created later. 'counting': False, 'killed': False } def __kill(): """ Helper function to execute the killing of a hung process. """ LOG.debug("Process {0} has ran for too long, killing it!".format( watch['pid'])) watch['counting'] = False watch['killed'] = True kill_process_tree(watch['pid'], True) if failure_callback: failure_callback() timer = Timer(timeout, __kill) watch['timer'] = timer LOG.debug("Setup timeout of {1} for PID {0}".format(proc.pid, timeout)) timer.start() watch['counting'] = True def __cleanup_timeout(): """ Stops the timer associated with the timeout watch if the process finished within the grace period. Due to race conditions and the possibility of the OS, or another process also using signals to kill the watched process in particular, it is possible that checking subprocess.Process.returncode on the watched process is not enough to see if the timeout watched killed it, or something else. (Note: returncode is -N where N is the signal's value, but only on Unix systems!) It is safe to call this function multiple times to check for the result of the watching. :return: Whether or not the process was killed by the watcher. If this is False, the process could have finished gracefully, or could have been destroyed by other means. """ if watch['counting'] and not watch['killed'] and watch['timer']: watch['timer'].cancel() watch['timer'] = None watch['counting'] = False return watch['killed'] return __cleanup_timeout
class IForest(Model): def __init__(self, md_instance, store, model, data_source, slack_channel, timeout): super(IForest, self).__init__( IFOREST_MODEL_NAME, md_instance, store, IForestConfig(store.get_model_template(IFOREST_MODEL_NAME), model.get("config", None))) self.api = PrometheusAPI(data_source) self.slack_channel = slack_channel self.df = dict() self.ilf = dict() self.timer = Timer(timeparse(timeout), self.timeout_action) self.metrics = model.get("metrics") def train(self, metric, query_expr, config): logger.info( "{log_prefix}[metric:{metric}] starting to get sample data".format( log_prefix=self.log_prefix, metric=metric)) self.df[metric] = pd.DataFrame(columns=config["features"]) self.ilf[metric] = IsolationForest(n_estimators=100, verbose=2) for index in range(0, self.cfg.model["train_count"], 1): if self._exit: logger.info("{log_prefix}[metric:{metric}] stop".format( log_prefix=self.log_prefix, metric=metric)) return False now = datetime.datetime.now() query = PrometheusQuery( query_expr, time.mktime( (now - datetime.timedelta(minutes=15)).timetuple()), time.mktime(now.timetuple()), "15s") self.train_task(metric, query, config) if index % 10 == 0: df_one = {} for key in config["features"]: if key in Features: df_one[key] = float(random.randint(0, 10000)) self.df[metric] = self.df[metric].append(df_one, ignore_index=True) logger.info( "{log_prefix}[metric:{metric}] append data to train df:{df_one}" .format(log_prefix=self.log_prefix, metric=metric, df_one=df_one)) self.event.wait(timeparse(self.cfg.model["train_interval"])) logger.info( "{log_prefix}[metric:{metric}] starting to train sample data". format(log_prefix=self.log_prefix, metric=metric)) self.ilf[metric].fit(self.df[metric][config["features"]]) return True def train_task(self, metric, query, config): data_set = self.api.query(query) if len(data_set) > 0: values = [] for data in data_set.values(): values.append(float(data)) df_one = {} for key in config["features"]: if key in Features: df_one[key] = Features[key](values) logger.info( "{log_prefix}[metric:{metric}] append data to train df:{df_one}" .format(log_prefix=self.log_prefix, metric=metric, df_one=df_one)) self.df[metric] = self.df[metric].append(df_one, ignore_index=True) def predict(self, metric, query_expr, config): logger.info("{log_prefix}[metric:{metric}] starting to predict".format( log_prefix=self.log_prefix, metric=metric)) while not self._exit: now = datetime.datetime.now() query = PrometheusQuery( query_expr, time.mktime( (now - datetime.timedelta(minutes=10)).timetuple()), time.mktime(now.timetuple()), "15s") report = { "metric": metric, "time": now, } is_match, predict_data = self.predict_task(metric, query, config) if is_match == 1: logger.info("{log_prefix}[metric:{metric}] predict OK".format( log_prefix=self.log_prefix, metric=metric)) report["is_match"] = True else: report["is_match"] = False self.on_error(metric, predict_data) report["predict_data"] = predict_data with self.lock: self.report.metrics_report[metric].append(report) self.save_model() self.event.wait(timeparse(self.cfg.model["predict_interval"])) logger.info("{log_prefix}[metric:{metric}] stop".format( log_prefix=self.log_prefix, metric=metric)) def predict_task(self, metric, query, config): data_set = self.api.query(query) values = [] for data in data_set.values(): values.append(float(data)) df_one = [] for key in config["features"]: if key in Features: df_one.append(Features[key](values)) predict_data = np.array([df_one]) logger.info( "{log_prefix}[metric:{metric}] predict data:{predict_data}".format( log_prefix=self.log_prefix, metric=metric, predict_data=predict_data)) return self.ilf[metric].predict(predict_data), df_one def run(self): logger.info("{log_prefix} start to run".format( log_prefix=self.log_prefix, model=self.name)) self.timer.start() for metric in self.metrics: if metric not in Metrics: logger.error( "{log_prefix}[metric:{metric}] is not supported".format( log_prefix=self.log_prefix, metric=metric)) continue val = Metrics[metric] if metric not in self.cfg.metrics: logger.error( "{log_prefix}[metric:{metric}] can't found the config of this metric" .format(log_prefix=self.log_prefix, metric=metric)) continue self.report.metrics_report[metric] = [] t = Thread(target=self.run_action, args=(metric, val, self.cfg.metrics[metric])) t.start() self.threads[metric] = t self.status = MODEL_RUNNING self.save_model() def run_action(self, metric, val, config): logger.info("{log_prefix}[metric:{metric}] start to run".format( log_prefix=self.log_prefix, metric=metric)) if self.train(metric, val, config): self.predict(metric, val, config) def close(self): # TODO: close this job logger.info("{log_prefix} closing".format(log_prefix=self.log_prefix)) super(IForest, self).close() self.timer.cancel() self.status = MODEL_STOP self.save_model() def timeout_action(self): logger.info( "{log_prefix} finish the model".format(log_prefix=self.log_prefix)) super(IForest, self).close() self.status = MODEL_FINISH self.save_model() def on_error(self, metric, predict_data): logger.info( "{log_prefix}[metric:{metric}] Predict Error, predict data:{predict_data}" .format(log_prefix=self.log_prefix, metric=metric, predict_data=predict_data)) send_to_slack( "{log_prefix}[model:{model}], predict metric {metric} error, " "predict data:{predict_data}".format(log_prefix=self.log_prefix, model=self.name, metric=metric, predict_data=predict_data), self.slack_channel) def get_report(self): with self.lock: return self.report.to_dict()
def updateAll(): updateDB() updateSite() # refreshing database and site every set time def refresh(): global t t = Timer(60, refresh) getItemsOnSale() priceChange() updateAll() t.start() #main app setter def setMinMax(): loginDetails() getItemsOnSale() printItems() index = userInput() priceChange() printChosen(index) #price could change after the input so its a seperate function updateAll() #main t = Timer(60, refresh) while True: setMinMax() t = Timer(60, refresh) t.start() print('Press enter to set another weapon.') input()
def restart_lnd(): t = Timer(1.0, restart_lnd_actual) t.start()
def timr(): sms = AliYunSMS() response = sms.send_single("13xxxxxxxxx", ALISMS_SIGN, ALISMS_TPL_REGISTER, {"name": "女朋友", "weather": message}) t = Timer(86400,timr) t.start()
def do_timeout_add(self, tid, timeout, fn, *args, **kwargs): #emulate glib's timeout_add using Timers args = (tid, timeout, fn, args, kwargs) t = Timer(timeout/1000.0, self.queue_timeout_function, args) self.timers[tid] = t t.start()
class Connection(Thread): def __init__(self, event_handler, url, reconnect_handler=None, log_level=None, daemon=True, reconnect_interval=10, socket_kwargs=None, **thread_kwargs): self.event_handler = event_handler self.url = url self.reconnect_handler = reconnect_handler or (lambda: None) self.socket = None self.socket_id = "" self.event_callbacks = defaultdict(list) self.disconnect_called = False self.needs_reconnect = False self.default_reconnect_interval = reconnect_interval self.reconnect_interval = reconnect_interval self.socket_kwargs = socket_kwargs or dict() self.pong_timer = None self.pong_received = False self.pong_timeout = 30 self.bind("pusher:connection_established", self._connect_handler) self.bind("pusher:connection_failed", self._failed_handler) self.bind("pusher:pong", self._pong_handler) self.bind("pusher:ping", self._ping_handler) self.bind("pusher:error", self._pusher_error_handler) self.state = "initialized" self.logger = logging.getLogger(self.__module__) # create a new logger if log_level: self.logger.setLevel(log_level) if log_level == logging.DEBUG: websocket.enableTrace(True) # From Martyn's comment at: # https://pusher.tenderapp.com/discussions/problems/36-no-messages-received-after-1-idle-minute-heartbeat # "We send a ping every 5 minutes in an attempt to keep connections # alive..." # This is why we set the connection timeout to 5 minutes, since we can # expect a pusher heartbeat message every 5 minutes. Adding 5 sec to # account for small timing delays which may cause messages to not be # received in exact 5 minute intervals. self.connection_timeout = 305 self.connection_timer = None self.ping_interval = 120 self.ping_timer = None Thread.__init__(self, **thread_kwargs) self.daemon = daemon def bind(self, event_name, callback, *args, **kwargs): """Bind an event to a callback :param event_name: The name of the event to bind to. :type event_name: str :param callback: The callback to notify of this event. """ self.event_callbacks[event_name].append((callback, args, kwargs)) def disconnect(self, timeout=None): self.needs_reconnect = False self.disconnect_called = True if self.socket: self.socket.close() self.join(timeout) def reconnect(self, reconnect_interval=None): if reconnect_interval is None: reconnect_interval = self.default_reconnect_interval self.logger.info("Connection: Reconnect in %s" % reconnect_interval) self.reconnect_interval = reconnect_interval self.needs_reconnect = True if self.socket: self.socket.close() def run(self): self._connect() def _connect(self): self.state = "connecting" self.socket = websocket.WebSocketApp(self.url, on_open=self._on_open, on_message=self._on_message, on_error=self._on_error, on_close=self._on_close) self.socket.run_forever(**self.socket_kwargs) while self.needs_reconnect and not self.disconnect_called: self.logger.info("Attempting to connect again in %s seconds." % self.reconnect_interval) self.state = "unavailable" time.sleep(self.reconnect_interval) # We need to set this flag since closing the socket will set it to # false self.socket.keep_running = True self.socket.run_forever(**self.socket_kwargs) def _on_open(self, ws): self.logger.info("Connection: Connection opened") # Since we've opened a connection, we don't need to try to reconnect self.needs_reconnect = False # Send a ping right away to inform that the connection is alive. If you # don't do this, it takes the ping interval to subcribe to channel and # events self.send_ping() self._start_timers() def _on_error(self, ws, error): self.logger.info("Connection: Error - %s" % error) self.state = "failed" self.needs_reconnect = True def _on_message(self, ws, message): self.logger.info("Connection: Message - %s" % message) # Stop our timeout timer, since we got some data self._stop_timers() params = self._parse(message) if 'event' in params.keys(): if 'channel' not in params.keys(): # We've got a connection event. Lets handle it. if params['event'] in self.event_callbacks.keys(): for func, args, kwargs in self.event_callbacks[ params['event']]: try: func(params['data'], *args, **kwargs) except Exception: self.logger.exception("Callback raised unhandled") else: self.logger.info("Connection: Unhandled event") else: # We've got a channel event. Lets pass it up to the pusher # so it can be handled by the appropriate channel. self.event_handler(params['event'], params['data'], params['channel']) # We've handled our data, so restart our connection timeout handler self._start_timers() def _on_close(self, ws, *args): self.logger.info("Connection: Connection closed") self.state = "disconnected" self._stop_timers() @staticmethod def _parse(message): return json.loads(message) def _stop_timers(self): if self.ping_timer: self.ping_timer.cancel() if self.connection_timer: self.connection_timer.cancel() if self.pong_timer: self.pong_timer.cancel() def _start_timers(self): self._stop_timers() self.ping_timer = Timer(self.ping_interval, self.send_ping) self.ping_timer.start() self.connection_timer = Timer(self.connection_timeout, self._connection_timed_out) self.connection_timer.start() def send_event(self, event_name, data, channel_name=None): event = {'event': event_name, 'data': data} if channel_name: event['channel'] = channel_name self.logger.info("Connection: Sending event - %s" % event) try: self.socket.send(json.dumps(event)) except Exception as e: self.logger.error("Failed send event: %s" % e) def send_ping(self): self.logger.info("Connection: ping to pusher") try: self.socket.send(json.dumps({'event': 'pusher:ping', 'data': ''})) except Exception as e: self.logger.error("Failed send ping: %s" % e) self.pong_timer = Timer(self.pong_timeout, self._check_pong) self.pong_timer.start() def send_pong(self): self.logger.info("Connection: pong to pusher") try: self.socket.send(json.dumps({'event': 'pusher:pong', 'data': ''})) except Exception as e: self.logger.error("Failed send pong: %s" % e) def _check_pong(self): self.pong_timer.cancel() if self.pong_received: self.pong_received = False else: self.logger.info( "Did not receive pong in time. Will attempt to reconnect.") self.state = "failed" self.reconnect() def _connect_handler(self, data): parsed = json.loads(data) self.socket_id = parsed['socket_id'] self.state = "connected" if self.needs_reconnect: self.reconnect_handler() else: self.logger.debug('Connection: Establisheds first connection') def _failed_handler(self, data): self.state = "failed" def _ping_handler(self, data): self.send_pong() # Restart our timers since we received something on the connection self._start_timers() def _pong_handler(self, data): self.logger.info("Connection: pong from pusher") self.pong_received = True def _pusher_error_handler(self, data): if 'code' in data: error_code = None try: error_code = int(data['code']) except: pass if error_code is not None: self.logger.error("Connection: Received error %s" % error_code) if (error_code >= 4000) and (error_code <= 4099): # The connection SHOULD NOT be re-established unchanged self.logger.info( "Connection: Error is unrecoverable. Disconnecting") self.disconnect() elif (error_code >= 4100) and (error_code <= 4199): # The connection SHOULD be re-established after backing off self.reconnect() elif (error_code >= 4200) and (error_code <= 4299): # The connection SHOULD be re-established immediately self.reconnect(0) else: pass else: self.logger.error("Connection: Unknown error code") else: self.logger.error("Connection: No error code supplied") def _connection_timed_out(self): self.logger.info("Did not receive any data in time. Reconnecting.") self.state = "failed" self.reconnect()
def closewindow(): global flag flag=0 timer=Timer(60*5,closewindow) nowtime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(type(nowtime)) print("Begin at {}.".format(nowtime)) cap = cv2.VideoCapture(0) #plt.figure() timer.start() while(flag==1): stime = time.time() ret, img = cap.read() # 读取视频流的一帧 gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # 转成灰度图像 dets = detector(gray, 0) #cv2.rectangle(img, (170, 60),(180,70) ,(255, 0, 0), thickness=-1) cv2.putText(img, "Begin at {}".format(nowtime), (170, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (250, 255, 250), 2) cv2.putText(img, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (250, 255, 250), 2)
def threading_timer(inc): print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) # time.sleep(3) 执行时间会和延时时间叠加 # Timer三个参数分别为:间隔时间、被调用触发的函数,给该触发函数的参数(tuple形式) t = Timer(inc, threading_timer, (inc, )) t.start()
def scan(self, hosts='127.0.0.1', ports=None, arguments='-sV', sudo=False, timeout=60 * 5): if sys.version_info[0] == 2: assert type(hosts) in ( str, unicode ), 'Wrong type for [hosts], should be a string [was {0}]'.format( type(hosts)) # noqa assert type(ports) in ( str, unicode, type(None) ), 'Wrong type for [ports], should be a string [was {0}]'.format( type(ports)) # noqa assert type(arguments) in ( str, unicode ), 'Wrong type for [arguments], should be a string [was {0}]'.format( type(arguments)) # noqa else: assert type( hosts ) is str, 'Wrong type for [hosts], should be a string [was {0}]'.format( type(hosts)) # noqa assert type(ports) in ( str, type(None) ), 'Wrong type for [ports], should be a string [was {0}]'.format( type(ports)) # noqa assert type( arguments ) is str, 'Wrong type for [arguments], should be a string [was {0}]'.format( type(arguments)) # noqa for redirecting_output in ['-oX', '-oA']: assert redirecting_output not in arguments, 'Xml output can\'t be redirected from command line.\nYou can access it after a scan using:\nnmap.nm.get_nmap_last_output()' # noqa h_args = shlex.split(hosts) f_args = shlex.split(arguments) # Launch scan args = [self._nmap_path, '-oX', '-' ] + h_args + ['-p', ports] * (ports is not None) + f_args if sudo: args = ['sudo'] + args kill = lambda process: process.kill() p = subprocess.Popen(args, bufsize=100000, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) my_timer = Timer(timeout, kill, [p]) # try: # my_timer.start() # stdout, stderr = p.communicate() # finally: # my_timer.cancel() # wait until finished # get output try: my_timer.start() (self._nmap_last_output, nmap_err) = p.communicate() finally: my_timer.cancel() self._nmap_last_output = bytes.decode(self._nmap_last_output) nmap_err = bytes.decode(nmap_err) nmap_err_keep_trace = [] nmap_warn_keep_trace = [] if len(nmap_err) > 0: regex_warning = re.compile('^Warning: .*', re.IGNORECASE) for line in nmap_err.split(os.linesep): if len(line) > 0: rgw = regex_warning.search(line) if rgw is not None: # sys.stderr.write(line+os.linesep) nmap_warn_keep_trace.append(line + os.linesep) else: # raise PortScannerError(nmap_err) nmap_err_keep_trace.append(nmap_err) return self.analyse_nmap_xml_scan( nmap_xml_output=self._nmap_last_output, nmap_err=nmap_err, nmap_err_keep_trace=nmap_err_keep_trace, nmap_warn_keep_trace=nmap_warn_keep_trace)
class CommandRequest: def __init__(self, command, stdin=None, timeout=None, ignore_error=False): # Write data to tmp file to avoid PIPEs and execution blocking stdin_file = tempfile.TemporaryFile() stdin_file.write(stdin) stdin_file.seek(0) self.stdout_file = tempfile.TemporaryFile() self.stderr_file = tempfile.TemporaryFile() self.ignore_error = ignore_error #print " ".join(command) # we suppress stderror on client becase sometimes thread sanitizer # can print some debug information there env = {} env["TSAN_OPTIONS"] = "verbosity=0" self.process = sp.Popen(command, stdin=stdin_file, stdout=self.stdout_file, stderr=self.stderr_file, env=env) self.timer = None self.process_finished_before_timeout = True if timeout is not None: def kill_process(): if self.process.poll() is None: self.process_finished_before_timeout = False self.process.kill() self.timer = Timer(timeout, kill_process) self.timer.start() def get_answer(self): self.process.wait() self.stdout_file.seek(0) self.stderr_file.seek(0) stdout = self.stdout_file.read() stderr = self.stderr_file.read() if self.timer is not None and not self.process_finished_before_timeout and not self.ignore_error: raise QueryTimeoutExceedException('Client timed out!') if (self.process.returncode != 0 or stderr) and not self.ignore_error: raise QueryRuntimeException( 'Client failed! Return code: {}, stderr: {}'.format( self.process.returncode, stderr)) return stdout def get_error(self): self.process.wait() self.stdout_file.seek(0) self.stderr_file.seek(0) stdout = self.stdout_file.read() stderr = self.stderr_file.read() if self.timer is not None and not self.process_finished_before_timeout and not self.ignore_error: raise QueryTimeoutExceedException('Client timed out!') if (self.process.returncode == 0): raise QueryRuntimeException( 'Client expected to be failed but succeeded! stdout: {}'. format(stdout)) return stderr def get_answer_and_error(self): self.process.wait() self.stdout_file.seek(0) self.stderr_file.seek(0) stdout = self.stdout_file.read() stderr = self.stderr_file.read() if self.timer is not None and not self.process_finished_before_timeout and not self.ignore_error: raise QueryTimeoutExceedException('Client timed out!') return (stdout, stderr)
class allianceauthauthenticatorApp(Ice.Application): def run(self, args): self.shutdownOnInterrupt() if not self.initializeIceConnection(): return 1 if cfg.ice.watchdog > 0: self.failedWatch = True self.checkConnection() # Serve till we are stopped self.communicator().waitForShutdown() self.watchdog.cancel() if self.interrupted(): warning('Caught interrupt, shutting down') threadDB.disconnect() return 0 def initializeIceConnection(self): """ Establishes the two-way Ice connection and adds the authenticator to the configured servers """ ice = self.communicator() if cfg.ice.secret: debug('Using shared ice secret') ice.getImplicitContext().put("secret", cfg.ice.secret) elif not cfg.glacier.enabled: warning('Consider using an ice secret to improve security') if cfg.glacier.enabled: # info('Connecting to Glacier2 server (%s:%d)', glacier_host, glacier_port) error('Glacier support not implemented yet') # TODO: Implement this info('Connecting to Ice server (%s:%d)', cfg.ice.host, cfg.ice.port) base = ice.stringToProxy('Meta:tcp -h %s -p %d' % (cfg.ice.host, cfg.ice.port)) self.meta = Murmur.MetaPrx.uncheckedCast(base) adapter = ice.createObjectAdapterWithEndpoints( 'Callback.Client', 'tcp -h %s' % cfg.ice.host) adapter.activate() metacbprx = adapter.addWithUUID(metaCallback(self)) self.metacb = Murmur.MetaCallbackPrx.uncheckedCast(metacbprx) authprx = adapter.addWithUUID(allianceauthauthenticator()) self.auth = Murmur.ServerUpdatingAuthenticatorPrx.uncheckedCast( authprx) return self.attachCallbacks() def attachCallbacks(self, quiet=False): """ Attaches all callbacks for meta and authenticators """ # Ice.ConnectionRefusedException # debug('Attaching callbacks') try: if not quiet: info('Attaching meta callback') self.meta.addCallback(self.metacb) for server in self.meta.getBootedServers(): if not cfg.murmur.servers or server.id( ) in cfg.murmur.servers: if not quiet: info('Setting authenticator for virtual server %d', server.id()) server.setAuthenticator(self.auth) except (Murmur.InvalidSecretException, Ice.UnknownUserException, Ice.ConnectionRefusedException) as e: if isinstance(e, Ice.ConnectionRefusedException): error('Server refused connection') elif isinstance(e, Murmur.InvalidSecretException) or \ isinstance(e, Ice.UnknownUserException) and ( e.unknown == 'Murmur::InvalidSecretException'): error('Invalid ice secret') else: # We do not actually want to handle this one, re-raise it raise e self.connected = False return False self.connected = True return True def checkConnection(self): """ Tries reapplies all callbacks to make sure the authenticator survives server restarts and disconnects. """ # debug('Watchdog run') try: if not self.attachCallbacks(quiet=not self.failedWatch): self.failedWatch = True else: self.failedWatch = False except Ice.Exception as e: error( 'Failed connection check, will retry in next watchdog run (%ds)', cfg.ice.watchdog) debug(str(e)) self.failedWatch = True # Renew the timer self.watchdog = Timer(cfg.ice.watchdog, self.checkConnection) self.watchdog.start()
def hammett_tests_pass(config, callback): # noinspection PyUnresolvedReferences from hammett import main_cli modules_before = set(sys.modules.keys()) # set up timeout import _thread from threading import ( Timer, current_thread, main_thread, ) timed_out = False def timeout(): _thread.interrupt_main() nonlocal timed_out timed_out = True assert current_thread() is main_thread() timer = Timer(config.baseline_time_elapsed * 10, timeout) timer.daemon = True timer.start() # Run tests try: class StdOutRedirect(TextIOBase): def write(self, s): callback(s) return len(s) redirect = StdOutRedirect() sys.stdout = redirect sys.stderr = redirect returncode = main_cli( shlex.split(config.test_command[len(hammett_prefix):])) sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ timer.cancel() except KeyboardInterrupt: timer.cancel() if timed_out: raise TimeoutError('In process tests timed out') raise modules_to_force_unload = { x.partition(os.sep)[0].replace('.py', '') for x in config.paths_to_mutate } for module_name in list( sorted(set(sys.modules.keys()) - set(modules_before), reverse=True)): if any(module_name.startswith(x) for x in modules_to_force_unload) or module_name.startswith( 'tests') or module_name.startswith('django'): del sys.modules[module_name] return returncode == 0
def __run_clipclient(self): """ With consideration for the current global client state: Periodically check for new items on the local clipboard and paste them onto the global clip. Similarly, check for new items on the global clipboard and copy them onto the local clip """ if self._current_action_mode == ActionMode.NONE: t = Timer(10,self.run_clipclient) t.start() elif self._current_action_mode == ActionMode.EXIT: pass #we should now quit once no more timers are running else: t = None if self.session_key is None: #try to get a session... r = requests.request('post',"%s/session" % self._conf['server']['url'], data = {'email' : self.creds['email'] , 'password' : self.creds['password'] }) rj = r.json if r.ok and rj['status'] == 200: self.session_key = rj['payload'] self.save_session(self._conf,self.session_key) logging.info('Obtained New Session Key') else: logging.error('Failed to obtain New Session Key [%s]' % rj['payload']) if self.session_key is None: #try to login then... r = requests.request('post',"%s/register" % self._conf['server']['url'], data = {'email' : self.creds['email'] , 'password' : self.creds['password'] }) rj = r.json if r.ok and rj['status'] == 200: logging.info("Registration Successfull: %s" % rj['payload']) #since's we've just registered... give user 1 min to complete registration before using clip client again t = Timer(60,self.run_clipclient) else: logging.error("Registration Not Completed : %s" % rj['payload']) #possibly, registration not complete yet or activation not yet done. delay for like 2 min now before trying again t = Timer(120,self.run_clipclient) else: #we have session key, so... #check if there's anything here on the clipboard that needs to be copied over... current_local_clip = pyperclip.paste() if (self._current_action_mode == ActionMode.RECEIVE_SEND ) or (self._current_action_mode == ActionMode.SEND_ONLY): if self.local_clipboard != current_local_clip: #there's new stuff on the local clip to copy over... #First, secure the paste... secure_local_clip = self.encrypt_clip(current_local_clip,self.encryption_conf) #then send r = requests.request('post',"%s/paste" % self._conf['server']['url'], data = {'session' : self.session_key , 'paste' : secure_local_clip }) rj = r.json if r.ok and rj['status'] == 200: logging.debug('Copying to Global Clipboard Successful : %s' % rj['payload']) self.local_clipboard = current_local_clip t = Timer(10,self.run_clipclient) else: try: print secure_local_clip logging.error('Copying to Global Clipboard Failed : %s' % rj['payload']) except Exception as e: logging.error('Fatal Error while trying to Copy to Clipboard : %s' % e) t = Timer(30,self.run_clipclient) else: logging.debug("Local Clip : old") t = Timer(10,self.run_clipclient) if (self._current_action_mode == ActionMode.RECEIVE_SEND ) or (self._current_action_mode == ActionMode.RECEIVE_ONLY): #get the remote / global clip contents if any... r = requests.request('post',"%s/copy" % self._conf['server']['url'], data = {'session' : self.session_key }) rj = r.json if r.ok and rj['status'] == 200: secure_global_clipboard = rj['payload'] #we expect data to be sent encoded base64 secure_global_clipboard = secure_global_clipboard.decode('base64') #First, decrypt the stuff... global_clipboard = self.decrypt_clip(secure_global_clipboard,self.encryption_conf) if global_clipboard != self.local_clipboard: pyperclip.copy(global_clipboard) self.local_clipboard = global_clipboard logging.info("Found new Data on Global Clip from IP : %s" % (rj['source_ip'])) t = Timer(10,self.run_clipclient) else: logging.debug("Global Clip : old") t = Timer(10,self.run_clipclient) else: try: logging.error("Failed to Read Global Clip : %s " % rj['payload']) t = Timer(30,self.run_clipclient) except Exception as e: logging.error("Exception : %s" % e) if t: t.start() #schedule next run of the clip client...
def küsi_küsimus(sõnastik, skoor=0, kordi=0): if kordi == 10: küsimuste_aken.destroy() tagasisideaken = tk.Toplevel(aken) tagasisideaken.title( 'Kõrgema Matemaatika kordamistesti tagasiside') tagasisideaken.geometry('400x100') tagasisideraam = Frame(tagasisideaken) tagasisideraam.pack(pady=20) if skoor > 8: tekst = 'Kogusid ' + str( skoor ) + ' punkti kümnest võimalikust. Suurepärane tulemus!' elif skoor > 5: tekst = 'Kogusid ' + str( skoor) + ' punkti kümnest võimalikust. Täitsa hästi!' elif skoor > 3: tekst = 'Kogusid ' + str( skoor ) + ' punkti kümnest võimalikust. Polegi kõige hullem!' else: tekst = 'Kogusid ' + str( skoor) + ' punkti kümnest võimalikust.' tagasisidesilt = Label(tagasisideraam, text=tekst, wraplength=380) tagasisidesilt.pack() def sulge_aken(): tagasisideaken.destroy() t = Timer(8.0, sulge_aken) t.start() else: küsimuste_raam = Frame(küsimuste_aken) küsimuste_raam.pack(pady=20) # siin saab järjendi (vastusevariandid), kus on 4 vastusevarianti, millest kolm on valed ja üks õige. võtmed = [] for k in sõnastik: võtmed.append(k) koopia = võtmed[:] õige = randint(0, len(võtmed) - 1) koopia.remove(võtmed[õige]) valed = sample(range(0, len(koopia)), 3) vastusevariandid = [] vastusevariandid.append(võtmed[õige]) for i in range(3): vastusevariandid.append(koopia[valed[i]]) shuffle(vastusevariandid) # sõna on vaja silt2 loomisel if 'astakuks ' in võtmed: küsimus = 'Mis sobib lünka?' else: küsimus = 'Millise teoreemiga on tegu?' # testiaknasse Label, kuhu tuleb küsimus silt2 = Label(küsimuste_raam, text=str(kordi + 1) + '. ' + küsimus + '\n\n' + sõnastik[võtmed[õige]], wraplength=480) silt2.pack() # testiaknasse Frame, kuhu tulevad vastusevariandid vastuste_raam = Frame(küsimuste_raam) vastuste_raam.pack() valikusilt = Label(vastuste_raam) valikusilt.pack(pady=10) v = StringVar() ttk.Radiobutton( valikusilt, text=vastusevariandid[0], variable=v, value=vastusevariandid[0], command=lambda: vastuse_kontroll(vastusevariandid[0], võtmed[ õige], skoor)).pack(anchor=W) ttk.Radiobutton( valikusilt, text=vastusevariandid[1], variable=v, value=vastusevariandid[1], command=lambda: vastuse_kontroll(vastusevariandid[1], võtmed[ õige], skoor)).pack(anchor=W) ttk.Radiobutton( valikusilt, text=vastusevariandid[2], variable=v, value=vastusevariandid[2], command=lambda: vastuse_kontroll(vastusevariandid[2], võtmed[ õige], skoor)).pack(anchor=W) ttk.Radiobutton( valikusilt, text=vastusevariandid[3], variable=v, value=vastusevariandid[3], command=lambda: vastuse_kontroll(vastusevariandid[3], võtmed[ õige], skoor)).pack(anchor=W) def uus_küsimus(väärtus): küsimuste_raam.destroy() if väärtus == 'vale': küsi_küsimus(küsimustepagas, skoor, kordi + 1) else: küsi_küsimus(küsimustepagas, skoor + 1, kordi + 1) def vastuse_kontroll(valik, õige_vastus, skoor): for child in valikusilt.winfo_children(): child['state'] = 'disabled' # et ei tuleks korduvaid küsimusi del sõnastik[õige_vastus] if valik == õige_vastus: väärtus = 'õige' skoor += 1 tagasiside = Label( küsimuste_raam, text='Suurepärane! Õige vastus! Punkte kokku: ' + str(skoor)) tagasiside.configure(bg="#4eed5e") tagasiside.pack() else: väärtus = 'vale' tagasiside = Label( küsimuste_raam, text='Peaaegu!!! Aga siiski mitte... Punkte kokku: ' + str(skoor)) tagasiside.configure(bg="#ed8a72") tagasiside.pack() if kordi == 9: edasi_nupu_tekst = 'Lõpeta' else: edasi_nupu_tekst = 'Järgmine küsimus' edasi = ttk.Button( küsimuste_raam, text=edasi_nupu_tekst, style="danger.TButton", command=lambda: uus_küsimus(väärtus)).pack(pady=5) lõpeta = ttk.Button( küsimuste_raam, text='Katkesta test', style="danger.TButton", command=lambda: küsimuste_aken.destroy()).pack()
def start_send_thread(self): def temp(): buffer = self.data_buffer resent_buffer = self.resent_buffer if buffer.empty() and resent_buffer.empty(): _t = Timer(self.sending_time_interval, temp) _t.start() return if not resent_buffer.empty(): package = self.pack_data_buffer(resent_buffer, self.cwnd) total_package_length = len( self.discard_package.get_payload(parse=True)) log.info( f"resent package {self.discard_package.get_header().get_package_hashcode(parse=True)} " f"| {total_package_length - resent_buffer.qsize()} of {total_package_length} " f"| current package {package.get_header().get_package_hashcode(parse=True)}, cwnd: {self.cwnd}" ) self.resending_flag = True else: package = self.pack_data_buffer(buffer, self.cwnd) try: send_package(package, self.socket) except (ConnectionAbortedError, ConnectionResetError): log.info( "Connection has been closed by proxy, remote job finished." ) return log.debug(f"<- {package.get_desc()}") # There should be a header contains ack received after every package sending header = receive_package(self.socket) message = header.get_message(parse=True) ack = header.get_ack(parse=True) log.debug(f"-> ack: {ack} " f"| message: \"{message}\" ") # ---> sending failed if message == Header.MSG_PACKAGE_DISCARD: if self.resending_flag: log.warning(f"The resent package also be discarded!") else: log.warning( f"package {ack} has been discarded, will be resend.") self.discard_package = package self.unpack_data_buffer(package) self.cwnd = 1 self.ssthresh /= 2 # <--- sending failed # ---> sending successfully else: # resending finished if self.resending_flag and self.resent_buffer.empty(): self.resending_flag = False self.discard_package = None if self.cwnd >= self.ssthresh: self.cwnd += 1 self.ssthresh += 1 # slow start else: self.cwnd *= 2 # <--- sending successfully _t = Timer(self.sending_time_interval, temp) _t.start() # check the buffer every second, if the buffer is not empty, combine the buffer into one t = Timer(self.sending_time_interval, temp) t.start()
class Adjust(object): ''' Base class for Optune adjust driver command. This implements common functionality and is meant to be sub-classed, not run as is. Example usage: from adjust import Adjust class MyClass(Adjust): def info(self): ... def query(self): ... def handle_cancel(self, signal, frame): ... def adjust(self, data): ... if __name__ == '__main__': foo = MyClass(VERSION, DESC, HAS_CANCEL, PROGRESS_INTERVAL) foo.run() ''' ################################################## # METHODS THAT SHOULD NOT BE OVERWRITTEN # (unless you know what you are doing) ################################################## def __init__(self, version, cli_desc, supports_cancel, progress_interval=None): # Parse Args self.parser = argparse.ArgumentParser(description=cli_desc) self.parser.add_argument( '--version', help='print version and exit', default=False, action='store_true') self.parser.add_argument( '--info', help='output driver info and exit', default=False, action='store_true') qry_help = 'output current state of settings for this application' self.parser.add_argument( '--query', help=qry_help, default=False, action='store_true') # alias for query self.parser.add_argument( '--describe', dest='query', help=qry_help, default=False, action='store_true') self.parser.add_argument( 'app_id', help='Name/ID of the application to adjust', nargs='?') self.args = self.parser.parse_args() self.version = version self.app_id = self.args.app_id self.supports_cancel = supports_cancel self.progress_interval = progress_interval self.progress = 0 self.progress_message = None self.timer = None def run(self): if self.args.version: print(self.version) sys.exit(0) if self.args.info: print(json.dumps( {"version": self.version, "has_cancel": self.supports_cancel})) sys.exit(0) # Valcheck if self.args.app_id is None: self.parser.error( 'Missing required param app_id') # Handle --query if self.args.query: try: query = self.query() if "application" not in query: query = { "application" : query } # legacy compat. print(json.dumps(query)) sys.exit(0) except AdjustError as e: self._print_json_error( e.status, e.reason, str(e) ) raise except Exception as e: self._print_json_error( "failed", "unknown", cls=e.__class__.__name__, message=str(e) ) raise # Parse input try: # self.debug("Reading stdin") try: input_data = json.loads(sys.stdin.read()) except json.decoder.JSONDecodeError as e: raise Exception('Failed to read input descriptor on stdin -> ({}) {}'.format(e.__class__.__name__, str(e))) self.input_data = input_data # LEGACY mode, remove when drivers are updated to use arg except Exception as e: self._print_json_error( "failed", "unknown", cls=e.__class__.__name__, message="failed to parse input:" + str(e) ) raise # Start progress timer self.start_progress_timer() # Adjust // TODO: print output?? try: c = self.adjust.__code__.co_argcount if c == 2: query = self.adjust(input_data) else: query = self.adjust() # LEGACY mode if not query: # for old drivers that return None query = {} # if the above didn't raise an exception, all done (empty completion data, status 'ok') if "status" not in query: query["status"] = "ok" query["reason"] = "success" print(json.dumps(query)) except AdjustError as e: self._print_json_error( e.status, e.reason, str(e) ) raise except Exception as e: self._print_json_error( "failed", "unknown", cls=e.__class__.__name__, message=str(e) ) raise finally: self.stop_progress_timer() def stop_progress_timer(self): if self.timer: self.timer.cancel() def start_progress_timer(self): self.stop_progress_timer() if not self.progress_interval: return self.timer = Timer(self.progress_interval, self.print_progress) self.timer.start() def print_progress( self, message=None, msg_index=None, stage=None, stageprogress=None): data = dict( progress=self.progress, message=message if (message is not None) else self.progress_message, ) if msg_index is not None: data['msg_index'] = msg_index if stage is not None: data['stage'] = stage if stageprogress is not None: data['stageprogress'] = stageprogress print(json.dumps(data), flush=True) # Schedule the next progress update self.start_progress_timer() def debug(self, *message): print(*message, flush=True, file=sys.stderr) @staticmethod def print_json_error(error, cl, message): ''' Prints JSON formatted error (exported backward-compatible method, new drivers should raise an exception dervided from AdjustError) ''' Adjust._print_json_error("failed", "unknown", message, err=error, cls=cl) @staticmethod def _print_json_error(status, reason, message, err="failure", cls="AdjustError"): """Print JSON-formatted status message. Internal method for use in this file only. Subclasses should raise an exception dervied from AdjustError.""" print(json.dumps( { "status": status, "reason": reason, "error": err, # used for backward-compatibility only "class": cls, # used for backward-compatibility only "message": message }), flush=True) ################################################## # METHODS THAT MUST BE OVERWRITTEN ################################################## def query(self): ''' ''' raise Exception("Not implemented") def adjust(self, data = None): ''' ''' raise Exception("Not implemented") ################################################## # METHODS THAT CAN BE OVERWRITTEN ################################################## def handle_cancel(self, signal, frame): ''' Handles SIGUSR1 signal ''' self.debug("Received cancel signal", signal) def encode_value_if_needed(self, name, cfg_setting_data, adjust_data): """ Takes: * a setting name * setting data (as defined in the config) * adjust data for the component that setting belongs to (as provided in the adjust event) Returns the value for the setting. If the config for that setting specifies an encoder to be used, the returned value will be encoded by the encoder specified in the config """ # If there is no encoder, return as is if not "encoder" in cfg_setting_data: return adjust_data[name]["value"] # Else, call the encoder import encoders.base as enc value, _ = enc.encode(cfg_setting_data["encoder"], adjust_data) return value def encode_describe_if_needed(self, name, data, value): """ Takes: * a setting name * setting data (as defined in the config) * value (as returned by the underlying infrastructure) Returns a dict in the format { <setting_name> : { <setting_data> }}, suitable for returning as a description. At the very minimun, <setting_data> will return the current "value". If the config for that setting specifies an encoder to be used, the returned "value" ( in <setting_data>) will be decoded by the encoder specified in the config. """ # If there is no encoder, return description with the current value and # any other params defined for the setting if not "encoder" in data: s_data = {"value": value} for i in ["type", "min", "max", "step", "values", "unit"]: if i in data: s_data[i] = data[i] return {name: s_data} # Else, call the encoder import encoders.base as enc return enc.describe(data["encoder"], value.split()) def get_oco_settings(self, cfg_settings): """ Takes a config section with settings (key-value pair, where key is the setting name and the value is the setting params, i.e. min/max/step, etc.) and returns the list of setting names. If any of the settings require an ecoder, they will be run through the encoder and the list of the underlying settings (as OCO expects/provides them) will be returned instead of the setting name in the config. """ settings = [] for s_name, s_data in cfg_settings.items(): if "encoder" in s_data: settings.extend(s_data["encoder"]["settings"].keys()) else: settings.append(s_name) return settings # helper: run a Bash shell command and raise an Exception on failure # note: if cmd is a string, this supports shell pipes, environment variable # expansion, etc. The burden of safety is entirely on the user. def _run_command(self, cmd, pre=True): cmd_type = 'Pre-command' if pre else 'Post-command' res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, executable='/bin/bash') msg = "cmd '{}', exit code {}, stdout {}, stderr {}".format(cmd, res.returncode, res.stdout, res.stderr) assert res.returncode == 0, '{} failed: {}'.format(cmd_type, msg) self.debug('{}: {}'.format(cmd_type, msg))
def popen_streaming_output(cmd, callback, timeout=None): """Open a subprocess and stream its output without hard-blocking. :param cmd: the command to execute within the subprocess :type cmd: str :param callback: function that intakes the subprocess' stdout line by line. It is called for each line received from the subprocess' stdout stream. :type callback: Callable[[Context], bool] :param timeout: the timeout time of the subprocess :type timeout: float :raises TimeoutError: if the subprocess' execution time exceeds the timeout time :return: the return code of the executed subprocess :rtype: int """ if os.name == 'nt': # pragma: no cover process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout = process.stdout else: master, slave = os.openpty() process = subprocess.Popen(shlex.split(cmd, posix=True), stdout=slave, stderr=slave) stdout = os.fdopen(master) os.close(slave) def kill(process_): """Kill the specified process on Timer completion""" try: process_.kill() except OSError: pass # python 2-3 agnostic process timer timer = Timer(timeout, kill, [process]) timer.setDaemon(True) timer.start() while process.returncode is None: try: if os.name == 'nt': # pragma: no cover line = stdout.readline() # windows gives readline() raw stdout as a b'' # need to decode it line = line.decode("utf-8") if line: # ignore empty strings and None callback(line) else: while True: line = stdout.readline() if not line: break callback(line) except (IOError, OSError): # This seems to happen on some platforms, including TravisCI. # It seems like it's ok to just let this pass here, you just # won't get as nice feedback. pass if not timer.is_alive(): raise TimeoutError( "subprocess running command '{}' timed out after {} seconds". format(cmd, timeout)) process.poll() # we have returned from the subprocess cancel the timer if it is running timer.cancel() return process.returncode
def blink_green_led(): green_led.on() blink_timer = Timer(BLINK_LENGTH, turn_off_green) blink_timer.start()
class Screenkey(gtk.Window): STATE_FILE = os.path.join(glib.get_user_config_dir(), 'screenkey.json') def __init__(self, logger, options, show_settings=False): gtk.Window.__init__(self, gtk.WINDOW_POPUP) self.timer_hide = None self.timer_min = None self.logger = logger defaults = Options({ 'no_systray': False, 'timeout': 2.5, 'recent_thr': 0.1, 'compr_cnt': 3, 'ignore': [], 'position': 'bottom', 'persist': False, 'font_desc': 'Sans Bold', 'font_size': 'medium', 'font_color': 'white', 'bg_color': 'black', 'opacity': 0.8, 'key_mode': 'composed', 'bak_mode': 'baked', 'mods_mode': 'normal', 'mods_only': False, 'multiline': False, 'vis_shift': False, 'vis_space': True, 'geometry': None, 'screen': 0 }) self.options = self.load_state() if self.options is None: self.options = defaults else: # copy missing defaults for k, v in defaults.iteritems(): if k not in self.options: self.options[k] = v if options is not None: # override with values from constructor for k, v in options.iteritems(): if v is not None: self.options[k] = v self.set_keep_above(True) self.set_accept_focus(False) self.set_focus_on_map(False) self.label = gtk.Label() self.label.set_attributes(pango.AttrList()) self.label.set_ellipsize(pango.ELLIPSIZE_START) self.label.set_justify(gtk.JUSTIFY_CENTER) self.label.show() self.add(self.label) self.set_gravity(gtk.gdk.GRAVITY_CENTER) self.connect("configure-event", self.on_configure) scr = self.get_screen() scr.connect("size-changed", self.on_configure) scr.connect("monitors-changed", self.on_monitors_changed) self.set_active_monitor(self.options.screen) self.font = pango.FontDescription(self.options.font_desc) self.update_colors() self.update_label() self.labelmngr = None self.enabled = True self.on_change_mode() self.make_menu() self.make_about_dialog() self.make_preferences_dialog() if not self.options.no_systray: self.make_systray() self.connect("delete-event", self.quit) if show_settings: self.on_preferences_dialog() if self.options.persist: self.show() def quit(self, widget, data=None): self.labelmngr.stop() gtk.main_quit() def load_state(self): """Load stored options""" options = None try: with open(self.STATE_FILE, 'r') as f: options = Options(json.load(f)) self.logger.debug("Options loaded.") except IOError: self.logger.debug("file %s does not exists." % self.STATE_FILE) except ValueError: self.logger.debug("file %s is invalid." % self.STATE_FILE) # compatibility with previous versions (0.5) if options and options.key_mode == 'normal': options.key_mode = 'composed' return options def store_state(self, options): """Store options""" try: with open(self.STATE_FILE, 'w') as f: json.dump(options, f) self.logger.debug("Options saved.") except IOError: self.logger.debug("Cannot open %s." % self.STATE_FILE) def set_active_monitor(self, monitor): scr = self.get_screen() if monitor >= scr.get_n_monitors(): self.monitor = 0 else: self.monitor = monitor self.update_geometry() def on_monitors_changed(self, *_): self.set_active_monitor(self.monitor) def override_font_attributes(self, attr, text): window_width, window_height = self.get_size() lines = text.count('\n') + 1 attr.insert( pango.AttrSizeAbsolute((50 * window_height // lines // 100) * 1000, 0, -1)) attr.insert(pango.AttrFamily(self.font.get_family(), 0, -1)) attr.insert(pango.AttrWeight(self.font.get_weight(), 0, -1)) def update_label(self): attr = self.label.get_attributes() text = self.label.get_text() self.override_font_attributes(attr, text) self.label.set_attributes(attr) def update_colors(self): self.label.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(self.options.font_color)) self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(self.options.bg_color)) self.set_opacity(self.options.opacity) def on_configure(self, *_): window_x, window_y = self.get_position() window_width, window_height = self.get_size() mask = gtk.gdk.Pixmap(None, window_width, window_height, 1) gc = gtk.gdk.GC(mask) gc.set_foreground(gtk.gdk.Color(pixel=0)) mask.draw_rectangle(gc, True, 0, 0, window_width, window_height) self.input_shape_combine_mask(mask, 0, 0) # set some proportional inner padding self.label.set_padding(window_width // 100, 0) self.update_label() def update_geometry(self, configure=False): if self.options.position == 'fixed' and self.options.geometry is not None: self.move(*self.options.geometry[0:2]) self.resize(*self.options.geometry[2:4]) return if self.options.geometry is not None: area_geometry = self.options.geometry else: geometry = self.get_screen().get_monitor_geometry(self.monitor) area_geometry = [ geometry.x, geometry.y, geometry.width, geometry.height ] if self.options.font_size == 'large': window_height = 24 * area_geometry[3] // 100 elif self.options.font_size == 'medium': window_height = 12 * area_geometry[3] // 100 else: window_height = 8 * area_geometry[3] // 100 self.resize(area_geometry[2], window_height) if self.options.position == 'top': window_y = area_geometry[1] + window_height * 2 elif self.options.position == 'center': window_y = area_geometry[1] + area_geometry[3] // 2 else: window_y = area_geometry[1] + area_geometry[3] - window_height * 2 self.move(area_geometry[0], window_y) def on_statusicon_popup(self, widget, button, timestamp, data=None): if button == 3 and data: data.show() data.popup(None, None, gtk.status_icon_position_menu, 3, timestamp, widget) def show(self): self.update_geometry() super(Screenkey, self).show() def on_label_change(self, markup): attr, text, _ = pango.parse_markup(markup) self.override_font_attributes(attr, text) self.label.set_text(text) self.label.set_attributes(attr) if not self.get_property('visible'): self.show() if self.timer_hide: self.timer_hide.cancel() if self.options.timeout > 0: self.timer_hide = Timer(self.options.timeout, self.on_timeout_main) self.timer_hide.start() if self.timer_min: self.timer_min.cancel() self.timer_min = Timer(self.options.recent_thr * 2, self.on_timeout_min) self.timer_min.start() def on_timeout_main(self): if not self.options.persist: self.hide() self.label.set_text('') self.labelmngr.clear() def on_timeout_min(self): attr = self.label.get_attributes() attr.change(pango.AttrUnderline(pango.UNDERLINE_NONE, 0, -1)) self.label.set_attributes(attr) def restart_labelmanager(self): self.logger.debug("Restarting LabelManager.") if self.labelmngr: self.labelmngr.stop() self.labelmngr = LabelManager(self.on_label_change, logger=self.logger, key_mode=self.options.key_mode, bak_mode=self.options.bak_mode, mods_mode=self.options.mods_mode, mods_only=self.options.mods_only, multiline=self.options.multiline, vis_shift=self.options.vis_shift, vis_space=self.options.vis_space, recent_thr=self.options.recent_thr, compr_cnt=self.options.compr_cnt, ignore=self.options.ignore, pango_ctx=self.label.get_pango_context()) self.labelmngr.start() def on_change_mode(self): if not self.enabled: return self.restart_labelmanager() def on_show_keys(self, widget, data=None): self.enabled = widget.get_active() if self.enabled: self.logger.debug("Screenkey enabled.") self.restart_labelmanager() else: self.logger.debug("Screenkey disabled.") self.labelmngr.stop() def on_preferences_dialog(self, widget=None, data=None): self.prefs.show() def on_preferences_changed(self, widget=None, data=None): self.store_state(self.options) self.prefs.hide() return True def make_preferences_dialog(self): # TODO: switch to something declarative or at least clean-up the following mess self.prefs = prefs = gtk.Dialog(APP_NAME, None, gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)) prefs.connect("response", self.on_preferences_changed) prefs.connect("delete-event", self.on_preferences_changed) def on_sb_time_changed(widget, data=None): self.options.timeout = widget.get_value() self.logger.debug("Timeout value changed: %f." % self.options.timeout) def on_cbox_sizes_changed(widget, data=None): index = widget.get_active() self.options.font_size = FONT_SIZES.keys()[index] self.update_geometry() self.logger.debug("Window size changed: %s." % self.options.font_size) def on_cbox_modes_changed(widget, data=None): index = widget.get_active() self.options.key_mode = KEY_MODES.keys()[index] self.on_change_mode() self.logger.debug("Key mode changed: %s." % self.options.key_mode) def on_cbox_bak_changed(widget, data=None): index = widget.get_active() self.options.bak_mode = BAK_MODES.keys()[index] self.on_change_mode() self.logger.debug("Bak mode changed: %s." % self.options.bak_mode) def on_cbox_mods_changed(widget, data=None): index = widget.get_active() self.options.mods_mode = MODS_MODES.keys()[index] self.on_change_mode() self.logger.debug("Mods mode changed: %s." % self.options.mods_mode) def on_cbox_modsonly_changed(widget, data=None): self.options.mods_only = widget.get_active() self.on_change_mode() self.logger.debug("Modifiers only changed: %s." % self.options.mods_only) def on_cbox_visshift_changed(widget, data=None): self.options.vis_shift = widget.get_active() self.on_change_mode() self.logger.debug("Visible Shift changed: %s." % self.options.vis_shift) def on_cbox_visspace_changed(widget, data=None): self.options.vis_space = widget.get_active() self.on_change_mode() self.logger.debug("Show Whitespace changed: %s." % self.options.vis_space) def on_cbox_position_changed(widget, data=None): index = widget.get_active() new_position = POSITIONS.keys()[index] if new_position == 'fixed': new_geom = on_btn_sel_geom(widget) if not new_geom: self.cbox_positions.set_active(POSITIONS.keys().index( self.options.position)) return elif self.options.position == 'fixed': # automatically clear geometry self.options.geometry = None self.options.position = new_position self.update_geometry() self.logger.debug("Window position changed: %s." % self.options.position) def on_cbox_screen_changed(widget, data=None): self.options.screen = widget.get_active() self.set_active_monitor(self.options.screen) self.logger.debug("Screen changed: %d." % self.options.screen) def on_cbox_persist_changed(widget, data=None): self.options.persist = widget.get_active() if not self.get_property('visible'): self.show() else: self.on_label_change(self.label.get_text()) self.logger.debug("Persistent changed: %s." % self.options.persist) def on_sb_compr_changed(widget, data=None): self.options.compr_cnt = widget.get_value_as_int() self.on_change_mode() self.logger.debug("Compress repeats value changed: %d." % self.options.compr_cnt) def on_cbox_compr_changed(widget, data=None): compr_enabled = widget.get_active() self.sb_compr.set_sensitive(compr_enabled) self.options.compr_cnt = self.sb_compr.get_value_as_int( ) if compr_enabled else 0 self.on_change_mode() self.logger.debug("Compress repeats value changed: %d." % self.options.compr_cnt) def on_btn_sel_geom(widget, data=None): try: ret = subprocess.check_output(['slop', '-f', '%x %y %w %h']) except subprocess.CalledProcessError: return False except OSError: msg = gtk.MessageDialog( parent=self, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK, message_format="Error running \"slop\"") msg.format_secondary_markup( "\"slop\" is required for interactive selection. " "See <a href=\"https://github.com/naelstrof/slop\">" "https://github.com/naelstrof/slop</a>") msg.run() msg.destroy() return False self.options.geometry = map(int, ret.split(' ')) self.update_geometry() self.btn_reset_geom.set_sensitive(True) return True def on_btn_reset_geom(widget, data=None): self.options.geometry = None if self.options.position == 'fixed': self.options.position = 'bottom' self.cbox_positions.set_active(POSITIONS.keys().index( self.options.position)) self.update_geometry() widget.set_sensitive(False) def on_adj_opacity_changed(widget, data=None): self.options.opacity = widget.get_value() self.update_colors() def on_font_color_changed(widget, data=None): self.options.font_color = widget.get_color().to_string() self.update_colors() def on_bg_color_changed(widget, data=None): self.options.bg_color = widget.get_color().to_string() self.update_colors() def on_btn_font(widget, data=None): self.options.font_desc = widget.get_font_name() self.font = pango.FontDescription(self.options.font_desc) self.update_label() frm_main = gtk.Frame(_("Preferences")) frm_main.set_border_width(6) frm_time = gtk.Frame("<b>%s</b>" % _("Time")) frm_time.set_border_width(4) frm_time.get_label_widget().set_use_markup(True) frm_time.set_shadow_type(gtk.SHADOW_NONE) vbox_time = gtk.VBox(spacing=6) hbox_time = gtk.HBox() lbl_time1 = gtk.Label(_("Display for")) lbl_time2 = gtk.Label(_("seconds")) sb_time = gtk.SpinButton(digits=1) sb_time.set_increments(0.5, 1.0) sb_time.set_range(0.5, 10.0) sb_time.set_numeric(True) sb_time.set_update_policy(gtk.UPDATE_IF_VALID) sb_time.set_value(self.options.timeout) sb_time.connect("value-changed", on_sb_time_changed) hbox_time.pack_start(lbl_time1, expand=False, fill=False, padding=6) hbox_time.pack_start(sb_time, expand=False, fill=False, padding=4) hbox_time.pack_start(lbl_time2, expand=False, fill=False, padding=4) vbox_time.pack_start(hbox_time) chk_persist = gtk.CheckButton(_("Persistent window")) chk_persist.connect("toggled", on_cbox_persist_changed) chk_persist.set_active(self.options.persist) vbox_time.pack_start(chk_persist) frm_time.add(vbox_time) frm_time.show_all() frm_position = gtk.Frame("<b>%s</b>" % _("Position")) frm_position.set_border_width(4) frm_position.get_label_widget().set_use_markup(True) frm_position.set_shadow_type(gtk.SHADOW_NONE) vbox_position = gtk.VBox(spacing=6) lbl_screen = gtk.Label(_("Screen")) cbox_screen = gtk.combo_box_new_text() scr = self.get_screen() for n in range(scr.get_n_monitors()): cbox_screen.insert_text( n, '%d: %s' % (n, scr.get_monitor_plug_name(n))) cbox_screen.set_active(self.monitor) cbox_screen.connect("changed", on_cbox_screen_changed) hbox0_position = gtk.HBox() hbox0_position.pack_start(lbl_screen, expand=False, fill=False, padding=6) hbox0_position.pack_start(cbox_screen, expand=False, fill=False, padding=4) vbox_position.pack_start(hbox0_position) lbl_positions = gtk.Label(_("Position")) self.cbox_positions = cbox_positions = gtk.combo_box_new_text() cbox_positions.set_name('position') for key, value in enumerate(POSITIONS): cbox_positions.insert_text(key, value) cbox_positions.set_active(POSITIONS.keys().index( self.options.position)) cbox_positions.connect("changed", on_cbox_position_changed) self.btn_reset_geom = btn_reset_geom = gtk.Button(_("Reset")) btn_reset_geom.connect("clicked", on_btn_reset_geom) btn_reset_geom.set_sensitive(self.options.geometry is not None) hbox1_position = gtk.HBox() hbox1_position.pack_start(lbl_positions, expand=False, fill=False, padding=6) hbox1_position.pack_start(cbox_positions, expand=False, fill=False, padding=4) hbox1_position.pack_start(btn_reset_geom, expand=False, fill=False, padding=4) vbox_position.pack_start(hbox1_position) btn_sel_geom = gtk.Button(_("Select window/region")) btn_sel_geom.connect("clicked", on_btn_sel_geom) vbox_position.pack_start(btn_sel_geom) frm_aspect = gtk.Frame("<b>%s</b>" % _("Aspect")) frm_aspect.set_border_width(4) frm_aspect.get_label_widget().set_use_markup(True) frm_aspect.set_shadow_type(gtk.SHADOW_NONE) vbox_aspect = gtk.VBox(spacing=6) frm_position.add(vbox_position) hbox0_font = gtk.HBox() lbl_font = gtk.Label(_("Font")) btn_font = gtk.FontButton(self.options.font_desc) btn_font.set_use_size(False) btn_font.set_show_size(False) btn_font.connect("font-set", on_btn_font) hbox0_font.pack_start(lbl_font, expand=False, fill=False, padding=6) hbox0_font.pack_start(btn_font, expand=False, fill=False, padding=4) hbox2_aspect = gtk.HBox() lbl_sizes = gtk.Label(_("Size")) cbox_sizes = gtk.combo_box_new_text() cbox_sizes.set_name('size') for key, value in enumerate(FONT_SIZES): cbox_sizes.insert_text(key, value) cbox_sizes.set_active(FONT_SIZES.keys().index(self.options.font_size)) cbox_sizes.connect("changed", on_cbox_sizes_changed) hbox2_aspect.pack_start(lbl_sizes, expand=False, fill=False, padding=6) hbox2_aspect.pack_start(cbox_sizes, expand=False, fill=False, padding=4) hbox3_font_color = gtk.HBox() lbl_font_color = gtk.Label(_("Font color")) btn_font_color = gtk.ColorButton( color=gtk.gdk.color_parse(self.options.font_color)) btn_font_color.connect("color-set", on_font_color_changed) btn_bg_color = gtk.ColorButton( color=gtk.gdk.color_parse(self.options.bg_color)) btn_bg_color.connect("color-set", on_bg_color_changed) hbox3_font_color.pack_start(lbl_font_color, expand=False, fill=False, padding=6) hbox3_font_color.pack_start(btn_font_color, expand=False, fill=False, padding=4) hbox3_font_color.pack_start(btn_bg_color, expand=False, fill=False, padding=4) hbox4_aspect = gtk.HBox() lbl_opacity = gtk.Label(_("Opacity")) adj_opacity = gtk.Adjustment(self.options.opacity, 0.1, 1.0, 0.1, 0, 0) adj_opacity.connect("value-changed", on_adj_opacity_changed) adj_scale = gtk.HScale(adj_opacity) hbox4_aspect.pack_start(lbl_opacity, expand=False, fill=False, padding=6) hbox4_aspect.pack_start(adj_scale, expand=True, fill=True, padding=4) vbox_aspect.pack_start(hbox0_font) vbox_aspect.pack_start(hbox2_aspect) vbox_aspect.pack_start(hbox3_font_color) vbox_aspect.pack_start(hbox4_aspect) frm_aspect.add(vbox_aspect) frm_kbd = gtk.Frame("<b>%s</b>" % _("Keys")) frm_kbd.set_border_width(4) frm_kbd.get_label_widget().set_use_markup(True) frm_kbd.set_shadow_type(gtk.SHADOW_NONE) vbox_kbd = gtk.VBox(spacing=6) hbox_kbd = gtk.HBox() lbl_kbd = gtk.Label(_("Keyboard mode")) cbox_modes = gtk.combo_box_new_text() cbox_modes.set_name('mode') for key, value in enumerate(KEY_MODES): cbox_modes.insert_text(key, value) cbox_modes.set_active(KEY_MODES.keys().index(self.options.key_mode)) cbox_modes.connect("changed", on_cbox_modes_changed) hbox_kbd.pack_start(lbl_kbd, expand=False, fill=False, padding=6) hbox_kbd.pack_start(cbox_modes, expand=False, fill=False, padding=4) vbox_kbd.pack_start(hbox_kbd) hbox_kbd = gtk.HBox() lbl_kbd = gtk.Label(_("Backspace mode")) cbox_modes = gtk.combo_box_new_text() for key, value in enumerate(BAK_MODES): cbox_modes.insert_text(key, value) cbox_modes.set_active(BAK_MODES.keys().index(self.options.bak_mode)) cbox_modes.connect("changed", on_cbox_bak_changed) hbox_kbd.pack_start(lbl_kbd, expand=False, fill=False, padding=6) hbox_kbd.pack_start(cbox_modes, expand=False, fill=False, padding=4) vbox_kbd.pack_start(hbox_kbd) hbox_kbd = gtk.HBox() lbl_kbd = gtk.Label(_("Modifiers mode")) cbox_modes = gtk.combo_box_new_text() for key, value in enumerate(MODS_MODES): cbox_modes.insert_text(key, value) cbox_modes.set_active(MODS_MODES.keys().index(self.options.mods_mode)) cbox_modes.connect("changed", on_cbox_mods_changed) hbox_kbd.pack_start(lbl_kbd, expand=False, fill=False, padding=6) hbox_kbd.pack_start(cbox_modes, expand=False, fill=False, padding=4) vbox_kbd.pack_start(hbox_kbd) chk_kbd = gtk.CheckButton(_("Show Modifier sequences only")) chk_kbd.connect("toggled", on_cbox_modsonly_changed) chk_kbd.set_active(self.options.mods_only) vbox_kbd.pack_start(chk_kbd) chk_kbd = gtk.CheckButton(_("Always show Shift")) chk_kbd.connect("toggled", on_cbox_visshift_changed) chk_kbd.set_active(self.options.vis_shift) vbox_kbd.pack_start(chk_kbd) chk_vspace = gtk.CheckButton(_("Show Whitespace characters")) chk_vspace.connect("toggled", on_cbox_visspace_changed) chk_vspace.set_active(self.options.vis_space) vbox_kbd.pack_start(chk_vspace) hbox_compr = gtk.HBox() chk_compr = gtk.CheckButton(_("Compress repeats after")) chk_compr.set_active(self.options.compr_cnt > 0) chk_compr.connect("toggled", on_cbox_compr_changed) self.sb_compr = sb_compr = gtk.SpinButton(digits=0) sb_compr.set_increments(1, 1) sb_compr.set_range(1, 100) sb_compr.set_numeric(True) sb_compr.set_update_policy(gtk.UPDATE_IF_VALID) sb_compr.set_value(self.options.compr_cnt or 3) sb_compr.connect("value-changed", on_sb_compr_changed) hbox_compr.pack_start(chk_compr, expand=False, fill=False) hbox_compr.pack_start(sb_compr, expand=False, fill=False, padding=4) vbox_kbd.pack_start(hbox_compr) frm_kbd.add(vbox_kbd) hbox_main = gtk.HBox() vbox_main = gtk.VBox() vbox_main.pack_start(frm_time, False, False, 6) vbox_main.pack_start(frm_position, False, False, 6) vbox_main.pack_start(frm_aspect, False, False, 6) hbox_main.pack_start(vbox_main) vbox_main = gtk.VBox() vbox_main.pack_start(frm_kbd, False, False, 6) hbox_main.pack_start(vbox_main) frm_main.add(hbox_main) prefs.vbox.pack_start(frm_main) prefs.set_destroy_with_parent(True) prefs.set_resizable(False) prefs.set_has_separator(False) prefs.set_default_response(gtk.RESPONSE_CLOSE) prefs.vbox.show_all() def make_menu(self): self.menu = menu = gtk.Menu() show_item = gtk.CheckMenuItem(_("Show keys")) show_item.set_active(True) show_item.connect("toggled", self.on_show_keys) show_item.show() menu.append(show_item) preferences_item = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES) preferences_item.connect("activate", self.on_preferences_dialog) preferences_item.show() menu.append(preferences_item) about_item = gtk.ImageMenuItem(gtk.STOCK_ABOUT) about_item.connect("activate", self.on_about_dialog) about_item.show() menu.append(about_item) separator_item = gtk.SeparatorMenuItem() separator_item.show() menu.append(separator_item) image = gtk.ImageMenuItem(gtk.STOCK_QUIT) image.connect("activate", self.quit) image.show() menu.append(image) menu.show() def make_systray(self): try: import appindicator self.systray = appindicator.Indicator( APP_NAME, 'indicator-messages', appindicator.CATEGORY_APPLICATION_STATUS) self.systray.set_status(appindicator.STATUS_ACTIVE) self.systray.set_attention_icon("indicator-messages-new") self.systray.set_icon("preferences-desktop-keyboard-shortcuts") self.systray.set_menu(self.menu) self.logger.debug("Using AppIndicator.") except ImportError: self.systray = gtk.StatusIcon() self.systray.set_from_icon_name( "preferences-desktop-keyboard-shortcuts") self.systray.connect("popup-menu", self.on_statusicon_popup, self.menu) self.logger.debug("Using StatusIcon.") def make_about_dialog(self): self.about = about = gtk.AboutDialog() about.set_program_name(APP_NAME) about.set_version(VERSION) about.set_copyright(""" Copyright(c) 2010-2012: Pablo Seminario <*****@*****.**> Copyright(c) 2015-2016: wave++ "Yuri D'Elia" <*****@*****.**> """) about.set_comments(APP_DESC) about.set_documenters(["José María Quiroga <*****@*****.**>"]) about.set_website(APP_URL) about.set_icon_name('preferences-desktop-keyboard-shortcuts') about.set_logo_icon_name('preferences-desktop-keyboard-shortcuts') about.connect("response", about.hide_on_delete) about.connect("delete-event", about.hide_on_delete) def on_about_dialog(self, widget, data=None): self.about.show()
class testcase(unittest.TestCase): logger = tools.set_logger(__name__) sdk = tools.get_api() model = tools.get_model() filters = tools.get_filter() timeout = tools.read_job_config()['case_timeout'] filter_count = int(tools.read_job_config()['filter_count']) unfilter_count = int(tools.read_job_config()['unfilter_count']) def setUp(self): self.timeout_flag = None self.flag1 = 0 self.flag2 = 0 self.logger.info('测试chip0不加过虑主动扫描,chip1 filter mac 主动扫描') self.timer = Timer(self.timeout, self.set_timeout) self.timer.start() def tearDown(self): self.timer.cancel() # 测试方法 # noinspection PyUnreachableCode,PyUnreachableCode def test_scan_filter_mac(self): if self.model.startswith('S') or self.model.startswith('s'): a = threading.Thread(target=self.chip0_scan, args=(1, )) a.setDaemon(True) a.start() b = threading.Thread(target=self.chip0_scan, args=(1, self.filters['filter_mac'])) b.setDaemon(True) b.start() while True: if self.flag1 == 2: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,case test timeout.') self.logger.error("Case failed,case test timeout.") break else: a = threading.Thread(target=self.chip0_scan, args=(1, )) b = threading.Thread(target=self.chip1_scan, args=(1, self.filters['filter_mac'])) a.setDaemon(True) b.setDaemon(True) a.start() b.start() while True: if self.flag1 == 1 and self.flag2 == 1: self.assertTrue(True) self.logger.info('pass\n') break elif self.timeout_flag: self.logger.info('fail\n') self.fail('Case failed,start scan timeout.') self.logger.error("Case failed,start scan timeout.") break def chip0_scan(self, active=0, filter_mac=None): # step1:chip 1 start passive scan,then start chip0 scan. with closing( self.sdk.scan(chip=0, active=active, filter_mac=filter_mac)) as self.sse1: count = 0 for message in self.sse1: if message.startswith('data'): msg = json.loads(message[5:]) if filter_mac: # 进入开启过滤的扫描结果判断流程 if count < self.filter_count: print('chip0', count, message) mac = msg['bdaddrs'][0]['bdaddr'] if mac != self.filters['filter_mac']: self.fail('filter mac failed.') self.logger.debug('filter mac failed.') break else: count += 1 else: self.flag1 += 1 self.logger.debug( 'Step 1:chip0 start scan with filter mac success.') break else: # 进入不开启过滤的扫描结果判断流程 if count < self.unfilter_count: print('chip0', count, message) count += 1 else: self.flag1 += 1 self.logger.debug( 'Step 1:chip0 start scan with no filter mac success.' ) break # noinspection PyUnreachableCode def chip1_scan(self, active=0, filter_mac=None): # step2:start chip0 scan. with closing( self.sdk.scan(chip=1, active=active, filter_mac=filter_mac)) as self.sse2: count = 0 for message in self.sse2: if message.startswith('data'): msg = json.loads(message[5:]) if filter_mac: if count < self.filter_count: print('chip1', count, message) mac = msg['bdaddrs'][0]['bdaddr'] if mac != filter_mac: self.fail('filter mac failed.') self.logger.debug('filter mac failed.') break else: count += 1 else: self.flag2 += 1 self.logger.debug( 'Step 2:chip1 start scan with filter mac success.' ) break else: # 进入不开启过滤的扫描结果判断流程 if count < self.unfilter_count: print('chip1', count, message) count += 1 else: self.flag2 += 1 self.logger.debug( 'Step 2:chip1 start scan with no filter mac success.' ) break def set_timeout(self): self.timeout_flag = True
def _job_popen( commands, # type: List[Text] stdin_path, # type: Optional[Text] stdout_path, # type: Optional[Text] stderr_path, # type: Optional[Text] env, # type: MutableMapping[AnyStr, AnyStr] cwd, # type: Text job_dir, # type: Text job_script_contents=None, # type: Optional[Text] timelimit=None, # type: Optional[int] name=None, # type: Optional[Text] monitor_function=None # type: Optional[Callable[[subprocess.Popen], None]] ): # type: (...) -> int if job_script_contents is None and not FORCE_SHELLED_POPEN: stdin = subprocess.PIPE # type: Union[IO[Any], int] if stdin_path is not None: stdin = open(stdin_path, "rb") stdout = sys.stderr # type: IO[Any] if stdout_path is not None: stdout = open(stdout_path, "wb") stderr = sys.stderr # type: IO[Any] if stderr_path is not None: stderr = open(stderr_path, "wb") sproc = subprocess.Popen(commands, shell=False, close_fds=not onWindows(), stdin=stdin, stdout=stdout, stderr=stderr, env=env, cwd=cwd) processes_to_kill.append(sproc) if sproc.stdin is not None: sproc.stdin.close() tm = None if timelimit is not None and timelimit > 0: def terminate(): # type: () -> None try: _logger.warning( u"[job %s] exceeded time limit of %d seconds and will" "be terminated", name, timelimit) sproc.terminate() except OSError: pass tm = Timer(timelimit, terminate) tm.daemon = True tm.start() if monitor_function: monitor_function(sproc) rcode = sproc.wait() if tm is not None: tm.cancel() if isinstance(stdin, IOBase): stdin.close() if stdout is not sys.stderr: stdout.close() if stderr is not sys.stderr: stderr.close() return rcode else: if job_script_contents is None: job_script_contents = SHELL_COMMAND_TEMPLATE env_copy = {} key = None # type: Any for key in env: env_copy[key] = env[key] job_description = { u"commands": commands, u"cwd": cwd, u"env": env_copy, u"stdout_path": stdout_path, u"stderr_path": stderr_path, u"stdin_path": stdin_path } if PY2: with open(os.path.join(job_dir, "job.json"), mode="wb") as job_file: json_dump(job_description, job_file, ensure_ascii=False) else: with open(os.path.join(job_dir, "job.json"), mode="w", encoding='utf-8') as job_file: json_dump(job_description, job_file, ensure_ascii=False) try: job_script = os.path.join(job_dir, "run_job.bash") with open(job_script, "wb") as _: _.write(job_script_contents.encode('utf-8')) job_run = os.path.join(job_dir, "run_job.py") with open(job_run, "wb") as _: _.write(PYTHON_RUN_SCRIPT.encode('utf-8')) sproc = subprocess.Popen( ["bash", job_script.encode("utf-8")], shell=False, cwd=job_dir, # The nested script will output the paths to the correct files if they need # to be captured. Else just write everything to stderr (same as above). stdout=sys.stderr, stderr=sys.stderr, stdin=subprocess.PIPE, ) processes_to_kill.append(sproc) if sproc.stdin is not None: sproc.stdin.close() rcode = sproc.wait() return rcode finally: shutil.rmtree(job_dir)
class LeaderElection(object): def __init__(self, redis: Redis, lease_timeout: int = 10000, acquire_lock_interval: int = 1000, lock_key: str = 'FillYourServiceName') -> None: self.id = str(uuid.uuid4()) self.redis = redis self.lease_timeout = lease_timeout self.acquire_lock_interval = acquire_lock_interval sha = hashlib.sha1() sha.update(lock_key.encode()) self.lock_key = sha.hexdigest() self.callbacks = {} self.released = True def _renew(self): if self.is_leader(): ok = -1 try: ok = self.redis.pexpire(self.lock_key, self.lease_timeout) except RedisError as e: self._emit('error', '_renew', e) if ok == 0: self._emit('error', '_renew', KeyError('lock key does not exist when renew')) if ok != 1: self.release() self.elect() else: self._emit('error', '_renew', RuntimeError('renewing when not a leader')) self.release() self.elect() def elect(self): if not self.released: e = RuntimeError('Duplicated calls to elect before release') self._emit('error', 'elect', e) raise e res = None try: res = self.redis.set(self.lock_key, self.id, px=self.lease_timeout, nx=True) except RedisError as e: self._emit('error', 'elect', e) raise e if res is None: self.elect_timer = Timer(self.acquire_lock_interval / 1000, self.elect) self.elect_timer.start() else: self._emit('elected') self.released = False self.renew_timer = RepeatTimer(self.lease_timeout / 1000 / 2, self._renew) self.renew_timer.start() def is_leader(self) -> bool: id = None try: id = self.redis.get(self.lock_key) except RedisError as e: self._emit('error', 'is_leader', e) id = id.decode() return (id is not None and id == self.id) def release(self): if self.is_leader(): try: self.redis.delete(self.lock_key) except RedisError as e: self._emit('error', 'release', e) if self.renew_timer: self.renew_timer.cancel() if self.elect_timer: self.elect_timer.cancel() self.released = True self._emit('released') def on(self, event_name: str, callback: Callable): if event_name not in self.callbacks: self.callbacks[event_name] = [callback] else: self.callbacks[event_name].append(callback) def _emit(self, event_name: str, *args): if event_name not in self.callbacks: return for callback in self.callbacks[event_name]: callback(*args)
def blink_red_led(): red_led.on() blink_timer = Timer(BLINK_LENGTH, turn_off_red) blink_timer.start()
class ModbusTcpSocket: def __init__(self, host, port, commands): self._killer = None self._port=port self._host=host self.sock=None #self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #self.sock.settimeout(TIMEOUT) self._commands=commands def create(self): logging.debug('Create socket') self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(TIMEOUT) while True: try: self.sock.connect((self._host, self._port)) except ConnectionRefusedError as ex: logging.exception(ex) time.sleep(3) else: break def start_timer(self): if KILL_TIMEOUT > 0: self._killer = Timer(KILL_TIMEOUT, self.kill) self._killer.start() def stop_timer(self): if self._killer: self._killer.cancel() self._killer = None def kill(self): if isinstance(self.sock, socket.socket): logging.debug('Kill socket') self.sock.close() self.sock = None def send_message(self, data, r_size): self.stop_timer() if self.sock is None: self.create() #out = b'' self.sock.send(data) logging.debug('[-> ]: {}'.format(' '.join(hex(b)[2:].rjust(2, '0').upper() for b in data))) # while len(out) < r_size: # out += self.sock.recv(1024) #if len(out) > r_size: # out = out[out.rindex(data[0]):] out=self.sock.recv(2048) logging.debug('[ <-]: {}'.format(' '.join(hex(b)[2:].rjust(2, '0').upper() for b in out))) out_str='{}'.format(''.join(hex(b)[2:].rjust(2, '0').upper() for b in out)) return out_str def commands(self): return self._commands
class DbManager(object): def __init__(self, db_key, db_path): self.db_key = db_key self.db_path = db_path self.commitTimer = None self.queue = [] self.create_action_table() def create_action_table(self): # create the main db table for storing action data self.conn = sqlite3.connect(self.db_path) self.c = self.conn.cursor() self.c.execute('''CREATE TABLE IF NOT EXISTS actions (time integer, name text, cell_index integer, selected_cells text, cell_order text, diff text)''') self.conn.commit() self.conn.close() def add_to_commit_queue(self, action_data, diff, cell_order): # add data to the queue ad = action_data action_data_tuple = (str(ad['time']), ad['name'], str(ad['index']), str(ad['indices']), str(cell_order), pickle.dumps(diff)) self.queue.append(action_data_tuple) if self.commitTimer: if self.commitTimer.is_alive(): self.commitTimer.cancel() self.commitTimer = None # commit data before notebook closes, otherwise let data queue for a # while to prevent rapid serial writing to the db if ad['name'] == 'notebook-closed': self.commit_queue() else: self.commitTimer = Timer(2.0, self.commit_queue) self.commitTimer.start() def commit_queue(self): # commit the queued data self.conn = sqlite3.connect(self.db_path) self.c = self.conn.cursor() try: self.c.executemany('INSERT INTO actions VALUES (?,?,?,?,?,?)', self.queue) self.conn.commit() self.queue = [] except: self.conn.rollback() raise def record_action_to_db(self, action_data, dest_fname): """ save action to sqlite database action_data: (dict) data about action, see above for more details dest_fname: (str) full path to where file is saved on volume db_manager: (DbManager) object managing DB read / write """ # handle edge cases of copy-cell and undo-cell-deletion events diff, cell_order = get_nb_diff(action_data, dest_fname, True) # don't track extraneous events if action_data['name'] in ['unselect-cell'] and diff == {}: return # save the data to the database queue self.add_to_commit_queue(action_data, diff, cell_order)