Beispiel #1
0
def loglevel(loglevel="error", colors=True):
    logger = logging.getLogger("")
    if colors: formatter = Formatter(format, datefmt=datefmt)
    else: formatter = FormatterClean(format, datefmt=datefmt)
    level = LEVELS.get(str(loglevel).lower(), logging.NOTSET)
    filehandler = None
    logger.setLevel(level)
    if logger.handlers:
        for handler in logger.handlers:
            logger.removeHandler(handler)
    if not os.path.exists(logdir): cdir(logdir)
    try:
        filehandler = logging.handlers.TimedRotatingFileHandler(
            j(logdir, "meds.log"), 'midnight')
    except Exception as ex:
        logging.error(ex)
    ch = logging.StreamHandler()
    ch.setLevel(level)
    if colors: ch.setFormatter(formatter)
    else: ch.setFormatter(formatter)
    logger.addHandler(ch)
    if filehandler:
        ch.setFormatter(formatter)
        filehandler.setLevel(level)
        logger.addHandler(filehandler)
    global enabled
    enabled = True
    return logger
 def prefixed(self, *args, **kwargs):
     """ return all filename in a workdir subdirectory, the 'prefix'. """
     from meds.core import kernel
     p = kernel._cfg.workdir
     if not os.path.exists(p): os.mkdir(p)
     args = list(args)
     if args and args[0]:
         p = j(p, args[0])
         d = os.path.abspath(p)
         if not os.path.exists(d): return []
     return sorted(self.scan(p, *args, **kwargs), key=lambda x: fn_time(x))
 def scan(self, path, *args, **kwargs):
     """ scan all files in the cfg.main.workdir directory. """
     p = Event(**kwargs)
     if not path.endswith(os.sep): path += os.sep
     for root, dirs, files in os.walk(path, topdown=True):
         if not os.path.isdir(root): continue
         for fn in files:
             fnn = j(root, fn)
             timed = fn_time(fnn)
             if timed and p.start and timed < p.start: continue
             if timed and p.end and timed > p.end: continue
             yield fnn
Beispiel #4
0
 def load(self, path="", force=True):
     from meds.core import cfg
     if not path: path = self._path
     if cfg.workdir not in path: path = j(cfg.workdir, path)
     ondisk = self.read(path)
     fromdisk = json.loads(ondisk)
     if "signature" in fromdisk:
         if not verify_signature(fromdisk["data"],
                                 fromdisk["signature"]) and not force:
             raise ESIGNATURE
     if "data" in fromdisk: self.update(fromdisk["data"])
     else: self.update(fromdisk)
     if "saved" in fromdisk: self._saved = fromdisk["saved"]
     self._path = path
     return self
Beispiel #5
0
 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")
Beispiel #6
0
 def sync(self, path=""):
     from meds.core import kernel
     if not path:
         try:
             path = self._path
         except AttributeError:
             pass
     if not path: path = j(dopath(self), rtime())
     self._path = os.path.abspath(path)
     if kernel._cfg.workdir not in self._path: raise EBORDER(self._path)
     logging.info("! sync %s" % path)
     d, fn = os.path.split(path)
     cdir(d)
     todisk = self.prepare()
     datafile = open(os.path.abspath(path) + ".tmp", 'w')
     fcntl.flock(datafile, fcntl.LOCK_EX | fcntl.LOCK_NB)
     datafile.write(headertxt % "%s characters" % len(todisk))
     datafile.write(todisk)
     datafile.write("\n")
     fcntl.flock(datafile, fcntl.LOCK_UN)
     datafile.flush()
     datafile.close()
     os.rename(path + ".tmp", path)
     return path
Beispiel #7
0
def path(obj):
    if "workdir" in obj: p = obj.workdir
    else: p = root(obj)
    if "prefix" in obj: p = j(p, obj.prefix)
    return os.path.abspath(p)
Beispiel #8
0
 def save(self, stime=""):
     if not stime: stime = rtime()
     path = j(dopath(self), stime)
     return self.sync(path)
Beispiel #9
0
 def parse(self, txt=""):
     """ parse provided text or available self.txt and determine cmnd, args, rest and other values. Adds a _parsed object to the event. """
     from meds.core import aliases, cmnds, cfg, names
     if txt: self.txt = txt.rstrip()
     if self.txt.endswith("&"): self.threaded = True ; self.txt = self.txt[:-1]
     splitted = self.txt.split()
     quoted = False
     key2 = ""
     counter = -1
     parsed = Parsed()
     for word in splitted:
         counter += 1
         if counter == 0:
             if "cc" in self and self.cc:
                 if self.cc != word[0]: continue
                 else: word = word[1:]
             alias = aliases.get(word, None)
             if alias: word = alias
             cmnd = word.lower().strip()
             if cmnd:
                 parsed.cmnd = cmnd
                 funcs = cmnds.get(cmnd, None)
                 if not funcs:
                     modnames = names.get(cmnd, [])
                     for modname in modnames:
                         self.load(modname)
             if funcs:
                 self._funcs.extend(funcs)
                 self._funcs = list(set(self._funcs))
             continue
         try: key, value = word.split("=", 1) 
         except (IndexError,ValueError):
             key = ""
             value = word 
         if value.startswith('"'):
             if value.endswith('"'):
                 value = value[1:-1]
                 parsed.words.append(value)
             else:
                 key2 = key
                 value = value[1:]
                 parsed.words.append(value)
                 quoted = True
                 continue
         if quoted:
             if value.endswith('"'):
                 key = key2
                 parsed.words.append(value[:-1])
                 value = sj(*parsed.words)
                 self._parsed.words = []
                 quoted = False
             else:
                 parsed.words.append(value)
                 continue
         if quoted:
             parsed.words.append(value)
             continue
         if counter == 1:
             if word.count("[") == 1:
                 value, index = word.split("[")
                 try: parsed.index = int(index[:-1])
                 except ValueError: pass
             if not self.prefix:
                 if key and os.path.isdir(j(cfg.workdir, key)):
                     self.prefix = key
                 elif os.path.isdir(j(cfg.workdir, value)):
                     self.prefix = value
         if "http" in value:
             parsed.args.append(value)
             parsed.rest += " " + value
             continue
         if key=="index":
             parsed.index = int(value)
             continue
         if key == "start":
             parsed.start = to_time(value)
             continue 
         if key == "end":
             parsed.end = to_time(value)
             continue 
         if value.startswith("+") or value.startswith("-"):
             try: parsed.time_diff = int(value[1:])
             except ValueError: parsed.time_diff = 0
         if value.endswith("++") or value.endswith("--"):
             try: parsed.karma = value[:-2]
             except ValueError: pass
         if key and value:
             pre = key[0]
             op = key[-1]
             post = value[0]
             last = value[-1]
             if key.startswith("!"):
                  key = key[1:]
                  parsed.switch[key] = value
                  continue
             if post == "-":
                  value = value[1:]
                  parsed.ignore.register(key, value)
                  continue
             if op == "-":
                  key = key[:-1]
                  parsed.notwant[key] = value
                  continue
             if last == "-":
                 value = value[:-1]
             parsed.want[key] = value 
             if last == "-" :
                  continue
             if counter > 1: parsed.fields.append(key)
             parsed.args.append(key)
             parsed.rest += " " + key
         else:
             if counter > 1: parsed.fields.append(value)
             parsed.args.append(value)
             parsed.rest += " " + str(value)
     self._parsed.update(parsed)
     self._parsed.rest = self._parsed.rest.strip()
     return parsed