当前位置: 首页>>代码示例>>Python>>正文


Python LazyDict.has_key方法代码示例

本文整理汇总了Python中jsb.utils.lazydict.LazyDict.has_key方法的典型用法代码示例。如果您正苦于以下问题:Python LazyDict.has_key方法的具体用法?Python LazyDict.has_key怎么用?Python LazyDict.has_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jsb.utils.lazydict.LazyDict的用法示例。


在下文中一共展示了LazyDict.has_key方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Pdod

# 需要导入模块: from jsb.utils.lazydict import LazyDict [as 别名]
# 或者: from jsb.utils.lazydict.LazyDict import has_key [as 别名]
class Pdod(Persist):

    """ pickled dicts of dicts """

    def __init__(self, filename):
        Persist.__init__(self, filename)
        if not self.data: self.data = LazyDict()

    def __getitem__(self, name):
        """ return item with name """
        if self.data.has_key(name): return self.data[name]

    def save(self):
        Persist.save(self)

    def __delitem__(self, name):
        """ delete name item """
        if self.data.has_key(name): return self.data.__delitem__(name)

    def __setitem__(self, name, item):
        """ set name item """
        self.data[name] = item

    def __contains__(self, name):
        return self.data.__contains__(name)

    def setdefault(self, name, default):
        """ set default of name """
        return self.data.setdefault(name, default)

    def has_key(self, name):
        """ has name key """
        return self.data.has_key(name)

    def has_key2(self, name1, najsb):
        """ has [name1][najsb] key """
        if self.data.has_key(name1): return self.data[name1].has_key(najsb)

    def get(self, name1, najsb):
        """ get data[name1][najsb] """
        try:
            result = self.data[name1][najsb]
            return result
        except KeyError: pass

    def set(self, name1, najsb, item):
        """ set name, najsb item """
        if not self.data.has_key(name1): self.data[name1] = {}
        self.data[name1][najsb] = item
开发者ID:melmothx,项目名称:jsonbot,代码行数:51,代码来源:pdod.py

示例2: savecallbacktable

# 需要导入模块: from jsb.utils.lazydict import LazyDict [as 别名]
# 或者: from jsb.utils.lazydict.LazyDict import has_key [as 别名]
def savecallbacktable(modname=None):
    """ save command -> plugin list to db backend. """
    if modname:
        logging.warn("boot - module name is %s" % modname)
    global callbacktable
    assert callbacktable
    if not callbacktable.data:
        callbacktable.data = {}
    if modname:
        target = LazyDict(callbacktable.data)
    else:
        target = LazyDict()
    from jsb.lib.callbacks import first_callbacks, callbacks, last_callbacks, remote_callbacks

    for cb in [first_callbacks, callbacks, last_callbacks, remote_callbacks]:
        for type, cbs in cb.cbs.iteritems():
            for c in cbs:
                if modname and c.modname != modname:
                    continue
                if not target.has_key(type):
                    target[type] = []
                if not c.modname in target[type]:
                    target[type].append(c.modname)
    logging.warn("saving callback table")
    assert callbacktable
    assert target
    callbacktable.data = target
    callbacktable.save()
开发者ID:jvanvinkenroye,项目名称:painload,代码行数:30,代码来源:boot.py

示例3: savecmndtable

# 需要导入模块: from jsb.utils.lazydict import LazyDict [as 别名]
# 或者: from jsb.utils.lazydict.LazyDict import has_key [as 别名]
def savecmndtable(modname=None, saveperms=True):
    """ save command -> plugin list to db backend. """
    global cmndtable
    if not cmndtable.data: cmndtable.data = {}
    if modname: target = LazyDict(cmndtable.data)
    else: target = LazyDict()
    global shorttable
    if not shorttable.data: shorttable.data = {}
    if modname: short = LazyDict(shorttable.data)
    else: short = LazyDict()
    global cmndperms
    from jsb.lib.commands import cmnds
    assert cmnds
    for cmndname, c in cmnds.iteritems():
        if modname and c.modname != modname or cmndname == "subs": continue
        if cmndname and c:
            target[cmndname] = c.modname  
            cmndperms[cmndname] = c.perms
            try:
                 s = cmndname.split("-")[1]
                 if not target.has_key(s):
                     if not short.has_key(s): short[s] = [cmndname, ]
                     if cmndname not in short[s]: short[s].append(cmndname)
            except (ValueError, IndexError): pass
    logging.warn("saving command table")
    assert cmndtable
    assert target
    cmndtable.data = target
    cmndtable.save()
    logging.warn("saving short table")
    assert shorttable
    assert short
    shorttable.data = short
    shorttable.save()
    logging.warn("saving RE table")
    for command in cmnds.regex:
        retable.data[command.regex] = command.modname
    assert retable
    retable.save()
    if saveperms:
        logging.warn("saving command perms")
        cmndperms.save()
开发者ID:NURDspace,项目名称:jsonbot,代码行数:44,代码来源:boot.py

示例4: FloodControl

# 需要导入模块: from jsb.utils.lazydict import LazyDict [as 别名]
# 或者: from jsb.utils.lazydict.LazyDict import has_key [as 别名]
class FloodControl(object):

    def __init__(self):
        self.stats = StatDict()
        self.times = LazyDict()
        self.wait = LazyDict()
        self.warned = LazyDict()

    def reset(self, userhost):
        try: del self.times[userhost]
        except KeyError: pass
        try: del self.stats[userhost]
        except KeyError: pass
        try: del self.wait[userhost]
        except KeyError: pass
        try: del self.warned[userhost]
        except KeyError: pass

    def check(self, userhost, timetomonitor=60, threshold=10, wait=120, floodrate=1):
        u = userhost
        t = time.time()
        w = wait
        if self.times.has_key(u):
            if t - self.times[u] > w: self.reset(u) ; return False
            if (t - self.times[u] < timetomonitor): self.stats.upitem(u)
            if (self.stats.get(u) >  threshold) or (t - self.times[u] < floodrate): self.wait[userhost] = wait ; return True
        else: self.times[u] = t ; return False
        if self.stats.get(u) <= threshold: return False
        return True

    def checkevent(self, event, dobind=True):
        if not event.iscommand: return False
        if getmainconfig().floodallow: return False
        if dobind: event.bind()
        if not event.user: got = False
        else: got = True
        t = got and event.user.data.floodtime or 60
        if t < 60: t = 60
        threshold = got and event.user.data.floodthreshold or 20
        if threshold < 20: threshold = 20
        wait = got and event.user.data.floodwait or 120
        if wait < 120: wait = 120
        floodrate = got and event.user.data.floodrate or 0.1
        if floodrate < 0.1: floodrate = 0.1
        if not self.check(event.userhost, t, threshold, wait, floodrate): return False 
        if event.user and "OPER" in event.user.data.perms: return False
        logging.warn("floodcontrol block on %s" % event.userhost)
        if event.userhost not in self.warned:
            logging.warn("floodcontrol block on %s" % event.userhost)
            event.reply("floodcontrol enabled (%s seconds)" % wait)
        self.warned[event.userhost] = time.time()
        return True
开发者ID:Lujeni,项目名称:old-projects,代码行数:54,代码来源:floodcontrol.py

示例5: makeoptions

# 需要导入模块: from jsb.utils.lazydict import LazyDict [as 别名]
# 或者: from jsb.utils.lazydict.LazyDict import has_key [as 别名]
def makeoptions(ievent, options={}):
    options = LazyDict(options)
    try:
        optargs = ""
        optlist = []
        if not options.has_key('--filter'):
            options['--filter'] = ""
        if not options.has_key('--to'):
            options['--to'] = None
        if not options.has_key('--chan'):
            options['--chan'] = ievent.channel
        if not options.has_key('--how'):
            options['--how'] = "msg"
        if not options.has_key('--speed'):
            options['--speed'] = str(ievent.speed)
        else:
            options['--speed'] = str(options['--speed'])
        for i, j in options.iteritems():
            if '--' in i:
                optlist.append("%s=" % i[2:])
                if j:
                    optlist.append(j)
                continue
            if '-' in i:
                if j:
                    optargs += ":%s" % i[1:]
                else:
                    optargs += i[1:]
        args = ievent.txt.split()
        try:
            (opts, rest) = getopt.getopt(args[1:], optargs, optlist)
        except AttributeError, ex:
            print "option not allowed: %s" % str(ex), ievent.txt, options
            return 0
        except getopt.GetoptError, ex:
            return 0
开发者ID:melmothx,项目名称:jsonbot,代码行数:38,代码来源:generic.py

示例6: Commands

# 需要导入模块: from jsb.utils.lazydict import LazyDict [as 别名]
# 或者: from jsb.utils.lazydict.LazyDict import has_key [as 别名]
class Commands(LazyDict):

    """
        the commands object holds all commands of the bot.
 
    """

    def add(self, cmnd, func, perms, threaded=False, wait=False, orig=None, how=None, *args, **kwargs):
        """ add a command. """
        modname = calledfrom(sys._getframe())
        target = Command(modname, cmnd, func, perms, threaded, wait, orig, how)
        self[cmnd] = target
        try:
            c = cmnd.split('-')[1]
            if not self.subs: self.subs = LazyDict()
            if self.subs.has_key(c):
                if not self.subs[c]: self.subs[c] = []
                if target not in self.subs[c]: self.subs[c].append(target)
            else: self.subs[c] = [target, ]
        except IndexError: pass
        try:
            p = cmnd.split('-')[0]
            if not self.pre: self.pre = LazyDict()
            if self.pre.has_key(p):
                if not self.pre[p]: self.pre[p] = []
                if target not in self.pre[p]: self.pre[p].append(target)
            else: self.pre[p] = [target, ]
        except IndexError: pass
        return self

    def woulddispatch(self, bot, event, cmnd=""):
        """ 
            dispatch an event if cmnd exists and user is allowed to exec this 
            command.

        """
        event.bind(bot)
        cmnd = cmnd or event.usercmnd.lower()
        if not cmnd: return
        try:
            cmnd = event.chan.data.aliases[cmnd]
        except (KeyError, TypeError):
            try: cmnd = bot.aliases.data[cmnd]
            except (KeyError, TypeError): pass
        try:
            if cmnd:
                event.txt = cmnd +  ' ' + ' '.join(event.txt.split()[1:])
                event.usercmnd = cmnd.split()[0]
                event.prepare()
        except (TypeError, KeyError, AttributeError): pass
        logging.debug("commands - %s" % cmnd)
        bot.plugs.reloadcheck(bot, event)
        result = None
        cmnd = event.usercmnd
        try:
            result = self[cmnd]
        except KeyError:
            if self.subs and self.subs.has_key(cmnd):
                cmndlist = self.subs[cmnd]
                if len(cmndlist) == 1: result = cmndlist[0]
                else: event.reply("try one of: %s" % ", ".join([x.cmnd for x in cmndlist])) ; return
            else:
                if self.pre and self.pre.has_key(cmnd):
                    cmndlist = self.pre[cmnd]
                    if len(cmndlist) == 1: result = cmndlist[0]
                    else: event.reply("try one of: %s" % ", ".join([x.cmnd for x in cmndlist])) ; return
        logging.debug("commands - woulddispatch result: %s" % result)
        return result

    def dispatch(self, bot, event, wait=0):
        """ 
            dispatch an event if cmnd exists and user is allowed to exec this 
            command.

        """
        event.bind(bot)
        if event.groupchat: id = event.auth = event.userhost
        else: id = event.auth
        if not event.user: raise NoSuchUser(event.auth)
        c = self.woulddispatch(bot, event)
        if not c: raise NoSuchCommand()
        if bot.cmndperms and bot.cmndperms[c.cmnd]: perms = bot.cmndperms[c.cmnd]
        else: perms = c.perms
        if bot.allowall: return self.doit(bot, event, c, wait=wait)
        elif event.chan and event.chan.data.allowcommands and event.usercmnd in event.chan.data.allowcommands: 
            if not 'OPER' in perms:  return self.doit(bot, event, c, wait=wait)
            else: logging.warn("commands - %s is not in allowlist" % c)
        elif not bot.users or bot.users.allowed(id, perms, bot=bot): return self.doit(bot, event, c, wait=wait)
        elif bot.users.allowed(id, perms, bot=bot): return self.doit(bot, event, c, wait=wait)
        return event

    def doit(self, bot, event, target, wait=0):
        """ do the dispatching. """
        if not target.enable: return
        if target.modname in event.chan.data.denyplug:
             logging.warn("commands - %s is denied in channel %s - %s" % (target.plugname, event.channel, event.userhost))
             return
        id = event.auth or event.userhost
        event.iscommand = True
        event.how = target.how
#.........这里部分代码省略.........
开发者ID:melmothx,项目名称:jsonbot,代码行数:103,代码来源:commands.py

示例7: Commands

# 需要导入模块: from jsb.utils.lazydict import LazyDict [as 别名]
# 或者: from jsb.utils.lazydict.LazyDict import has_key [as 别名]
class Commands(LazyDict):

    """
        the commands object holds all commands of the bot.
 
    """

    regex = []

    def add(self, cmnd, func, perms, threaded=False, wait=False, orig=None, how=None, speed=None, regex=False, needcc=False, *args, **kwargs):
        """ add a command. """
        modname = calledfrom(sys._getframe())
        try: prev = self[cmnd]
        except KeyError: prev = None
        target = Command(modname, cmnd, func, perms, threaded, wait, orig, how, speed=speed, needcc=needcc)
        if regex:
            logging.info("regex command detected - %s" % cmnd)
            self.regex.append(target)
            target.regex = cmnd 
            return self
        self[cmnd] = target
        try:
            p = cmnd.split('-')[0]
            if not self.pre: self.pre = LazyDict()
            if self.pre.has_key(p):
                if not self.pre[p]: self.pre[p] = []
                if prev in self.pre[p]: self.pre[p].remove(prev) 
                if target not in self.pre[p]: self.pre[p].append(target)
            else: self.pre[p] = [target, ]
        except IndexError: pass
        return self

    def checkre(self, bot, event):
        gotcc = event.gotcc()
        for r in self.regex:
            if not r.enable: continue
            if r.needcc and not event.gotcc(): logging.debug("RE needs cc but not found") ; continue
            s = re.search(r.cmnd, event.txt)
            if s:
                logging.info("regex matches %s" % r.cmnd)
                event.groups = list(s.groups())
                return r

    def wouldmatchre(self, bot, event, cmnd=""):
        groups = self.checkre(bot, event)
        if groups: return groups
        
    def woulddispatch(self, bot, event):
        """ 
            dispatch an event if cmnd exists and user is allowed to exec this 
            command.

        """
        event.bind(bot)
        aliased = False
        try:
            cmnd = event.stripcc().split()[0]
            if not cmnd: cmnd = event.execstr.split()[0]
            if not cmnd: cmnd = event.txt.split()[0]
        except Exception, ex: logging.warn("can't determine command from %s" % event.txt) ; return None
        try:
            a = event.chan.data.aliases[cmnd]
            if a: cmnd = a.split()[0] ; aliased = True
        except (KeyError, TypeError):
            try:
                a = getaliases()[cmnd]
                if a: cmnd = a.split()[0] ; aliased = True
            except (KeyError, TypeError): 
                if not self.has_key(cmnd):
                    try:
                        short = getshorttable()
                        if short.has_key(cmnd):
                            cmndlist = short[cmnd]
                            if len(cmndlist) == 1: cmnd = cmndlist[0]
                            else: event.reply("choose one of: ", cmndlist) ; return
                    except Exception, ex: handle_exception()
        logging.info("trying for %s" % cmnd)
        result = None
        try:
            result = self[cmnd]
        except KeyError: pass
        logging.debug("woulddispatch result: %s" % result)
        if result: event.bloh() ; event.makeargs()
        if aliased: event.usercmnd = cmnd
        return result

    def dispatch(self, bot, event, direct=False):
        """ 
            dispatch an event if cmnd exists and user is allowed to exec this 
            command.

        """
        if event.nodispatch: logging.info("nodispatch is set on event") ; return
        if event.groupchat and bot.cfg.fulljids: id = event.auth
        elif event.groupchat: id = event.auth = event.userhost
        else: id = event.auth
        if not event.user: event.bind(bot)
        if not event.user: raise NoSuchUser(event.userhost)
        self.reloadcheck(bot, event)
        c = self.woulddispatch(bot, event)
#.........这里部分代码省略.........
开发者ID:NURDspace,项目名称:jsonbot,代码行数:103,代码来源:commands.py

示例8: HubbubWatcher

# 需要导入模块: from jsb.utils.lazydict import LazyDict [as 别名]
# 或者: from jsb.utils.lazydict.LazyDict import has_key [as 别名]
class HubbubWatcher(PlugPersist):

    """ 
        this watcher helps with the handling of incoming POST. also maitains 
        index of feed names.
    """

    def __init__(self, filename, host):
        PlugPersist.__init__(self, filename)
        if not self.data: self.data = LazyDict()
        if not self.data.has_key('names'): self.data['names'] = []
        if not self.data.has_key('urls'): self.data['urls'] = {}
        if not self.data.has_key('feeds'): self.data['feeds'] = {}
        if not self.data.has_key('host'): self.data['host'] = host
        self.feeds = {}

    def add(self, name, url, owner):
        """ add a feed to the database. """
        if not name in self.data['names']:
            self.data['names'].append(name)
            self.data['urls'][url] = name
            self.save()

        item = HubbubItem(name, url, owner)
        if item.data.url != url:
            return False

        item.save()
        self.feeds[name] = item
        return True

    def byname(self, name):
        """ retrieve a feed by it's name. """
        if name in self.feeds: return self.feeds[name]
        item = HubbubItem(name)
        if item.data.url: self.feeds[name] = item ; return item

    def byurl(self, url):
        """ retrieve a feed by it's url. """
        try:
            name = self.data['urls'][url]
            return self.byname(name)
        except KeyError: pass

    def cloneurl(self, url, auth):
        """ add feeds from remote url. """
        data = geturl2(url)
        got = []
        for line in data.split('\n'):
            try: (name, url) = line.split()
            except ValueError:
                logging.debug("hubbub - cloneurl - can't split %s line" % line)
                continue
            if url.endswith('<br>'): url = url[:-4]
            self.add(name, url, auth)
            got.append(name)
        return got

    def watch(self, name):
        """ make feed ready for watching and mark it running. """
        logging.debug('trying %s hubbub feed' % name)
        item = self.byname(name)
        if item == None: raise NoSuchFeed(name)
        if not item.data.running:
            item.data.running = 1
            item.data.stoprunning = 0
            item.save()
            subscribe(item.data['url'], self.data.host)
        logging.info('hubbub - started %s watch' % name)

    def work(self, botname, type, channel, entries, url, *args, **kwargs):
        logging.debug("in work .. %s" % str(entries))
        try:
            item = self.byurl(url)
            name = item.data.name
            try:
                fleet = getfleet()
                bot = fleet.byname(botname)
                if not bot: logging.warn("can't find %s bot in fleet" % botname) ; return
            except NoSuchBotType, ex: logging.warn("hubbub - %s" % str(ex)) ; return
            if not bot: logging.error("hubbub - can't find %s bot in fleet" % type) ; return
            res2 = entries
            if not res2: logging.info("no updates for %s (%s) feed available" % (item.data.name, channel)) ; return
            if item.markup.get(jsonstring([name, channel]), 'reverse-order'): res2 = res2[::-1]
            if item.markup.get(jsonstring([name, channel]), 'all-lines'):
                for i in res2:
                    response = self.makeresponse(name, [i, ], channel)
                    try: bot.say(channel, response)
                    except Exception, ex: handle_exception()
            else:
                sep =  item.markup.get(jsonstring([name, channel]), 'separator')
                if sep: response = self.makeresponse(name, res2, channel, sep=sep)
                else: response = self.makeresponse(name, res2, channel)
                bot.say(channel, response)
        except Exception, ex: handle_exception()

    def incoming(self, data):
        """ process incoming hubbub data. """
        result = feedparser.parse(data)
        url = find_self_url(result.feed.links)
#.........这里部分代码省略.........
开发者ID:NURDspace,项目名称:jsonbot,代码行数:103,代码来源:hubbub.py


注:本文中的jsb.utils.lazydict.LazyDict.has_key方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。