# Copyright (C) 2006-2012 Mortal Coil Games # See LICENSE for details. """ -*- test-case-name: mv3d.test.test_event -*- Event Related Things """ from hashlib import md5 from twisted.spread import pb from mv3d.net.security import Securable from mv3d.util.date import Date from mv3d.net.pb import Copyable from mv3d.net.pb import Cacheable class EventLog: """ You need to add a docstring here ! """ def __init__(self): self.options = {} self.subscription = 0 self.server = 0 def getOption(self, o, dv=None): """ You need to add a docstring here ! """ return self.options.get(o, dv) def setOption(self, o, v): """ You need to add a docstring here ! """ self.options[o] = v def remOption(self, o): """ You need to add a docstring here ! """ if self.options.has_key(o): del self.options[o] return 1 return 0 def receiveEvent(self, e): """ You need to add a docstring here ! """ pass def initialize(self): """ You need to add a docstring here ! """ df = Filter() df.addRule(FilterCondition(item="date", test=">", parameters=["yesterday"])) self.subscription = EventSubscription(self.receiveEvent) filters = self.getOption("filters", [df]) for f in filters: self.subscription.addFilter(f) def startLogging(self, server): """ You need to add a docstring here ! """ if not self.subscription: self.initialize() self.server = server server.getEventService().subscribe(self.subscription) def stopLogging(self): """ You need to add a docstring here ! """ self.server.getEventService().unsubscribe(self.subscription) def finalize(self): """ You need to add a docstring here ! """ self.stopLogging() class Noun(Copyable): """ You need to add a docstring here ! """ def __init__(self, setFrom=None): self.name = "" if setFrom is not None: if hasattr(setFrom, "getName"): self.name = setFrom.getName() else: self.name = unicode(setFrom) def getName(self): """ You need to add a docstring here ! """ return self.name def setName(self, n): """ You need to add a docstring here ! """ self.name = n return self def setFrom(self, o): """ You need to add a docstring here ! """ self.setName(o.getName()) return self def getStateToCopy(self): """ You need to add a docstring here ! """ return Copyable.getStateToCopy(self) def setCopyableState(self, st): """ You need to add a docstring here ! """ Copyable.setCopyableState(self, st) class Event(Securable, Copyable): """ A single generated event. It has a subject, verb, and object along with several other fields. """ allowedState = Securable.allowedState + ["date", "category", "server", "beento", "location", "importance", "falloff", "subject", "verb", "object", "prepositions"] def __init__(self, **kw): Securable.__init__(self) self.date = Date().now() self.category = "" self.server = 0 self.beento = [] self.location = 0 self.importance = 0 self.falloff = 0 self.subject = Noun().setName("") self.object = Noun().setName("") self.verb = "" self.prepositions = {} if kw.has_key("subject"): if isinstance(kw["subject"], str) or isinstance(kw["subject"], unicode): self.subject = Noun().setName(kw.get("subject")) else: self.subject = kw["subject"] if kw.has_key("verb"): self.verb = kw.get("verb") if kw.has_key("object"): if isinstance(kw["object"], str) or isinstance(kw["object"], unicode): self.object = Noun().setName(kw.get("object")) else: self.object = kw["object"] if kw.has_key("category"): self.category = kw.get("category") if kw.has_key("prepositions"): self.prepositions = kw.get("prepositions") for k in self.prepositions.keys(): if isinstance(self.prepositions[k], str) or isinstance(self.prepositions[k], unicode): self.prepositions[k] = Noun().setName(self.prepositions[k]) def getHash(self): """ You need to add a docstring here ! """ m = md5() m.update(self.date.format()) if not isinstance(self.server, int): m.update(self.server.host) m.update("%d" % self.server.port) m.update(self.category) if hasattr(self.subject, "getName"): m.update(self.subject.getName()) else: m.update(str(self.subject)) m.update(self.verb) if hasattr(self.object, "getName"): m.update(self.object.getName()) else: m.update(str(self.object)) for x in self.prepositions.keys(): m.update(x) return m.hexdigest() def getDate(self): """ You need to add a docstring here ! """ return self.date def setDate(self, d): """ You need to add a docstring here ! """ self.date = d def getCategory(self): """ You need to add a docstring here ! """ return self.category def setCategory(self, c): """ You need to add a docstring here ! """ self.category = c def getServer(self): """ You need to add a docstring here ! """ return self.server def setServer(self, s): """ You need to add a docstring here ! """ self.server = s def getLocation(self): """ You need to add a docstring here ! """ return self.location def setLocation(self, l): """ You need to add a docstring here ! """ self.location = l def getImportance(self): """ You need to add a docstring here ! """ return self.importance def setImportance(self, i): """ You need to add a docstring here ! """ self.importance = i def getSubject(self): """ You need to add a docstring here ! """ return self.subject def setSubject(self, s): """ You need to add a docstring here ! """ self.subject = s def getObject(self): """ You need to add a docstring here ! """ return self.object def setObject(self, o): """ You need to add a docstring here ! """ self.object = o def getVerb(self): """ You need to add a docstring here ! """ return self.verb def setVerb(self, v): """ You need to add a docstring here ! """ self.verb = v def getPrepositions(self): """ You need to add a docstring here ! """ return self.prepositions def addPreposition(self, prep, o): """ You need to add a docstring here ! """ self.prepositions[prep] = o def remPreposition(self, prep): """ You need to add a docstring here ! """ if not self.prepositions.has_key(prep): return 0 del self.prepositions[prep] return 1 def convertToNouns(self): """ You need to add a docstring here ! """ ee = Event() ee.__dict__.update(self.__dict__) # if self.subject: ee.subject = Noun().setFrom(self.subject) # if self.object: ee.object = Noun().setFrom(self.object) p = {} for k in self.prepositions.keys(): p[k] = Noun().setFrom(self.prepositions[k]) ee.prepositions = p # print ee.__dict__ return ee def getStateToCopy(self): """ You need to add a docstring here ! """ r = Copyable.getStateToCopy(self) if not isinstance(r["subject"], Copyable) and not isinstance(r["subject"], Cacheable) and r["subject"]: r["subject"] = Noun().setFrom(self.subject) if not isinstance(r["object"], Copyable) and not isinstance(r["object"], Cacheable) and r["object"]: r["object"] = Noun().setFrom(self.object) return r def setCopyableState(self, st): """ You need to add a docstring here ! """ Copyable.setCopyableState(self, st) def getData(self): """ You need to add a docstring here ! """ s = self.subject.getName() + " " + self.verb + " " + self.object.getName() for p in self.prepositions.keys(): s += " " + p + " " + self.prepositions[p].getName() if self.getImportance() > 5: s += "!" else: s += "." return s def __str__(self): s = self.date.format() + " " + self.category + " " + self.subject.getName() + " " + self.verb + " " + self.object.getName() for p in self.prepositions.keys(): s += " " + p + " " + self.prepositions[p].getName() if self.getImportance() > 5: s += "!" else: s += "." return s class EventSubscription(pb.Viewable): """ You need to add a docstring here ! """ def __init__(self, receiver): self.receiver = receiver self.filters = [] def receiveEvent(self, e): """ You need to add a docstring here ! """ #print "EventSubscription got event", self.filters, self for f in self.filters: if f.checkObject(e): self.receiver(e) #print "EventSubscription sent event", e, self.receiver def addFilter(self, f): """ You need to add a docstring here ! """ self.filters.append(f) def view_addFilter(self, con, f): """ You need to add a docstring here ! """ #print "Got Add Filter" self.addFilter(f) def remFilter(self, f): """ You need to add a docstring here ! """ if not f in self.filters: return 0 self.filters.remove(f) return 1 def view_remFilter(self, con, f): """ You need to add a docstring here ! """ return self.remFilter(f) class FilterCondition(Copyable): """ You need to add a docstring here ! """ def __init__(self, **kw): self.item = kw.get("item", "") self.test = kw.get("test", "") self.parameters = kw.get("parameters", []) self.positive = kw.get("positive", 1) self.tests = {">":self.greaterThan, "<":self.lessThan, "==":self.equalTo} self.addExclude("tests") def addTest(self, name, func): """ You need to add a docstring here ! """ self.tests[name] = func def addParameter(self, p): """ You need to add a docstring here ! """ self.parameters.append(p) def remParameter(self, p): """ You need to add a docstring here ! """ if not p in self.parameters: return 0 self.parameters.remove(p) return 1 def checkObject(self, e): """ You need to add a docstring here ! """ return self.tests[self.test](e) def _item(self, e): """ You need to add a docstring here ! """ return e.__dict__[self.item] def greaterThan(self, e): """ You need to add a docstring here ! """ #print self._item(e),">",self.parameters[0],"==",self._item(e)>self.parameters[0] if self._item(e) > self.parameters[0]: return 1 return 0 def lessThan(self, e): """ You need to add a docstring here ! """ #print self._item(e),"<",self.parameters[0] if self._item(e) < self.parameters[0]: return 1 return 0 def equalTo(self, e): """ You need to add a docstring here ! """ #print self._item(e),"==",self.parameters[0],"==",self._item(e)==self.parameters[0] if self._item(e) == self.parameters[0]: return 1 return 0 def getStateToCopy(self): """ You need to add a docstring here ! """ return Copyable.getStateToCopy(self) def setCopyableState(self, st): """ You need to add a docstring here ! """ self.tests = {">":self.greaterThan, "<":self.lessThan, "==":self.equalTo} Copyable.setCopyableState(self, st) class Filter(Copyable): """ You need to add a docstring here ! """ def __init__(self): self.rules = [] def checkObject(self, e): """ You need to add a docstring here ! """ for r in self.rules: if not r.checkObject(e): return 0 return 1 def addRule(self, r): """ You need to add a docstring here ! """ self.rules.append(r) def remRule(self, r): """ You need to add a docstring here ! """ if not r in self.rules: return 0 self.rules.remove(r) return 1 def getRules(self): """ You need to add a docstring here ! """ return self.rules def getStateToCopy(self): """ You need to add a docstring here ! """ return Copyable.getStateToCopy(self) def setCopyableState(self, st): """ You need to add a docstring here ! """ Copyable.setCopyableState(self, st) class EventService: """ You need to add a docstring here ! """ def __init__(self, server): self.server = server self.subscribers = [] self.recentevents = [] self.maxrecentevents = 500 def pushEvent(self, e): """ You need to add a docstring here ! """ h = e.getHash() if h in self.recentevents: #print "Dup event", self.recentevents return #print "Broadcasting event", e, "to", self.subscribers self.recentevents.append(h) if len(self.recentevents) > self.maxrecentevents: self.recentevents = self.recentevents[1:] for s in self.subscribers: s.receiveEvent(e) def subscribe(self, s): """ You need to add a docstring here ! """ self.subscribers.append(s) def unsubscribe(self, s): """ You need to add a docstring here ! """ if not s in self.subscribers: return 0 self.subscribers.remove(s) return 1 def newSubscription(self, con): """ You need to add a docstring here ! """ s = EventSubscription(con.receiveEvent) self.subscribe(s) return s pb.setUnjellyableForClass(FilterCondition, FilterCondition) pb.setUnjellyableForClass(Filter, Filter) pb.setUnjellyableForClass(Event, Event) pb.setUnjellyableForClass(Noun, Noun)