summaryrefslogtreecommitdiff
path: root/scripts/python/pyexec.py
blob: 93bef6851183201ebc8258f84a3952949e24ec1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
	PyExec:  Python code execution script for WeeChat
	Author:  Christian Taylor <cht@chello.at>
	License: GPL version 2 or later
"""

version = "0.2"
helptext = """  The WeeChat script-API is imported into the global namespace, you can
  call all API functions (for instance "get_info") directly. The modules
  "sys", "os" and "math" are imported by default.
  Any occurance of ";; " is treated as a newline.

  For automatic argument conversion to string, use:
    "send" instead of "command"
    "echo" instead of "prnt" (prints only to current buffer)
    (also provided: "echo_server", "echo_infobar")

  Additional shortcut functions:
    "nicks()" returns a dictionary of nicknames for the current channel.
    It takes a channelname and a servername as optional arguments.

  Examples:
    /pyexec for i in range(3): send(i+1);; echo("Done")
    /pyexec for nick in nicks(): send("/voice " + nick)
    /pyexec echo(2**64)
"""


from __future__ import division
import sys, os, math
from weechat import *

register("PyExec", version, "", "Run Python code in WeeChat")
add_command_handler("pyexec", "pyexec", "  Runs Python code directly from the WeeChat command line.", "[Python code]", helptext)

def echo(text):
	return prnt(str(text))

def echo_server(text):
	return print_server(str(text))

def echo_infobar(time, text):
	return print_infobar(time, str(text))

def send(text):
	return command(str(text))

def nicks(channel=None, server=None):
	if not server:
		server = get_info("server")
	if not channel:
		channel = get_info("channel")
	return get_nick_info(server, channel)

def pyexec(server, pycode):
	try:
		exec pycode.replace(";; ", "\n")
	except:
		(e_type, e_value, e_trace) = sys.exc_info()
		prnt("PyExec: %s: %s" % (str(e_type).replace("exceptions.", ""), e_value))
	
	return PLUGIN_RC_OK