- Support for importing python packages. (directories of python scripts containing...
authorCampbell Barton <ideasman42@gmail.com>
Sun, 19 Jul 2009 17:45:14 +0000 (17:45 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 19 Jul 2009 17:45:14 +0000 (17:45 +0000)
- BLI_add_slash returns the new string length.
- BLI_where_am_i() would often have /./ in the path (not incorrect but annoying, got into python exceptions)
- release/ui/space_image.py, py error referencing invalid keyword args.

release/ui/space_image.py
source/blender/blenkernel/intern/pointcache.c
source/blender/blenlib/BLI_fileops.h
source/blender/blenlib/intern/fileops.c
source/blender/blenlib/intern/util.c
source/blender/editors/space_file/file_ops.c
source/blender/python/intern/bpy_interface.c

index 7084a0f..3bd8c4c 100644 (file)
@@ -223,7 +223,7 @@ class IMAGE_HT_header(bpy.types.Header):
                        if show_uvedit:
                                row.itemM("IMAGE_MT_uvs")
 
-               layout.template_ID(sima, "image", new="image.new", open="image.open")
+               layout.template_ID(sima, "image", new="image.new") # open="image.open"
 
                """
                /* image select */
index 6107510..cde0587 100644 (file)
@@ -196,15 +196,14 @@ static int ptcache_path(PTCacheID *pid, char *filename)
                
                snprintf(filename, MAX_PTCACHE_PATH, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
                BLI_convertstringcode(filename, blendfilename);
-               BLI_add_slash(filename);
-               return strlen(filename);
+               return BLI_add_slash(filename); /* new strlen() */
        }
        
        /* use the temp path. this is weak but better then not using point cache at all */
        /* btempdir is assumed to exist and ALWAYS has a trailing slash */
        snprintf(filename, MAX_PTCACHE_PATH, "%s"PTCACHE_PATH"%d", btempdir, abs(getpid()));
-       BLI_add_slash(filename);
-       return strlen(filename);
+       
+       return BLI_add_slash(filename); /* new strlen() */
 }
 
 static int BKE_ptcache_id_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
index 146ca16..d2dd40b 100644 (file)
@@ -53,7 +53,7 @@ int   BLI_delete(char *file, int dir, int recursive);
 int   BLI_move(char *file, char *to);
 int   BLI_touch(const char *file);
 char *BLI_last_slash(const char *string);
-void  BLI_add_slash(char *string);
+int      BLI_add_slash(char *string);
 void  BLI_del_slash(char *string);
 char *first_slash(char *string);
 
index 917537b..42fd75a 100644 (file)
@@ -83,19 +83,22 @@ char *BLI_last_slash(const char *string) {
 }
 
 /* adds a slash if there isnt one there alredy */
-void BLI_add_slash(char *string) {
+int BLI_add_slash(char *string) {
        int len = strlen(string);
 #ifdef WIN32
        if (len==0 || string[len-1]!='\\') {
                string[len] = '\\';
                string[len+1] = '\0';
+               return len+1;
        }
 #else
        if (len==0 || string[len-1]!='/') {
                string[len] = '/';
                string[len+1] = '\0';
+               return len+1;
        }
 #endif
+       return len;
 }
 
 /* removes a slash if there is one */
index 78a4599..8eeca69 100644 (file)
@@ -499,9 +499,8 @@ int BLI_has_parent(char *path)
        int len;
        int slashes = 0;
        BLI_clean(path);
-       BLI_add_slash(path);
+       len = BLI_add_slash(path) - 1;
 
-       len = strlen(path)-1;
        while (len>=0) {
                if ((path[len] == '\\') || (path[len] == '/'))
                        slashes++;
@@ -1276,22 +1275,12 @@ void BLI_split_dirfile(char *string, char *dir, char *file)
 /* simple appending of filename to dir, does not check for valid path! */
 void BLI_join_dirfile(char *string, const char *dir, const char *file)
 {
-       int sl_dir = strlen(dir);
-       BLI_strncpy(string, dir, FILE_MAX);
-       if (sl_dir > FILE_MAX-1) sl_dir = FILE_MAX-1;
+       int sl_dir;
        
-       /* only add seperator if needed */
-#ifdef WIN32
-       if (string[sl_dir-1] != '\\') {
-               string[sl_dir] = '\\';
-               sl_dir++;
-       }
-#else
-       if (string[sl_dir-1] != '/') {
-               string[sl_dir] = '/';
-               sl_dir++;
-       }
-#endif
+       if(string != dir) /* compare pointers */
+               BLI_strncpy(string, dir, FILE_MAX);
+       
+       sl_dir= BLI_add_slash(string);
        
        if (sl_dir <FILE_MAX) {
                BLI_strncpy(string + sl_dir, file, FILE_MAX-sl_dir);
@@ -1343,13 +1332,13 @@ void BLI_where_am_i(char *fullname, const char *name)
 {
        char filename[FILE_MAXDIR+FILE_MAXFILE];
        char *path = NULL, *temp;
-       int len;
+       
 #ifdef _WIN32
        char *seperator = ";";
-       char *slash = "\\";
+       char slash = '\\';
 #else
        char *seperator = ":";
-       char *slash = "/";
+       char slash = '/';
 #endif
 
        
@@ -1369,11 +1358,13 @@ void BLI_where_am_i(char *fullname, const char *name)
                if (name[0] == '.') {
                        // relative path, prepend cwd
                        BLI_getwdN(fullname);
-                       len = strlen(fullname);
-                       if (len && fullname[len -1] != slash[0]) {
-                               strcat(fullname, slash);
-                       }
-                       strcat(fullname, name);
+                       
+                       // not needed but avoids annoying /./ in name
+                       if(name && name[0]=='.' && name[1]==slash)
+                               BLI_join_dirfile(fullname, fullname, name+2);
+                       else
+                               BLI_join_dirfile(fullname, fullname, name);
+                       
                        add_win32_extension(fullname);
                } else if (BLI_last_slash(name)) {
                        // full path
@@ -1392,11 +1383,7 @@ void BLI_where_am_i(char *fullname, const char *name)
                                        } else {
                                                strncpy(filename, path, sizeof(filename));
                                        }
-                                       len = strlen(filename);
-                                       if (len && filename[len - 1] != slash[0]) {
-                                               strcat(filename, slash);
-                                       }
-                                       strcat(filename, name);
+                                       BLI_join_dirfile(fullname, fullname, name);
                                        if (add_win32_extension(filename)) {
                                                strcpy(fullname, filename);
                                                break;
index 0d36bac..cd981b7 100644 (file)
@@ -716,7 +716,6 @@ int file_directory_exec(bContext *C, wmOperator *unused)
                                while ( (*d == '\\') || (*d == '/') )
                                        d++;
                                BLI_strncpy(homestr,  BLI_gethome(), FILE_MAX);
-                               BLI_add_slash(homestr);
                                BLI_join_dirfile(tmpstr, homestr, d);
                                BLI_strncpy(sfile->params->dir, tmpstr, sizeof(sfile->params->dir));
                        }
index 18dae97..520856a 100644 (file)
@@ -28,6 +28,7 @@
 #include "MEM_guardedalloc.h"
 
 #include "BLI_util.h"
+#include "BLI_fileops.h"
 #include "BLI_string.h"
 
 #include "BKE_context.h"
@@ -441,6 +442,26 @@ int BPY_run_python_script_space(const char *modulename, const char *func)
 #include "PIL_time.h"
 #endif
 
+/* for use by BPY_run_ui_scripts only */
+static int bpy_import_module(char *modname, int reload)
+{
+       PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0);
+       if (mod) {
+               if (reload) {
+                       PyObject *mod_orig= mod;
+                       mod= PyImport_ReloadModule(mod);
+                       Py_DECREF(mod_orig);
+               }
+       }
+
+       if(mod) {
+               Py_DECREF(mod); /* could be NULL from reloading */
+               return 0;
+       } else {
+               return -1;
+       }
+}
+
 /* XXX this is temporary, need a proper script registration system for 2.5 */
 void BPY_run_ui_scripts(bContext *C, int reload)
 {
@@ -453,10 +474,9 @@ void BPY_run_ui_scripts(bContext *C, int reload)
        char *dirname;
        char path[FILE_MAX];
        char *dirs[] = {"ui", "io", NULL};
-       int a;
+       int a, err;
        
        PyGILState_STATE gilstate;
-       PyObject *mod;
        PyObject *sys_path;
 
        gilstate = PyGILState_Ensure();
@@ -486,27 +506,32 @@ void BPY_run_ui_scripts(bContext *C, int reload)
                while((de = readdir(dir)) != NULL) {
                        /* We could stat the file but easier just to let python
                         * import it and complain if theres a problem */
+                       err = 0;
 
-                       file_extension = strstr(de->d_name, ".py");
-                       
-                       if(file_extension && file_extension[3] == '\0') {
-                               BLI_strncpy(path, de->d_name, (file_extension - de->d_name) + 1); /* cut off the .py on copy */
-                               mod= PyImport_ImportModuleLevel(path, NULL, NULL, NULL, 0);
-                               if (mod) {
-                                       if (reload) {
-                                               PyObject *mod_orig= mod;
-                                               mod= PyImport_ReloadModule(mod);
-                                               Py_DECREF(mod_orig);
-                                       }
+                       if (de->d_name[0] == '.') {
+                               /* do nothing, probably .svn */
+                       }
+                       else if(de->d_type==DT_DIR) {
+                               /* support packages */
+                               BLI_join_dirfile(path, dirname, de->d_name);
+                               BLI_join_dirfile(path, path, "__init__.py");
+
+                               if(BLI_exists(path)) {
+                                       bpy_import_module(de->d_name, reload);
                                }
+                       } else {
+                               /* normal py files */
+                               file_extension = strstr(de->d_name, ".py");
                                
-                               if(mod) {
-                                       Py_DECREF(mod); /* could be NULL from reloading */
-                               } else {
-                                       BPy_errors_to_report(NULL);
-                                       fprintf(stderr, "unable to import \"%s\"  %s/%s\n", path, dirname, de->d_name);
+                               if(file_extension && file_extension[3] == '\0') {
+                                       de->d_name[(file_extension - de->d_name) + 1] = '\0';
+                                       bpy_import_module(de->d_name, reload);
                                }
+                       }
 
+                       if(err==-1) {
+                               BPy_errors_to_report(NULL);
+                               fprintf(stderr, "unable to import %s/%s\n", dirname, de->d_name);
                        }
                }