summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Davis <loafier@gmail.com>2006-08-21 05:49:03 +0000
committerChristopher Davis <loafier@gmail.com>2006-08-21 05:49:03 +0000
commit45e5419cb663fb922b297f50af3c227a6cb7cfd2 (patch)
tree9b2558d2bc6486830ef8d25d992905af69cb42b0
parent0fe369309b7bebdabc6899ae1106c9dee4ff2b45 (diff)
downloadirssi-python-45e5419cb663fb922b297f50af3c227a6cb7cfd2.tar.gz
irssi-python-45e5419cb663fb922b297f50af3c227a6cb7cfd2.tar.xz
irssi-python-45e5419cb663fb922b297f50af3c227a6cb7cfd2.zip
made minor changes to loader
git-svn-id: http://svn.irssi.org/repos/irssi-python@4323 dbcabf3a-b0e7-0310-adc4-f8d773084564
-rw-r--r--ChangeLog3
-rw-r--r--src/irssi_startup.py187
-rw-r--r--src/pycore.c2
-rw-r--r--src/pyloader.c97
-rw-r--r--src/pyloader.h1
5 files changed, 82 insertions, 208 deletions
diff --git a/ChangeLog b/ChangeLog
index 381bda9..60eead0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,3 +12,6 @@ loafier, 2006-08-17:
added a few simple scripts
added settings_add_* wrappers to irssi.py
+loafier, 2006-08-20:
+ added autoload
+ changed behavior of load to reload previously loaded scripts
diff --git a/src/irssi_startup.py b/src/irssi_startup.py
index ee8dbdd..19f0ff0 100644
--- a/src/irssi_startup.py
+++ b/src/irssi_startup.py
@@ -15,188 +15,5 @@ class Output:
_irssi.active_win().prnt(line, self.level)
self.buf = []
-#XXX: hardcode
-#try:
-sys.stdout = Output(level = 0x0080000)
-sys.stderr = Output(level = 0x0100000)
-#except Exception, e:
-# print 'Cant set output', e
-# sys.stdout = sys.__stdout__
-# sys.stderr = sys.__stderr__
-
-"""
-#Import stuff modified from <Python>/Demo/imputil/knee.py
-#If a script instance is available, it's module dictionary
-#is used in place of sys.modules
-#XXX: needs testing
-#XXX: copyright?
-
-# Save the original hooks
-original_import = __builtin__.__import__
-original_reload = __builtin__.reload
-
-# Replacement for __import__()
-def import_hook(name, globals=None, locals=None, fromlist=None):
- print 'LOADING', name
- if name == 'sys':
- return sys
- #if name == '_irssi':
- # return _irssi
-
- try:
- script = _irssi.get_script()
- except RuntimeError:
- print 'ORIG IMPORT', name
- return original_import(name, globals, locals, fromlist)
-
- parent = determine_parent(globals, script)
- q, tail = find_head_package(parent, name, script)
- m = load_tail(q, tail, script)
- if not fromlist:
- return q
-
- if hasattr(m, "__path__"):
- ensure_fromlist(m, fromlist, script)
-
- recur -= 1
- return m
-
-def determine_parent(globals, script):
- if not globals or not globals.has_key("__name__"):
- print 'DP no __name__ in globals'
- return None
- pname = globals['__name__']
- print 'DP pname', pname
- if globals.has_key("__path__"):
- parent = script.modules[pname]
- assert globals is parent.__dict__
- return parent
- if '.' in pname:
- i = pname.rfind('.')
- pname = pname[:i]
- parent = script.modules[pname]
- assert parent.__name__ == pname
- return parent
- return None
-
-def find_head_package(parent, name, script):
- if '.' in name:
- i = name.find('.')
- head = name[:i]
- tail = name[i+1:]
- else:
- head = name
- tail = ""
- if parent:
- qname = "%s.%s" % (parent.__name__, head)
- else:
- qname = head
- q = import_module(head, qname, parent, script)
- if q:
- return q, tail
- else:
- print 'FHP (1) no q module for', name
-
- if parent:
- qname = head
- parent = None
- q = import_module(head, qname, parent, script)
- if q:
- return q, tail
- else:
- print 'FHP (2) no q module for', name
-
- raise ImportError, "No module named " + qname
-
-def load_tail(q, tail, script):
- m = q
- while tail:
- i = tail.find('.')
- if i < 0: i = len(tail)
- head, tail = tail[:i], tail[i+1:]
- mname = "%s.%s" % (m.__name__, head)
- m = import_module(head, mname, m, script)
- if not m:
- raise ImportError, "No module named " + mname
- return m
-
-def ensure_fromlist(m, fromlist, script, recursive=0):
- for sub in fromlist:
- if sub == "*":
- if not recursive:
- try:
- all = m.__all__
- except AttributeError:
- pass
- else:
- assert recursive == 0
- ensure_fromlist(m, all, script, 1)
- continue
- if sub != "*" and not hasattr(m, sub):
- subname = "%s.%s" % (m.__name__, sub)
- submod = import_module(sub, subname, m, script)
- if not submod:
- raise ImportError, "No module named " + subname
-
-def import_module(partname, fqname, parent, script):
- try:
- m = script.modules[fqname]
- #if hasattr(m, '_script'):
- # assert m._script == script
-
- _irssi.prnt('LOADING CACHED %s script -> %s' % (fqname, repr(script)))
-
- return m
- except KeyError:
- print 'IM no cached moddule for', fqname
- pass
-
- try:
- fp, pathname, stuff = imp.find_module(partname,
- parent and parent.__path__)
- except ImportError, e:
- print 'IM import error', e
- return None
-
- try:
- m = imp.load_module(fqname, fp, pathname, stuff)
- finally:
- if fp: fp.close()
- if parent:
- setattr(parent, partname, m)
-
- #don't load script into builtins, extensions, or this wrapper
- if hasattr(m, '__file__') and fqname != 'irssi':
- m._script = script
- script.modules[fqname] = m
-
- #if hasattr(m, '__file__') and fqname not in ('__builtin__', 'sys', '__main__', '_irssi'):
- # del sys.modules[fqname]
-
- _irssi.prnt('GOT -> %s, SCRIPT -> %s' % (m, repr(script)))
-
- return m
-
-
-# Replacement for reload()
-def reload_hook(module):
- _irssi.prnt('reloading ' + repr(module))
- try:
- script = _irssi.get_script()
- except RuntimeError:
- return original_reload(module)
-
- name = module.__name__
- if '.' not in name:
- return import_module(name, name, None, script)
- i = name.rfind('.')
- pname = name[:i]
- parent = script.modules[pname]
- return import_module(name[i+1:], name, parent, script)
-
-
-# Now install our hooks
-__builtin__.__import__ = import_hook
-__builtin__.reload = reload_hook
-"""
-
+sys.stdout = Output(level = _irssi.MSGLEVEL_CLIENTCRAP)
+sys.stderr = Output(level = _irssi.MSGLEVEL_CLIENTERROR)
diff --git a/src/pycore.c b/src/pycore.c
index 0003392..a5c4b55 100644
--- a/src/pycore.c
+++ b/src/pycore.c
@@ -163,6 +163,8 @@ void python_init(void)
"import irssi_startup\n"
);
+ pyloader_auto_load();
+
/* assert(signal(SIGINT, intr_catch) != SIG_ERR); */
command_bind("py", NULL, (SIGNAL_FUNC) cmd_default);
diff --git a/src/pyloader.c b/src/pyloader.c
index e7cf88e..d7d65cc 100644
--- a/src/pyloader.c
+++ b/src/pyloader.c
@@ -113,23 +113,10 @@ static char *py_find_script(const char *name)
* (such as from g_strsplit) of the command line.
* The array needs at least one item
*/
-int pyloader_load_script_argv(char **argv)
+static int py_load_script_path_argv(const char *path, char **argv)
{
PyObject *module = NULL, *script = NULL;
- char *name = NULL, *path = NULL;
-
- if (py_get_script(argv[0], NULL) != NULL)
- {
- printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "script %s already loaded", argv[0]);
- return 0;
- }
-
- path = py_find_script(argv[0]);
- if (!path)
- {
- printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "script %s does not exist", argv[0]);
- return 0;
- }
+ char *name = NULL;
name = file_get_filename(path);
module = PyModule_New(name);
@@ -158,15 +145,14 @@ int pyloader_load_script_argv(char **argv)
/* PySys_WriteStdout("load %s, script -> 0x%x\n", argv[0], script); */
Py_DECREF(script);
- g_free(path);
return 1;
error:
if (PyErr_Occurred())
PyErr_Print();
- else
- printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "error loading script %s", argv[0]);
+
+ printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "error loading script %s", argv[0]);
if (script)
{
@@ -175,12 +161,48 @@ error:
pyscript_cleanup(script);
Py_DECREF(script);
}
-
- g_free(path);
return 0;
}
+static int py_load_script_path(const char *path)
+{
+ int ret;
+ char *argv[2];
+
+ argv[0] = file_get_filename(path);
+ argv[1] = NULL;
+
+ if (py_get_script(argv[0], NULL) != NULL)
+ pyloader_unload_script(argv[0]);
+
+ ret = py_load_script_path_argv(path, argv);
+ g_free(argv[0]);
+
+ return ret;
+}
+
+int pyloader_load_script_argv(char **argv)
+{
+ char *path;
+ int ret;
+
+ if (py_get_script(argv[0], NULL) != NULL)
+ pyloader_unload_script(argv[0]);
+
+ path = py_find_script(argv[0]);
+ if (!path)
+ {
+ printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "script %s does not exist", argv[0]);
+ return 0;
+ }
+
+ ret = py_load_script_path_argv(path, argv);
+ g_free(path);
+
+ return ret;
+}
+
int pyloader_load_script(char *name)
{
char *argv[2];
@@ -227,7 +249,7 @@ int pyloader_unload_script(const char *name)
return 0;
}
- PySys_WriteStdout("unload %s, script -> 0x%x\n", name, script);
+ /* PySys_WriteStdout("unload %s, script -> 0x%x\n", name, script); */
pyscript_cleanup(script);
@@ -332,6 +354,36 @@ void pyloader_list_destroy(GSList **list)
*list = NULL;
}
+void pyloader_auto_load(void)
+{
+ GSList *node;
+ char *autodir;
+ const char *name;
+ GDir *gd;
+
+ for (node = script_paths; node; node = node->next)
+ {
+ autodir = g_strdup_printf("%s/autorun", (char *)node->data);
+ gd = g_dir_open(autodir, 0, NULL);
+ g_free(autodir);
+
+ if (!gd)
+ continue;
+
+ while ((name = g_dir_read_name(gd)))
+ {
+ char *path = g_strdup_printf("%s/autorun/%s", (char*)node->data, name);
+
+ if (!strcmp(file_get_ext(name), "py"))
+ py_load_script_path(path);
+
+ g_free(path);
+ }
+
+ g_dir_close(gd);
+ }
+}
+
int pyloader_init(void)
{
char *pyhome;
@@ -343,7 +395,6 @@ int pyloader_init(void)
if (!script_modules)
return 0;
- /* XXX: load autorun scripts here */
/* Add script location to the load path */
pyhome = g_strdup_printf("%s/scripts", get_irssi_dir());
pyloader_add_script_path(pyhome);
@@ -351,7 +402,7 @@ int pyloader_init(void)
/* typically /usr/local/share/irssi/scripts */
pyloader_add_script_path(SCRIPTDIR);
-
+
return 1;
}
diff --git a/src/pyloader.h b/src/pyloader.h
index b7befd3..01ab3ed 100644
--- a/src/pyloader.h
+++ b/src/pyloader.h
@@ -18,6 +18,7 @@ GSList *pyloader_list(void);
void pyloader_list_destroy(GSList **list);
int pyloader_init(void);
+void pyloader_auto_load(void);
void pyloader_deinit(void);
#endif