Advertisement
nucular

Pyld HexChat Python script manager replacement

Aug 26th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. import hexchat
  2. import imp
  3. import sys
  4. import os
  5.  
  6. __module_name__ = "Pyld script manager"
  7. __module_version__ = "1.0"
  8. __module_description__ = "Replacement for the broken Hexchat script manager."
  9.  
  10. basedir = "Wherever/you/put/your/stuff/"
  11.  
  12. scripts = {}
  13. hooks = {}
  14.  
  15. def load_autorun():
  16.     global autorun
  17.     if os.path.exists(basedir + "autorun.txt"):
  18.         with open(basedir + "autorun.txt", "rt") as f:
  19.             autorun = f.read().split("\n")
  20.     else:
  21.         autorun = []
  22.  
  23. def save_autorun():
  24.     with open(basedir + "autorun.txt", "wt") as f:
  25.         f.write("\n".join(autorun))
  26.  
  27. # load a script, first unloading if it's already loaded
  28. def script_load(name):
  29.     if name in scripts:
  30.         script_unload(name)
  31.     try:
  32.         scripts[name] = imp.load_source(name, basedir + name + ".py")
  33.         hooks[name] = scripts[name].hooks
  34.         print "Loaded script \"%s\"" % name
  35.     except:
  36.         print "Error loading script \"%s\": %s" % (name, sys.exc_info()[1])
  37.         pass
  38.  
  39. # unload a script
  40. def script_unload(name):
  41.     try:
  42.         for i in hooks[name]:
  43.             hexchat.unhook(i)
  44.         hooks.pop(name)
  45.         scripts.pop(name)
  46.         print "Unloaded script \"%s\"" % name
  47.     except:
  48.         print "Error unloading script \"%s\": %s" % (name, sys.exc_info()[1])
  49.         pass
  50.  
  51. # hook for commands
  52. def script_manager_hook(word, word_eol, userdata):
  53.     argnum = len(word) - 1
  54.     if argnum == 0 or (argnum == 1 and word[1] == "help"):
  55.         print "Available commands: list, load [name], unload [name], reload [name]"
  56.     elif argnum == 1 and word[1] == "info":
  57.         print "PyLd script manager by nucular, based on a script by jacob1"
  58.     elif argnum == 1 and word[1] == "list":
  59.         print "Loaded scripts: " + ", ".join(scripts)
  60.  
  61.     elif argnum == 2 and word[1] == "unload":
  62.         if not word[2] in scripts:
  63.             print "Script \"%s\" is not loaded." % word[2]
  64.         else:
  65.             script_unload(word[2])
  66.     elif argnum == 2 and word[1] == "load":
  67.         if word[2] in scripts:
  68.             print "Script \"%s\" is already loaded." % word[2]
  69.         else:
  70.             script_load(word[2])
  71.     elif argnum == 2 and word[1] == "reload":
  72.         if not word[2] in scripts:
  73.             print "Script \"%s\" is not loaded." % word[2]
  74.         else:
  75.             script_load(word[2])
  76.  
  77.     elif argnum == 2 and word[1] == "auto":
  78.         if word[2] in autorun:
  79.             autorun.remove(word[2])
  80.             print "Script \"%s\" removed from autorun." % word[2]
  81.         else:
  82.             autorun.append(word[2])
  83.             print "Script \"%s\" added to autorun." % word[2]
  84.         save_autorun()
  85.  
  86.     else:
  87.         print "Unknown command or wrong number of arguments. Try /pyld help."
  88.     return hexchat.EAT_HEXCHAT
  89.  
  90. hexchat.hook_command("pyld", script_manager_hook)
  91.  
  92. load_autorun()
  93. for i in autorun:
  94.     script_load(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement