mirror of
https://github.com/gryf/weechat-replacer.git
synced 2025-12-19 04:20:19 +01:00
Added Python3 support
This commit is contained in:
11
README.rst
11
README.rst
@@ -6,9 +6,10 @@ Weechat plugin for replacing user defined keywords with specified text.
|
|||||||
Installation and configuration
|
Installation and configuration
|
||||||
------------------------------
|
------------------------------
|
||||||
|
|
||||||
In order to use it, you have to have Weechat with Python plugin support compiled
|
In order to use it, you have to have Weechat with Python plugin support
|
||||||
in. Than, copy ``replacer.py`` to ``~/.weechat/python/``. You can optionally
|
compiled in. Than, copy ``replacer.py`` to ``~/.weechat/python/``. You can use
|
||||||
create symbolic link to this script:
|
any of the Python version - both Python2 and Python3 are supported. You can
|
||||||
|
optionally create symbolic link to this script:
|
||||||
|
|
||||||
.. code:: shell-session
|
.. code:: shell-session
|
||||||
|
|
||||||
@@ -34,8 +35,8 @@ Usage
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
Abbreviations will be stored as json file ``~/.weechat/replacement_map.json``,
|
Abbreviations will be stored as json file ``~/.weechat/replacement_map.json``,
|
||||||
which role is to simply persist dictionary object on the filesystem. To add some
|
which role is to simply persist dictionary object on the filesystem. To add
|
||||||
replacement words, and text which would those words replaced with:
|
some replacement words, and text which would those words replaced with:
|
||||||
|
|
||||||
.. code::
|
.. code::
|
||||||
|
|
||||||
|
|||||||
32
replacer.py
32
replacer.py
@@ -21,13 +21,14 @@ Examples:
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
import weechat
|
import weechat
|
||||||
|
|
||||||
|
|
||||||
NAME = 'replacer'
|
NAME = 'replacer'
|
||||||
AUTHOR = 'Roman Dobosz <gryf73@gmail.com>'
|
AUTHOR = 'Roman Dobosz <gryf73@gmail.com>'
|
||||||
VERSION = '1.0'
|
VERSION = '1.1'
|
||||||
LICENSE = 'Apache 2'
|
LICENSE = 'Apache 2'
|
||||||
DESC = 'Word replacer for WeeChat'
|
DESC = 'Word replacer for WeeChat'
|
||||||
COMMAND = 'replacer'
|
COMMAND = 'replacer'
|
||||||
@@ -38,6 +39,18 @@ COLOR_NICK = weechat.color('chat_nick')
|
|||||||
COLOR_RESET = weechat.color('reset')
|
COLOR_RESET = weechat.color('reset')
|
||||||
|
|
||||||
|
|
||||||
|
def _decode(string):
|
||||||
|
if sys.version_info.major == 2:
|
||||||
|
return string.decode('utf-8')
|
||||||
|
return string
|
||||||
|
|
||||||
|
|
||||||
|
def _encode(string):
|
||||||
|
if sys.version_info.major == 2:
|
||||||
|
return string.encode('utf-8')
|
||||||
|
return string
|
||||||
|
|
||||||
|
|
||||||
class Replacer(object):
|
class Replacer(object):
|
||||||
"""Replacer"""
|
"""Replacer"""
|
||||||
|
|
||||||
@@ -127,8 +140,7 @@ def replace_cmd(replacer_obj, _, weechat_buffer, args):
|
|||||||
for key, value in sorted(replacer_obj.replacement_map.items()):
|
for key, value in sorted(replacer_obj.replacement_map.items()):
|
||||||
|
|
||||||
echo('%(key)s %(color_delimiters)s->%(color_reset)s %(val)s',
|
echo('%(key)s %(color_delimiters)s->%(color_reset)s %(val)s',
|
||||||
weechat_buffer, key=key.encode('utf-8'),
|
weechat_buffer, key=_encode(key), val=_encode(value))
|
||||||
val=value.encode('utf-8'))
|
|
||||||
|
|
||||||
return weechat.WEECHAT_RC_OK
|
return weechat.WEECHAT_RC_OK
|
||||||
|
|
||||||
@@ -143,18 +155,18 @@ def replace_cmd(replacer_obj, _, weechat_buffer, args):
|
|||||||
if cmd == 'add':
|
if cmd == 'add':
|
||||||
key = args.split(' ')[1].strip()
|
key = args.split(' ')[1].strip()
|
||||||
value = ' '.join(args.split(' ')[2:]).strip()
|
value = ' '.join(args.split(' ')[2:]).strip()
|
||||||
replacer_obj.add(key.decode('utf-8'), value.decode('utf-8'))
|
replacer_obj.add(_decode(key), _decode(value))
|
||||||
echo('added: %(key)s %(color_delimiters)s->%(color_reset)s %(val)s',
|
echo('added: %(key)s %(color_delimiters)s->%(color_reset)s %(val)s',
|
||||||
weechat_buffer, 'network', key=key, val=value)
|
weechat_buffer, 'network', key=key, val=value)
|
||||||
|
|
||||||
if cmd == 'del':
|
if cmd == 'del':
|
||||||
key = ' '.join(args.split(' ')[1:]).strip()
|
key = ' '.join(args.split(' ')[1:]).strip()
|
||||||
if not replacer_obj.delete(key.decode('utf-8')):
|
if not replacer_obj.delete(_decode(key)):
|
||||||
echo('No such keyword in replacement table: %(key)s',
|
echo('No such keyword in replacement table: %(key)s',
|
||||||
weechat_buffer, 'error', key=key)
|
weechat_buffer, 'error', key=key)
|
||||||
else:
|
else:
|
||||||
echo('Successfully removed key: %(key)s', weechat_buffer,
|
echo('Successfully removed key: %(key)s', weechat_buffer,
|
||||||
'network', key=key.encode('utf-8'))
|
'network', key=_encode(key))
|
||||||
|
|
||||||
return weechat.WEECHAT_RC_OK
|
return weechat.WEECHAT_RC_OK
|
||||||
|
|
||||||
@@ -164,7 +176,7 @@ def completion_cb(replacer_obj, data, completion_item, weechat_buffer,
|
|||||||
completion):
|
completion):
|
||||||
"""Complete keys from replacement table for add/del command"""
|
"""Complete keys from replacement table for add/del command"""
|
||||||
for key in replacer_obj.replacement_map:
|
for key in replacer_obj.replacement_map:
|
||||||
weechat.hook_completion_list_add(completion, key.encode('utf-8'), 0,
|
weechat.hook_completion_list_add(completion, _encode(key), 0,
|
||||||
weechat.WEECHAT_LIST_POS_SORT)
|
weechat.WEECHAT_LIST_POS_SORT)
|
||||||
return weechat.WEECHAT_RC_OK
|
return weechat.WEECHAT_RC_OK
|
||||||
|
|
||||||
@@ -176,7 +188,7 @@ def replace_cb(replacer_obj, data, completion_item, weechat_buffer,
|
|||||||
position = weechat.buffer_get_integer(weechat_buffer, 'input_pos')
|
position = weechat.buffer_get_integer(weechat_buffer, 'input_pos')
|
||||||
|
|
||||||
input_line = weechat.buffer_get_string(weechat_buffer, 'input')
|
input_line = weechat.buffer_get_string(weechat_buffer, 'input')
|
||||||
input_line = input_line.decode('utf-8')
|
input_line = _decode(input_line)
|
||||||
|
|
||||||
if len(input_line) == 0:
|
if len(input_line) == 0:
|
||||||
return weechat.WEECHAT_RC_OK
|
return weechat.WEECHAT_RC_OK
|
||||||
@@ -203,8 +215,7 @@ def replace_cb(replacer_obj, data, completion_item, weechat_buffer,
|
|||||||
new_position = len(new_line)
|
new_position = len(new_line)
|
||||||
new_line += input_line[position:]
|
new_line += input_line[position:]
|
||||||
|
|
||||||
weechat.buffer_set(weechat_buffer, 'input',
|
weechat.buffer_set(weechat_buffer, 'input', _encode(new_line))
|
||||||
new_line.encode('utf-8'))
|
|
||||||
weechat.buffer_set(weechat_buffer, 'input_pos', str(new_position))
|
weechat.buffer_set(weechat_buffer, 'input_pos', str(new_position))
|
||||||
|
|
||||||
return weechat.WEECHAT_RC_OK
|
return weechat.WEECHAT_RC_OK
|
||||||
@@ -224,5 +235,6 @@ def main():
|
|||||||
__doc__ % {"command": COMMAND},
|
__doc__ % {"command": COMMAND},
|
||||||
'add|del %(completion_cb)', 'replace_cmd', '')
|
'add|del %(completion_cb)', 'replace_cmd', '')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user