Example #1
0
class SimpleCSVWriter:
    def __init__(self, filename, fields=None):
        self.filename = filename
        self.lock = RLock()
        self.isFirstRow = True
        self.fields = fields
        
    def writerow(self, d):
        self.lock.acquire()
        fields = self.fields if self.fields is not None else d.keys()
        if self.isFirstRow:
            # dump fields
            f = open(self.filename , "w")
            writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_ALL)
            row = [k for k in fields]
            writer.writerow(row)
            f.close()
            self.isFirstRow = False
        # dump object
        row = [d.get(k,'') for k in fields]
        f = open(self.filename , "a")
        writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_ALL)
        writer.writerow(row)
        f.close()
        self.lock.release()
Example #2
0
class ThreadSafeFSM(InstrumentFSM):
    """
    A FSM class that provides thread locking in on_event to
    prevent simultaneous thread reentry.
    """

    def __init__(self, states, events, enter_event, exit_event):
        """
        """
        super(ThreadSafeFSM, self).__init__(states, events, enter_event, exit_event)
        self._lock = RLock()

    def on_event(self, event, *args, **kwargs):
        """
        """

        self._lock.acquire(True)
        ex = None

        try:
            result = super(ThreadSafeFSM, self).on_event(event, *args, **kwargs)

        except Exception as ex:
            result = None
            log.error("Unhandled Exception")
            log.exception(ex)

        finally:
            self._lock.release()

        if ex:
            raise ex

        return result
Example #3
0
class Database(object):
    UNICODE_TRANSLATE = {ord(u'ö'): u'o', ord(u'ä'): u'a', ord(u'ü'): u'u'}

    def __init__(self, filename):
        self.database_file = filename
        self.channel_map = {}
        self._db = None
        self.lock = RLock()

    def close(self):
        self.commit()

        cur = self.cursor(lock=True)
        log('Optimizing database for faster startup.', sender=self)
        cur.execute("VACUUM")
        cur.close()
        self.lock.release()

        self._db.close()
        self._db = None

    def log(self, message, *args, **kwargs):
        if False:
            try:
                message = message % args
                log('%s', message, sender=self)
            except TypeError, e:
                log('Exception in log(): %s: %s', e, message, sender=self)
Example #4
0
File: model.py Project: loles/solar
class SingleIndexCache(object):
    def __init__(self):
        self.lock = RLock()
        self.cached_vals = []

    def __enter__(self):
        self.lock.acquire()
        return self

    def fill(self, values):
        self.cached_vals = values

    def wipe(self):
        self.cached_vals = []

    def get_index(self, real_funct, ind_name, **kwargs):
        kwargs.setdefault("max_results", 999999)
        if not self.cached_vals:
            recvs = real_funct(ind_name, **kwargs).results
            self.fill(recvs)

    def filter(self, startkey, endkey, max_results=1):
        c = self.cached_vals
        for (curr_val, obj_key) in c:
            if max_results == 0:
                break
            if curr_val >= startkey:
                if curr_val <= endkey:
                    max_results -= 1
                    yield (curr_val, obj_key)
                else:
                    break

    def __exit__(self, *args, **kwargs):
        self.lock.release()
Example #5
0
class XBeeTransparentListener(Thread):

    def __init__(self, xbee_serial):
        super().__init__()
        self.xbser = xbee_serial
        self.daemon = True
        self.stopped = False
        self.pause = RLock()

    def run(self):
        while not self.stopped and self.xbser.is_open:
            with self.pause:
                try:
                    line = self.xbser.readline()
                    if line:
                        print('>', line.strip())
                except Exception as ex:
                    print(str(ex))

    def stop(self):
        self.stopped = True

    def pause(self):
        self.pause.acquire()

    def unpause(self):
        self.pause.release()
Example #6
0
 def __init__(self, req, proxy, logger, task, exit_check=None, ignored_errors=[]):
     Thread.__init__(self, name = "monitor%s" % task.guid)
     Thread.setDaemon(self, True)
     # the count of votes per error code
     self.vote_result = {}
     # the error code to be ignored
     self.vote_cleared = set().union(ignored_errors)
     self.thread_last_seen = {}
     self.dctlock = RLock()
     self.votelock = RLock()
     self.thread_ref = {}
     self.thread_zombie = set()
     # HttpReq instance
     self.req = req
     # proxy.Pool instance
     self.proxy = proxy
     self.logger = logger
     self.task = task
     self._exit = exit_check if exit_check else lambda x: False
     self._cleaning_up = False
     if os.name == "nt":
         self.set_title = lambda s:os.system("TITLE %s" % (
             s if PY3K else s.encode(CODEPAGE, 'replace')))
     elif os.name == 'posix':
         import sys
         self.set_title = lambda s:sys.stdout.write("\033]2;%s\007" % (
             s if PY3K else s.encode(CODEPAGE, 'replace')))
Example #7
0
class XBeeTransparentListener(Thread):

    def __init__(self, on_received=None):
        super().__init__()
        self.xbser = None
        self.on_received = on_received
        self.daemon = True
        self.stopped = False
        self.pause = RLock()

    def run(self):
        while not self.stopped and self.xbser.is_open:
            with self.pause:
                try:
                    line = self.xbser.readline()
                    if line:
                        self.received(line)
                except Exception as ex:
                    print(str(ex))

    def received(self, line):
        """Subclasses may override this method, or provide a callback function when instance is created"""
        if self.on_received:
            self.on_received(line)
        else:
            print('[XBee]', line.strip())

    def stop(self):
        self.stopped = True

    def pause(self):
        self.pause.acquire()

    def unpause(self):
        self.pause.release()
Example #8
0
class CacheDict(dict):
  
  _cacheTimeout = 0
  _accesslock   = None

  def __init__(self, timeout):
    self._cacheTimeout = timeout
    self._accesslock   = RLock()

  def get(self, key):
    return self[key]['value']

  def cache(self, key, value):
    self[key] = { 'time' : time.time(), 'value' : value }
    
  def isObsolete(self, key):
    return (not self.has_key(key) or
            time.time() - self[key]['time'] > self._cacheTimeout)
    
  def invalidate(self, key):
    if self.has_key(key):
      return self.pop(key)['value']

  def acquire(self):
    self._accesslock.acquire()

  def release(self):
    self._accesslock.release()
Example #9
0
    def __init__(self):
        Thread.__init__(self)

        self.positions = list()
        self.stopped = False

        self.startTime = time.time()

        self.timer = 0
        self.counter = 0
        self.total_uavs = 0  # Total number of UAVs in the air
        self.steps_counter = 0

        self.turn_kill_counter = 0

        self.writeState = Event()
        self.writeCounter = 0
        self.readState = Event()
        self.readCounter = 0

        self.lock = RLock()
        self.lock2 = RLock()
        try:
            self.readState.set()
            logging.info('Read state set.')
        except:
            logging.error('Read state not set.')
Example #10
0
 def __init__(self, ip, port):
     self.ip = ip
     self.port = port
     self.name = "http://" + str(ip) + ":" + str(port)
     self.hashid = getHash(self.name)
     self.server = RPCThreading(("", port), logRequests=False)
     # register functions here
     self.server.register_function(self.notify, "notify")
     self.server.register_function(self.getPred, "getPred")
     self.server.register_function(self.find, "find")
     self.server.register_function(self.findSuccessor, "findSuccessor")
     self.server.register_function(self.isAlive, "isAlive")
     self.server.register_function(self.alert, "alert")
     self.server.register_function(self.join, "join")
     self.server.register_function(self.create, "create")
     self.server.register_function(self.getSuccessorList, "getSuccessorList")
     self.server.register_function(self.getPredID, "getPredID")
     # finger[k] = successor of (n + 2**(k-1)) % mod MAX, 1 <= k <= HASHSIZE
     self.fingers = [None] * HASHSIZE  # finger[k] = successor of (n + 2**(k-1)) % mod MAX, 1 <= k <= HASHSIZE
     self.next = 0
     self.pred = self
     self.succ = self
     self.successorList = [self.name] * NUM_SUCCESSORS
     self.running = False
     self.myThread = None
     self.successorLock = Lock()
     self.runningLock = Lock()
     t = Thread(target=self.server.serve_forever)
     t.daemon = True
     t.start()
Example #11
0
class DataSignaler(object):
    def __init__(self, name, pruneFunc, data):
        super(DataSignaler, self).__init__()

        assert isinstance(data,TreeFunctioned)

        self.data = data
        self.event_signaler = EventSignaler(key=name)

        if pruneFunc is not None:
            assert callable(pruneFunc)

        self.prune_func = pruneFunc
        self._lock = RLock()

    def add(self, value):
        self._lock.acquire()

        try:
            self.data.addToTreeByFunction(value)
        finally:
            self._lock.release()

        data = {self.event_signaler.key : {'data': self.data}}
        self.event_signaler.signalEvent(data)

    def prune(self):
        if self.prune_func is not None:
            return criticalSection(self._lock, lambda: self.prune_func(dataStructure=self.data))

    def inByFunction(self, value, hashFuncList=None, depth=0):
       return criticalSection(self._lock, lambda: self.data.inByFunction(value, hashFuncList, depth))

    def getOriginalByFunction(self, value, hashFuncList=None, depth=0):
        return criticalSection(self._lock, lambda: self.data.getOriginalByFunction(value, hashFuncList, depth))
Example #12
0
class VM_Pool(object):

    def __init__(self, vm_map):
        self.proc_mgr = proc_mgmt.ProcMgr()
        self.vm_map = vm_map
        self.vm_rdy = {}
        self.init_map()
        self.pool_gate = RLock()
        
    def acquire(self, *names):
        self.pool_gate.acquire()
        for name in names:
            if self.vm_rdy.get(name):
                self.vm_rdy[name] = False
                return self.vm_map.get(name)
        self.pool_gate.release()
        return None
    
    def release(self, name):
        self.vm_rdy[name] = True
      
    def init_map(self):
        for name, vm_obj in self.vm_map.items():
            self.vm_rdy[name] = True

    def __str__(self):
        string = 'Pool:'
        for vm in self.vm_map.keys():
            string += vm + ": " + str(self.vm_rdy.get(vm)) + ", "
        return string
Example #13
0
class Framer(Packer):

  HEADER="!4s4B"

  def __init__(self, sock):
    self.sock = sock
    self.sock_lock = RLock()
    self.tx_buf = ""
    self.rx_buf = ""
    self.security_layer_tx = None
    self.security_layer_rx = None
    self.maxbufsize = 65535

  def aborted(self):
    return False

  def write(self, buf):
    self.tx_buf += buf

  def flush(self):
    self.sock_lock.acquire()
    try:
      if self.security_layer_tx:
        try:
          cipher_buf = self.security_layer_tx.encode(self.tx_buf)
        except SASLError, e:
          raise Closed(str(e))
        self._write(cipher_buf)
      else:
 def __init__(self, cache, store, logger, max_writer_threads=30):
     '''Max. throughput for files < 1MB is max_writer_threads * 100KB per second.
     :param max_writer_threads: Max. number of writer threads to use.
     '''
     self.logger = logger
     self.stats = WorkerStats()
     self.cache = cache
     self.store = store
     self.max_writer_threads = max_writer_threads
     self.WRITE_TIMELIMIT = 60*60*2 #2h
     self.lock = RLock()
     self.protect_cache_from_write_access = RLock() #could also be normal lock
     self.oldest_modified_date = {} #keep track of modified date of a cache entry when it is first enqueued for upload. Their contents might change during upload.
     self.removers = []
     self.writers = []
     self.readers = []
     self._stop = False
     self.thread = None
     self.last_reconnect = time.time()
     self._heartbeat = time.time()
     #used for waiting when quota errors occur
     self.skip_starting_new_writers_for_next_x_cycles = 0
     self.do_profiling = False
     self.upload_process_pool = WriteWorkerProcesses(store, logger)
     self.logger.info("initialized StoreSyncThread")
Example #15
0
    def open_dynamic_queue(self):
        if self._disconnecting:
            self.logger.info("Connection factory disconnecting, aborting open_dynamic_queue")
            return
        else:
            self.logger.log(TRACE1, "open_dynamic_queue -> not disconnecting")

        if not self._is_connected:
            self.logger.log(TRACE1, "open_dynamic_queue -> _is_connected1 %s" % self._is_connected)
            self._connect()
            self.logger.log(TRACE1, "open_dynamic_queue -> _is_connected2 %s" % self._is_connected)

        dynamic_queue = self.mq.Queue(self.mgr, self.dynamic_queue_template,
            self.CMQC.MQOO_INPUT_SHARED)

        # A bit hackish, but there's no other way to get its name.
        dynamic_queue_name = dynamic_queue._Queue__qDesc.ObjectName.strip()

        lock = RLock()
        lock.acquire()
        try:
            self._open_dynamic_queues_cache[dynamic_queue_name] = dynamic_queue
        finally:
            lock.release()

        self.logger.log(TRACE1, "Successfully created a dynamic queue, descriptor [%s]" % (
            dynamic_queue._Queue__qDesc))

        return dynamic_queue_name
Example #16
0
    def close_dynamic_queue(self, dynamic_queue_name):
        if self._disconnecting:
            self.logger.info("Connection factory disconnecting, aborting close_dynamic_queue")
            return
        else:
            self.logger.log(TRACE1, "close_dynamic_queue -> not disconnecting")

        if not self._is_connected:
            # If we're not connected then all dynamic queues had been already closed.
            self.logger.log(TRACE1, "close_dynamic_queue -> _is_connected1 %s" % self._is_connected)
            return
        else:
            self.logger.log(TRACE1, "close_dynamic_queue -> _is_connected2 %s" % self._is_connected)
            lock = RLock()
            lock.acquire()
            try:
                dynamic_queue = self._open_dynamic_queues_cache[dynamic_queue_name]
                dynamic_queue.close()

                self._open_dynamic_queues_cache.pop(dynamic_queue_name, None)
                self._open_send_queues_cache.pop(dynamic_queue_name, None)
                self._open_receive_queues_cache.pop(dynamic_queue_name, None)

                self.logger.log(TRACE1, "Successfully closed a dynamic queue [%s]" % (
                    dynamic_queue_name))

            finally:
                lock.release()
Example #17
0
class Main(HTTPServlet):
    registerShutdown = 1
    
    def __init__(self):
        HTTPServlet.__init__(self)
        
        self.blogDirectory = WeblogDirectory("../blogs.xml")    
        self.mutex = RLock()
             
    def awake(self, transaction):
        # Register our shutdown handler if it hasn't already been done. This is to
        # make sure the databases are properly closed when the system is shutdown.
        self.mutex.acquire()
        try:        
            if (Main.registerShutdown == 1):
                transaction.application().addShutDownHandler(self.blogDirectory.shutdown)
                Main.registerShutdown = 0
        finally:
            self.mutex.release()
            
    def respondToGet(self, transaction):
        request = transaction.request()
        response = transaction.response()
        
        pathInfo = request.extraURLPath() 

        try:
            (blog, pathInfo) = self._parsePathInfo(pathInfo)
            weblog = self.blogDirectory.getBlog(blog)
        
            try:
                stylesheet = request.field('t', "")
                # Extra optional argument that can be passed to the stylesheet
                arg = request.field('a', "")
            
                # Content query that can be applied as a final step to extract
                # something from the rendered content
                contentQuery = request.field('c', "")
            
                result = weblog.handleRequest(pathInfo, stylesheet, arg, contentQuery)
            
                # Determine the content-type for the result
                if (result.startswith("<?xml")):                             
                    contentType = "text/xml"         
                elif (result.startswith("<html")):
                    contentType = "text/html"
                else:
                    contentType = "text/plain"
                #print result
                
                response.setStatus(200, 'OK')
                response.setHeader('Content-type', contentType)
                response.setHeader('Content-length', str(len(result)))
                response.write(result)
            except NotFoundError:
                response.setStatus(404, 'Not Found')
        except KeyError, IndexError:
            response.setStatus(404, 'Weblog Not Found')
Example #18
0
class ScrollableHandler(StreamHandlerNoLF):
    """ ScrollableHandler is a StreamHandler that specially handles scrolling (log
    messages at the SCROLL level). It allows you to temporarily interrupt the constant
    scroll with other log messages of different levels (printed at the top of the scroll
    area) """

    # the SCROLL level (a class var)
    LOGFILE = 11
    SCROLL = 12
    SHUTDOWN = 13
    NOLOGFILE = 14
    
    def __init__(self, *args, **kwargs):
        self.scrollLock = RLock()
        self.scrollFlag = False
        StreamHandlerNoLF.__init__(self, *args, **kwargs)

    def handle(self, record):
        """ The 'scroll' level is a constant scroll that can be interrupted. This interruption is
        done via prepending text to the scroll area """
        rv = self.filter(record)
        if rv:

            if record.levelno == ScrollableHandler.SCROLL:
                self.emitSynchronized(record)
            elif record.levelno == ScrollableHandler.SHUTDOWN:
                record.msg = '\n\n\n%s\n' % record.msg
                self.emitSynchronized(record)
            else:
                self.scrollLock.acquire()
                # If scroll is on, interrupt scroll
                if ScrollableHandler.scrollFlag:
                    self.scrollHeader(record)
                else:
                    # otherwise if scroll isn't on, just log the message normally
                    self.emitSynchronized(record)
                self.scrollLock.release()
                            
        return rv

    def emitSynchronized(self, record):
        """ Write a log message atomically. Normal python logging Handler behavior """
        self.acquire()
        try:
            self.emit(record)
        finally:
            self.release()

    def scrollHeader(self, record):
        """ Print a log message so that the user can see it during a SCROLL """
        msg = self.format(record).rstrip() # Scroller appends newline for us
        from twisted.internet import reactor
        if inMainThread():
            # FIXME: scrollBegin() should really be creating the scroller instance
            # FIXME: no unicode crap from normal python log emit
            Hellanzb.scroller.scrollHeader(msg)
        else:
            reactor.callFromThread(Hellanzb.scroller.scrollHeader, msg)
Example #19
0
 def _threadCallback(self,thread):
   """
   A callback function which gets called when a code thread terminates.
   """
   lock = RLock()
   lock.acquire()
   if thread.failed():
     self._exceptions[thread._id] = thread.exceptionInfo()
     self._tracebacks[thread._id] = thread.tracebackInfo()
   lock.release()
Example #20
0
 def exit(self):
     global command_output_thread
     print("Exiting thread {0}...".format(self))
     lock = RLock()
     lock.acquire()
     self.EXIT_FLAG = True
     pgid = self.get_pgid(True)
     if pgid is not None:
         call(["ionice", "-c", "0", "-P", str(self.get_pgid(True))])
     lock.release()
Example #21
0
class Chopstick:
    def __init__(self, name):
        self.lock = RLock()
        self.name = name

    def pickUp(self):
        return self.lock.acquire(blocking=False)

    def putDown(self):
        self.lock.release()
Example #22
0
class MonitorLock(object):
	def __init__(self):
		self.lock = RLock()
	def acquire(self, flag=True):
		self.lock.acquire()
	def release(self):
		self.lock.release()
	def MONITOR_ENTER(self):
		self.acquire()
	def MONITOR_EXIT(self):
		self.release()
class ThreadSafeDict(dict) :
    def __init__(self, * p_arg, ** n_arg) :
        dict.__init__(self, * p_arg, ** n_arg)
        self._lock = RLock()

    def __enter__(self) :
        self._lock.acquire()
        return self

    def __exit__(self, type, value, traceback) :
        self._lock.release()
Example #24
0
File: dhtlib.py Project: Shu-Ji/dht
class Client(KRPC):
    def __init__(self, table):
        self.table = table
        self.lock = RLock()

        timer(KRPC_TIMEOUT, self.timeout)
        KRPC.__init__(self)

    def find_node(self, address, nid=None):
        nid = self.get_neighbor(nid) if nid else self.table.nid
        tid = entropy(TID_LENGTH)
        msg = {
            "t": tid,
            "y": "q",
            "q": "find_node",
            "a": {"id": nid, "target": random_id()}
        }
        self.send_krpc(msg, address)

    def bootstrap(self):
        for address in BOOTSTRAP_NODES:
            self.find_node(address)

    def timeout(self):
        if not self.join_successed:
            self.bootstrap()
        timer(KRPC_TIMEOUT, self.timeout)

    def run(self):
        self.bootstrap()
        while 1:
            time.sleep(.001)
            try:
                data, address = self.ufd.recvfrom(65536)
                msg = bdecode(data)
                self.types[msg["y"]](msg, address)
            except Exception:
                pass

    def foreverloop(self):
        self.start()
        while 1:
            time.sleep(.001)
            if not self.table.nodes:
                self.join_successed = False
                time.sleep(1)
                continue

            for node in self.table.nodes:
                self.find_node((node.ip, node.port), node.nid)

            self.lock.acquire()
            self.table.nodes = []
            self.lock.release()
Example #25
0
    def __init__(self, name=""):
        self.name = name
        self.readers = 0
        self.reader_stacks = []
        self.lock = RLock()
        self.reader_lock = RLock()

        self.writer_stacks = []
        self.writer_id = 0

        alllocks.append(self)
Example #26
0
class Counter:
    def __init__(self, start=0, increment=1):
        self.counter = start
        self.increment = increment
        self.lock = RLock()

    def __call__(self):
        self.lock.acquire()
        self.counter += self.increment
        i = self.counter
        self.lock.release()
        return i
Example #27
0
class SynchronizedContainer:
    def __init__(self, obj):
        self._o = obj
        self._lock = RLock()
    def __enter__(self):
        if self._lock.__enter__():
            return self._o
    def __exit__(self, t, v, tb):
        self._lock.__exit__(t, v, tb)
    def __call__(self, *a, **kw):
        with self as sfnc:
            return sfnc(*a, **kw)
Example #28
0
class NoDispersyRLock():
    
    def __init__(self):
        self.lock = RLock()
    
    @warnDisperyThread
    def acquire(self, blocking=1):
        return self.lock.acquire(blocking)
    
    @warnDisperyThread    
    def release(self):
        return self.lock.release()
Example #29
0
        def wrapper(self, *args, **kwargs):
            retval = getattr(self, attr, None)
            if retval is None:
                retval = fn(self, *args, **kwargs)
                _lock = RLock()
                try:
                    _lock.acquire()
                    setattr(self, attr, retval)
                finally:
                    _lock.release()

            return retval
Example #30
0
 def __init__(self):
     self._msg_queue_lock = RLock()
     self._users_lock = RLock()
     self.msg_queue = {}
     self.bootstrap()
     self._dying = False
     Thread.__init__(self)
     self.daemon = True
     self.start()
     self.users = {}
     self.tags2user = {}
     self.tags2msg = {}
Example #31
0
class WPApp(object):

    Version = "Undefined"

    MIME_TYPES_BASE = {
        "gif": "image/gif",
        "jpg": "image/jpeg",
        "jpeg": "image/jpeg",
        "js": "text/javascript",
        "html": "text/html",
        "txt": "text/plain",
        "css": "text/css"
    }

    def __init__(self,
                 root_class,
                 strict=False,
                 static_path="/static",
                 static_location=None,
                 enable_static=False,
                 prefix=None,
                 replace_prefix=None,
                 disable_robots=True):
        if not isinstance(root_class, type) and callable(root_class):
            # if it's in fact a function, use LambdaHandlerFactory to wrap
            # the function into a LambdaHandler
            root_class = LambdaHandlerFactory(root_class)

        enable_static = enable_static or (static_location is not None)
        if static_location is None: static_location = "./static"
        self.StaticPath = static_path
        self.StaticLocation = static_location
        #print("App init: StaticLocation:", static_location)
        self.StaticEnabled = enable_static and static_location

        self.RootClass = root_class
        self.JEnv = None
        self._AppLock = RLock()
        self.ScriptHome = None
        self.Initialized = False
        self.DisableRobots = disable_robots
        self.Prefix = prefix
        self.ReplacePrefix = replace_prefix

    def _app_lock(self):
        return self._AppLock

    def __enter__(self):
        return self._AppLock.__enter__()

    def __exit__(self, *params):
        return self._AppLock.__exit__(*params)

    # override
    @app_synchronized
    def acceptIncomingTransfer(self, method, uri, headers):
        return True

    @app_synchronized
    def initJinjaEnvironment(self, tempdirs=[], filters={}, globals={}):
        # to be called by subclass
        #print "initJinja2(%s)" % (tempdirs,)
        from jinja2 import Environment, FileSystemLoader
        if not isinstance(tempdirs, list):
            tempdirs = [tempdirs]
        self.JEnv = Environment(loader=FileSystemLoader(tempdirs))
        for n, f in filters.items():
            self.JEnv.filters[n] = f
        self.JGlobals = {}
        self.JGlobals.update(globals)

    @app_synchronized
    def setJinjaFilters(self, filters):
        for n, f in filters.items():
            self.JEnv.filters[n] = f

    @app_synchronized
    def setJinjaGlobals(self, globals):
        self.JGlobals = {}
        self.JGlobals.update(globals)

    def applicationErrorResponse(self, headline, exc_info):
        typ, val, tb = exc_info
        exc_text = traceback.format_exception(typ, val, tb)
        exc_text = ''.join(exc_text)
        text = """<html><body><h2>Application error</h2>
            <h3>%s</h3>
            <pre>%s</pre>
            </body>
            </html>""" % (headline, exc_text)
        #print exc_text
        return Response(text, status='500 Application Error')

    def static(self, relpath):
        #print("WPApp.static: relpath:", relpath)
        while ".." in relpath:
            relpath = relpath.replace("..", ".")
        home = self.StaticLocation
        #print("WPApp.static: home:", home)
        path = os.path.join(home, relpath)
        #print ("static: path:", path)
        try:
            st_mode = os.stat(path).st_mode
            if not stat.S_ISREG(st_mode):
                #print "not a regular file"
                raise ValueError("Not regular file")
        except:
            raise
            return Response("Not found", status=404)

        ext = path.rsplit('.', 1)[-1]
        mime_type = self.MIME_TYPES_BASE.get(ext, "text/html")

        def read_iter(f):
            while True:
                data = f.read(100000)
                if not data: break
                yield data

        #print "returning response..."
        return Response(app_iter=read_iter(open(path, "rb")),
                        content_type=mime_type)

    def convertPath(self, path):
        if self.Prefix is not None:
            matched = ""
            ok = False
            if path == self.Prefix:
                matched = path
                ok = True
            elif path.startswith(self.Prefix + '/'):
                matched = self.Prefix
                ok = True

            if not ok:
                return None

            if self.ReplacePrefix is not None:
                path = self.ReplacePrefix + (path[len(matched):] or "/")

        return path

    def __call__(self, environ, start_response):
        #print 'app call ...'
        path = environ.get('PATH_INFO', '')
        environ["WebPie.original_path"] = path
        #print 'path:', path_down

        path = self.convertPath(path)
        if path is None:
            return HTTPNotFound()(environ, start_response)

        environ["PATH_INFO"] = path

        req = Request(environ)
        if not self.Initialized:
            self.ScriptName = environ.get('SCRIPT_NAME', '')
            self.Script = environ.get('SCRIPT_FILENAME',
                                      os.environ.get('UWSGI_SCRIPT_FILENAME'))
            self.ScriptHome = os.path.dirname(self.Script
                                              or sys.argv[0]) or "."
            if self.StaticEnabled:
                if not self.StaticLocation[0] in ('.', '/'):
                    self.StaticLocation = self.ScriptHome + "/" + self.StaticLocation
                    #print "static location:", self.StaticLocation
            self.Initialized = True

        if self.StaticEnabled and path.startswith(self.StaticPath + "/"):
            resp = self.static(path[len(self.StaticPath) + 1:])
        elif self.DisableRobots and path.endswith("/robots.txt"):
            resp = Response("User-agent: *\nDisallow: /\n",
                            content_type="text/plain")
        else:
            root = self.RootClass(req, self)
            try:
                return root.wsgi_call(environ, start_response)
            except:
                resp = self.applicationErrorResponse("Uncaught exception",
                                                     sys.exc_info())
        return resp(environ, start_response)

    def jinja_globals(self):
        # override me
        return {}

    def add_globals(self, d):
        params = {}
        params.update(self.JGlobals)
        params.update(self.jinja_globals())
        params.update(d)
        return params

    def render_to_string(self, temp, **kv):
        t = self.JEnv.get_template(temp)
        return t.render(self.add_globals(kv))

    def render_to_iterator(self, temp, **kv):
        t = self.JEnv.get_template(temp)
        return t.generate(self.add_globals(kv))

    def run_server(self, port, **args):
        from .HTTPServer import HTTPServer
        srv = HTTPServer(port, self, **args)
        srv.start()
        srv.join()
Example #32
0
class Query2HitsMap:

    def __init__(self):
        self.lock = RLock()
        self.d = {}

    def add_query(self, id, searchstr, timestamp):
        if DEBUG:
            print >> sys.stderr, 'q2h: lock1', id
        self.lock.acquire()
        try:
            qrec = self.d.get(id, {})
            qrec['searchstr'] = searchstr
            qrec['timestamp'] = timestamp
            qrec['hitlist'] = {}
            self.d[id] = qrec
        finally:
            if DEBUG:
                print >> sys.stderr, 'q2h: unlock1'
            self.lock.release()

    def add_hits(self, id, hits):
        if DEBUG:
            print >> sys.stderr, 'q2h: lock2', id, len(hits)
        self.lock.acquire()
        try:
            qrec = self.d[id]
            qrec['hitlist'].update(hits)
        finally:
            if DEBUG:
                print >> sys.stderr, 'q2h: unlock2'
            self.lock.release()

    def get_hits(self, id):
        if DEBUG:
            print >> sys.stderr, 'q2h: lock3', id
        self.lock.acquire()
        try:
            qrec = self.d[id]
            return copy.copy(qrec['hitlist'])
        finally:
            if DEBUG:
                print >> sys.stderr, 'q2h: unlock3'
            self.lock.release()

    def get_searchstr(self, id):
        if DEBUG:
            print >> sys.stderr, 'q2h: lock4'
        self.lock.acquire()
        try:
            qrec = self.d[id]
            return qrec['searchstr']
        finally:
            if DEBUG:
                print >> sys.stderr, 'q2h: unlock4'
            self.lock.release()

    def garbage_collect_timestamp_smaller(self, timethres):
        self.lock.acquire()
        try:
            idlist = []
            for id, qrec in self.d.iteritems():
                if qrec['timestamp'] < timethres:
                    idlist.append(id)

            for id in idlist:
                del self.d[id]

        finally:
            self.lock.release()
Example #33
0
 def __init__(self, *args, **kwargs):
     self.socket_list = set()
     self.send_queue = Queue(0)
     self.received_queue = Queue(0)
     self.sockets_lock = RLock()
Example #34
0
 def __init__(self, my_node_id):
     self.my_node_id = my_node_id
     self.trie = Trie(u'01')
     self.trie[u''] = Bucket(u'')
     self.lock = RLock()
Example #35
0
class WebcamForwarder(StubClientMixin):

    __signals__ = ["webcam-changed"]

    def __init__(self):
        StubClientMixin.__init__(self)
        #webcam:
        self.webcam_option = ""
        self.webcam_forwarding = False
        self.webcam_device = None
        self.webcam_device_no = -1
        self.webcam_last_ack = -1
        self.webcam_ack_check_timer = None
        self.webcam_send_timer = None
        self.webcam_lock = RLock()
        self.server_webcam = False
        self.server_virtual_video_devices = 0
        if not hasattr(self, "send"):
            self.send = self.noop

    def noop(self, *_args):
        pass

    def cleanup(self):
        self.stop_sending_webcam()

    def init(self, opts, _extra_args=[]):
        self.webcam_option = opts.webcam
        self.webcam_forwarding = self.webcam_option.lower(
        ) not in FALSE_OPTIONS
        self.server_webcam = False
        self.server_virtual_video_devices = 0
        if self.webcam_forwarding:
            with OSEnvContext():
                os.environ["LANG"] = "C"
                os.environ["LC_ALL"] = "C"
                try:
                    import cv2
                    from PIL import Image
                    assert cv2 and Image
                except ImportError as e:
                    log("init webcam failure", exc_info=True)
                    if WIN32 and BITS == 32:
                        log.info(
                            "32-bit builds do not support webcam forwarding")
                    else:
                        log.warn("Warning: failed to import opencv:")
                        log.warn(" %s", e)
                        log.warn(" webcam forwarding is disabled")
                    self.webcam_forwarding = False
        log("webcam forwarding: %s", self.webcam_forwarding)

    def parse_server_capabilities(self):
        c = self.server_capabilities
        self.server_webcam = c.boolget("webcam")
        self.server_webcam_encodings = c.strlistget("webcam.encodings",
                                                    ("png", "jpeg"))
        self.server_virtual_video_devices = c.intget("virtual-video-devices")
        log("webcam server support: %s (%i devices, encodings: %s)",
            self.server_webcam, self.server_virtual_video_devices,
            csv(self.server_webcam_encodings))
        if self.webcam_forwarding and self.server_webcam and self.server_virtual_video_devices > 0:
            if self.webcam_option == "on" or self.webcam_option.find(
                    "/dev/video") >= 0:
                self.start_sending_webcam()
        return True

    def webcam_state_changed(self):
        self.idle_add(self.emit, "webcam-changed")

    ######################################################################
    def start_sending_webcam(self):
        with self.webcam_lock:
            self.do_start_sending_webcam(self.webcam_option)

    def do_start_sending_webcam(self, device_str):
        assert self.server_webcam
        device = 0
        virt_devices, all_video_devices, non_virtual = {}, {}, {}
        try:
            from xpra.platform.webcam import get_virtual_video_devices, get_all_video_devices
            virt_devices = get_virtual_video_devices()
            all_video_devices = get_all_video_devices()
            non_virtual = dict([(k, v) for k, v in all_video_devices.items()
                                if k not in virt_devices])
            log("virtual video devices=%s", virt_devices)
            log("all_video_devices=%s", all_video_devices)
            log("found %s known non-virtual video devices: %s",
                len(non_virtual), non_virtual)
        except ImportError as e:
            log("no webcam_util: %s", e)
        log("do_start_sending_webcam(%s)", device_str)
        if device_str in ("auto", "on", "yes", "off", "false", "true"):
            if len(non_virtual) > 0:
                device = tuple(non_virtual.keys())[0]
        else:
            log("device_str: %s", device_str)
            try:
                device = int(device_str)
            except:
                p = device_str.find("video")
                if p >= 0:
                    try:
                        log("device_str: %s", device_str[p:])
                        device = int(device_str[p + len("video"):])
                    except:
                        device = 0
        if device in virt_devices:
            log.warn("Warning: video device %s is a virtual device",
                     virt_devices.get(device, device))
            if WEBCAM_ALLOW_VIRTUAL:
                log.warn(" environment override - this may hang..")
            else:
                log.warn(" corwardly refusing to use it")
                log.warn(" set WEBCAM_ALLOW_VIRTUAL=1 to force enable it")
                return
        import cv2
        log("do_start_sending_webcam(%s) device=%i", device_str, device)
        self.webcam_frame_no = 0
        try:
            #test capture:
            webcam_device = cv2.VideoCapture(device)  #0 -> /dev/video0
            ret, frame = webcam_device.read()
            log("test capture using %s: %s, %s", webcam_device, ret, frame
                is not None)
            assert ret, "no device or permission"
            assert frame is not None, "no data"
            assert frame.ndim == 3, "unexpected  number of dimensions: %s" % frame.ndim
            w, h, Bpp = frame.shape
            assert Bpp == 3, "unexpected number of bytes per pixel: %s" % Bpp
            assert frame.size == w * h * Bpp
            self.webcam_device_no = device
            self.webcam_device = webcam_device
            self.send("webcam-start", device, w, h)
            self.webcam_state_changed()
            log("webcam started")
            if self.send_webcam_frame():
                delay = 1000 // WEBCAM_TARGET_FPS
                log("webcam timer with delay=%ims for %i fps target)", delay,
                    WEBCAM_TARGET_FPS)
                self.cancel_webcam_send_timer()
                self.webcam_send_timer = self.timeout_add(
                    delay, self.may_send_webcam_frame)
        except Exception as e:
            log.warn("webcam test capture failed: %s", e)

    def cancel_webcam_send_timer(self):
        wst = self.webcam_send_timer
        if wst:
            self.webcam_send_timer = None
            self.source_remove(wst)

    def cancel_webcam_check_ack_timer(self):
        wact = self.webcam_ack_check_timer
        if wact:
            self.webcam_ack_check_timer = None
            self.source_remove(wact)

    def webcam_check_acks(self, ack=0):
        self.webcam_ack_check_timer = None
        log("check_acks: webcam_last_ack=%s", self.webcam_last_ack)
        if self.webcam_last_ack < ack:
            log.warn(
                "Warning: no acknowledgements received from the server for frame %i, stopping webcam",
                ack)
            self.stop_sending_webcam()

    def stop_sending_webcam(self):
        log("stop_sending_webcam()")
        with self.webcam_lock:
            self.do_stop_sending_webcam()

    def do_stop_sending_webcam(self):
        self.cancel_webcam_send_timer()
        self.cancel_webcam_check_ack_timer()
        wd = self.webcam_device
        log("do_stop_sending_webcam() device=%s", wd)
        if not wd:
            return
        self.send("webcam-stop", self.webcam_device_no)
        assert self.server_webcam
        self.webcam_device = None
        self.webcam_device_no = -1
        self.webcam_frame_no = 0
        self.webcam_last_ack = -1
        try:
            wd.release()
        except Exception as e:
            log.error("Error closing webcam device %s: %s", wd, e)
        self.webcam_state_changed()

    def may_send_webcam_frame(self):
        self.webcam_send_timer = None
        if self.webcam_device_no < 0 or not self.webcam_device:
            return False
        not_acked = self.webcam_frame_no - 1 - self.webcam_last_ack
        #not all frames have been acked
        latency = 100
        if len(self.server_ping_latency) > 0:
            l = [x for _, x in tuple(self.server_ping_latency)]
            latency = int(1000 * sum(l) / len(l))
        #how many frames should be in flight
        n = max(
            1, latency //
            (1000 // WEBCAM_TARGET_FPS))  #20fps -> 50ms target between frames
        if not_acked > 0 and not_acked > n:
            log(
                "may_send_webcam_frame() latency=%i, not acked=%i, target=%i - will wait for next ack",
                latency, not_acked, n)
            return False
        log(
            "may_send_webcam_frame() latency=%i, not acked=%i, target=%i - trying to send now",
            latency, not_acked, n)
        return self.send_webcam_frame()

    def send_webcam_frame(self):
        if not self.webcam_lock.acquire(False):
            return False
        log("send_webcam_frame() webcam_device=%s", self.webcam_device)
        try:
            assert self.webcam_device_no >= 0, "device number is not set"
            assert self.webcam_device, "no webcam device to capture from"
            from xpra.codecs.pillow.encode import get_encodings
            client_webcam_encodings = get_encodings()
            common_encodings = list(
                set(self.server_webcam_encodings).intersection(
                    client_webcam_encodings))
            log("common encodings (server=%s, client=%s): %s",
                csv(self.server_encodings), csv(client_webcam_encodings),
                csv(common_encodings))
            if not common_encodings:
                log.error("Error: cannot send webcam image, no common formats")
                log.error(" the server supports: %s",
                          csv(self.server_webcam_encodings))
                log.error(" the client supports: %s",
                          csv(client_webcam_encodings))
                self.stop_sending_webcam()
                return False
            preferred_order = ["jpeg", "png", "png/L", "png/P", "webp"]
            formats = [x for x in preferred_order if x in common_encodings
                       ] + common_encodings
            encoding = formats[0]
            start = monotonic_time()
            import cv2
            ret, frame = self.webcam_device.read()
            assert ret, "capture failed"
            assert frame.ndim == 3, "invalid frame data"
            h, w, Bpp = frame.shape
            assert Bpp == 3 and frame.size == w * h * Bpp
            rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            end = monotonic_time()
            log("webcam frame capture took %ims", (end - start) * 1000)
            start = monotonic_time()
            from PIL import Image
            image = Image.fromarray(rgb)
            buf = BytesIOClass()
            image.save(buf, format=encoding)
            data = buf.getvalue()
            buf.close()
            end = monotonic_time()
            log("webcam frame compression to %s took %ims", encoding,
                (end - start) * 1000)
            frame_no = self.webcam_frame_no
            self.webcam_frame_no += 1
            self.send("webcam-frame", self.webcam_device_no, frame_no,
                      encoding, w, h, compression.Compressed(encoding, data))
            self.cancel_webcam_check_ack_timer()
            self.webcam_ack_check_timer = self.timeout_add(
                10 * 1000, self.webcam_check_acks)
            return True
        except Exception as e:
            log.error("webcam frame %i failed",
                      self.webcam_frame_no,
                      exc_info=True)
            log.error("Error sending webcam frame: %s", e)
            self.stop_sending_webcam()
            summary = "Webcam forwarding has failed"
            body = "The system encountered the following error:\n" + \
                ("%s\n" % e)
            self.may_notify(XPRA_WEBCAM_NOTIFICATION_ID,
                            summary,
                            body,
                            expire_timeout=10 * 1000,
                            icon_name="webcam")
            return False
        finally:
            self.webcam_lock.release()

    ######################################################################
    #packet handlers
    def _process_webcam_stop(self, packet):
        device_no = packet[1]
        if device_no != self.webcam_device_no:
            return
        self.stop_sending_webcam()

    def _process_webcam_ack(self, packet):
        log("process_webcam_ack: %s", packet)
        with self.webcam_lock:
            if self.webcam_device:
                frame_no = packet[2]
                self.webcam_last_ack = frame_no
                if self.may_send_webcam_frame():
                    self.cancel_webcam_send_timer()
                    delay = 1000 // WEBCAM_TARGET_FPS
                    log("new webcam timer with delay=%ims for %i fps target)",
                        delay, WEBCAM_TARGET_FPS)
                    self.webcam_send_timer = self.timeout_add(
                        delay, self.may_send_webcam_frame)

    def init_authenticated_packet_handlers(self):
        log("init_authenticated_packet_handlers()")
        self.set_packet_handlers(
            self._ui_packet_handlers, {
                "webcam-stop": self._process_webcam_stop,
                "webcam-ack": self._process_webcam_ack,
            })
 def __init__(self):
     self.time = 0
     self.lock = RLock()
Example #37
0
 def __init__(self, parent):
     self.parent = parent
     self.is_disposed = False
     self.lock = RLock()
Example #38
0
from functools import partial
from hashlib import sha1
from threading import Lock, RLock

from calibre.constants import cache_dir, iswindows
from calibre.customize.ui import plugin_for_input_format
from calibre.srv.errors import BookNotFound, HTTPNotFound
from calibre.srv.metadata import book_as_json
from calibre.srv.render_book import RENDER_VERSION
from calibre.srv.routes import endpoint, json
from calibre.srv.utils import get_db, get_library_data
from calibre.utils.filenames import rmtree
from calibre.utils.serialize import json_dumps
from polyglot.builtins import as_unicode, itervalues

cache_lock = RLock()
queued_jobs = {}
failed_jobs = {}


def abspath(x):
    x = os.path.abspath(x)
    if iswindows and not x.startswith('\\\\?\\'):
        x = '\\\\?\\' + os.path.abspath(x)
    return x


_books_cache_dir = None


def books_cache_dir():
Example #39
0
class Session:
    def __init__(self, io):
        self._io = io
        self._mutex = RLock()
        self.isclosed = False
        self.handler = Handler
        self.onopen = lambda session: None
        self.onclose = lambda session: None

    # Call a function
    def call(self, fid, *args):
        self.invoke(MSG_CALL, fid, args)
        return self._wait_return()

    def notify(self, fid, *args):
        self.invoke(MSG_NOTIFY, fid, args)

    def invoke(self, t, fid, args):
        self._mutex.acquire()
        self._pack = [t, fid, *args]
        b = self._send_pack()
        self._mutex.release()
        self._lastfid = fid
        return b

    def close(self):
        self._mutex.acquire()
        self._pack = [MSG_CLOSE]
        b = self._send_pack()
        self._mutex.release()
        self._io.close()
        self._closed = True
        return b

    def _return(self, v, t=MSG_RETVAL):
        self._mutex.acquire()
        self._pack = [t, v]
        b = self._send_pack()
        self._mutex.release()
        return b

    # Return a value
    def run(self):
        self.onopen(self)
        while self._recv_pack():
            self._handle_invoke()
            if self.isclosed:
                break
        self.onclose(self)

    def _recv_pack(self):
        if self.isclosed:
            return False
        try:
            self._pack = msgpack.unpack(self._io)
            return True
        except msgpack.UnpackException as e:
            return False
        except Exception as e:
            print("Receive package failure", e)
            return False

    def _send_pack(self):
        if self.isclosed:
            return False
        try:
            msgpack.pack(self._pack, self._io)
            self._io.flush()
            return True
        except Exception as e:
            # print("Send package failure", e)
            return False

    def _type(self):
        return self._pack[0]

    # Wait a value from subprocess
    def _wait_return(self):
        while self._recv_pack():
            t = self._type()
            if t & 0xF0 == MSG_INVOKE:
                self._handle_invoke()  # handle invoke
            elif t == MSG_RETVAL:
                pack = self._pack
                return pack[1:] if len(pack) > 2 else pack[1]
            elif t == MSG_NOFUNC:
                raise Exception('No such function: ' + self._lastfid)
            else:
                raise Exception('Unkown flag')

    # Handle a invoke
    def _handle_invoke(self):
        t = self._type()
        assert t & 0xF0 == MSG_INVOKE
        if t == MSG_CLOSE:
            self.isclosed = True
        else:
            pack = self._pack
            fid = pack[1]
            ret = None
            try:
                fun = vars(self.handler).get(fid)
                if fun:
                    ret = fun(self, *pack[2:])
                elif self._type() == MSG_CALL:
                    return self._return(None, MSG_NOFUNC)
            except Exception as e:
                print('Throwed a exception during:', fid, e)
                ret = None
            except TypeError as e:
                print('Args\'s number not matched:', fid, e)
            finally:
                if t == MSG_CALL:
                    self._return(ret)
Example #40
0
    def __init__(self, maxsize=10, dispose_func=None):
        self._maxsize = maxsize
        self.dispose_func = dispose_func

        self._container = self.ContainerCls()
        self.lock = RLock()
Example #41
0
 def init_process(self):
     self.tpool = futures.ThreadPoolExecutor(max_workers=self.cfg.threads)
     self.poller = selectors.DefaultSelector()
     self._lock = RLock()
     super(ThreadWorker, self).init_process()
Example #42
0
class WindowsBalloonTip(object):
    '''
    Implementation of balloon tip notifications through Windows API.

    * Register Window class name:
      https://msdn.microsoft.com/en-us/library/windows/desktop/ms632596.aspx
    * Create an overlapped window using the registered class.
      - It's hidden everywhere in GUI unless ShowWindow(handle, SW_SHOW)
        function is called.
    * Show/remove a tray icon and a balloon tip notification.

    Each instance is a separate notification with different parameters.
    Can be used with Threads.
    '''

    _class_atom = 0
    _wnd_class_ex = None
    _hwnd = None
    _hicon = None
    _balloon_icon = None
    _notify_data = None
    _count = 0
    _lock = RLock()

    @staticmethod
    def _get_unique_id():
        '''
        Keep track of each created balloon tip notification names,
        so that they can be easily identified even from outside.

        Make sure the count is shared between all the instances
        i.e. use a lock, so that _count class variable is incremented
        safely when using balloon tip notifications with Threads.
        '''

        WindowsBalloonTip._lock.acquire()
        val = WindowsBalloonTip._count
        WindowsBalloonTip._count += 1
        WindowsBalloonTip._lock.release()
        return val

    def __init__(self,
                 title,
                 message,
                 app_name,
                 app_icon='',
                 timeout=10,
                 **kwargs):
        # pylint: disable=unused-argument,too-many-arguments
        '''
        The app_icon parameter, if given, is an .ICO file.
        '''

        wnd_class_ex = win_api_defs.get_WNDCLASSEXW()
        class_name = 'PlyerTaskbar' + str(WindowsBalloonTip._get_unique_id())

        if PY2:
            class_name = class_name.decode('utf8')
        wnd_class_ex.lpszClassName = class_name

        # keep ref to it as long as window is alive
        wnd_class_ex.lpfnWndProc = win_api_defs.WindowProc(
            win_api_defs.DefWindowProcW)
        wnd_class_ex.hInstance = win_api_defs.GetModuleHandleW(None)
        if wnd_class_ex.hInstance is None:
            raise Exception('Could not get windows module instance.')

        class_atom = win_api_defs.RegisterClassExW(wnd_class_ex)
        if class_atom == 0:
            raise Exception('Could not register the PlyerTaskbar class.')

        self._class_atom = class_atom
        self._wnd_class_ex = wnd_class_ex

        # create window
        self._hwnd = win_api_defs.CreateWindowExW(
            # dwExStyle, lpClassName, lpWindowName, dwStyle
            0,
            class_atom,
            '',
            WS_OVERLAPPED,
            # x, y, nWidth, nHeight
            0,
            0,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            # hWndParent, hMenu, hInstance, lpParam
            None,
            None,
            wnd_class_ex.hInstance,
            None)
        if self._hwnd is None:
            raise Exception('Could not get create window.')
        win_api_defs.UpdateWindow(self._hwnd)

        # load .ICO file for as balloon tip and tray icon
        if app_icon:
            icon_flags = LR_LOADFROMFILE | LR_DEFAULTSIZE
            hicon = win_api_defs.LoadImageW(None, app_icon, IMAGE_ICON, 0, 0,
                                            icon_flags)

            if hicon is None:
                raise Exception('Could not load icon {}'.format(app_icon))
            self._balloon_icon = self._hicon = hicon
        else:
            self._hicon = win_api_defs.LoadIconW(
                None, ctypes.cast(IDI_APPLICATION, win_api_defs.LPCWSTR))

        # show the notification
        self.notify(title, message, app_name)
        if timeout:
            time.sleep(timeout)

    def __del__(self):
        '''
        Clean visible parts of the notification object, then free all resources
        allocated for creating the nofitication Window and icon.
        '''
        self.remove_notify()
        if self._hicon is not None:
            win_api_defs.DestroyIcon(self._hicon)
        if self._wnd_class_ex is not None:
            win_api_defs.UnregisterClassW(self._class_atom,
                                          self._wnd_class_ex.hInstance)
        if self._hwnd is not None:
            win_api_defs.DestroyWindow(self._hwnd)

    def notify(self, title, message, app_name):
        '''
        Displays a balloon in the systray. Can be called multiple times
        with different parameter values.
        '''
        # remove previous visible balloon tip nofitication if available
        self.remove_notify()

        # add icon and messages to window
        hicon = self._hicon
        flags = NIF_TIP | NIF_INFO
        icon_flag = 0

        if hicon is not None:
            flags |= NIF_ICON

            # if icon is default app's one, don't display it in message
            if self._balloon_icon is not None:
                icon_flag = NIIF_USER | NIIF_LARGE_ICON

        notify_data = win_api_defs.get_NOTIFYICONDATAW(
            0, self._hwnd, id(self), flags, 0, hicon, app_name, 0,
            0, message, NOTIFYICON_VERSION_4, title, icon_flag,
            win_api_defs.GUID(), self._balloon_icon)

        self._notify_data = notify_data
        if not win_api_defs.Shell_NotifyIconW(NIM_ADD, notify_data):
            raise Exception('Shell_NotifyIconW failed.')
        if not win_api_defs.Shell_NotifyIconW(NIM_SETVERSION, notify_data):
            raise Exception('Shell_NotifyIconW failed.')

    def remove_notify(self):
        '''
        Removes the notify balloon, if displayed.
        '''
        if self._notify_data is not None:
            win_api_defs.Shell_NotifyIconW(NIM_DELETE, self._notify_data)
            self._notify_data = None
Example #43
0
 def __init__(self, keep_time=10):
     super(TimedCache, self).__init__()
     self._lock = RLock()
     self._cache = {}
     self._keep_time = keep_time  # time in seconds before purging blocks
Example #44
0
# coding: utf-8
import threading
import os

from multiprocessing.pool import ThreadPool
from threading import RLock
from src.job.job import LogType
from src.frp.frp import frp
import traceback

from src.job.remote_script_job import RemoteScriptJob
from src.utils.quick import run_all_arm

log_lock = RLock()


def deploy(park_list):
    all_conns = {
        k: v
        for k, v in {x: frp.get_conn_info(x)
                     for x in park_list}.items() if v is not None
    }

    all_conns = all_conns.values()
    all_conns.reverse()

    pool = ThreadPool(100)

    pool.map(_handle_one_park, all_conns)
    pool.close()
    pool.join()
Example #45
0
class Program(QtWidgets.QApplication, Logger):
    PAGE_SIZE = 100
    BUG_PAGE = "https://github.com/seanegoodwin/pandaviewer/issues"
    THUMB_DIR = Utils.convert_from_relative_lsv_path("thumbs")
    QML_PATH = Utils.convert_from_relative_path("qml/")
    MAX_TAG_RETURN_COUNT = 5
    gallery_lock = RLock()

    class SortMethodMap(Enum):
        NameSort = "sort_name"
        ReadCountSort = "read_count"
        LastReadSort = "last_read"
        RatingSort = "sort_rating"
        DateAddedSort = "time_added"
        FilePathSort = "sort_path"

    def __init__(self, args):
        self.addLibraryPath(Utils.convert_from_relative_path())
        super().__init__(args)
        self.setOrganizationName("PV")
        self.setOrganizationDomain(self.BUG_PAGE)
        self.setApplicationName("PandaViewer")
        self.tags = []  # type: List[str]
        self.pages = [[]]  # type: List[List[GenericGallery]]
        self.galleries = []  # type: List[GenericGallery]
        self.removed_galleries = []  #  type: List[GenericGallery]
        self.version = "0.1"  # Most likely used for db changes only
        self.page_number = 0
        self.search_text = ""

    def setup(self):
        if not os.path.exists(self.THUMB_DIR):
            os.makedirs(self.THUMB_DIR)
        user_database.setup()

        self.qml_engine = QtQml.QQmlApplicationEngine()
        self.qml_engine.addImportPath(self.QML_PATH)
        # self.qml_engine.addPluginPath(self.QML_PATH)
        self.setAttribute(QtCore.Qt.AA_UseOpenGLES, True)
        self.qml_engine.load(os.path.join(self.QML_PATH, "main.qml"))
        self.app_window = self.qml_engine.rootObjects()[0]
        self.app_window.updateGalleryRating.connect(self.update_gallery_rating)
        self.app_window.askForTags.connect(self.get_tags_from_search)
        self.app_window.saveSettings.connect(self.update_config)
        self.app_window.setSortMethod.connect(self.set_sorting)
        self.app_window.setSearchText.connect(self.update_search)
        self.app_window.pageChange.connect(self.switch_page)
        self.app_window.scanGalleries.connect(self.find_galleries)
        self.app_window.openGallery.connect(self.open_gallery)
        self.app_window.openGalleryFolder.connect(self.open_gallery_folder)
        self.app_window.metadataSearch.connect(self.get_metadata)
        self.app_window.removeGallery.connect(self.remove_gallery_by_uuid)
        self.app_window.openOnEx.connect(self.open_on_ex)
        self.app_window.saveGallery.connect(self.save_gallery_customization)
        self.app_window.searchForDuplicates.connect(self.remove_duplicates)
        self.app_window.closedUI.connect(self.close)
        self.app_window.getDetailedGallery.connect(self.get_detailed_gallery)
        self.app_window.getGalleryImageFolder.connect(
            self.get_gallery_image_folder)
        self.app_window.setGalleryImage.connect(self.set_gallery_image)
        self.app_window.setUISort.emit(Config.sort_type,
                                       1 if Config.sort_mode_reversed else 0)
        self.completer_line = QtWidgets.QLineEdit()
        self.completer_line.hide()
        self.setup_completer()
        self.setWindowIcon(
            QtGui.QIcon(Utils.convert_from_relative_path("icon.ico")))
        self.set_ui_config()
        self.app_window.show()

    def setup_completer(self):
        self.completer = QtWidgets.QCompleter(self.tags)
        self.completer.setModelSorting(
            self.completer.CaseInsensitivelySortedModel)
        self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer_line.setCompleter(self.completer)

    def set_sorting(self, sort_type, reversed):
        Config.sort_type = sort_type
        Config.sort_mode_reversed = reversed
        Config.save()
        self.sort()
        self.search()

    def update_search(self, search_text: str):
        self.search_text = search_text
        self.search()

    def set_ui_gallery(self, gallery: GenericGallery):
        self.app_window.setGallery.emit(gallery.ui_uuid, gallery.get_json())

    def remove_gallery_by_uuid(self, uuid: str):
        gallery = self.get_gallery_by_ui_uuid(uuid)
        gallery.mark_for_deletion()  # Also removes
        self.setup_tags()

    def get_detailed_gallery(self, uuid: str):
        self.app_window.openDetailedGallery.emit(
            self.get_gallery_by_ui_uuid(uuid).get_detailed_json())

    def get_tags_from_search(self, search: str):
        self.completer_line.setText(search)
        self.completer.setCompletionPrefix(search)
        tags = []
        for i in range(self.completer.completionCount()):
            self.completer.setCurrentRow(i)
            tags.append(self.completer.currentCompletion())
        tags.sort(key=len)
        self.app_window.setTags(tags[:self.MAX_TAG_RETURN_COUNT])

    def get_gallery_by_ui_uuid(self, uuid: str) -> GenericGallery:
        assert uuid
        return next(g for g in self.current_page if g.ui_uuid == uuid)

    def update_gallery_rating(self, uuid: str, rating: str):
        self.get_gallery_by_ui_uuid(uuid).set_rating(rating)

    def get_gallery_image_folder(self, uuid: str):
        self.app_window.setGalleryImageFolder.emit(
            uuid,
            self.get_gallery_by_ui_uuid(uuid).image_folder)

    def set_gallery_image(self, uuid, image_path):
        self.get_gallery_by_ui_uuid(uuid).set_thumbnail_source(image_path)

    def open_gallery(self, uuid, index):
        self.get_gallery_by_ui_uuid(uuid).open(index)

    def open_random_gallery(self):
        galleries = self.filter_galleries(self.galleries)
        index = randint(0, len(galleries) - 1)
        galleries[index].open()

    def open_gallery_folder(self, uuid):
        self.get_gallery_by_ui_uuid(uuid).open_folder()

    def open_on_ex(self, uuid):
        self.get_gallery_by_ui_uuid(uuid).open_on_ex()

    def save_gallery_customization(self, uuid, gallery):
        self.get_gallery_by_ui_uuid(uuid).save_customization(gallery)

    def exec_(self):
        self.find_galleries(initial=True)
        return super().exec_()

    @property
    def current_page(self) -> List[GenericGallery]:
        return self.pages[self.page_number]

    @property
    def page_count(self) -> int:
        return len(self.pages)

    def set_ui_config(self):
        self.app_window.setSettings(Config.get_ui_config())

    def update_config(self, ui_config):
        if Config.update_from_ui_config(ui_config):
            self.set_auto_metadata_collection()
        self.setup_tags()
        self.search()

    def set_scan_folder(self, folder):
        self.app_window.setScanFolder(folder)

    def set_auto_metadata_collection(self,
                                     galleries: List[GenericGallery] = None):
        galleries = galleries or self.galleries
        for gallery in self.filter_galleries(galleries):
            metadata_changed = False
            matching_dir = Utils.get_parent_folder(Config.folders,
                                                   gallery.folder)
            assert matching_dir
            auto_collection = Config.folder_options.get(matching_dir, {}).get(
                Config.AUTO_METADATA_KEY, True)
            for metadata_name in metadata.MetadataClassMap:
                metadata_entry = gallery.metadata_manager.get_metadata(
                    metadata_name)
                if hasattr(
                        metadata_entry, "auto_collection"
                ) and metadata_entry.auto_collection != auto_collection:
                    metadata_changed = True
                    metadata_entry.auto_collection = auto_collection
            if metadata_changed:
                gallery.save_metadata(update_ui=False)

    def process_search(self,
                       search_text: str) -> (List[str], [List[str], str]):
        search_text = search_text.lower()
        quote_regex = re.compile(r"(-)?\"(.*?)\"")
        filter_regex = re.compile(r"(?:^|\s)\-(.*?)(?=(?:$|\ ))")
        rating_method = re.search(r"rating:(\S*)", search_text)
        if rating_method:
            search_text = re.sub("rating:\S*", "", search_text)
            rating_method = rating_method.groups()[0]
            if rating_method:
                if rating_method[0] == "=" and rating_method[1] != "=":
                    rating_method = "=" + rating_method
                try:
                    eval("0.0" + rating_method)
                except:
                    raise exceptions.InvalidRatingSearch
            else:
                raise exceptions.InvalidRatingSearch
        quoted_words = re.findall(quote_regex, search_text)
        quoted_words = " ".join(
            map(lambda x: "".join(x).replace(" ", "_"), quoted_words))
        search_text = re.sub(quote_regex, "", search_text) + quoted_words
        filter_words = re.findall(filter_regex, search_text)
        words = re.sub(filter_regex, "", search_text).split()
        self.logger.info("Search words: %s" % words)
        self.logger.info("Filter words: %s" % filter_words)
        self.logger.info("Rating function: %s" % rating_method)
        return words, filter_words, rating_method

    def search(self):
        self.logger.info("Search_text: %s" % self.search_text)
        if self.search_text:
            words, filters, rating_method = self.process_search(
                self.search_text)
            galleries = []
            with self.gallery_lock:
                for gallery in self.filter_galleries(self.galleries):
                    if gallery.expired:
                        continue
                    title = gallery.clean_name.lower().split()
                    rating = gallery.metadata_manager.get_value("rating")
                    tags = [
                        t.replace(" ", "_").lower()
                        for t in gallery.metadata_manager.all_tags
                    ]
                    if rating_method and (
                            not rating
                            or not eval(str(rating) + rating_method)):
                        continue
                    if any(self.in_search(tags, title, w) for w in filters):
                        continue
                    if all(self.in_search(tags, title, w)
                           for w in words) or len(words) == 0:
                        galleries.append(gallery)
            self.app_window.setNoSearchResults.emit(not bool(galleries))
            self.setup_pages(galleries)
            self.show_page()
        else:
            self.app_window.setNoSearchResults.emit(False)
            self.setup_pages()
            self.show_page()

    @staticmethod
    def in_search(tags: List[str], title: str, input_tag: str) -> bool:
        input_tag, namespace = Utils.separate_tag(input_tag)
        for title_word in title:
            if input_tag in title_word:
                return True
        for tag in tags:
            tag, tag_namespace = Utils.separate_tag(tag)
            if input_tag in tag and (namespace is None
                                     or tag_namespace == namespace):
                return True
        return False

    def setup_tags(self):
        tags = []
        # tag_count_map = {}
        for gallery in self.filter_galleries(self.galleries):
            new_tags = list(
                set(
                    map(lambda x: x.replace(" ", "_").lower(),
                        gallery.metadata_manager.all_tags)))
            # for tag in new_tags:
            #     tag_count_map[tag] = tag_count_map.get(tag, 0) + 1
            tags += new_tags
        self.tags = list(set(tags))
        for tag in self.tags[:]:
            raw_tag, namespace = Utils.separate_tag(tag)
            if namespace:
                self.tags.append(raw_tag)
        self.tags += list(map(lambda x: "-" + x, self.tags))
        self.tags = list(set(self.tags))
        self.tags.sort()
        self.setup_completer()

    def find_galleries(self, initial: bool = False):
        self.app_window.setScanningMode(True)
        self.logger.debug("Sending start signal to gallery thread")
        folders = Config.folders[:] if not initial else None
        threads.gallery_thread.queue.put(folders)

    def find_galleries_done(self, galleries: List[GenericGallery]):
        self.app_window.setScanningMode(False)
        self.set_auto_metadata_collection(galleries)
        with self.gallery_lock:
            self.galleries += galleries
        self.logger.debug("Gallery thread done")
        self.setup_tags()
        self.sort()
        self.search()

    def send_page(self, reset_scroll=True):
        index_list = (i for i in range(0, self.PAGE_SIZE))
        for gallery in self.current_page:
            self.app_window.setUIGallery.emit(next(index_list),
                                              gallery.get_json(), reset_scroll)
        for i in list(index_list)[::-1]:
            self.app_window.removeUIGallery.emit(i, 1)
        # index_list = list(index_list)
        # if index_list:
        #     self.app_window.removeUIGallery.emit(index_list[0], len(index_list))
        self.garbage_collect()
        threads.image_thread.bg_run_count = 0

    def garbage_collect(self):
        return
        self.qml_engine.clearComponentCache()
        gc.collect()
        self.app_window.garbageCollect()

    def show_page(self, reset_scroll=True):
        need_images = [
            g for g in self.current_page if not g.thumbnail_verified
        ]
        if need_images:
            self.generate_images(need_images)
        else:
            self.send_page(reset_scroll)

    def hide_page(self):
        self.app_window.clearGalleries.emit()

    def generate_images(self, galleries: List[GenericGallery]):
        self.app_window.setScanningMode(True)
        threads.image_thread.queue.put(galleries)

    def image_thread_done(self):
        self.app_window.setScanningMode(False)
        self.logger.debug("Image thread done.")
        self.send_page()

    def get_metadata(self, uuid: str):
        try:
            uuid = int(uuid)
            galleries = [
                g for g in self.filter_galleries(self.galleries)
                if g.metadata_manager.metadata_collection_enabled()
            ]
            if uuid == -1:
                for gallery in galleries:
                    gallery.force_metadata = True
        except ValueError:
            gallery = self.get_gallery_by_ui_uuid(uuid)
            gallery.force_metadata = True
            galleries = [gallery]
        self.logger.debug("Starting metadata thread")
        self.app_window.setSearchMode(True)
        threads.search_thread.queue.put(galleries)

    def set_current_metadata_gallery(self, gallery):
        self.app_window.setCurrentMetadataGallery(gallery.title)

    def update_gallery_metadata(self, gallery: GenericGallery, metadata: dict):
        gallery.update_metadata(metadata)
        gallery.save_metadata()

    def get_metadata_done(self):
        self.app_window.setSearchMode(False)
        self.logger.debug("Metadata thread done.")
        self.setup_tags()

    def remove_duplicates(self):
        self.app_window.setScanningMode(True)
        threads.duplicate_thread.queue.put(
            self.filter_galleries(self.galleries))

    def duplicate_thread_done(self):
        self.app_window.setScanningMode(False)
        self.setup_tags()
        self.sort()

    def close(self):
        try:
            with self.gallery_lock:
                for g in self.galleries:
                    g.release()
            for g in self.removed_galleries:
                g.release()
        except:
            self.logger.error("Failed to complete release, check log",
                              exc_info=True)
        self.quit()

    def sort(self):
        key = attrgetter([t.value
                          for t in self.SortMethodMap][Config.sort_type])
        assert key
        with self.gallery_lock:
            self.galleries.sort(key=key, reverse=Config.sort_mode_reversed)

    def switch_page(self, page_num: int):
        self.page_number = int(page_num) - 1
        self.show_page()
        self.app_window.setPage(self.page_number + 1)

    def filter_galleries(
            self, galleries: List[GenericGallery]) -> List[GenericGallery]:
        return_galleries = []
        for gallery in galleries:
            if gallery.expired:
                continue
            if not any(
                    Utils.path_exists_under_directory(d, gallery.folder)
                    for d in Config.folders):
                continue
            return_galleries.append(gallery)
        return return_galleries

    def setup_pages(self, galleries: List[GenericGallery] = None):
        if galleries is None:  # Need to do it this way because passing in galleries of [] would cause problems
            galleries = self.galleries
        galleries = self.filter_galleries(galleries)
        self.pages = [
            galleries[i:i + self.PAGE_SIZE]
            for i in range(0, len(galleries), self.PAGE_SIZE)
        ] or [[]]
        self.app_window.setPageCount(self.page_count)
        self.page_number = 0
        self.app_window.setPage(self.page_number + 1)

    def remove_gallery_and_recalculate_pages(self,
                                             gallery: GenericGallery,
                                             force_assertion=True):
        gallery_in_galleries = gallery in self.galleries
        if force_assertion:
            assert gallery_in_galleries
        elif not gallery_in_galleries:
            return
        working_page = None
        with self.gallery_lock:
            self.galleries.remove(gallery)
            for i in range(0, self.page_count):
                if gallery in self.pages[i]:
                    self.pages[i].remove(gallery)
                    working_page = i
                    break
            assert working_page is not None
            initial_page_is_displayed = working_page == self.page_number
            while working_page < self.page_count - 1:
                self.pages[working_page].append(self.pages[working_page +
                                                           1].pop(0))
                working_page += 1
            self.pages = [p for p in self.pages if p]
            if self.page_number >= self.page_count:
                self.switch_page(self.page_count)
            elif initial_page_is_displayed:
                self.show_page(reset_scroll=False)
        self.removed_galleries.append(gallery)

    def thread_exception_handler(self, thread, exception):
        self.exception_hook(*exception)

    def exception_hook(self, extype, exvalue, extraceback):
        fatal = True  # Default to true for unhandled exceptions
        if issubclass(extype, exceptions.CustomBaseException):
            fatal = exvalue.fatal
            message = exvalue.msg
        else:
            message = "Sorry, PandaViewer got an unhandled %s exception and must close.\nPlease feel free to submit a bug report" % extype
            self.logger.error("Got unhandled exception",
                              exc_info=(extype, exvalue, extraceback))
        self.app_window.setException(message, fatal)
Example #46
0
 def __init__(self, func, name=None, doc=None):
     self.__name__ = name or func.__name__
     self.__module__ = func.__module__
     self.__doc__ = doc or func.__doc__
     self.func = func
     self.lock = RLock()
Example #47
0
        for name in ('RPiGPIOPin', 'RPIOPin', 'PiGPIOPin', 'NativePin'):
            try:
                return pkg_resources.load_entry_point(dist, group, name)
            except ImportError:
                pass
        raise BadPinFactory('Unable to locate any default pin factory!')
    else:
        for factory in pkg_resources.iter_entry_points(group, name):
            return factory.load()
        raise BadPinFactory('Unable to locate pin factory "%s"' % name)

pin_factory = _default_pin_factory()


_PINS = set()
_PINS_LOCK = RLock() # Yes, this needs to be re-entrant

def _shutdown():
    _threads_shutdown()
    with _PINS_LOCK:
        while _PINS:
            _PINS.pop().close()
    # Any cleanup routines registered by pins libraries must be called *after*
    # cleanup of pin objects used by devices
    _pins_shutdown()

atexit.register(_shutdown)


class GPIOMeta(type):
    # NOTE Yes, this is a metaclass. Don't be scared - it's a simple one.
'''

RETURN = '''
url:
  description:
    - An image file url used to download the file without authentication.
  returned: on success always
  type: str
error_msg:
  description:
    - Its an error message that is returned if there is any exception or error.
  returned: on failure
  type: str
'''

connection_lock = RLock()


class FileSvcUtil(object):  # pragma: no cover
    def __init__(self, authtoken, authurl, user, key, tenant_name,
                 user_domain_name, project_domain_name, auth_version,
                 container_name, temp_url_key, temp_url_key2,
                 connection_retry_count, chosen_temp_url_key):
        """Init routine."""
        self.requests = requests
        self.authurl = authurl
        self.preauthtoken = authtoken
        self.user = user
        self.key = key
        self.auth_version = auth_version
        self.container_name = container_name
Example #49
0
            sd[key] = sd_temp[key]
except IOError:  # If file does not exist, it will be created using defaults.
    with open('./data/sd.json', 'w') as sdf:  # save file
        json.dump(sd, sdf, indent=4, sort_keys=True)

nowt = time.localtime()
now = timegm(nowt)
tz_offset = int(
    time.time() - timegm(time.localtime())
)  # compatible with Javascript (negative tz shown as positive value)
plugin_menu = []  # Empty list of lists for plugin links (e.g. ['name', 'URL'])

srvals = [0] * (sd['nst'])  # Shift Register values
output_srvals = [0] * (sd['nst']
                       )  # Shift Register values last set by set_output()
output_srvals_lock = RLock()
rovals = [0] * sd['nbrd'] * 7  # Run Once durations
snames = station_names()  # Load station names from file
pd = load_programs()  # Load program data from file
plugin_data = {}  # Empty dictionary to hold plugin based global data
ps = []  # Program schedule (used for UI display)
for i in range(sd['nst']):
    ps.append([0, 0])

pon = None  # Program on (Holds program number of a running program)
sbits = [0] * (sd['nbrd'] + 1)  # Used to display stations that are on in UI

rs = []  # run schedule
for j in range(sd['nst']):
    rs.append([
        0, 0, 0, 0
Example #50
0
from threading import Thread
from Queue import Queue
from threading import RLock
from datetime import datetime
from functools import partial

from calibre.ebooks.metadata import title_sort, author_to_author_sort
from calibre.utils.date import parse_date, isoformat, local_tz, UNDEFINED_DATE
from calibre import isbytestring, force_unicode
from calibre.constants import iswindows, DEBUG, plugins
from calibre.utils.icu import sort_key
from calibre import prints

from dateutil.tz import tzoffset

global_lock = RLock()

_c_speedup = plugins['speedup'][0]


def _c_convert_timestamp(val):
    if not val:
        return None
    try:
        ret = _c_speedup.parse_date(val.strip())
    except:
        ret = None
    if ret is None:
        return parse_date(val, as_utc=False)
    year, month, day, hour, minutes, seconds, tzsecs = ret
    try:
Example #51
0
 def __init__(self, file):
     if not file.isatty():
         raise RuntimeError('TTYFileWrapper is supposed to wrap a tty file')
     self.file = file
     self.buffer = ''
     self.lock = RLock()
Example #52
0
 def __init__(self):
     self._context_dict = {}
     self._context_dict_lock = RLock()
     self._context_id_poll = _IdPool(0x10000000)
def get_caching_wrapper(user_function, max_size, ttl, algorithm, thread_safe, order_independent, custom_key_maker):
    """Get a caching wrapper for statistics only, without any actual caching"""

    misses = 0                                          # number of misses of the cache
    lock = RLock() if thread_safe else DummyWithable()  # ensure thread-safe

    def wrapper(*args, **kwargs):
        """The actual wrapper"""
        nonlocal misses
        with lock:
            misses += 1
        return user_function(*args, **kwargs)

    def cache_clear():
        """Clear the cache and statistics information"""
        nonlocal misses
        with lock:
            misses = 0

    def cache_info():
        """
        Show statistics information

        :return: a CacheInfo object describing the cache
        """
        with lock:
            return CacheInfo(0, misses, 0, max_size, algorithm,
                             ttl, thread_safe, order_independent, custom_key_maker is not None)

    def cache_is_empty():
        """Return True if the cache contains no elements"""
        return True

    def cache_is_full():
        """Return True if the cache is full"""
        return True

    def cache_contains_argument(function_arguments, alive_only=True):
        """
        Return True if the cache contains a cached item with the specified function call arguments

        :param function_arguments:  Can be a list, a tuple or a dict.
                                    - Full arguments: use a list to represent both positional arguments and keyword
                                      arguments. The list contains two elements, a tuple (positional arguments) and
                                      a dict (keyword arguments). For example,
                                        f(1, 2, 3, a=4, b=5, c=6)
                                      can be represented by:
                                        [(1, 2, 3), {'a': 4, 'b': 5, 'c': 6}]
                                    - Positional arguments only: when the arguments does not include keyword arguments,
                                      a tuple can be used to represent positional arguments. For example,
                                        f(1, 2, 3)
                                      can be represented by:
                                        (1, 2, 3)
                                    - Keyword arguments only: when the arguments does not include positional arguments,
                                      a dict can be used to represent keyword arguments. For example,
                                        f(a=4, b=5, c=6)
                                      can be represented by:
                                        {'a': 4, 'b': 5, 'c': 6}

        :param alive_only:          Whether to check alive cache item only (default to True).

        :return:                    True if the desired cached item is present, False otherwise.
        """
        return False

    def cache_contains_result(return_value, alive_only=True):
        """
        Return True if the cache contains a cache item with the specified user function return value. O(n) time
        complexity.

        :param return_value:        A return value coming from the user function.

        :param alive_only:          Whether to check alive cache item only (default to True).

        :return:                    True if the desired cached item is present, False otherwise.
        """
        return False

    def cache_for_each(consumer):
        """
        Perform the given action for each cache element in an order determined by the algorithm until all
        elements have been processed or the action throws an error

        :param consumer:            an action function to process the cache elements. Must have 3 arguments:
                                      def consumer(user_function_arguments, user_function_result, is_alive): ...
                                    user_function_arguments is a tuple holding arguments in the form of (args, kwargs).
                                      args is a tuple holding positional arguments.
                                      kwargs is a dict holding keyword arguments.
                                      for example, for a function: foo(a, b, c, d), calling it by: foo(1, 2, c=3, d=4)
                                      user_function_arguments == ((1, 2), {'c': 3, 'd': 4})
                                    user_function_result is a return value coming from the user function.
                                    cache_result is a return value coming from the user function.
                                    is_alive is a boolean value indicating whether the cache is still alive
                                    (if a TTL is given).
        """
        pass

    def cache_arguments():
        """
        Get user function arguments of all alive cache elements

        see also: cache_items()

        Example:
            @cached
            def f(a, b, c, d):
                ...
            f(1, 2, c=3, d=4)
            for argument in f.cache_arguments():
                print(argument)  # ((1, 2), {'c': 3, 'd': 4})

        :return: an iterable which iterates through a list of a tuple containing a tuple (positional arguments) and
                 a dict (keyword arguments)
        """
        yield from ()

    def cache_results():
        """
        Get user function return values of all alive cache elements

        see also: cache_items()

        Example:
            @cached
            def f(a):
                return a
            f('hello')
            for result in f.cache_results():
                print(result)  # 'hello'

        :return: an iterable which iterates through a list of user function result (of any type)
        """
        yield from ()

    def cache_items():
        """
        Get cache items, i.e. entries of all alive cache elements, in the form of (argument, result).

        argument: a tuple containing a tuple (positional arguments) and a dict (keyword arguments).
        result: a user function return value of any type.

        see also: cache_arguments(), cache_results().

        Example:
            @cached
            def f(a, b, c, d):
                return 'the answer is ' + str(a)
            f(1, 2, c=3, d=4)
            for argument, result in f.cache_items():
                print(argument)  # ((1, 2), {'c': 3, 'd': 4})
                print(result)    # 'the answer is 1'

        :return: an iterable which iterates through a list of (argument, result) entries
        """
        yield from ()

    def cache_remove_if(predicate):
        """
        Remove all cache elements that satisfy the given predicate

        :param predicate:           a predicate function to judge whether the cache elements should be removed. Must
                                    have 3 arguments, and returns True or False:
                                      def consumer(user_function_arguments, user_function_result, is_alive): ...
                                    user_function_arguments is a tuple holding arguments in the form of (args, kwargs).
                                      args is a tuple holding positional arguments.
                                      kwargs is a dict holding keyword arguments.
                                      for example, for a function: foo(a, b, c, d), calling it by: foo(1, 2, c=3, d=4)
                                      user_function_arguments == ((1, 2), {'c': 3, 'd': 4})
                                    user_function_result is a return value coming from the user function.
                                    cache_result is a return value coming from the user function.
                                    is_alive is a boolean value indicating whether the cache is still alive
                                    (if a TTL is given).

        :return:                    True if at least one element is removed, False otherwise.
        """
        return False

    # expose operations and members of wrapper
    wrapper.cache_clear = cache_clear
    wrapper.cache_info = cache_info
    wrapper.cache_is_empty = cache_is_empty
    wrapper.cache_is_full = cache_is_full
    wrapper.cache_contains_argument = cache_contains_argument
    wrapper.cache_contains_result = cache_contains_result
    wrapper.cache_for_each = cache_for_each
    wrapper.cache_arguments = cache_arguments
    wrapper.cache_results = cache_results
    wrapper.cache_items = cache_items
    wrapper.cache_remove_if = cache_remove_if
    wrapper._cache = None

    return wrapper
Example #54
0
class MapQueue:
    def __init__(self, iterable=None, maxlen=0):
        self.stack = []
        self.maxlen = maxlen
        if iterable is None:
            self.length = 0
        else:
            self.length = max(len(iterable, maxlen))
            for i in range(self.length):
                self.stack.append(iterable[i])
        self.pending = 0
        self.mainlock = Lock()
        self.innerlock = RLock()

    def qsize(self):
        self.innerlock.acquire()
        try:
            ret = self.length
        finally:
            self.innerlock.release()
        return ret

    def task_done(self):
        self.mainlock.acquire()
        try:
            self.pending -= 1
        finally:
            self.mainlock.release()

    def join(self):
        self.mainlock.acquire()
        try:
            while self.pending > 0:
                self.mainlock.release()
                sleep(0.1)
                self.mainlock.acquire()
        finally:
            self.mainlock.release()

    def get(self, block=True, timeout=None):
        infinite = timeout is None
        timeout = ternary(timeout is None, 0, timeout)
        self.mainlock.acquire()
        if (self.empty()):
            if not block:
                self.mainlock.release()
                raise Queue.Empty
            t = clock()
            curtime = t

            while self.empty() and curtime - t <= timeout:
                self.mainlock.release()
                sleep(0.1)
                curtime = clock()
                self.mainlock.acquire()
                if infinite and self.empty():
                    timeout = curtime + 1

            if (infinite or clock() - t > timeout) and self.empty():
                self.mainlock.release()
                raise Queue.Empty

        ret = self.stack.pop()
        self.pending += 1
        self.length -= 1
        self.mainlock.release()
        return ret

    def get_nowait(self):
        return self.get(False)

    def put(self, item):
        self.mainlock.acquire()
        try:
            self.stack.append(item)
            self.length += 1
        finally:
            self.mainlock.release()

    def empty(self):
        self.innerlock.acquire()
        try:
            ret = self.qsize() == 0
        finally:
            self.innerlock.release()
        return ret
Example #55
0
    def __init__(self):

        #Instagram Account Login Bool
        self.logged_in = False
        #Thread Lock Variable
        self.lock = RLock()
        self.que = Queue()

        #Import Config.ini and Configure
        cwd = os.getcwd()
        platform = sys.platform
        if platform == 'win32' or 'cygwin':
            config_file = (cwd + '\\config.ini')
            self.xmlfile = (cwd + '\\XML.txt')
            CHROME_USERDATA = (cwd + '\\chromeuserdata')
            CHROMEDRIVER_PATH = (cwd + "\\chromedriver.exe")
        else:
            config_file = (cwd + '/config.ini')
            self.xmlfile = (cwd + '/XML.txt')
            CHROME_USERDATA = (cwd + '/chromeuserdata')
            CHROMEDRIVER_PATH = (cwd + "/chromedriver.exe")

        try:
            config = configparser.ConfigParser()
            config.read(config_file)
        except configparser.Error:
            print('''config.ini Error, Please Check File Paths \n 
                    Make sure Chromedriver.exe is in the Current Working Directory and\n
                    Chromedriver and Google Chrome are at compatable versions.'''
                  )

        #Instagram Account Username and Password
        self.username = config['IG_AUTH']['USERNAME']
        self.password = config['IG_AUTH']['PASSWORD']

        #Logger for Instagram Account Actions- Likes, Follows, ect
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
            datefmt='%m-%d %H:%M',
            filename=config['LOGS']['INSTAGRAM'],
            filemode='w')
        self.logger = logging.getLogger(self.username)

        #URL Variables
        self.base_url = config['IG_URLS']['BASE']
        self.login_url = config['IG_URLS']['LOGIN']
        self.nav_url = config['IG_URLS']['NAV_USER']
        self.tag_url = config['IG_URLS']['SEARCH_TAGS']

        #Instagram Variables
        self.tags = config['TAGS']['TAGS_TO_LIKE'].split(',')
        self.max_likes = int(config['VARS']['MAX_LIKES'])
        self.MINIMUM_NUMBER_OF_LIKES = int(
            config['VARS']['MINIMUM_NUMBER_OF_LIKES'])
        self.liked_pictures = 0
        self.new_followers = 0

        #Selenium Driver
        options = webdriver.ChromeOptions()
        options.add_argument('user-data-dir={}'.format(CHROME_USERDATA))
        options.add_argument('--incognito')
        #       options.add_argument('--headless')
        self.driver = webdriver.Chrome(
            executable_path=CHROMEDRIVER_PATH,
            service_log_path=config['LOGS']['CHROME'],
            options=options)

        #Populate Queue with Tags from config.ini
        for item in self.tags:
            self.que.put(item)
Example #56
0
"""
   motorControl.cpp
  
    Created on: 17 mars 2019
        Author: clubRobot
"""

from math import pi
from threading import RLock

VERROU = RLock()
OBSTACLE_DETECTED = False

#Variables de communications avec la Teensy
RADIAN_TO_MSG_ADDER = pi
RADIAN_TO_MSG_FACTOR = 1000
SPEED_ADDER = 1 << 15
XY_ADER = 1 << 15
ANGULAR_SPEED_TO_MSG_FACTOR = (1 << 15) / 30  #max omega=30rad/s
ANGULAR_SPEED_TO_MSG_ADDER = (1 << 15)  #split uint16 in two
SERIAL_BAUDRATE = 115200
SERIAL_PATH = "/dev/ttyAMA0"
TIMEOUT = 0.01
STOP_DELAY = 1
MATCH_DURATION = 100

#Couleurs des côtés du terrain et tirette
UNDEFINED = 2
VIOLET = 1
JAUNE = 0
Example #57
0
 def __init__(self):
     self.lock = RLock()
     self.d = {}
Example #58
0
class PlayerRobot(object):
    """
    Player Robot interface.

    A PlayerRobot instance encapsulates an interface to a Player
    robot. It creates and manages a playerc.client object and a set of
    device proxies wrapped in PlayerDevice objects.  In addition, it
    maintains a run-loop in a separate thread that calls the client's
    .read() method at regular intervals.  The devices are published
    through standard interfaces on the PlayerRobot instance, and their
    methods and properties are synchronized with the run thread
    through a mutex.

    Example:

      # set up a robot object with position, laser, and camera objects
      robot = PlayerRobot(host='myrobot.mydomain.edu',port=6665,
                          devices = [('position2d',0),
                                     ('laser',0),
                                     ('camera',1)])

      # start the run thread, devices will be subscribed
      # automatically.
      robot.start()

      # start the robot turning at 30 deg/sec
      robot.position2d[0].set_cmd_vel(0, 0, 30*pi/180)

      # wait for a while
      time.sleep(5.0)

      # all stop
      robot.position2d[0].set_cmd_vel(0,0,0)

      # shut down the robot's thread, unsubscribing all devices and
      # disconnecting the client
      robot.stop()
    """

    def __init__(self,host='localhost',port=6665,speed=20,
                 devices=[]):

        self._thread = None
        self._running = False
        self._lock = RLock()
        self.speed = speed

        self._client = PlayerClient(playerc.playerc_client(None,host,port),self._lock)

        self._queues_running = False
        self._devices = []
        for devname,devnum in devices:
            self.add_device(devname,devnum=devnum)

    def start(self):
        assert self._thread is None
        self._thread = Thread(target=self.run_loop,name="PlayerRobot Run Loop")
        self._thread.setDaemon(True)
        self._thread.start()

    def stop(self):
        self._running = False
        self._thread.join()
        self._thread = None

    def run_loop(self):
        self._client.connect()
        self._running = True
        self.subscribe_all()
        try:
            while self._running:
                self._client.read()
                if self._queues_running:
                    self.process_queues()
                time.sleep(1.0/self.speed)
        finally:
            self.unsubscribe_all()
            self._client.disconnect()

    def run_queues(self,run_state):
        """
        When using queues for communication with devices, this method
        toggles queue processing.  It is often useful to turn off
        queue processing, e.g. when a client does not plan on using queued
        data for a while.
        """
        self._queues_running = run_state

    def process_queues(self):
        for d in self._devices:
            d.process_queues()

    def subscribe_all(self):
        for dev in self._devices:
            dev.subscribe()

    def unsubscribe_all(self):
        for dev in self._devices:
            dev.unsubscribe()

    def add_device(self,devname,devnum=0):
        if devname not in dir(self):
            setattr(self,devname,{})

        proxy_constr =  getattr(playerc,'playerc_'+devname)
        devtype = device_table.get(devname,PlayerDevice)

        try:
            self._lock.acquire()
            dev = devtype(proxy_constr(self._client.proxy,devnum),self._lock)
        finally:
            self._lock.release()

        self._devices.append(dev)
        getattr(self,devname)[devnum] = dev
        if self._running:
            dev.subscribe()
Example #59
0
 def __init__(self, left=0, right=0):
     self.__left = left
     self.__right = right
     self.__lock = RLock()
Example #60
0
MAX_BLOCK_SIZE = 1000000

COIN = 100000000L # 1 btc in satoshis

# Keep our own socket map for asyncore, so that we can track disconnects
# ourselves (to workaround an issue with closing an asyncore socket when
# using select)
mininode_socket_map = dict()

# One lock for synchronizing all data access between the networking thread (see
# NetworkThread below) and the thread running the test logic.  For simplicity,
# NodeConn acquires this lock whenever delivering a message to to a NodeConnCB,
# and whenever adding anything to the send buffer (in send_message()).  This
# lock should be acquired in the thread running the test logic to synchronize
# access to any data shared with the NodeConnCB or NodeConn.
mininode_lock = RLock()

# Serialization/deserialization tools
def sha256(s):
    return hashlib.new('sha256', s).digest()


def hash256(s):
    return sha256(sha256(s))

def bitglohash(s):
    return bitglo_hash.getPoWHash(s)

def deser_string(f):
    nit = struct.unpack("<B", f.read(1))[0]
    if nit == 253: