本文整理汇总了Python中learn.Main.get_db方法的典型用法代码示例。如果您正苦于以下问题:Python Main.get_db方法的具体用法?Python Main.get_db怎么用?Python Main.get_db使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类learn.Main
的用法示例。
在下文中一共展示了Main.get_db方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Main
# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import get_db [as 别名]
class Main(Module):
pattern = re.compile(u'^\s*(names?)(?:\s+(.*)$)?')
require_addressing = False
dbname = u'realname'
help = u'names show list of associated nicks and names (staff only)\n\
name <nick> show real name associated with user (staff only)\n\
name <nick> <real_name> set real name for user (staff only)'
def init(self):
self.learn = Learn(madcow=self.madcow)
self.staff = Staff(madcow=self.madcow)
def set(self, nick, name):
self.learn.set(self.dbname, nick.lower(), name)
def unset(self, nick):
dbm = self.learn.dbm(self.dbname)
try:
key = encode(nick.lower())
if dbm.has_key(nick):
del dbm[nick]
return True
return False
finally:
dbm.close()
def get_names(self):
name_db = self.learn.get_db('realname');
return name_db
def has_name(self, nick):
name_db = self.get_names()
return nick in name_db
def get(self, nick):
return self.learn.lookup(self.dbname, nick)
def response(self, nick, args, kwargs):
cmd = args[0]
target_nick = False
real_name = False
params = []
if args[1]:
params = args[1].partition(' ')
try:
target_nick = params[0]
except IndexError:
target_nick = False
try:
real_name = params[2]
except IndexError:
real_name = False
# only staff members & bot owner are allowed to get & set real_name data
if self.staff.is_staff(nick) or settings.OWNER_NICK == nick:
if target_nick:
if real_name:
self.set(target_nick, real_name)
return u'%s: Setting name for %s to %s' % ( nick, target_nick, real_name )
else:
name = self.get(nick=target_nick)
if name:
return u'%s: %s is %s' % ( nick, target_nick, name )
else:
return u'%s: Sorry, I don\'t who %s is.' % ( nick, target_nick )
else:
name_list = "\n\nRecorded names:\n"
for user_nick, name in self.get_names().iteritems():
name_list = name_list + user_nick + ": " + name + "\n"
return u'%s: %s' % (nick, name_list)
示例2: Main
# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import get_db [as 别名]
class Main(Module):
pattern = re.compile(u'^\s*(staff)(?:\s+(.*)$)?')
require_addressing = False
dbname = u'staff'
help = u'staff list staff members\n\
staff <nick> check if specific user is a staff member\n\
staff <nick> add/remove add/remove user from staff list (staff only)'
def init(self):
self.learn = Learn(madcow=self.madcow)
def set(self, nick):
self.learn.set(self.dbname, nick.lower(), True)
def unset(self, nick):
dbm = self.learn.dbm(self.dbname)
try:
key = encode(nick.lower())
if dbm.has_key(key):
del dbm[key]
return True
return False
finally:
dbm.close()
def get_staff(self):
staff_db = self.learn.get_db('staff');
return staff_db
def is_staff(self, nick):
staff_db = self.get_staff()
return nick in staff_db
def response(self, nick, args, kwargs):
cmd = args[0]
target_nick = False
action = False
params = []
if args[1]:
params = args[1].split()
try:
target_nick = params[0]
except IndexError:
target_nick = False
try:
action = params[1]
except IndexError:
action = False
""" if nick passed, set user as staff """
if target_nick:
# if an action is passed, and requesting user is staff or owner, perform the action
if action and (self.is_staff(nick) or settings.OWNER_NICK == nick):
if action == 'remove':
self.unset(nick=target_nick)
return u'%s: %s is no longer marked as staff' % (nick, target_nick)
elif action == "add":
self.set(nick=target_nick)
return u'%s: %s is now marked as staff' % (nick, target_nick)
else:
return u'%s: I\'m not sure what you want me to do here. \'%s\' is not a valid staff action.' %(nick, action)
# otherwise, respond with staff membership status for nick, ignoring the action
if self.is_staff(target_nick):
return u'%s: Yes, %s is a staff member' %(nick, target_nick)
else:
return u'%s: No, %s is not a staff member' %(nick, target_nick)
else:
staff_db = self.get_staff()
staff_nicks = staff_db.keys()
if len(staff_nicks):
return u'%s: Staff members are: %s' % (nick, ", ".join(staff_nicks))
else:
return u'%s: There are currently no users marked as staff members.' % (nick)
示例3: Main
# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import get_db [as 别名]
class Main(Module):
pattern = re.compile(u'^\s*(compan[y|ies])(?:\s+(.*)$)?')
require_addressing = False
dbname = u'company'
help = u'companies list all company/nick associations (staff only)\n\
company <nick> show company associated with user (staff only)\n\
company <nick> <company_name> set company information for user (staff only)'
def init(self):
self.learn = Learn(madcow=self.madcow)
self.staff = Staff(madcow=self.madcow)
def set(self, nick, company):
self.learn.set(self.dbname, nick.lower(), company)
def unset(self, nick):
dbm = self.learn.dbm(self.dbname)
try:
key = encode(nick.lower())
if dbm.has_key(nick):
del dbm[nick]
return True
return False
finally:
dbm.close()
def get_companies(self):
company_db = self.learn.get_db('company');
return company_db
def has_company(self, nick):
company_db = self.get_companies()
return nick in company_db
def get(self, nick):
return self.learn.lookup(self.dbname, nick)
def response(self, nick, args, kwargs):
cmd = args[0]
target_nick = False
company = False
params = []
if args[1]:
params = args[1].partition(' ')
try:
target_nick = params[0]
except IndexError:
target_nick = False
try:
company = params[2]
except IndexError:
company = False
# only staff members & bot owner are allowed to get & set company data
if self.staff.is_staff(nick) or settings.OWNER_NICK == nick:
if target_nick:
if company:
self.set(target_nick, company)
return u'%s: setting company for %s to %s' % ( nick, target_nick, company )
else:
company_name = self.get(target_nick)
if company_name:
return u'%s: %s works at %s' % ( nick, target_nick, company_name )
else:
return u'%s: I don\'t know where %s works' % ( nick, target_nick )
else:
company_list = "\n\nRecorded companies:\n"
for user_nick, company_name in self.get_companies().iteritems():
company_list = company_list + user_nick + " works at " + company_name + "\n"
return u'%s: %s' % (nick, company_list)
示例4: Main
# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import get_db [as 别名]
class Main(Module):
pattern = re.compile(u'^\s*(link|shortcut)s?(?:\s+(.*)$)?')
require_addressing = False
dbname = u'links'
help = u'shortcuts show list of resource shortcuts\n\
link <shortcut> show link for specified shortcut \n\
link <shortcut> <url> set link for specified shortcut (staff only)\n\
link <shortcut> delete remove link for specified shortcut (staff only)'
def init(self):
self.learn = Learn(madcow=self.madcow)
self.staff = Staff(madcow=self.madcow)
def set(self, shortcut, url):
self.learn.set(self.dbname, shortcut.lower(), url)
def unset(self, shortcut):
dbm = self.learn.dbm(self.dbname)
try:
key = encode(shortcut.lower())
if dbm.has_key(shortcut):
del dbm[shortcut]
return True
return False
finally:
dbm.close()
def get_shortcuts(self):
link_db = self.learn.get_db(self.dbname);
return link_db
def has_name(self, shortcut):
link_db = self.get_shortcuts()
return shortcut in link_db
def get(self, shortcut):
return self.learn.lookup(self.dbname, shortcut)
def response(self, nick, args, kwargs):
cmd = args[0]
shortcut = False
link = False
params = []
if args[1]:
params = args[1].partition(' ')
try:
shortcut = params[0]
except IndexError:
shortcut = False
try:
link = params[2]
except IndexError:
link = False
# only staff members & bot owner are allowed to set/change shortcuts
if shortcut and link and (self.staff.is_staff(nick) or settings.OWNER_NICK == nick):
if link == "delete":
self.unset(shortcut)
return u'%s: Okay, I\'ve deleted the link for %s. It used to be %s, and now it\'s nothing. How sad.' % ( nick, shortcut, link )
else:
self.set(shortcut, link)
return u'%s: Okay, I\'ve set the link for %s to %s' % ( nick, shortcut, link )
elif shortcut:
link_url = self.get(shortcut)
if link_url:
return u'%s: %s - %s' % (nick, shortcut, link_url)
else:
return u'%s: Sorry, there is no \'%s\' shortcut.' % (nick, shortcut)
else:
kwargs['req'].make_private() # don't spam the channel with all the links. there could be a lot of them.
links = self.get_shortcuts()
if len(links):
link_list = "Here are all the current shortcuts:\n"
for shortcut, url in links.iteritems():
link_list = link_list + shortcut + ": " + url + "\n"
return u'%s' % (link_list)
else:
return u'Sorry, no shortcuts have been defined yet.'