-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventos.py
165 lines (137 loc) · 5.91 KB
/
eventos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from singleton import singleton
import tornado.ioloop
from datetime import timedelta
class Suscripcion(object):
def __init__(self, suscriptor,emisor=None):
self.suscriptor = suscriptor
self.emisor = emisor
class Evento(object):
def __init__(self, emisor):
self.emisor = emisor
self.t_publicacion = None
def publicar(self, *args, **kwargs):
self.t_publicacion = tornado.ioloop.IOLoop.current().time()
if args: self.args = list(args)
if kwargs: self.kwargs = kwargs
GestorEventos().propagar(self)
def __repr__(self):
return type(self).__name__+": "+str(self.__dict__)
class SuscriptorEvento(object):
class Fin(Evento):
def __init__(self, emisor, evento_original):
self.__dict__.update(evento_original.__dict__)
self.emisor = emisor
self.t_publicacion = None
self.evento_original = evento_original
print(self)
activables = []
def recibir(self, evento):
pass
def si(self, condicion):
self.condicion = condicion
return self
def si_activo_para_tren(self, por_defecto=True):
# Registrar acciones activables para un tren
SuscriptorEvento.activables.append( (self, por_defecto) )
# Guardar condicion
self.activo = lambda evento: evento and evento.tren and self in [ x[0] for x in evento.tren.opciones_activas if x[1] ]
return self
def comprobar_y_recibir(self, evento):
if not hasattr(self, "condicion") or not self.condicion or self.condicion(evento):
if not hasattr(self, "activo") or not self.activo or self.activo(evento):
if hasattr(self, "retardo") and self.retardo:
def rr():
print("propagacion retardada: "+str(evento))
self.recibir(evento)
tornado.ioloop.IOLoop.current().add_timeout(timedelta(seconds=self.retardo), rr)
else:
self.recibir(evento)
def cuando(self, tipo_evento, emisor=None): # El metodo es encadenable: xxx.cuando(Evento).cuando(Evento)...
GestorEventos().suscribir_evento(tipo_evento, self.comprobar_y_recibir, emisor)
if not hasattr(self, "eventos"):
self.eventos = []
if emisor:
ev = { "emisor": str(emisor), "tipo_evento": tipo_evento.__name__ }
else:
ev = { "tipo_evento": tipo_evento.__name__ }
self.eventos.append(ev)
return self
def ya_no(self):
GestorEventos().eliminar_suscriptor(self.comprobar_y_recibir)
def tras(self, segundos):
self.retardo = segundos
def __repr__(self):
ret = type(self).__name__
if hasattr(self, "repr"):
ret+=" "
ret+=str(self.repr)
if hasattr(self, "eventos") and self.eventos:
ret+=" cuando " + ", ".join(map(str,self.eventos))
return ret
def json_friendly_dict(self):
real = self
ret = {}
# Delegación es un patrón por el que un suscriptor de eventos
# traslada los eventos a otro suscriptor, procesándolos antes
if hasattr(self, "delegado") and self.delegado:
real = self.delegado
ret["delegacion"] = type(self).__name__
# Tipo de accion y representacion vienen del evento real
ret["tipo_accion"] = type(real).__name__
if hasattr(real, "repr"):
ret["repr"] = real.repr
# Eventos siempre del propio suscriptor
if hasattr(self, "eventos") and self.eventos:
ret["cuando"] = self.eventos
return ret
def mapear_clave_concurrencia(self, evento):
return None
class SuscriptorGenerico(SuscriptorEvento):
def __init__(self, metodo_recibir):
self.metodo_recibir = metodo_recibir
self.repr = metodo_recibir.__repr__()
def recibir(self, evento):
self.metodo_recibir(evento)
class DescartarConcurrencia(SuscriptorEvento):
def __init__(self, delegado, max_concurrencia = 1):
self.delegado = delegado
self.max_concurrencia = 1
self.concurrencia = {}
GestorEventos().suscribir_evento(SuscriptorEvento.Fin, self.fin, self.delegado)
def recibir(self, evento):
autorizar = False
clave = self.delegado.mapear_clave_concurrencia(evento)
if clave in self.concurrencia:
if self.concurrencia[clave] < self.max_concurrencia:
self.concurrencia[clave] += 1
autorizar = True
else:
self.concurrencia[clave] = 1
autorizar = True
if autorizar:
self.delegado.recibir(evento)
def fin(self, evento):
clave = self.delegado.mapear_clave_concurrencia(evento)
self.concurrencia[clave] -= 1
if self.concurrencia[clave] == 0: del self.concurrencia[clave]
@singleton
class GestorEventos(object):
def __init__(self):
self.suscriptores = {}
def propagar(self, evento):
print("Propagando "+str(evento))
for tipo in self.suscriptores:
if issubclass(type(evento),tipo):
for s in self.suscriptores[tipo]:
if s.emisor == evento.emisor or not s.emisor:
tornado.ioloop.IOLoop.current().add_callback(s.suscriptor, evento)
#s.suscriptor(evento) llamado en la siguiente iteracion del bucle
def suscribir_evento(self, tipo, suscriptor, emisor=None): # Suscriptor es un metodo que recibe el evento como parametro
if tipo in self.suscriptores:
self.suscriptores[tipo].append(Suscripcion(suscriptor,emisor))
else:
self.suscriptores[tipo]=[Suscripcion(suscriptor,emisor)]
def eliminar_suscriptor(self, suscriptor):
print("eliminar_suscriptor: suscriptor="+str(suscriptor))
for sr in self.suscriptores.values():
[ sr.remove(sn) for sn in sr if sn.suscriptor == suscriptor ]