Skip to content

Instantly share code, notes, and snippets.

@nucular
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nucular/2fdd4c6660d39ce2395a to your computer and use it in GitHub Desktop.
Save nucular/2fdd4c6660d39ce2395a to your computer and use it in GitHub Desktop.
(He)XChat Python script manager replacement

Pyld

This script replaces the broken Python script manager of HexChat. Just drop it in the "addons" folder and move all other Python scripts to another folder. Replace the basedir variable in this script with the absolute path to your folder and restart.
The script will create a file named auto.txt to store a list of scripts loaded at startup too.

For unloading/reloading to work properly, you need to add a list named hooks inside your scripts and append the return values of the hook functions to it, like this:

hooks = []
(...)
hooks.append(xchat.hook_command("", myfunc))

Commands

  • /pyld help
  • /pyld list (displays a list of all loaded scripts)
  • /pyld load NAME (imports a script from the basedir)
  • /pyld unload NAME (unhooks the specified script)
  • /pyld reload NAME (do both)
  • /pyld auto NAME (add/remove the specified script to the autorun list)
import hexchat
import imp
import sys
import os
__module_name__ = "Pyld script manager"
__module_version__ = "1.1"
__module_description__ = "Replacement for the broken Hexchat script manager."
basedir = "Wherever/you/put/your/stuff"
scripts = {}
hooks = {}
autolist = os.path.join(basedir, "autorun.txt")
def load_autorun():
global autorun
if os.path.exists(autolist):
with open(autolist, "rt") as f:
autorun = f.read().split("\n")
else:
autorun = []
def save_autorun():
with open(autolist, "wt") as f:
f.write("\n".join(autorun))
# load a script, first unloading if it's already loaded
def script_load(name):
if name in scripts:
script_unload(name)
try:
scripts[name] = imp.load_source(name, os.path.join(basedir, name + ".py"))
hooks[name] = scripts[name].hooks
print "Loaded script \"%s\"" % name
except:
print "Error loading script \"%s\": %s" % (name, sys.exc_info()[1])
pass
# unload a script
def script_unload(name):
try:
for i in hooks[name]:
hexchat.unhook(i)
hooks.pop(name)
scripts.pop(name)
print "Unloaded script \"%s\"" % name
except:
print "Error unloading script \"%s\": %s" % (name, sys.exc_info()[1])
pass
# hook for commands
def script_manager_hook(word, word_eol, userdata):
argnum = len(word) - 1
if argnum == 0 or (argnum == 1 and word[1] == "help"):
print "Available commands: list, load [name], unload [name], reload [name], auto [name]"
elif argnum == 1 and word[1] == "info":
print "PyLd script manager by nucular, based on a script by jacob1"
elif argnum == 1 and word[1] == "list":
print "Loaded scripts: " + ", ".join(scripts)
elif argnum == 2 and word[1] == "unload":
if not word[2] in scripts:
print "Script \"%s\" is not loaded." % word[2]
else:
script_unload(word[2])
elif argnum == 2 and word[1] == "load":
if word[2] in scripts:
print "Script \"%s\" is already loaded." % word[2]
else:
script_load(word[2])
elif argnum == 2 and word[1] == "reload":
if not word[2] in scripts:
print "Script \"%s\" is not loaded." % word[2]
else:
script_load(word[2])
elif argnum == 2 and word[1] == "auto":
if word[2] in autorun:
autorun.remove(word[2])
print "Script \"%s\" removed from autorun." % word[2]
else:
autorun.append(word[2])
print "Script \"%s\" added to autorun." % word[2]
save_autorun()
else:
print "Unknown command or wrong number of arguments. Try /pyld help."
return hexchat.EAT_HEXCHAT
hexchat.hook_command("pyld", script_manager_hook)
load_autorun()
for i in autorun:
script_load(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment