Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2009-07-19 21:45:14 +0400
committerCampbell Barton <ideasman42@gmail.com>2009-07-19 21:45:14 +0400
commit979bec79c373b5bfae0bfe6e74e6e92bf3214ff2 (patch)
tree291054f0baf5e95ff118137863cccf0867b4b53c /source
parentd410135408fcce7856cc044ba717297c89302a34 (diff)
- Support for importing python packages. (directories of python scripts containing an __init__.py)
- 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.
Diffstat (limited to 'source')
-rw-r--r--source/blender/blenkernel/intern/pointcache.c7
-rw-r--r--source/blender/blenlib/BLI_fileops.h2
-rw-r--r--source/blender/blenlib/intern/fileops.c5
-rw-r--r--source/blender/blenlib/intern/util.c47
-rw-r--r--source/blender/editors/space_file/file_ops.c1
-rw-r--r--source/blender/python/intern/bpy_interface.c61
6 files changed, 68 insertions, 55 deletions
diff --git a/source/blender/blenkernel/intern/pointcache.c b/source/blender/blenkernel/intern/pointcache.c
index 6107510fa47..cde0587772e 100644
--- a/source/blender/blenkernel/intern/pointcache.c
+++ b/source/blender/blenkernel/intern/pointcache.c
@@ -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)
diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index 146ca16606e..d2dd40b21a5 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -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);
diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c
index 917537bf03d..42fd75a543e 100644
--- a/source/blender/blenlib/intern/fileops.c
+++ b/source/blender/blenlib/intern/fileops.c
@@ -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 */
diff --git a/source/blender/blenlib/intern/util.c b/source/blender/blenlib/intern/util.c
index 78a4599b3b3..8eeca6900a1 100644
--- a/source/blender/blenlib/intern/util.c
+++ b/source/blender/blenlib/intern/util.c
@@ -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;
diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c
index 0d36bac7505..cd981b7b419 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -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));
}
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 18dae972db4..520856a8d0b 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -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);
}
}