def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._connected = Object() self._poll = select.epoll() self._resume = Object() self._status = "running" self._starttime = time.time() thr = self.launch(self._start, name="%s.engine" % sname(self), daemon=True) self._thrs.append(thr)
def __getattr__(self, name): if name == "cmnd": self.cmnd = "" if name == "switch": self.switch = Object() if name == "want": self.want = Object() if name == "ignore": self.ignore = OOL() if name == "notwant": self.notwant = Object() if name == "args": self.args = [] if name == "rest": self.rest = "" if name == "fields": self.fields = [] if name == "words": self.words = [] if name == "index": self.index = 0 if name not in self: raise AttributeError(name) return self[name]
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._connected = Object() self._queue = queue.Queue() self._cbs = OOL() self._table = Object() self._handlers = OOL() self._status = "running" self._thrs = [] thr = self.launch(self.scheduling, name="%s.schedule" % sname(self), daemon=True) self._thrs.append(thr)
def ps(event): res = [] nr = 1 for thr in sorted(launcher.running(), key=lambda x: name(x)): obj = Object() obj.update(vars(thr)) try: obj = obj.__class__.__self__ except: pass if "sleep" in obj: next = int(obj.sleep) - int(time.time() - int(obj._last)) else: next = 0 thrname = naam(thr) try: stxt = obj._status.strip() except: stxt = "working" try: etxt = obj._error.strip() except: etxt = "" try: uptime = int(time.time()) - int(obj._starttime) except: uptime = int(time.time() - kernel._starttime) txt = psformat % (elapsed(next), thrname, stxt, elapsed(uptime), etxt) res.append((next, txt)) for next, txt in sorted(res, key=lambda x: x[0], reverse=True): event.reply("%-5s %s" % (nr, txt)) nr += 1 if nr == 1: event.reply("no tasks running.")
def mbox(event): if not event._parsed.rest: event.reply("mbox <path>") return fn = os.path.expanduser(event._parsed.args[0]) nr = 0 if os.path.isdir(fn): thing = mailbox.Maildir(fn, create=False) elif os.path.isfile(fn): thing = mailbox.mbox(fn, create=False) else: event.reply("need a mbox or maildir.") return for m in thing: try: o = Object() o.update(m.items()) try: sdate = os.sep.join(to_date(o.Date).split()) except AttributeError: sdate = None o.text = "" for load in m.walk(): if load.get_content_type() == 'text/plain': o.text += load.get_payload() o.text = o.text.replace("\\n", "\n") o.prefix = "email" o.email = o.From if sdate: o.save(sdate) else: o.save() nr += 1 except: logging.error(get_exception()) if nr: event.ok(nr)
def feed(text): from meds.object import Object result = [] chunks = text.split("\r\n") for chunk in chunks: obj = Object().feed(chunk) result.append(obj) return result
def log(event): if not event._parsed.rest: event.reply("log <txt>") return o = Object(event) o.prefix = "log" o.log = event._parsed.rest path = o.save() event.ok(1)
def todo(event): if not event._parsed.rest: event.reply("todo <txt>") return o = Object(event) o.prefix = "todo" o.todo = event._parsed.rest path = o.save() event.ok(1)
def shop(event): if not event._parsed.rest: event.reply("shop <txt>") return o = Object(event) o.prefix = "shop" o.shop = event._parsed.rest path = o.save() event.ok(1)
def rss(event): if not event._parsed.rest: event.reply("rss <url>") return o = Object(event) o.prefix = "rss" o.rss = event._parsed.rest o.service = "rss" path = o.save() event.ok(1)
def find(self, prefix, *args): """ find all objects stored with a prefix subdirectory. """ for fn in self.prefixed(prefix, *args): try: obj = Object().load(fn) except: logging.warn("fail %s" % fn) continue if "deleted" in obj and obj.deleted: continue yield obj
def first(self, *args, **kwargs): """ return first object matching provided prefix. """ for fn in self.prefixed(*args, **kwargs): try: obj = Object().load(fn) except: logging.warn("fail %s" % fn) continue if "deleted" in obj and obj.deleted: continue if len(args) > 1 and obj.get(args[0]) != args[1]: continue return obj
def since(self, start, *args, **kwargs): """ return all object since a given time. """ e = Event(**kwargs) e.start = start for fn in self.prefixed(*args, **e): try: obj = Object().load(fn) except: logging.warn("fail %s" % fn) continue if "deleted" in obj and obj.deleted: continue yield obj
def last(self, *args, **kwargs): """ return last record with a matching prefix. """ e = Event(**kwargs) for fn in self.prefixed(*args, **kwargs)[::-1]: try: obj = Object().load(fn) except: logging.warn("fail %s" % fn) continue if "deleted" in obj and obj.deleted: continue if len(args) > 1 and obj.get(args[0]) != args[1]: continue return obj
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._cfg = Config(rss) config = storage.last("cfg", "rss") if config: self._cfg.update(config) objs.seen = Object() objs.seen.list = [] objs.seen.seen = "rss" objs.seen.prefix = "seen" obj = storage.last("seen", "rss") if obj: objs.seen = obj logging.warn("# seen %s" % len(objs.seen.list))
def get_feed(url): from meds.object import Object if not gotparser: logging.error("no feedparser available") return [] if not "http" in url: logging.error("%s is not an url." % url) return [] try: result = feedparser.parse(get_url(url)) except Exception as ex: logging.error(get_exception(url)) return if "entries" in result: for entry in result["entries"][::-1]: yield Object(entry)
def resume(self): resume = Object().load(j(cfg.workdir, "resume")) for bot in resume.fleet: try: fd = int(bot["_resume"]["fd"]) break except: fd = None self._resume.fd = fd if self._cfg.ipv6: self._oldsock = socket.fromfd(fd, socket.AF_INET6, socket.SOCK_STREAM) else: self._oldsock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) self._config() self.announce("done")
def reboot(event): """ reboot the bot, allowing statefull reboot (keeping connections alive). """ if not cfg.reboot: event.reply("reboot is not enabled") return if not users.allowed(event.origin, "REBOOT"): event.reply("you don't have reboot permissions.") return event.announce("rebooting") resume = Object() resume.kernel = kernel._resume resume.fleet = fleet resume.save("resume") e = Event() e._funcs.append(real_reboot) print(e) kernel.put(e)
def slice(obj, keys=[]): from meds.object import Object o = Object() if not obj: return o if not keys: keys = obj.keys() for key in keys: if key.startswith("_"): continue try: val = obj[key] except KeyError: continue try: val.keys() o[key] = slice(val) except: o[key] = val return o
def selected(self, event): """ select objects based on a parsed event. """ nr = 0 if not event._parsed.args: return [] if event._parsed.args: event.prefix = event._parsed.args[0] for fn in self.prefixed(event.prefix, **event._parsed): try: obj = Object().load(fn) except: logging.warn("fail %s" % fn) continue if not event.nodel and "deleted" in obj and obj.deleted: continue if not selector(obj, event._parsed.fields): continue if notwanted(obj, event._parsed.notwant): continue if not wanted(obj, event._parsed.want): continue if ignore(obj, event._parsed.ignore): continue nr += 1 if event._parsed.index and event._parsed.index != nr: continue yield obj logging.warn("# selected %s" % nr)
#: cmnds = Cmnds() #: fleet = [] #: kernel = Kernel() #: launcher = Launcher() #: names = OOL() #: runner = Runner() #: storage = Storage() #: users = Users() #: cfgs = Config() #: objs = Object() objs.cfg = cfg objs.aliases = aliases objs.cmnds = cmnds objs.fleet = fleet objs.kernel = kernel objs.launcher = launcher objs.names = names objs.runner = runner objs.storage = storage objs.users = users
if needed: nrtimes = int(delta / needed) txt = "%s #%s" % (name.upper(), nrtimes) if name in omschrijving: txt += " (%s)" % omschrijving.get(name) txt += " elke %s" % elapsed(seconds(nr(name))) if name in soort: txt += " door een %s" % soort.get(name, "slachtoffer") txt += " bijv. in %s" % random.choice(gemeenten) if name in tags: txt += " %s" % tags.get(name) if cfg and cfg.showurl and name in urls: txt += " - %s" % urls.get(name) event.announce(txt) #: nrsec = Object() nrsec.dag = 24 * 60 * 60.0 nrsec.jaar = 365 * nrsec.dag nrsec.weekend = 2 / 7 * (24 * 60 * 60.0 * 365) / 52 nrsec.avond = 16 / 24 * (24 * 60 * 60.0) #: times = Object() times.weekend = 2 / 7 * (24 * 60 * 60.0 * 365) / 52 times.avond = 16 / 24 * (24 * 60 * 60.0) times.dag = 24 * 60 * 60.0 times.jaar = 365 * 24 * 60 * 60.0 #: rechter = Object() rechter.ibs = 8171
rss = Config() rss.cfg = "rss" rss.display_list = ["title", "link", "Date"] rss.descriptions = [ "officiel", ] rss.sleeptime = 600 rss.ignore = [] rss.dosave = [] rss.nosave = [] rss.showurl = False #: cli = Config() cli.welcome = "mogge!!" cli.cfg = "cli" cli.server = "user@cli" #: input = Config() input.server = "input@local" templates = Object() templates.xmpp = xmpp templates.irc = irc templates.main = main templates.udp = udp templates.rest = rest templates.stats = stats templates.rss = rss templates.cli = cli