Tidied python plugins

Use private naming convention
Move local callback functions to top
This commit is contained in:
James Booth
2013-09-20 00:49:13 +01:00
parent ccf9f7591a
commit 14b5b3ac23
7 changed files with 100 additions and 103 deletions

View File

@@ -1,12 +1,12 @@
import prof
import subprocess
def prof_init(version, status):
prof.register_command("/ascii", 1, 1, "/ascii", "ASCIIfy a message", "ASCIIfy a message.", cmd_ascii)
def cmd_ascii(text):
def _cmd_ascii(text):
recipient = prof.get_current_recipient()
if recipient:
proc = subprocess.Popen(['figlet', '--', text], stdout=subprocess.PIPE)
ascii_out = proc.communicate()[0].decode('utf-8')
prof.send_line(u'\u000A' + ascii_out)
def prof_init(version, status):
prof.register_command("/ascii", 1, 1, "/ascii", "ASCIIfy a message", "ASCIIfy a message.", _cmd_ascii)

View File

@@ -3,24 +3,10 @@ import os
import webbrowser
import re
lastlink = {}
_lastlink = {}
def prof_init(version, status):
prof.register_command("/browser", 0, 1,
"/browser [url]",
"View a URL in the browser.",
"View a URL in the browser, if no argument is supplied, " +
"the last received URL will be used.",
cmd_browser)
def prof_on_message_received(jid, message):
global lastlink
links = re.findall(r'(https?://\S+)', message)
if (len(links) > 0):
lastlink[jid] = links[len(links)-1]
def cmd_browser(url):
global lastlink
def _cmd_browser(url):
global _lastlink
link = None
# use arg if supplied
@@ -33,8 +19,8 @@ def cmd_browser(url):
if (jid != None):
# check for link from recipient
if jid in lastlink.keys():
link = lastlink[jid]
if jid in _lastlink.keys():
link = _lastlink[jid]
else:
prof.cons_show("No links found from " + jid)
@@ -45,9 +31,9 @@ def cmd_browser(url):
# open the browser if link found
if (link != None):
prof.cons_show("Opening " + link + " in browser")
open_browser(link)
_open_browser(link)
def open_browser(url):
def _open_browser(url):
savout = os.dup(1)
saverr = os.dup(2)
os.close(1)
@@ -58,3 +44,17 @@ def open_browser(url):
finally:
os.dup2(savout, 1)
os.dup2(saverr, 2)
def prof_init(version, status):
prof.register_command("/browser", 0, 1,
"/browser [url]",
"View a URL in the browser.",
"View a URL in the browser, if no argument is supplied, " +
"the last received URL will be used.",
_cmd_browser)
def prof_on_message_received(jid, message):
global _lastlink
links = re.findall(r'(https?://\S+)', message)
if (len(links) > 0):
_lastlink[jid] = links[len(links)-1]

View File

@@ -2,30 +2,16 @@ import prof
import urllib2
import json
#score_url = "http://api.scorescard.com/?type=score&teamone=Australia&teamtwo=England"
score_url = None
#_score_url = "http://api.scorescard.com/?type=score&teamone=Australia&teamtwo=England"
_score_url = None
_summary = None
summary = None
def prof_init(version, status):
if score_url:
prof.register_timed(get_scores, 60)
prof.register_command("/cricket", 0, 0,
"/cricket",
"Get latest cricket score.",
"Get latest cricket score.",
cmd_cricket)
def prof_on_start():
if score_url:
get_scores()
def cmd_cricket():
global score_url
global summary
def _cmd_cricket():
global _score_url
global _summary
new_summary = None
result_json = retrieve_scores_json()
result_json = _retrieve_scores_json()
if 'ms' in result_json.keys():
new_summary = result_json['ms']
@@ -44,23 +30,23 @@ def cmd_cricket():
if 't2SI' in result_json.keys():
prof.cons_show(" " + result_json['t2SI'])
summary = new_summary
_summary = new_summary
prof.cons_show("")
prof.cons_show(" " + summary)
prof.cons_show(" " + _summary)
prof.cons_alert()
def get_scores():
global score_url
global summary
def _get_scores():
global _score_url
global _summary
notify = None
new_summary = None
change = False
result_json = retrieve_scores_json()
result_json = _retrieve_scores_json()
if 'ms' in result_json.keys():
new_summary = result_json['ms']
if new_summary != summary:
if new_summary != _summary:
change = True
if change:
@@ -82,16 +68,28 @@ def get_scores():
notify += "\n" + result_json['t2SI']
prof.cons_show(" " + result_json['t2SI'])
summary = new_summary
notify += "\n\n" + summary
_summary = new_summary
notify += "\n\n" + _summary
prof.cons_show("")
prof.cons_show(" " + summary)
prof.cons_show(" " + _summary)
prof.cons_alert()
prof.notify(notify, 5000, "Cricket score")
def retrieve_scores_json():
req = urllib2.Request(score_url, None, {'Content-Type': 'application/json'})
def _retrieve_scores_json():
req = urllib2.Request(_score_url, None, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read()
f.close()
return json.loads(response)
def prof_init(version, status):
if _score_url:
prof.register_timed(_get_scores, 60)
prof.register_command("/cricket", 0, 0,
"/cricket",
"Get latest cricket score.",
"Get latest cricket score.",
_cmd_cricket)
def prof_on_start():
if _score_url:
_get_scores()

View File

@@ -1,6 +1,6 @@
import prof
def emote(input_str):
def _emote(input_str):
result = input_str
result = result.replace(":-)", u'\u263a')
result = result.replace(":)", u'\u263a')
@@ -8,6 +8,5 @@ def emote(input_str):
result = result.replace(":(", u'\u2639')
return result
def prof_before_message_displayed(message):
return emote(message)
return _emote(message)

View File

@@ -1,9 +1,9 @@
import prof
import platform
def prof_init(version, status):
prof.register_command("/platform", 0, 0, "/platform", "Output system information.", "Output system information", cmd_platform)
def cmd_platform():
def _cmd_platform():
result_summary = platform.platform()
prof.cons_show(result_summary)
def prof_init(version, status):
prof.register_command("/platform", 0, 0, "/platform", "Output system information.", "Output system information", _cmd_platform)

View File

@@ -1,12 +1,42 @@
import prof
win_tag = "Upper echo";
_win_tag = "Upper echo";
def _cmd_python(msg):
if msg:
prof.cons_show("python-test: /python command called, arg = " + msg)
else:
prof.cons_show("python-test: /python command called with no arg")
prof.cons_alert()
prof.notify("python-test: notify", 2000, "Plugins")
prof.send_line("/vercheck")
prof.cons_show("python-test: sent \"/vercheck\" command")
def _timer_test():
prof.cons_show("python-test: timer fired.")
recipient = prof.get_current_recipient()
if recipient:
prof.cons_show(" current recipient = " + recipient)
prof.cons_alert()
def _cmd_upper(line):
global _win_tag;
if prof.win_exists(_win_tag) == False:
prof.win_create(_win_tag, _handle_upper)
prof.win_focus(_win_tag)
if line:
prof.win_process_line(_win_tag, line)
def _handle_upper(win, line):
prof.win_show(win, line.upper())
def prof_init(version, status):
prof.cons_show("python-test: init, " + version + ", " + status)
prof.register_command("/python", 0, 1, "/python", "python-test", "python-test", cmd_python)
prof.register_command("/upper", 0, 1, "/upper", "Uppercase input string", "Uppercase input string", cmd_upper)
prof.register_timed(timer_test, 10)
prof.register_command("/python", 0, 1, "/python", "python-test", "python-test", _cmd_python)
prof.register_command("/upper", 0, 1, "/upper", "Uppercase input string", "Uppercase input string", _cmd_upper)
prof.register_timed(_timer_test, 10)
def prof_on_start():
prof.cons_show("python-test: on_start")
@@ -54,33 +84,3 @@ def prof_on_room_message_send(room, message):
def prof_on_shutdown():
prof.log_info("python-test: on_shutdown")
def cmd_python(msg):
if msg:
prof.cons_show("python-test: /python command called, arg = " + msg)
else:
prof.cons_show("python-test: /python command called with no arg")
prof.cons_alert()
prof.notify("python-test: notify", 2000, "Plugins")
prof.send_line("/vercheck")
prof.cons_show("python-test: sent \"/vercheck\" command")
def timer_test():
prof.cons_show("python-test: timer fired.")
recipient = prof.get_current_recipient()
if recipient:
prof.cons_show(" current recipient = " + recipient)
prof.cons_alert()
def cmd_upper(line):
global win_tag;
if prof.win_exists(win_tag) == False:
prof.win_create(win_tag, handle_upper)
prof.win_focus(win_tag)
if line:
prof.win_process_line(win_tag, line)
def handle_upper(win, line):
prof.win_show(win, line.upper())

View File

@@ -1,9 +1,9 @@
import prof
import getpass
def prof_init(version, status):
prof.register_command("/whoami", 0, 0, "/whoami", "Call shell whoami command.", "Call shell whoami command.", cmd_whoami)
def cmd_whoami():
def _cmd_whoami():
me = getpass.getuser()
prof.cons_show(me)
def prof_init(version, status):
prof.register_command("/whoami", 0, 0, "/whoami", "Call shell whoami command.", "Call shell whoami command.", _cmd_whoami)