class EventManager: """this object is responsible for coordinating most communication between the Model, View, and Controller.""" def __init__(self ): from weakref import WeakKeyDictionary self.listeners = WeakKeyDictionary() self.eventQueue= [] #---------------------------------------------------------------------- def RegisterListener( self, listener ): self.listeners[ listener ] = 1 #---------------------------------------------------------------------- def UnregisterListener( self, listener ): if listener in self.listeners.keys(): del self.listeners[ listener ] #---------------------------------------------------------------------- def Post( self, event ): if not isinstance(event, TickEvent): Debug( " Message: " + event.name ) for listener in self.listeners.keys(): #NOTE: If the weakref has died, it will be #automatically removed, so we don't have #to worry about it. listener.Notify( event )
class EventManager: """ Superclass for all event managers. Keeps a list of listeners and dispatches events to them. """ def __init__(self): self.listeners = WeakKeyDictionary() #-------------------------------------------------------------------------- def register_listener(self,listener): self.listeners[listener] = 1 #-------------------------------------------------------------------------- def unregister_listener(self,listener): if listener in self.listeners.keys(): del self.listeners[listener] #-------------------------------------------------------------------------- def post(self,event): for listener in self.listeners.keys(): listener.notify(event)
class EventManager: """ this class is responsible for coordinating most communication between the Model, View and Controller. I use WeakKeyDictionary to keep track of the registered listeners. From the python documentation: Mapping class that references keys weakly. Entries in the dictionary will be discarded when there is no longer a strong reference to the key. Using this dictionary takes care of object scope, avoiding dispatching to a non existing listener. @todo: I need to implement event categories, avoiding spamming notifies for events that only matters for some listeners. """ def __init__(self): self.listeners = WeakKeyDictionary() def RegisterListener(self, listener): self.listeners[listener] = 1 def UnregisterListener(self, listener): if listener in self.listeners.keys(): del self.listeners[listener] def Post(self, event): for listener in self.listeners.keys(): listener.Notify(event)
class EventMediator: def __init__(self): self.Listeners = WeakKeyDictionary() self.EventQueue = [] def RegisterListener(self, listener): self.Listeners[listener] = 1 def UnregisterListener(self, listener): if listener in self.Listeners.keys(): del self.Listeners[listener] def Post(self, event): if not isinstance(event, TickEvent): self.EventQueue.append(event) else: Events = copy(self.EventQueue) self.EventQueue = [] while len(Events) > 0: ev = Events.pop(0) for listener in self.Listeners.keys(): listener.Notify(ev) for listener in self.Listeners.keys(): listener.Notify(event)
class EventManager: """Event Manager -- coordinate communication between the Model, View, and Controller.""" def __init__(self): self.listeners = WeakKeyDictionary() # self.event_queue= [] def register_listener(self, listener): self.listeners[listener] = True def UnregisterListener(self, listener): if listener in self.listeners.keys(): del self.listeners[listener] def post(self, ev): if not (isinstance(ev, TickEvent) or \ isinstance(ev, StepEvent) or \ isinstance(ev, HelpEvent) or \ isinstance(ev, ToggleViewEvent) or \ isinstance(ev, ToggleAutoEvent) or \ isinstance(ev, BusyEvent) or \ isinstance(ev, ReadyEvent)): debug(" ** " + ev.name) for listener in self.listeners.keys(): # If the weakref has died, remove it and continue # through the list if listener is None: del self.listeners[listener] continue listener.notify(ev)
class EventManager(object): def __init__(self): from weakref import WeakKeyDictionary self.listeners = WeakKeyDictionary() def register_listener(self, listener): """ Register new listener """ self.listeners[listener] = 1 def unregister_listener(self, listener): """ Unregister existing listener """ if listener in self.listeners.keys(): del self.listeners[listener] def post(self, event): """ Post new event """ for listener in self.listeners.keys(): listener.notify(event)
class ShapeManager( object ): def __init__( self, canvas ): self.canvasItemToPartMap = WeakKeyDictionary() self.canvasItemToShapeMap = WeakKeyDictionary() self.canvas = canvas def associateCanvasItemWithShape( self, canvasItem, shape ): self.canvasItemToShapeMap[ canvasItem ] = ref( shape ) def associateCanvasItemWithPart( self, canvasItem, part ): self.canvasItemToPartMap[ canvasItem ] = mrt def getPartByCanvasItem( self, canvasItem ): return self.canvasItemToPartMap.get( canvasItem, None ) def getShapeByCanvasItem( self, canvasItem ): r = self.canvasItemToShapeMap.get( canvasItem, None ) if r != None: return r() return None def createComplexShape( self, molds, modelObject = None ): return ComplexShape( self, self.canvas, molds, modelObject ) def createConnector( self, modelObject = None ): return Connector( self, self.canvas, modelObject )
class FrameRegistry(object): """ This class implements a method to keep track of wrapped frames. In short, Qt will give us frames that are QWebFrames, and we want to only deal with WebFrames that we control. This registry will either wrap a given frame, or return the previously-wrapped one from the registry. """ def __init__(self, klass, *args, **kwargs): self._registry = WeakKeyDictionary() self.klass = klass self.args = args self.kwargs = kwargs def wrap(self, frame): if frame is None: return None existing = self._registry.get(frame) if existing is not None: return existing # Create web frame, passing the underlying value, and ourselves. new = self.klass(frame, self, *self.args, **self.kwargs) self._registry[frame] = new return new def clear(self): self._registry.clear()
class EventHandler: """this object is responsible for coordinating most communication between the Model, View, and Controller. """ def __init__(self ): from weakref import WeakKeyDictionary self.listeners = WeakKeyDictionary() #---------------------------------------------------------------------- def register_listener( self, listener ): self.listeners[ listener ] = 1 #---------------------------------------------------------------------- def unregister_listener( self, listener ): if listener in self.listeners.keys(): del self.listeners[ listener ] #---------------------------------------------------------------------- def post( self, event ): """Post a new event. It will be broadcast to all listeners""" for listener in self.listeners.keys(): #NOTE: If the weakref has died, it will be #automatically removed, so we don't have #to worry about it. listener.notify( event )
class LazyInvalidation(object): def __enter__(self): assert threading.current_thread() == MAIN_THREAD assert not evaluation_stack self._watchMap = WeakKeyDictionary() self._watchable_objects = WeakSet() global invalidation_strategy invalidation_strategy._invalidate_all() invalidation_strategy = self def _watch_object(self, object): if object.watcher is not None and object.watcher not in self._watchMap: self._watchMap[object.watcher] = WeakWatchIntermediary(object, object.watcher) def _add_dependency(self, object): if evaluation_stack: evaluation_stack[-1].deps.append(object) def _unwatch_object(self, object): object.invalidate() self._watchable_objects.discard(object) def __exit__(self, type, value, traceback): global invalidation_strategy invalidation_strategy = LazyConstants() for intermediary in self._watchMap.itervalues(): intermediary.release() self._watchMap.clear() def _invalidate_all(self): raise TypeError('Cannot nest lazy_invalidation contexts')
class EventManager(object): """ We coordinate communication between the Model, View, and Controller. """ def __init__(self): from weakref import WeakKeyDictionary self.listeners = WeakKeyDictionary() def register_listener(self, listener): """ Adds a listener to our spam list. It will receive post()ed events through it's notify(event) call. """ self.listeners[listener] = 1 def unregister_listener(self, listener): """ Remove a listener from our spam list. This is implemented but hardly used. Our weak ref spam list will auto remove any listeners who stop existing. """ if listener in self.listeners.keys(): del self.listeners[listener] def post(self, event): """ Post a new event to the message queue. It will be broadcast to all listeners. """ if not isinstance(event, TickEvent) and \ not isinstance(event, CharUpdateEvent): print(str(event)) # print the event (unless it is TickEvent) for listener in self.listeners.keys(): listener.notify(event)
def __init__(self, latency): self.latency = latency self.mounts = WeakKeyDictionary() self.contexts = WeakSet() self.lock = RLock() self.cell_updates = deque() self._tick = Event() self.paths = WeakKeyDictionary()
class Signal(object): """ Simple class to emit signals to connected callable receivers. """ def __init__(self): """ Instantiate a new object """ self.funcs = WeakSet() self.meths = WeakKeyDictionary() def connect(self, c): """ Connect a callable as receiver for the signal @param c: signal receiver @type c: Callable """ if inspect.ismethod(c): if c.__self__ not in self.meths: self.meths[c.__self__] = set() self.meths[c.__self__].add(c.__func__) else: if c not in self.funcs: self.funcs.add(c) def disconnect(self, c): """ Disconnect the callable from receiving the signal @param c: signal receiver @type c: Callable """ if inspect.ismethod(c): if c.__self__ in self.meths: self.meths[c.__self__].remove(c.__func__) else: if c in self.funcs: self.funcs.remove(c) def disconnectAll(self): """ Disconnects all signal receivers """ self.funcs.clear() self.meths.clear() def emit(self, *args, **kwargs): """ Fires the signal to all connected receivers """ for c in self.funcs: c(*args, **kwargs) for obj, funcs in self.meths.items(): for func in funcs: func(obj, *args, **kwargs)
def __init__(self, type, default=None, nillable=False): if default is None and not nillable: raise TypeError("default must be specified if object is not nillable") self.type = type self.default = default self.nillable = nillable self.values = WeakKeyDictionary() self.oldvalues = WeakKeyDictionary() self.dirty = WeakKeyDictionary()
class EventManager: """this object is responsible for coordinating most communication between the Model, View, and Controller.""" def __init__(self, initlist=None ): self.listeners = WeakKeyDictionary() self.eventQueue = [] self.__lock = threading.Lock() #---------------------------------------------------------------------- def RegisterListener( self, listener , eventList): #if not hasattr( listener, "Notify" ): raise blah blah... self.listeners[ listener ] = eventList def addListener(self, listener, eventList): if self.listeners.has_key( listener ): self.listeners[ listener ].append( eventList ) else: self.listeners[ listener ] = eventList #---------------------------------------------------------------------- def UnregisterListener( self, listener ): if listener in self.listeners.keys(): del self.listeners[ listener ] #---------------------------------------------------------------------- def Post( self, event ): if event==OneSecondEvent: self.sendEvent( event ) if not event==TickEvent: self.__lock.acquire() self.eventQueue.append( event ) self.__lock.release() else: self.flushEvents() #at the end, notify listeners of the Tick event for listener in self.listeners.keys(): listener.Notify( event ) def flushEvents(self): if self.eventQueue: for k in range(len(self.eventQueue)): ev = self.eventQueue.pop(0) self.sendEvent(ev) def sendEvent(self, ev): for listener in self.listeners.keys(): throwable_events = self.listeners[listener] if ev in throwable_events: listener.Notify( ev )
def _init_descriptor_environment(self): """There's no way to distinguis between description and simple generator mode, so the initialization of the necessary per-instance mappings is done at the first __get__ execution. """ self._dep = WeakKeyDictionary() self._value = WeakKeyDictionary() self._comp = WeakKeyDictionary() self._descriptor_initialized = True
def __init__(self, default_index=0, choices=None, display_func=None, **kwargs): if choices is not None and 'default' not in kwargs: kwargs['default'] = choices[default_index] super(SelectionCallbackProperty, self).__init__(**kwargs) self.default_index = default_index self.default_choices = choices or [] self._default_display_func = display_func self._choices = WeakKeyDictionary() self._display = WeakKeyDictionary() self._force_next_sync = WeakKeyDictionary()
def __init__(self, id, name, parent, user, priority, dispatchKey, maxRN, creationTime=None, startTime=None, updateTime=None, endTime=None, status=NODE_READY): ''' Base class for each node in dispatcher tree structure. Holds main model fields. :param id int: unique id for this node :param name str: a short string describing this node :param parent: a FolderNode or None if this node is a root node :param priority int: priority value :param dispatchKey int: dispatchKey value :param maxRN int: maximum number of render nodes that can be allocated to this tree node :param creationTime: timestamp indicating when the node was created :param startTime: timestamp indicating when the node was started :param updateTime: timestamp indicating when the node was updated :param endTime: timestamp indicating when the node was ended :param status int: current node's status ''' if not self.dispatcher: from octopus.dispatcher.dispatcher import Dispatcher self.dispatcher = Dispatcher(None) self.__dict__['parent'] = None models.Model.__init__(self) self.id = int(id) if id is not None else None self.name = str(name) self.parent = parent self.user = str(user) self.priority = int(priority) self.dispatchKey = int(dispatchKey) self.maxRN = int(maxRN) self.optimalMaxRN = 0 self.allocatedRN = 0 self.poolShares = WeakKeyDictionary() self.additionnalPoolShares = WeakKeyDictionary() self.completion = 1.0 self.status = status self.creationTime = time() if not creationTime else creationTime self.startTime = startTime self.updateTime = updateTime self.endTime = endTime self.dependencies = [] self.reverseDependencies = [] self.lastDependenciesSatisfaction = False self.lastDependenciesSatisfactionDispatchCycle = -1 self.readyCommandCount = 0 self.doneCommandCount = 0 self.commandCount = 0 self.averageTimeByFrameList = [] self.averageTimeByFrame = 0.0 self.minTimeByFrame = 0.0 self.maxTimeByFrame = 0.0 self.timer = None
class Signal(object): def __init__(self): self._functions = WeakSet() self._methods = WeakKeyDictionary() def __call__(self, *args, **kargs): # Call handler functions to_be_removed = [] for func in self._functions.copy(): try: func(*args, **kargs) except RuntimeError: Warning.warn('Signals func->RuntimeError: func "{}" will be removed.'.format(func)) to_be_removed.append(func) for remove in to_be_removed: self._functions.discard(remove) # Call handler methods to_be_removed = [] emitters = self._methods.copy() for obj, funcs in emitters.items(): msg_debug('obj is type "{}"'.format(type(obj))) for func in funcs.copy(): try: func(obj, *args, **kargs) except RuntimeError: warnings.warn('Signals methods->RuntimeError, obj.func "{}.{}" will be removed'.format(obj, func)) to_be_removed.append((obj, func)) for obj, func in to_be_removed: self._methods[obj].discard(func) def connect(self, slot): if inspect.ismethod(slot): if slot.__self__ not in self._methods: self._methods[slot.__self__] = set() self._methods[slot.__self__].add(slot.__func__) else: self._functions.add(slot) def disconnect(self, slot): if inspect.ismethod(slot): if slot.__self__ in self._methods: self._methods[slot.__self__].remove(slot.__func__) else: if slot in self._functions: self._functions.remove(slot) def clear(self): self._functions.clear() self._methods.clear()
class Signal(object): def __init__(self): self._functions = WeakSet() self._methods = WeakKeyDictionary() self._activated = True def __call__(self, *args, **kargs): # call connected functions only if activated if self._activated: # Call handler functions for func in self._functions: func(*args, **kargs) # Call handler methods for obj, funcs in self._methods.items(): for func in funcs: func(obj, *args, **kargs) def connect(self, slot): if inspect.ismethod(slot): if slot.__self__ not in self._methods: self._methods[slot.__self__] = set() self._methods[slot.__self__].add(slot.__func__) else: self._functions.add(slot) def disconnect(self, slot): if inspect.ismethod(slot): if slot.__self__ in self._methods: self._methods[slot.__self__].remove(slot.__func__) else: if slot in self._functions: self._functions.remove(slot) def clear(self): self._functions.clear() self._methods.clear() def activate(self): """ Activate the signal to emit. """ self._activated = True def deactivate(self): """ Deactivate the signal to emit. """ self._activated = False
class EventManager: def __init__(self): from weakref import WeakKeyDictionary self.listeners = WeakKeyDictionary() def RegisterListener(self, listener): self.listeners[listener] = 1 def UnregisterListener(self, listener): if listener in self.listeners.keys(): del self.listeners[ listener ] def Post(self, event): for listener in self.listeners.keys(): listener.Notify( event )
def __init__(self, tracked_cls): WeakKeyDictionary.__init__(self) self.tracked_cls = tracked_cls # Prepend Announcer if not present in class hierarchy if Announcer not in tracked_cls.__bases__: if '__new__' in tracked_cls.__dict__.keys(): tracked_cls.__new__ = AnnouncingClassMethod(tracked_cls.__new__) tracked_cls.__bases__ = (Announcer,)+tracked_cls.__bases__ # listen for new instances tracked_cls.__new__.listen(self.receive)
class EventManager(object): def __init__(self): self.listeners = WeakKeyDictionary() def register_listener(self, listener): self.listeners[listener] = 1 def unregister_listener(self, listener): if listener in self.listeners.keys(): del self.listeners[listener] def post(self, event): for listener in self.listeners.keys(): listener.notify(event)
class EventManager: """this object is responsible for coordinating most communication between the Model, View, and Controller.""" def __init__(self ): self.listeners = WeakKeyDictionary() self.eventQueue= [] #---------------------------------------------------------------------- def RegisterListener( self, listener ): #if not hasattr( listener, "Notify" ): raise blah blah... self.listeners[ listener ] = 1 #---------------------------------------------------------------------- def UnregisterListener( self, listener ): if listener in self.listeners.keys(): del self.listeners[ listener ] #---------------------------------------------------------------------- def Post( self, event ): from copy import copy if not isinstance(event, TickEvent): self.eventQueue.append( event ) else: events = copy( self.eventQueue ) self.eventQueue = [] while len(events) > 0: ev = events.pop(0) #self.Debug( ev ) for listener in self.listeners.keys(): listener.Notify( ev ) #at the end, notify listeners of the Tick event for listener in self.listeners.keys(): listener.Notify( event ) def Send( self, event ): for listener in self.listeners.keys(): listener.Notify( event ) def SendAll( self ): from copy import copy events = copy( self.eventQueue ) self.eventQueue = [] while len(events) > 0: ev = events.pop(0) #self.Debug( ev ) for listener in self.listeners.keys(): listener.Notify( ev )
def weakref_test(): print('weakref test begin') cache = WeakKeyDictionary() a = g() print(sys.getrefcount(a)) print(sys.getrefcount(a)) b = ref(a) cache[a] = 1 print(cache.keyrefs()) print(sys.getrefcount(a)) del a print(cache.keyrefs()) print(sys.getrefcount(b())) print(cache[b()])
class SettingsObjectID(object): """ Simple descriptor used for SettingsObject subclasses which have dynamic ids. """ def __init__(self, type): self.type = type self.values = WeakKeyDictionary() self.oldvalues = WeakKeyDictionary() self.dirty = WeakKeyDictionary() def __get__(self, obj, objtype): if obj is None: return self try: return self.values[obj] except KeyError: raise AttributeError("SettingsObject ID has not been defined") def __set__(self, obj, value): if not isinstance(value, self.type): value = self.type(value) # check whether the old value is the same as the new value if obj in self.values and self.values[obj] == value: return self.dirty[obj] = obj in self.values self.oldvalues[obj] = self.values.get(obj, value) self.values[obj] = value def isdirty(self, obj): """ Returns True if the ID has been changed on the specified configuration object. """ return self.dirty.get(obj, False) def clear_dirty(self, obj): """ Clears the dirty flag for the ID on the specified configuration object. """ del self.dirty[obj] self.oldvalues[obj] = self.values[obj] def get_old(self, obj): return self.oldvalues.get(obj, None) def undo(self, obj): if obj in self.oldvalues: self.dirty.pop(obj, None) self.values[obj] = self.oldvalues[obj]
class PropertyDescriptor: ''' Base Property Descriptor ''' def __init__(self): self.data = WeakKeyDictionary() def __get__(self, instance, type=None): return self.data.get(instance) def __set__(self, instance, value): self.data[instance] = value def __delete__(self, instance): if self.data.get(instance): del self.data[instance]
def __init__(self): self.listeners = {} self.cell_aliases = {} self.cell_rev_aliases = {} self.macro_listeners = {} self.observers = {} self.registrar_listeners = WeakKeyDictionary() self.rev_registrar_listeners = WeakKeyDictionary() self.pin_to_cells = {} self.cells = WeakValueDictionary() self.cell_to_output_pin = WeakKeyDictionary() self._childids = WeakValueDictionary() self.registrar_items = [] self.unstable_workers = WeakSet() super().__init__()
class Signal(object): def __init__(self): # The original implementation used WeakSet to store functions, # but that causes lambdas without any other reference to be # garbage collected. So we use a normal set to avoid that. self._functions = set() self._methods = WeakKeyDictionary() # The original implementation used __call__, so one would just call the signal itself: # # my_signal("foo") # # This has been changed to the emit() method to both be more consistent with how signals/slots # work in Qt & GTK and to make it more easily apparent that a signal is being triggered. # The correct way to trigger a signal is therefore: # # my_signal.emit("foo") def emit(self, *args, **kargs): # Call handler functions for func in self._functions: func(*args, **kargs) # Call handler methods for obj, funcs in self._methods.items(): for func in funcs: func(obj, *args, **kargs) def connect(self, slot): if inspect.ismethod(slot): if slot.__self__ not in self._methods: self._methods[slot.__self__] = set() self._methods[slot.__self__].add(slot.__func__) else: self._functions.add(slot) def disconnect(self, slot): if inspect.ismethod(slot): if slot.__self__ in self._methods: self._methods[slot.__self__].remove(slot.__func__) else: if slot in self._functions: self._functions.remove(slot) def clear(self): self._functions.clear() self._methods.clear()
def __init__(self, iterable=None): MutableSet.__init__(self) self.end = end = [] end += [None, end, end] # sentinel node for doubly linked list self.map = WeakKeyDictionary() # key --> [key, prev, next] if iterable is not None: self |= iterable
def __init__(self, *args, **kwargs): self.views = WeakKeyDictionary() self.positions = WeakKeyDictionary() self.home_views = WeakKeyDictionary() ToolBase.__init__(self, *args, **kwargs)
class SelectionCallbackProperty(CallbackProperty): def __init__(self, default_index=0, choices=None, display_func=None, **kwargs): if choices is not None and 'default' not in kwargs: kwargs['default'] = choices[default_index] super(SelectionCallbackProperty, self).__init__(**kwargs) self.default_index = default_index self.default_choices = choices or [] self._default_display_func = display_func self._choices = WeakKeyDictionary() self._display = WeakKeyDictionary() self._force_next_sync = WeakKeyDictionary() def __set__(self, instance, value): if value is not None: choices = self.get_choices(instance) # For built-in scalar types we use ==, and for other types we use # is, otherwise e.g. ComponentID returns something that evaluates # to true when using ==. if ((np.isscalar(value) and not any(value == x for x in choices)) or (not np.isscalar(value) and not any(value is x for x in choices))): raise ValueError( 'value {0} is not in valid choices: {1}'.format( value, choices)) super(SelectionCallbackProperty, self).__set__(instance, value) def force_next_sync(self, instance): self._force_next_sync[instance] = True def _get_full_info(self, instance): if self._force_next_sync.get(instance, False): try: return self.__get__(instance), random.random() finally: self._force_next_sync[instance] = False else: return self.__get__(instance), self.get_choices( instance), self.get_choice_labels(instance) def get_display_func(self, instance): return self._display.get(instance, self._default_display_func) def set_display_func(self, instance, display): self._display[instance] = display # selection = self.__get__(instance) # self.notify(instance, selection, selection) def get_choices(self, instance): return self._choices.get(instance, self.default_choices) def get_choice_labels(self, instance): display = self._display.get(instance, str) labels = [] for choice in self.get_choices(instance): if isinstance(choice, ChoiceSeparator): labels.append(str(choice)) else: labels.append(display(choice)) return labels def set_choices(self, instance, choices): self._choices[instance] = choices self._choices_updated(instance, choices) selection = self.__get__(instance) self.notify(instance, selection, selection) def _choices_updated(self, instance, choices): if not choices: self.__set__(instance, None) return selection = self.__get__(instance) # We do the following because 'selection in choice' actually compares # equality not identity (and we really just care about identity here) for choice in choices: if selection is choice: return choices_without_separators = [ choice for choice in choices if not isinstance(choice, ChoiceSeparator) ] if choices_without_separators: try: selection = choices_without_separators[self.default_index] except IndexError: if self.default_index > 0: selection = choices_without_separators[-1] else: selection = choices_without_separators[0] else: selection = None self.__set__(instance, selection)
if current_session: current_session.data.update(data or {}) current_session.save() cookie_value = itsdangerous.Signer(settings.SECRET_KEY).sign(current_session._id) else: session_id = str(bson.objectid.ObjectId()) session = Session(_id=session_id, data=data or {}) session.save() cookie_value = itsdangerous.Signer(settings.SECRET_KEY).sign(session_id) set_session(session) if response is not None: response.set_cookie(settings.COOKIE_NAME, value=cookie_value, domain=settings.OSF_COOKIE_DOMAIN) return response sessions = WeakKeyDictionary() session = LocalProxy(get_session) # Request callbacks # NOTE: This gets attached in website.app.init_app to ensure correct callback # order def before_request(): from framework.auth import cas # Central Authentication Server Ticket Validation and Authentication ticket = request.args.get('ticket') if ticket: service_url = furl.furl(request.url) service_url.args.pop('ticket') # Attempt autn wih CAS, and return a proper redirect response
from __future__ import annotations import enum from typing import TYPE_CHECKING from weakref import WeakKeyDictionary from pywayland.server import Signal from wlroots import ffi, Ptr, PtrHasData, lib from .output import Output from .surface import Surface if TYPE_CHECKING: from pywayland.server import Display _weakkeydict: WeakKeyDictionary = WeakKeyDictionary() class ForeignToplevelHandleV1State(enum.IntEnum): MAXIMIZED = 1 << 0 MINIMIZED = 1 << 1 ACTIVATED = 1 << 2 FULLSCREEN = 1 << 3 class ForeignToplevelManagerV1(PtrHasData): def __init__(self, ptr) -> None: """An foreign toplevel manager: wlr_foreign_toplevel_manager_v1.""" self._ptr = ffi.cast("struct wlr_foreign_toplevel_manager_v1 *", ptr) self.destroy_event = Signal(
def __init__(self): """ Configure a new instances of the descriptor """ self._instance_data = WeakKeyDictionary()
def isasyncgen(obj): return False def isasyncgenfunction(func): return False # Python 3.8+ try: from typing import ForwardRef evaluate_forwardref = ForwardRef._evaluate except ImportError: from typing import _ForwardRef as ForwardRef evaluate_forwardref = ForwardRef._eval_type _type_hints_map = WeakKeyDictionary( ) # type: Dict[FunctionType, Dict[str, Any]] _functions_map = WeakValueDictionary() # type: Dict[CodeType, FunctionType] _missing = object() T_CallableOrType = TypeVar('T_CallableOrType', bound=Callable[..., Any]) class ForwardRefPolicy(Enum): """Defines how unresolved forward references are handled.""" ERROR = 1 #: propagate the :exc:`NameError` from :func:`~typing.get_type_hints` WARN = 2 #: remove the annotation and emit a TypeHintWarning #: replace the annotation with the argument's class if the qualified name matches, else remove #: the annotation GUESS = 3
def __init__(self, cls): self.cls = cls self.values = WeakKeyDictionary()
def __init__(self, parent): super(_WidgetListDelegate, self).__init__(parent=parent) self.__editors = WeakValueDictionary() self.__sizes = WeakKeyDictionary() self.__size_hints = WeakKeyDictionary()
super().test_part_separation() def test_hashes(self): actual = [ self.function(request, **kwargs) for request, _, kwargs in self.known_hashes ] expected = [ bytes.fromhex(_fingerprint) for _, _fingerprint, _ in self.known_hashes ] self.assertEqual(actual, expected) _fingerprint_cache_2_6: Mapping[Request, Tuple[None, bool]] = WeakKeyDictionary() def request_fingerprint_2_6(request, include_headers=None, keep_fragments=False): if include_headers: include_headers = tuple( to_bytes(h.lower()) for h in sorted(include_headers)) cache = _fingerprint_cache_2_6.setdefault(request, {}) cache_key = (include_headers, keep_fragments) if cache_key not in cache: fp = sha1() fp.update(to_bytes(request.method)) fp.update( to_bytes(
def __init__(self, settings): self.always_store = settings.getbool('HTTPCACHE_ALWAYS_STORE') self.ignore_schemes = settings.getlist('HTTPCACHE_IGNORE_SCHEMES') self.ignore_response_cache_controls = [to_bytes(cc) for cc in settings.getlist('HTTPCACHE_IGNORE_RESPONSE_CACHE_CONTROLS')] self._cc_parsed = WeakKeyDictionary()
class _TypeClass( # noqa: WPS214 Generic[_InstanceType, _SignatureType, _AssociatedType, _Fullname], ): """ That's how we represent typeclasses. You probably don't need to use this type directly, use its public methods and public :func:`~typeclass` constructor. """ __slots__ = ( '_signature', '_associated_type', '_instances', '_protocols', '_dispatch_cache', '_cache_token', ) _dispatch_cache: Dict[type, Callable] _cache_token: Optional[object] def __init__( self, signature: _SignatureType, associated_type=None, ) -> None: """ Protected constructor of the typeclass. Use public :func:`~typeclass` constructor instead. How does this magic work? It heavily relies on custom ``mypy`` plugin. Without it - it is just a nonsense. The logic is quite unusual. We use "mypy-plugin-time" variables to construct a typeclass. What variables we use and why? - ``_TypeclassType`` is a type variable that indicates what type can be passed into this typeclass. This type is updated each time we call ``.instance``, because that how we introduce new types to the typeclass - ``_ReturnType`` is used to enforce the same return type for all cases. Only modified once during ``@typeclass`` creation - ``_SignatureType`` is used to ensure that all parameters for all type cases are the same. That's how we enforce consistency in all function signatures. The only exception is the first argument: it is polymorfic. """ self._instances: Dict[type, Callable] = {} self._protocols: Dict[type, Callable] = {} # We need this for `repr`: self._signature = signature self._associated_type = associated_type # Cache parts: self._dispatch_cache = WeakKeyDictionary() # type: ignore self._cache_token = None def __call__( self, instance: Union[ # type: ignore _InstanceType, Supports[_AssociatedType], ], *args, **kwargs, ) -> _ReturnType: """ We use this method to actually call a typeclass. The resolution order is the following: 1. Exact types that are passed as ``.instance`` arguments 2. Protocols that are passed with ``is_protocol=True`` We don't guarantee the order of types inside groups. Use correct types, do not rely on our order. .. rubric:: Callbacks Since, we define ``__call__`` method for this class, it can be used and typechecked everywhere, where a regular ``Callable`` is expected. .. code:: python >>> from typing import Callable >>> from classes import typeclass >>> @typeclass ... def used(instance, other: int) -> int: ... '''Example typeclass to be used later.''' >>> @used.instance(int) ... def _used_int(instance: int, other: int) -> int: ... return instance + other >>> def accepts_typeclass( ... callback: Callable[[int, int], int], ... ) -> int: ... return callback(1, 3) >>> assert accepts_typeclass(used) == 4 Take a note, that we use structural subtyping here. And all typeclasses that match ``Callable[[int, int], int]`` signature will typecheck. """ self._control_abc_cache() instance_type = type(instance) try: impl = self._dispatch_cache[instance_type] except KeyError: impl = self._dispatch( instance, instance_type, ) or self._default_implementation self._dispatch_cache[instance_type] = impl return impl(instance, *args, **kwargs) def __str__(self) -> str: """Converts typeclass to a string.""" associated_type = (': "{0}"'.format(self._associated_type.__qualname__) if self._associated_type else '') return '<typeclass "{0}"{1}>'.format( self._signature.__name__, associated_type, ) def supports( self, instance, ) -> TypeGuard[_InstanceType]: """ Tells whether a typeclass is supported by a given type. .. code:: python >>> from classes import typeclass >>> @typeclass ... def example(instance) -> str: ... '''Example typeclass.''' >>> @example.instance(int) ... def _example_int(instance: int) -> str: ... return 'Example: {0}'.format(instance) >>> assert example.supports(1) is True >>> assert example.supports('a') is False It also works with protocols: .. code:: python >>> from typing import Sized >>> @example.instance(Sized, is_protocol=True) ... def _example_sized(instance: Sized) -> str: ... return 'Size is {0}'.format(len(instance)) >>> assert example.supports([1, 2]) is True >>> assert example([1, 2]) == 'Size is 2' We also use new ``TypeGuard`` type to ensure that type is narrowed when ``.supports()`` is used: .. code:: python some_var: Any if my_typeclass.supports(some_var): reveal_type(some_var) # Revealed type is 'Supports[MyTypeclass]' See also: https://www.python.org/dev/peps/pep-0647 """ self._control_abc_cache() instance_type = type(instance) if instance_type in self._dispatch_cache: return True # This only happens when we don't have a cache in place: impl = self._dispatch(instance, instance_type) if impl is None: self._dispatch_cache[instance_type] = self._default_implementation return False self._dispatch_cache[instance_type] = impl return True def instance( self, type_argument: Optional[_NewInstanceType], *, is_protocol: bool = False, ) -> '_TypeClassInstanceDef[_NewInstanceType, _TypeClassType]': """ We use this method to store implementation for each specific type. The only setting we provide is ``is_protocol`` which is required when passing protocols. See our ``mypy`` plugin for that. """ if type_argument is None: # `None` is a special case type_argument = type(None) # type: ignore # That's how we check for generics, # generics that look like `List[int]` or `set[T]` will fail this check, # because they are `_GenericAlias` instance, # which raises an exception for `__isinstancecheck__` isinstance(object(), type_argument) # type: ignore def decorator(implementation): container = self._protocols if is_protocol else self._instances container[type_argument] = implementation # type: ignore if self._cache_token is None: # pragma: no cover if getattr(type_argument, '__abstractmethods__', None): self._cache_token = get_cache_token() self._dispatch_cache.clear() return implementation return decorator def _control_abc_cache(self) -> None: """ Required to drop cache if ``abc`` type got new subtypes in runtime. Copied from ``cpython``. """ if self._cache_token is not None: current_token = get_cache_token() if self._cache_token != current_token: self._dispatch_cache.clear() self._cache_token = current_token def _dispatch(self, instance, instance_type: type) -> Optional[Callable]: """ Dispatches a function by its type. How do we dispatch a function? 1. By direct ``instance`` types 2. By matching protocols 3. By its ``mro`` """ implementation = self._instances.get(instance_type, None) if implementation is not None: return implementation for protocol, callback in self._protocols.items(): if isinstance(instance, protocol): return callback return _find_impl(instance_type, self._instances) def _default_implementation(self, instance, *args, **kwargs) -> NoReturn: """By default raises an exception.""" raise NotImplementedError( 'Missing matched typeclass instance for type: {0}'.format( type(instance).__qualname__, ), )
def __init__(self, default: Callable[[], _StateType]): self._default = default self._state: WeakKeyDictionary[Thread, _StateType] = WeakKeyDictionary()
def singledispatch(func): """Single-dispatch generic function decorator. Transforms a function into a generic function, which can have different behaviours depending upon the type of its first argument. The decorated function acts as the default implementation, and additional implementations can be registered using the register() attribute of the generic function. """ registry = {} dispatch_cache = WeakKeyDictionary() cache_token = None def dispatch(cls): """generic_func.dispatch(cls) -> <function implementation> Runs the dispatch algorithm to return the best available implementation for the given *cls* registered on *generic_func*. """ nonlocal cache_token if cache_token is not None: current_token = get_cache_token() if cache_token != current_token: dispatch_cache.clear() cache_token = current_token try: impl = dispatch_cache[cls] except KeyError: try: impl = registry[cls] except KeyError: impl = _find_impl(cls, registry) dispatch_cache[cls] = impl return impl def register(cls, func=None): """generic_func.register(cls, func) -> func Registers a new implementation for the given *cls* on a *generic_func*. """ nonlocal cache_token if func is None: return lambda f: register(cls, f) registry[cls] = func if cache_token is None and hasattr(cls, '__abstractmethods__'): cache_token = get_cache_token() dispatch_cache.clear() return func def wrapper(*args, **kw): return dispatch(args[0].__class__)(*args, **kw) registry[object] = func wrapper.register = register wrapper.dispatch = dispatch wrapper.registry = MappingProxyType(registry) wrapper._clear_cache = dispatch_cache.clear update_wrapper(wrapper, func) return wrapper
if PY2: def raw_input(prompt=""): sys.stderr.flush() if prompt: stdout = _default_text_stdout() stdout.write(prompt) stdin = _default_text_stdin() return stdin.readline().rstrip("\r\n") try: import colorama except ImportError: pass else: _ansi_stream_wrappers = WeakKeyDictionary() def auto_wrap_for_ansi(stream, color=None): """This function wraps a stream so that calls through colorama are issued to the win32 console API to recolor on demand. It also ensures to reset the colors if a write call is interrupted to not destroy the console afterwards. """ try: cached = _ansi_stream_wrappers.get(stream) except Exception: cached = None if cached is not None: return cached strip = should_strip_ansi(stream, color) ansi_wrapper = colorama.AnsiToWin32(stream, strip=strip)
def __init__(self): self.instance_schemas = WeakKeyDictionary()
class VectorPdf(Flowable): # The filecache allows us to only read a given PDF file once # for every RstToPdf client object. This allows this module # to usefully cache, while avoiding being the cause of a memory # leak in a long-running process. filecache = WeakKeyDictionary() @classmethod def load_xobj(cls, srcinfo): client, uri = srcinfo loader = cls.filecache.get(client) if loader is None: loader = cls.filecache[client] = CacheXObj().load return loader(uri) def __init__(self, filename, width=None, height=None, kind='direct', mask=None, lazy=True, srcinfo=None): Flowable.__init__(self) self._kind = kind self.xobj = self.load_xobj(srcinfo) self.imageWidth = width self.imageHeight = height x1, y1, x2, y2 = self.xobj.BBox self._w, self._h = x2 - x1, y2 - y1 if not self.imageWidth: self.imageWidth = self._w if not self.imageHeight: self.imageHeight = self._h self.__ratio = float(self.imageWidth) / self.imageHeight if kind in ['direct', 'absolute']: self.drawWidth = width or self.imageWidth self.drawHeight = height or self.imageHeight elif kind in ['bound', 'proportional']: factor = min( float(width) / self.imageWidth, float(height) / self.imageHeight) self.drawWidth = self.imageWidth * factor self.drawHeight = self.imageHeight * factor def wrap(self, aW, aH): return self.drawWidth, self.drawHeight def drawOn(self, canv, x, y, _sW=0): if _sW > 0 and hasattr(self, 'hAlign'): a = self.hAlign if a in ('CENTER', 'CENTRE', TA_CENTER): x += 0.5 * _sW elif a in ('RIGHT', TA_RIGHT): x += _sW elif a not in ('LEFT', TA_LEFT): raise ValueError("Bad hAlign value " + str(a)) xobj = self.xobj xobj_name = makerl(canv._doc, xobj) xscale = self.drawWidth / self._w yscale = self.drawHeight / self._h x -= xobj.BBox[0] * xscale y -= xobj.BBox[1] * yscale canv.saveState() canv.translate(x, y) canv.scale(xscale, yscale) canv.doForm(xobj_name) canv.restoreState()
:license: BSD, see LICENSE for more details. """ import re import string import inspect from weakref import WeakKeyDictionary from datetime import datetime, date from itertools import chain from werkzeug._compat import iter_bytes, text_type, BytesIO, int_to_byte, \ range_type, integer_types _logger = None _empty_stream = BytesIO() _signature_cache = WeakKeyDictionary() _epoch_ord = date(1970, 1, 1).toordinal() _cookie_params = set((b'expires', b'path', b'comment', b'max-age', b'secure', b'httponly', b'version')) _legal_cookie_chars = (string.ascii_letters + string.digits + u"/=!#$%&'*+-.^_`|~:").encode('ascii') _cookie_quoting_map = { b',': b'\\054', b';': b'\\073', b'"': b'\\"', b'\\': b'\\\\', } for _i in chain(range_type(32), range_type(127, 256)):
def __init__(self, *args, **kwargs): self.views = WeakKeyDictionary() self.positions = WeakKeyDictionary() self.home_views = WeakKeyDictionary() super().__init__(*args, **kwargs)
class TaskState: """ Encapsulates auxiliary task information that cannot be added to the Task instance itself because there are no guarantees about its implementation. """ __slots__ = 'parent_id', 'name', 'cancel_scope' def __init__(self, parent_id: Optional[int], name: Optional[str], cancel_scope: Optional[CancelScope]): self.parent_id = parent_id self.name = name self.cancel_scope = cancel_scope _task_states = WeakKeyDictionary( ) # type: WeakKeyDictionary[asyncio.Task, TaskState] # # Task groups # class ExceptionGroup(BaseExceptionGroup): def __init__(self, exceptions: Sequence[BaseException]): super().__init__() self.exceptions = exceptions class TaskGroup(abc.TaskGroup): __slots__ = 'cancel_scope', '_active', '_exceptions'
def __init__(self, pause_ev, close_ev): """Initialization.""" super().__init__() self._pause_ev = pause_ev self._close_ev = close_ev self._input_update_ev = Event() self._input = MpInQueue(self._input_update_ev, pause_ev, close_ev) self._pulse_resolved = config["PULSE_RESOLVED"] self._require_geometry = config["REQUIRE_GEOMETRY"] self._queue = deque(maxlen=1) self.setAttribute(Qt.WA_DeleteOnClose) self.title = f"EXtra-foam {__version__} ({config['DETECTOR']})" self.setWindowTitle(self.title + " - main GUI") # ************************************************************* # Central widget # ************************************************************* self._ctrl_widgets = [] # book-keeping control widgets self._cw = QSplitter() self._cw.setChildrenCollapsible(False) self.setCentralWidget(self._cw) self._left_cw_container = QScrollArea() self._left_cw_container.setFrameShape(QFrame.NoFrame) self._left_cw = QTabWidget() self._right_cw_container = QScrollArea() self._right_cw_container.setFrameShape(QFrame.NoFrame) self._right_cw = QSplitter(Qt.Vertical) self._right_cw.setChildrenCollapsible(False) self._source_cw = self.createCtrlWidget(DataSourceWidget) self._extension_cw = self.createCtrlWidget(ExtensionCtrlWidget) self._ctrl_panel_cw = QTabWidget() self._analysis_cw = QWidget() self._statistics_cw = QWidget() self._util_panel_container = QWidget() self._util_panel_cw = QTabWidget() # ************************************************************* # Tool bar # Note: the order of 'addAction` affect the unittest!!! # ************************************************************* self._tool_bar = self.addToolBar("Control") # make icon a bit larger self._tool_bar.setIconSize(1.25 * self._tool_bar.iconSize()) self._start_at = self.addAction("Start bridge", "start.png") self._start_at.triggered.connect(self.onStart) self._stop_at = self.addAction("Stop bridge", "stop.png") self._stop_at.triggered.connect(self.onStop) self._stop_at.setEnabled(False) self._tool_bar.addSeparator() image_tool_at = self.addAction("Image tool", "image_tool.png") image_tool_at.triggered.connect(lambda: (self._image_tool.show( ), self._image_tool.activateWindow())) open_poi_window_at = self.addAction("Pulse-of-interest", "poi.png") open_poi_window_at.triggered.connect( functools.partial(self.onOpenPlotWindow, PulseOfInterestWindow)) if not self._pulse_resolved: open_poi_window_at.setEnabled(False) pump_probe_window_at = self.addAction("Pump-probe", "pump-probe.png") pump_probe_window_at.triggered.connect( functools.partial(self.onOpenPlotWindow, PumpProbeWindow)) open_statistics_window_at = self.addAction("Correlation", "correlation.png") open_statistics_window_at.triggered.connect( functools.partial(self.onOpenPlotWindow, CorrelationWindow)) open_statistics_window_at = self.addAction("Histogram", "histogram.png") open_statistics_window_at.triggered.connect( functools.partial(self.onOpenPlotWindow, HistogramWindow)) open_bin2d_window_at = self.addAction("Binning", "binning.png") open_bin2d_window_at.triggered.connect( functools.partial(self.onOpenPlotWindow, BinningWindow)) self._tool_bar.addSeparator() open_file_stream_window_at = self.addAction("File stream", "file_stream.png") open_file_stream_window_at.triggered.connect( lambda: self.onOpenSatelliteWindow(FileStreamWindow)) open_about_at = self.addAction("About EXtra-foam", "about.png") open_about_at.triggered.connect( lambda: self.onOpenSatelliteWindow(AboutWindow)) # ************************************************************* # Miscellaneous # ************************************************************* # book-keeping opened windows self._plot_windows = WeakKeyDictionary() self._satellite_windows = WeakKeyDictionary() self._gui_logger = GuiLogger(parent=self) logger.addHandler(self._gui_logger) self._configurator = Configurator() self._thread_logger = ThreadLoggerBridge() self.quit_sgn.connect(self._thread_logger.stop) self._thread_logger_t = QThread() self._thread_logger.moveToThread(self._thread_logger_t) self._thread_logger_t.started.connect(self._thread_logger.recv) self._thread_logger.connectToMainThread(self) # For real time plot self._running = False self._plot_timer = QTimer() self._plot_timer.timeout.connect(self.updateAll) # For checking the connection to the Redis server self._redis_timer = QTimer() self._redis_timer.timeout.connect(self.pingRedisServer) self.__redis_connection_fails = 0 self._mon_proxy = MonProxy() # ************************************************************* # control widgets # ************************************************************* # analysis control widgets self.analysis_ctrl_widget = self.createCtrlWidget(AnalysisCtrlWidget) self.pump_probe_ctrl_widget = self.createCtrlWidget( PumpProbeCtrlWidget) self.fom_filter_ctrl_widget = self.createCtrlWidget( FomFilterCtrlWidget) # statistics control widgets self.bin_ctrl_widget = self.createCtrlWidget(BinCtrlWidget) self.histogram_ctrl_widget = self.createCtrlWidget(HistogramCtrlWidget) self.correlation_ctrl_widget = self.createCtrlWidget( CorrelationCtrlWidget) # ************************************************************* # status bar # ************************************************************* # StatusBar to display topic name self.statusBar().showMessage(f"TOPIC: {config['TOPIC']}") self.statusBar().setStyleSheet("QStatusBar{font-weight:bold;}") # ImageToolWindow is treated differently since it is the second # control window. self._image_tool = ImageToolWindow( queue=self._queue, pulse_resolved=self._pulse_resolved, require_geometry=self._require_geometry, parent=self) self.initUI() self.initConnections() self.updateMetaData() self._configurator.onInit() self.setMinimumSize(640, 480) self.resize(self._WIDTH, self._HEIGHT) self.show()
handler = self.passlib_handler items = [ # since this is user-facing, we're reporting passlib's name, # without the distracting PASSLIB_HASHER_PREFIX prepended. (_('algorithm'), handler.name), ] if hasattr(handler, "parsehash"): kwds = handler.parsehash(encoded, sanitize=mask_hash) for key, value in iteritems(kwds): key = self._translate_kwds.get(key, key) items.append((_(key), value)) return SortedDict(items) # cache of hasher wrappers generated by get_passlib_hasher() _hasher_cache = WeakKeyDictionary() def get_passlib_hasher(handler): """create *Hasher*-compatible wrapper for specified passlib hash. This takes in the name of a passlib hash (or the handler object itself), and returns a wrapper instance which should be compatible with Django 1.4's Hashers framework. If the named hash corresponds to one of Django's builtin hashers, an instance of the real hasher class will be returned. Note that the format of the handler won't be altered, so will probably not be compatible with Django's algorithm format, so the monkeypatch provided by this plugin must have been applied.
class Fleet(WorldObject): """ Fleet object is responsible for moving a group of ship around the map in an ordered manner, that is: 1. provide a single move callback for a fleet as a whole, 2. resolve self-blocks in a group of ships 3. resolve MoveNotPossible exceptions. """ log = logging.getLogger("ai.aiplayer.fleet") # ship states inside a fleet, fleet doesn't care about AIPlayer.shipStates since it doesn't do any reasoning. # all fleet cares about is to move ships from A to B. shipStates = Enum('idle', 'moving', 'blocked', 'reached') RETRY_BLOCKED_TICKS = 16 # state for a fleet as a whole fleetStates = Enum('idle', 'moving') def __init__(self, ships, destroy_callback=None): super(Fleet, self).__init__() assert ships, "request to create a fleet from %s ships" % (len(ships)) self.__init(ships, destroy_callback) def __init(self, ships, destroy_callback=None): self.owner = ships[0].owner # dictionary of ship => state self._ships = WeakKeyDictionary() for ship in ships: self._ships[ship] = self.shipStates.idle #TODO: @below, this caused errors on one occasion but I was not able to reproduce it. ship.add_remove_listener(Callback(self._lost_ship, ship)) self.state = self.fleetStates.idle self.destroy_callback = destroy_callback def save(self, db): super(Fleet, self).save(db) # save the fleet # save destination if fleet is moving somewhere db("INSERT INTO fleet (fleet_id, owner_id, state_id) VALUES(?, ?, ?)", self.worldid, self.owner.worldid, self.state.index) if self.state == self.fleetStates.moving and hasattr(self, 'destination'): if isinstance(self.destination, Point): x, y = self.destination.x, self.destination.y db("UPDATE fleet SET dest_x = ?, dest_y = ? WHERE fleet_id = ?", x, y, self.worldid) elif isinstance(self.destination, Circle): x, y, radius = self.destination.center.x, self.destination.center.y, self.destination.radius db("UPDATE fleet SET dest_x = ?, dest_y = ?, radius = ? WHERE fleet_id = ?", x, y, radius, self.worldid) else: assert False, "destination is neither a Circle nor a Point: %s" % self.destination.__class__.__name__ if hasattr(self, "ratio"): db("UPDATE fleet SET ratio = ? WHERE fleet_id = ?", self.ratio, self.worldid) # save ships for ship in self.get_ships(): db("INSERT INTO fleet_ship (ship_id, fleet_id, state_id) VALUES(?, ?, ?)", ship.worldid, self.worldid, self._ships[ship].index) def _load(self, worldid, owner, db, destroy_callback): super(Fleet, self).load(db, worldid) self.owner = owner state_id, dest_x, dest_y, radius, ratio = db("SELECT state_id, dest_x, dest_y, radius, ratio FROM fleet WHERE fleet_id = ?", worldid)[0] if radius: # Circle self.destination = Circle(Point(dest_x, dest_y), radius) elif dest_x and dest_y: # Point self.destination = Point(dest_x, dest_y) else: # No destination pass if ratio: self.ratio = ratio ships_states = [(WorldObject.get_object_by_id(ship_id), self.shipStates[ship_state_id]) for ship_id, ship_state_id in db("SELECT ship_id, state_id FROM fleet_ship WHERE fleet_id = ?", worldid)] ships = [item[0] for item in ships_states] self.__init(ships, destroy_callback) self.state = self.fleetStates[state_id] for ship, state in ships_states: self._ships[ship] = state if self.state == self.fleetStates.moving: for ship in self.get_ships(): if self._ships[ship] == self.shipStates.moving: ship.add_move_callback(Callback(self._ship_reached, ship)) if destroy_callback: self.destroy_callback = destroy_callback @classmethod def load(cls, worldid, owner, db, destroy_callback=None): self = cls.__new__(cls) self._load(worldid, owner, db, destroy_callback) return self def get_ships(self): return self._ships.keys() def destroy(self): for ship in self._ships.keys(): ship.remove_remove_listener(self._lost_ship) if self.destroy_callback: self.destroy_callback() def _lost_ship(self, ship): """ Used when fleet was on the move and one of the ships was killed during that. This way fleet has to check whether the target point was reached. """ if ship in self._ships: del self._ships[ship] if self.size() == 0: self.destroy() elif self._was_target_reached(): self._fleet_reached() def _get_ship_states_count(self): """ Returns Counter about how many ships are in state idle, moving, reached. """ counter = defaultdict(lambda: 0) for value in self._ships.values(): counter[value] += 1 return counter def _was_target_reached(self): """ Checks whether required ratio of ships reached the target. """ state_counts = self._get_ship_states_count() # below: include blocked ships as "reached" as well since there's not much more left to do, # and it's better than freezing the whole fleet reached = state_counts[self.shipStates.reached] + state_counts[self.shipStates.blocked] total = len(self._ships) return self.ratio <= float(reached) / total def _ship_reached(self, ship): """ Called when a single ship reaches destination. """ self.log.debug("Fleet %s, Ship %s reached the destination", self.worldid, ship.get_component(NamedComponent).name) self._ships[ship] = self.shipStates.reached if self._was_target_reached(): self._fleet_reached() def _fleet_reached(self): """ Called when whole fleet reaches destination. """ self.log.debug("Fleet %s reached the destination", self.worldid) self.state = self.fleetStates.idle for ship in self._ships.keys(): self._ships[ship] = self.shipStates.idle if self.callback: self.callback() def _move_ship(self, ship, destination, callback): # retry ad infinitum. Not the most elegant solution but will do for a while. # Idea: mark ship as "blocked" through state and check whether they all are near the destination anyway # 1. If they don't make them sail again. # 2. If they do, assume they reached the spot. try: ship.move(destination, callback=callback, blocked_callback=Callback(self._move_ship, ship, destination, callback)) self._ships[ship] = self.shipStates.moving except MoveNotPossible: self._ships[ship] = self.shipStates.blocked if not self._was_target_reached(): Scheduler().add_new_object(Callback(self._retry_moving_blocked_ships), self, run_in=self.RETRY_BLOCKED_TICKS) def _get_circle_size(self): """ Destination circle size for movement calls that involve more than one ship. """ return 10 #return min(self.size(), 5) def _retry_moving_blocked_ships(self): if self.state != self.fleetStates.moving: return for ship in filter(lambda ship: self._ships[ship] == self.shipStates.blocked, self.get_ships()): self._move_ship(ship, self.destination, Callback(self._ship_reached, ship)) def move(self, destination, callback=None, ratio=1.0): """ Move fleet to a destination. @param ratio: what percentage of ships has to reach destination in order for the move to be considered done: 0.0 - None (not really useful, executes the callback right away) 0.0001 - effectively ANY ship 1.0 - ALL of the ships 0.5 - at least half of the ships etc. """ assert self.size() > 0, "ordered to move a fleet consisting of 0 ships" # it's ok to specify single point for a destination only when there's only one ship in a fleet if isinstance(destination, Point) and self.size() > 1: destination = Circle(destination, self._get_circle_size()) self.destination = destination self.state = self.fleetStates.moving self.ratio = ratio self.callback = callback # This is a good place to do something fancier later like preserving ship formation instead sailing to the same point for ship in self._ships.keys(): self._move_ship(ship, destination, Callback(self._ship_reached, ship)) def size(self): return len(self._ships) def __str__(self): if hasattr(self, '_ships'): ships_str = "\n " + "\n ".join(["%s (fleet state:%s)" % (ship.get_component(NamedComponent).name, self._ships[ship]) for ship in self._ships.keys()]) else: ships_str = 'N/A' return "Fleet: %s , state: %s, ships:%s" % (self.worldid, (self.state if hasattr(self, 'state') else 'unknown state'), ships_str)
import hashlib from typing import Dict, Iterable, Optional, Tuple, Union from urllib.parse import urlunparse from weakref import WeakKeyDictionary from w3lib.http import basic_auth_header from w3lib.url import canonicalize_url from scrapy import Request, Spider from scrapy.utils.httpobj import urlparse_cached from scrapy.utils.misc import load_object from scrapy.utils.python import to_bytes, to_unicode _fingerprint_cache: "WeakKeyDictionary[Request, Dict[Tuple[Optional[Tuple[bytes, ...]], bool], str]]" _fingerprint_cache = WeakKeyDictionary() def request_fingerprint( request: Request, include_headers: Optional[Iterable[Union[bytes, str]]] = None, keep_fragments: bool = False, ) -> str: """ Return the request fingerprint. The request fingerprint is a hash that uniquely identifies the resource the request points to. For example, take the following two urls: http://www.example.com/query?id=111&cat=222 http://www.example.com/query?cat=222&id=111
class DockLayout(gobject.GObject): """ Manage a dock layout. For this to work the toplevel widget in the layout hierarchy should be a DockFrame. The DockFrame is registered with the DockLayout. After that sophisticated drag-and-drop functionality is present. NB. When items are closed, the item-closed signal is emitted. The item is *not* destroyed, though. """ __gtype_name__ = 'EtkDockLayout' __gsignals__ = { 'item-closed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_OBJECT, gobject.TYPE_OBJECT)), 'item-selected': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_OBJECT, gobject.TYPE_OBJECT)), } def __init__(self): gobject.GObject.__init__(self) # Initialize logging self.log = getLogger('%s.%s' % (self.__gtype_name__, hex(id(self)))) self.frames = set() self._signal_handlers = {} # Map widget -> set([signals, ...]) self._focused_item = None self._focused_group = None self._focus_data = WeakKeyDictionary( ) # Map item -> last focused widget self._drag_data = None def add(self, frame): assert isinstance(frame, DockFrame) self.frames.add(frame) self.add_signal_handlers(frame) def remove(self, frame): self.remove_signal_handlers(frame) self.frames.remove(frame) def get_main_frames(self): """ Get the frames that are non-floating (the main frames). """ return (f for f in self.frames \ if not (isinstance(f.get_parent(), gtk.Window) \ and f.get_parent().get_transient_for())) def get_floating_frames(self): """ Get the floating frames. Floating frames have a gtk.Window as parent that is transient for some other window. """ return (f for f in self.frames \ if isinstance(f.get_parent(), gtk.Window) \ and f.get_parent().get_transient_for()) def get_widgets(self, name): """ Get a set of widgets based on their name. """ return [ w for w in itertools.chain.from_iterable( flatten(frame) for frame in self.frames) if w.get_name() == name ] def _get_signals(self, widget): """ Get a list of signals to be registered for a specific widget. """ if isinstance(widget, DockPaned): signals = (('item-added', self.on_widget_add), ('item-removed', self.on_widget_remove)) elif isinstance(widget, DockGroup): signals = (('item-added', self.on_widget_add), ('item-removed', self.on_widget_remove), ('item-selected', self.on_dockgroup_item_selected)) elif isinstance(widget, DockItem): signals = (('close', self.on_dockitem_close), ) elif isinstance(widget, gtk.Container): signals = (('add', self.on_widget_add), ('remove', self.on_widget_remove)) else: signals = () return signals + ( ('drag-motion', self.on_widget_drag_motion), ('drag-leave', self.on_widget_drag_leave), ('drag-drop', self.on_widget_drag_drop), ('drag-data-received', self.on_widget_drag_data_received), ('drag-end', self.on_widget_drag_end), ('drag-failed', self.on_widget_drag_failed), ('notify::is-focus', self.on_widget_is_focus)) def add_signal_handlers(self, widget): """ Set up signal handlers for layout and child widgets. Also group state is changed from selected to prelight, in order to have one focused widget. """ if self._signal_handlers.get(widget): return signals = set() drag_dest = widget.drag_dest_get_target_list() if not drag_dest: widget.drag_dest_set(gtk.DEST_DEFAULT_MOTION, [DRAG_TARGET_ITEM_LIST], gdk.ACTION_MOVE) elif DRAG_TARGET_ITEM_LIST not in drag_dest: widget.drag_dest_set_target_list(drag_dest + [DRAG_TARGET_ITEM_LIST]) # Use instance methods here, so layout can do additional bookkeeping for name, callback in self._get_signals(widget): try: signals.add(widget.connect(name, callback)) except TypeError as e: self.log.debug(e) self._signal_handlers[widget] = signals if isinstance(widget, gtk.Container): widget.foreach(self.add_signal_handlers) # Ensure SELECTED state is only for the selected item if isinstance(widget, DockGroup): widget.set_tab_state(gtk.STATE_PRELIGHT) def remove_signal_handlers(self, widget): """ Remove signal handlers. """ try: signals = self._signal_handlers[widget] except KeyError: pass # No signals else: for s in signals: widget.disconnect(s) del self._signal_handlers[widget] # TODO: widget.drag_dest_set_target_list(drag_dest - [DRAG_TARGET_ITEM_LIST])?? if isinstance(widget, gtk.Container): widget.foreach(self.remove_signal_handlers) def update_floating_window_title(self, widget): frame = widget.get_ancestor(DockFrame) if frame in self.get_floating_frames(): frame.get_toplevel().set_title(', '.join([ w.title for w in [w for w in flatten(frame) if isinstance(w, DockItem)] ])) def do_item_closed(self, group, item): """ If an item is closed, perform maintenance cleanup. """ if settings[item].auto_remove: cleanup(group, self) def do_item_selected(self, group, item): # Use this callback to grey out the selection on all but the active selection? self._focused_item = item if not (group is self._focused_group and group.get_tab_state() != gtk.STATE_PRELIGHT): if self._focused_group: self._focused_group.set_tab_state(gtk.STATE_PRELIGHT) self._focused_group = group group.set_tab_state(gtk.STATE_SELECTED) def on_widget_add(self, container, widget): """ Deal with new elements being added to the layout or it's children. """ if isinstance(widget, gtk.Container): self.add_signal_handlers(widget) self.update_floating_window_title(container) def on_widget_remove(self, container, widget): """ Remove signals from containers and subcontainers. """ if isinstance(widget, gtk.Container): self.remove_signal_handlers(widget) self.update_floating_window_title(container) def on_widget_drag_motion(self, widget, context, x, y, timestamp): if DRAG_TARGET_ITEM_LIST[0] in context.targets: context.docklayout = self drag_data = drag_motion(widget, context, x, y, timestamp) old_drop_widget = self._drag_data and self._drag_data.drop_widget new_drop_widget = drag_data and drag_data.drop_widget if new_drop_widget is not old_drop_widget: self.on_widget_drag_leave(widget, context, timestamp) self._drag_data = drag_data def on_widget_drag_leave(self, widget, context, timestamp): # Note: when dropping, drag-leave is invoked before drag-drop if DRAG_TARGET_ITEM_LIST[0] in context.targets: drag_data = self._drag_data if drag_data and drag_data.leave: self.log.debug('on widget drag leave %s' % drag_data.leave) drag_data.leave(drag_data.drop_widget) def on_widget_drag_drop(self, widget, context, x, y, timestamp): self.log.debug('drag_drop %s %s %s %s', context, x, y, timestamp) if DRAG_TARGET_ITEM_LIST[0] in context.targets: drag_data = self._drag_data if drag_data and drag_data.drop_widget: target = gdk.atom_intern(DRAG_TARGET_ITEM_LIST[0]) drag_data.drop_widget.drag_get_data(context, target, timestamp) return True # act as if drag failed: source = context.get_source_widget() source.emit('drag-failed', context, 1) cleanup(source, self) return False def on_widget_drag_data_received(self, widget, context, x, y, selection_data, info, timestamp): ''' Execute the received handler using the received handler retrieved in the drag_drop event handler. ''' self.log.debug('drag_data_received %s, %s, %s, %s, %s, %s' % (context, x, y, selection_data, info, timestamp)) if DRAG_TARGET_ITEM_LIST[0] in context.targets: drag_data = self._drag_data assert drag_data.received try: drag_data.received(selection_data, info) finally: self._drag_data = None def on_widget_drag_end(self, widget, context): if DRAG_TARGET_ITEM_LIST[0] in context.targets: context.docklayout = self return drag_end(widget, context) def on_widget_drag_failed(self, widget, context, result): if DRAG_TARGET_ITEM_LIST[0] in context.targets: context.docklayout = self return drag_failed(widget, context, result) def on_widget_is_focus(self, widget, pspec): """ The input focus moved to another widget. """ if isinstance(widget, DockItem): item = widget else: item = widget.get_ancestor(DockItem) if item: self._focus_data[item] = widget if item is not self._focused_item: group = item.get_parent() self.emit('item-selected', group, item) def on_dockitem_close(self, item): group = item.get_parent() self.emit('item-closed', group, item) cleanup(group, self) def on_dockgroup_item_selected(self, group, item): """ An item is selected by clicking on a tab. """ focus_child = self._focus_data.get(item) if focus_child: # item-selected is emited by is-focus handler focus_child.set_property('has-focus', True) self.emit('item-selected', group, item)
def __init__(self): from weakref import WeakKeyDictionary self.listeners = WeakKeyDictionary()
def __init__( self, required=False, nullable=False, help_text=None, validators=None, default=NotSet, name=None, # add by gloria min_value=None, max_value=None, default_value=None, value=None, # 打包成的目标数据类型,参看 base_data_convert_2_bytes.py pack_to_type=None, byte_value=None, # 转换成bytes之后的长度,就是byte的字节数。 byte_length=None, hex_value=None, # 是否是小端 is_little_endian=False, # 序列号 sequence=0, # 依赖的父亲属性 depending_field=None, # 数据偏转因子 * (乘积因子) data_factor=1, # 数据偏移量, +(累加偏移量) data_offset=0): # cached_bytes add by gloria self.cached_bytes = WeakKeyDictionary() self.memory = WeakKeyDictionary() self.required = required self.help_text = help_text self.nullable = nullable self._assign_validators(validators) self.name = name # add by gloria self.max_value = max_value # add by gloria self.min_value = min_value # 值类型的值 self.value = value # 用于表示属性的序列,打包bytes时需要。 self.sequence = sequence # 当前field所依赖的field,如果依赖的filed 值 = 0 或None,则此field是无效的filed,不需打包成bytes。 self.depending_field = depending_field self.default_value = default_value # add by gloria self.pack_to_type = pack_to_type self.is_little_endian = is_little_endian if default_value: self.byte_value = base_to_bytes(self.pack_to_type, default_value, self.is_little_endian) else: self.byte_value = byte_value if byte_length: self.byte_length = byte_length else: self.byte_length = base_data_from_bytes.get_bytes_length( pack_to_type) self.hex_value = hex_value self._validate_name() if default is not NotSet: self.validate(default) self._default = default self.data_factor = data_factor self.data_offset = data_offset
def __init__(self, default=None): self._hidden = WeakKeyDictionary() self.default = default
def __init__(self, uri, assets='./'): WebSocketServerFactory.__init__(self, uri) self._peers = WeakKeyDictionary() self.assets = assets
def is_yaml_parsing_issue(operation: APIOperation) -> bool: """Detect whether the API operation has problems because of YAML syntax. For example, unquoted 'on' is parsed as `True`. """ try: # Sorting keys involves their comparison, when there is a non-string value, it leads to a TypeError json.dumps(operation.schema.raw_schema, sort_keys=True) except TypeError: return True return False _BODY_STRATEGIES_CACHE: WeakKeyDictionary = WeakKeyDictionary() def _get_body_strategy( parameter: OpenAPIParameter, to_strategy: Callable[[Dict[str, Any]], st.SearchStrategy], parent_schema: BaseSchema ) -> st.SearchStrategy: # The cache key relies on object ids, which means that the parameter should not be mutated # Note, the parent schema is not included as each parameter belong only to one schema if parameter in _BODY_STRATEGIES_CACHE and to_strategy in _BODY_STRATEGIES_CACHE[parameter]: return _BODY_STRATEGIES_CACHE[parameter][to_strategy] schema = parameter.as_json_schema() schema = parent_schema.prepare_schema(schema) strategy = to_strategy(schema) if not parameter.is_required: strategy |= st.just(NOT_SET) _BODY_STRATEGIES_CACHE.setdefault(parameter, {})[to_strategy] = strategy
def __init__(self): self.timestamps = WeakKeyDictionary()