[gnome-mud] Python Plugin Patches



I've got two changes to how Python plugins are loaded:

1. Load the plugin scripts in alphabetical order. I had assumed this was
already the case due to the manual saying something to the affect of
"the scripts are loaded in order", but it turns out I was wrong. This is
useful since alphabetical order is significantly easier for users to
change than created date.

2. Add the plugin directory to the embedded python interpreter's
sys.path. This allows the scripts to import other scripts, which is
great! I have some thoughts of adding an additional directory to the
path, something like $HOME/.gnome-mud/plugins/lib which would contain
library modules only (those files would never be run in the
interpreter), but I haven't really thought that through yet. Also, note,
I appended the new directory to the path, rather than replacing
sys.path[0]. This runs counter to normal, where the "current" directory
is typically the first on the import search path. It seemed less
intrusive this way (and would prevent interference from a GnomeMud.py
compatibility module I created to allow me to do some minor debugging
using the python interpreter).

I have attached two patches, the first (alpha.patch) contains only the
alphabetical ordering patch, since I don't imagine anyone will have a
problem with that.

The second (loadplugins.patch) includes BOTH (sorry for being lazy! If
you need me to split them, I can do that too).
? alpha.patch
? src/modules.patch
Index: src/python.c
===================================================================
RCS file: /cvs/gnome/gnome-mud/src/python.c,v
retrieving revision 1.12
diff -U5 -r1.12 python.c
--- src/python.c	11 Feb 2004 12:58:25 -0000	1.12
+++ src/python.c	14 Sep 2005 02:58:23 -0000
@@ -418,43 +418,47 @@
     {NULL, NULL}
 };
 
 void python_init()
 {
-	DIR *dir;
-	FILE *fh;
-	gchar *foo, *bar;
-	struct dirent *flarp;
+  FILE *fh;
+  gchar *pluginDir, *pluginFilename;
+  struct dirent **files;
+  int numFiles;
+  int i;
 
-	(void) Py_InitModule("GnomeMud", GnomeMudMethods);
-	init_pyConnection();
+  (void) Py_InitModule("GnomeMud", GnomeMudMethods);
+  init_pyConnection();
 
-	foo = g_strdup_printf("%s/.gnome-mud/plugins/", g_get_home_dir());
-	dir = opendir(foo);
-	if (!dir) { g_free(foo); return; }
+  pluginDir = g_strdup_printf("%s/.gnome-mud/plugins/", g_get_home_dir());
 
-	while ((flarp = readdir(dir)))
-	{
-		if (strlen(flarp->d_name) < 4)
-			continue;
+  /* Open, and run all .py files in the plugin directory,
+     in alphabetical order.
+  */
+  numFiles = scandir(pluginDir, &files, NULL, alphasort);
+  for (i = 0; i < numFiles; i++)
+  {
+	  if (strlen(files[i]->d_name) < 4)
+	      continue;
 		
-		if (strcmp(".py",flarp->d_name + strlen(flarp->d_name) - 3))
-			continue;
+	  if (strcmp(".py",flarp->d_name + strlen(flarp->d_name) - 3))
+	      continue;
 		
-		bar = g_strdup_printf("%s%s",foo,flarp->d_name);
-		fh = fopen(bar, "r");
-		if (fh)
-		{
-			PyRun_AnyFile(fh, bar);
-			fclose(fh);
-		}
+	  pluginFilename = g_strdup_printf("%s%s",pluginDir,files[i]->d_name);
+	  fh = fopen(pluginFilename, "r");
+	  if (fh)
+	  {
+		  PyRun_AnyFile(fh, pluginFilename);
+		  fclose(fh);
+	  }
 
-		g_free(bar);
-	}
+	  g_free(pluginFilename);
+	  free(files[i]);
+  }
 
-	closedir(dir);
-	g_free(foo);
+  free(files);
+  g_free(pluginDir);
 }
 
 void python_end()
 {
 	GList *i = pymud_input_callbacks;
? pypath.patch
? src/modules.patch
Index: src/python.c
===================================================================
RCS file: /cvs/gnome/gnome-mud/src/python.c,v
retrieving revision 1.12
diff -u -5 -r1.12 python.c
--- src/python.c	11 Feb 2004 12:58:25 -0000	1.12
+++ src/python.c	14 Sep 2005 03:05:01 -0000
@@ -418,43 +418,76 @@
     {NULL, NULL}
 };
 
 void python_init()
 {
-	DIR *dir;
-	FILE *fh;
-	gchar *foo, *bar;
-	struct dirent *flarp;
-
-	(void) Py_InitModule("GnomeMud", GnomeMudMethods);
-	init_pyConnection();
-
-	foo = g_strdup_printf("%s/.gnome-mud/plugins/", g_get_home_dir());
-	dir = opendir(foo);
-	if (!dir) { g_free(foo); return; }
+  FILE *fh;
+  gchar *pluginDir, *pluginFilename;
+  struct dirent **files;
+  int numFiles;
+  int i;
+  PyObject *pstrSys, *pstrDir;
+  PyObject *pmodSys;
+  PyObject *sys_path;
+  
 
-	while ((flarp = readdir(dir)))
-	{
-		if (strlen(flarp->d_name) < 4)
-			continue;
+  (void) Py_InitModule("GnomeMud", GnomeMudMethods);
+  init_pyConnection();
+
+  pluginDir = g_strdup_printf("%s/.gnome-mud/plugins/", g_get_home_dir());
+
+  /* Append plugin path to sys.path
+   */
+  pstrSys = PyString_FromString("sys");
+  pmodSys = PyImport_Import(pstrSys);
+
+  Py_DECREF(pstrSys);
+  
+
+  if (pmodSys != NULL) {
+    sys_path = PyObject_GetAttrString(pmodSys, "path");
+
+    if (sys_path != NULL) {
+      pstrDir = PyString_FromString(pluginDir);
+
+      PyList_Append(sys_path, pstrDir);
+
+      Py_DECREF(pstrDir);
+
+    }
+    Py_DECREF(sys_path);
+
+  }
+  Py_DECREF(pmodSys);
+    
+
+  /* Open, and run all .py files in the plugin directory,
+     in alphabetical order.
+   */
+  numFiles = scandir(pluginDir, &files, NULL, alphasort);
+  for (i = 0; i < numFiles; i++)
+    {
+      if (strlen(files[i]->d_name) < 4)
+	continue;
 		
 		if (strcmp(".py",flarp->d_name + strlen(flarp->d_name) - 3))
 			continue;
 		
-		bar = g_strdup_printf("%s%s",foo,flarp->d_name);
-		fh = fopen(bar, "r");
-		if (fh)
-		{
-			PyRun_AnyFile(fh, bar);
-			fclose(fh);
-		}
-
-		g_free(bar);
+      pluginFilename = g_strdup_printf("%s%s",pluginDir,files[i]->d_name);
+      fh = fopen(pluginFilename, "r");
+      if (fh)
+	{
+	  PyRun_AnyFile(fh, pluginFilename);
+	  fclose(fh);
 	}
 
-	closedir(dir);
-	g_free(foo);
+      g_free(pluginFilename);
+      free(files[i]);
+    }
+
+  free(files);
+  g_free(pluginDir);
 }
 
 void python_end()
 {
 	GList *i = pymud_input_callbacks;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]