# -*- coding: utf-8 -*-## Copyright (c) 2017 Manu Koell <manu@koell.li>## 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/>.## 2017-11-03: fix script/issue #236# v0.2: add "%h" variable in option 'file'# 2018-10-23: fix script/issue #297# v0.3: make script python 3 compatiblefrom__future__importprint_functionimportosimportrefromfnmatchimportfnmatchtry:importweechataswexceptException:print("This script must be run under WeeChat.")print("Get WeeChat now at: http://www.weechat.org/")quit()NAME="autoconf"AUTHOR="Manu Koell <manu@koell.li>"VERSION="0.3"LICENSE="GPL3"DESCRIPTION="auto save/load changed options in a ~/.weerc file, useful to share dotfiles with"EXCLUDES=['*.nicks','*.username','*.sasl_username','*.password','*.sasl_password','irc.server.*.autoconnect','irc.server.*.autojoin']SETTINGS={'autosave':('on','auto save config on quit'),'autoload':('on','auto load config on start'),'ignore':(','.join(EXCLUDES),'comma separated list of patterns to exclude'),'file':('%h/.weerc','config file location ("%h" will be replaced by WeeChat home, "~/.weechat" by default)')}defcstrip(text):"""strip color codes"""returnw.string_remove_color(text,'')defget_config(args):"""get path to config file"""try:conf=args[1]exceptException:conf=w.config_get_plugin('file').replace("%h",w.info_get("weechat_dir",""))returnos.path.expanduser(conf)defload_conf(args):"""send config to fifo pipe"""fifo=w.info_get('fifo_filename','')conf=get_config(args)ifos.path.isfile(conf):w.command('','/exec -sh -norc cat | grep */set %s > %s'%(conf,fifo))defsave_conf(args):"""match options and save to config file"""try:f=open(get_config(args),'w+')exceptExceptionase:w.prnt('','%sError: %s'%(w.prefix('error'),e))returnw.WEECHAT_RC_ERRORheader=['#','# WeeChat %s (compiled on %s)'%(w.info_get('version',''),w.info_get('date','')),'#','# Use /autoconf load or cat this file to the FIFO pipe.','#','# For more info, see https://weechat.org/scripts/source/autoconf.py.html','#','']forlninheader:f.write('%s\n'%ln)w.command('','/buffer clear')w.command('','/set diff')infolist=w.infolist_get('buffer_lines','','')whilew.infolist_next(infolist):message=cstrip(w.infolist_string(infolist,'message'))ignore=w.config_get_plugin('ignore').split(',')option=re.match(RE['option'],message)ifoption:ifnotany(fnmatch(option.group(1),p.strip())forpinignore):f.write('*/set %s%s\n'%(option.group(1),option.group(2)))f.close()w.infolist_free(infolist)defautoconf_cb(data,buffer,args):"""the /autoconf command"""args=args.split()if'save'inargs:save_conf(args)elif'load'inargs:load_conf(args)else:# show help messagew.command('','/help '+NAME)returnw.WEECHAT_RC_OKdefquit_cb(data,signal,signal_data):"""save config on quit"""save_conf(None)returnw.WEECHAT_RC_OKif__name__=='__main__':ifw.register(NAME,AUTHOR,VERSION,LICENSE,DESCRIPTION,"",""):w.hook_command(NAME,DESCRIPTION,'save [path] || load [path]','','save || load','autoconf_cb','')default_txt=w.gettext("default: ")# check if string is translatedRE={'option':re.compile('\s*(.*) = (.*) \(%s'%default_txt)}# set default configforoption,valueinSETTINGS.items():ifnotw.config_is_set_plugin(option):w.config_set_plugin(option,value[0])w.config_set_desc_plugin(option,'%s (default: "%s")'%(value[1],value[0]))if'on'inw.config_get_plugin('autoload'):load_conf(None)if'on'inw.config_get_plugin('autosave'):w.hook_signal('quit','quit_cb','')