# -*- coding: utf-8 -*-#### Copyright (c) 2011 by Elián Hanisch <lambdae2@gmail.com>## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.####### View and use WeeChat's infos.## Commands:# * /infos## History:# 2013-01-06, Sebastien Helleu <flashcode@flashtux.org>:# version 0.2: make script compatible with Python 3.x# 2011-10-02# version 0.1: new script!####try:importweechatfromweechatimportWEECHAT_RC_OK,prntimport_ok=TrueexceptImportError:print("This script must be run under WeeChat.")print("Get WeeChat now at: http://www.weechat.org/")import_ok=FalseSCRIPT_NAME="infos"SCRIPT_AUTHOR="Elián Hanisch <lambdae2@gmail.com>"SCRIPT_VERSION="0.2"SCRIPT_LICENSE="GPL3"SCRIPT_DESC="View and use WeeChat's infos."# -------------------------------------------------------------------------# Class definitionsclassInfolist(object):"""Class for reading WeeChat's infolists."""fields={'info_name':'string','plugin_name':'string','description_nls':'string','args_description_nls':'string',}def__init__(self,name,args=''):self.cursor=0#debug('Generating infolist %r %r', name, args)self.pointer=weechat.infolist_get(name,'',args)ifself.pointer=='':raiseException("Infolist initialising failed (name:'%s' args:'%s')"%(name,args))def__len__(self):"""True False evaluation."""ifself.pointer:return1else:return0def__del__(self):"""Purge infolist if is no longer referenced."""self.free()def__getitem__(self,name):"""Implement the evaluation of self[name]."""value=getattr(weechat,'infolist_%s'%self.fields[name])(self.pointer,name)returnvaluedef__iter__(self):defgenerator():whileself.next():yieldselfreturngenerator()defnext(self):self.cursor=weechat.infolist_next(self.pointer)returnself.cursordefprev(self):self.cursor=weechat.infolist_prev(self.pointer)returnself.cursordefreset(self):"""Moves cursor to beginning of infolist."""ifself.cursor==1:# only if we aren't in the beginning alreadywhileself.prev():passdeffree(self):ifself.pointer:#debug('Freeing Infolist')weechat.infolist_free(self.pointer)self.pointer=''# -------------------------------------------------------------------------# FunctionsdefcatchExceptions(f):deffunction(*args,**kwargs):try:returnf(*args,**kwargs)exceptExceptionase:error(e)returnfunctionscript_nick=SCRIPT_NAMEdeferror(s,buffer=''):"""Error msg"""prnt(buffer,'%s%s%s'%(weechat.prefix('error'),script_nick,s))ifweechat.config_get_plugin('debug'):importtracebackiftraceback.sys.exc_type:trace=traceback.format_exc()prnt('',trace)# -------------------------------------------------------------------------# infos functionsdefget_infos_list():return[info['info_name']forinfoinInfolist('hook','info')]defprint_infos_description(buffer='',info_name=None):defprint_desc(infolist):name="%s[%s%s%s]"%(COLOR_CHAT_DELIMITERS,COLOR_CHAT_BUFFER,infolist['info_name'],COLOR_CHAT_DELIMITERS)prnt(buffer,'')prnt(buffer,name)prnt(buffer,"plugin ........: %s%s"%(COLOR_CYAN,infolist['plugin_name']or'core'))prnt(buffer,"arguments .....: %s%s"%(COLOR_CYAN,infolist['args_description_nls']or'(no description)'))prnt(buffer,"description ...: %s%s"%(COLOR_CYAN,infolist['description_nls']or"(no description)"))infolist=Infolist('hook','info')found=Falseforinfoininfolist:ifnotinfo_name:print_desc(infolist)elifinfo_name==info['info_name']:found=Trueprint_desc(infolist)ifinfo_nameandnotfound:prnt(buffer,"No info found with name \"%s\""%info_name)# -------------------------------------------------------------------------# callbacks@catchExceptionsdefcmd_infos(data,buffer,args):cmd,_,args=args.partition(' ')info,_,args=args.partition(' ')ifcmd=='get'andinfo:ifinfonotinget_infos_list():prnt('',"No info found with name \"%s\""%info)returnWEECHAT_RC_OKrt=weechat.info_get(info,args)header="%s[%s%s%s] (%s%r%s)"%(COLOR_CHAT_DELIMITERS,COLOR_CHAT_BUFFER,info,COLOR_CHAT_DELIMITERS,COLOR_RESET,args,COLOR_CHAT_DELIMITERS)prnt('','')prnt('',header)prnt('',"Result: %s%r"%(COLOR_CYAN,rt))elifcmd=='show':print_infos_description(info_name=info)else:weechat.command('',"/help infos")returnWEECHAT_RC_OKdefcmpl_infos_list(data,completion_item,buffer,completion):fornameinget_infos_list():weechat.hook_completion_list_add(completion,name,0,weechat.WEECHAT_LIST_POS_END)returnWEECHAT_RC_OK# -------------------------------------------------------------------------# Mainif__name__=='__main__'andimport_okand \
weechat.register(SCRIPT_NAME,SCRIPT_AUTHOR,SCRIPT_VERSION,SCRIPT_LICENSE,SCRIPT_DESC,'',''):# colorsCOLOR_RESET=weechat.color('reset')COLOR_CHAT_DELIMITERS=weechat.color('chat_delimiters')COLOR_CHAT_NICK=weechat.color('chat_nick')COLOR_CHAT_BUFFER=weechat.color('chat_buffer')COLOR_CYAN=weechat.color('cyan')# pretty [SCRIPT_NAME]script_nick='%s[%s%s%s]%s'%(COLOR_CHAT_DELIMITERS,COLOR_CHAT_NICK,SCRIPT_NAME,COLOR_CHAT_DELIMITERS,COLOR_RESET)weechat.hook_command("infos","View and use WeeChat infos","show [<info_name>] || get <info_name> [<arguments>]","show: Shows information about all infos or info <info_name>.\n"\
" get: Get info <info_name>.","get|show %(infos_info_list)","cmd_infos","")weechat.hook_completion('infos_info_list','List of info names','cmpl_infos_list','')# -------------------------------------------------------------------------# Debugifweechat.config_get_plugin('debug'):try:# custom debug module I use, allows me to inspect script's objects.importpybufferdebug=pybuffer.debugBuffer(globals(),'%s_debug'%SCRIPT_NAME)exceptImportError:defdebug(s,*args):ifnotisinstance(s,str):s=str(s)ifargs:s=s%argsprnt('','%s\t%s'%(script_nick,s))else:defdebug(s,*args):pass# vim:set shiftwidth=4 tabstop=4 softtabstop=4 expandtab textwidth=100: