3 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version. The Blender
9 * Foundation also sells licenses for use in proprietary software under
10 * the Blender License. See http://www.blender.org/BL/ for information
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * This is a new part of Blender.
27 * Contributor(s): Willian P. Germano
29 * ***** END GPL/BL DUAL LICENSE BLOCK *****
32 #include <BKE_utildefines.h>
33 #include <BLI_blenlib.h>
36 #include "gen_utils.h"
39 /*****************************************************************************/
40 /* Python API function prototypes for the sys module. */
41 /*****************************************************************************/
42 static PyObject *M_sys_basename (PyObject *self, PyObject *args);
43 static PyObject *M_sys_dirname (PyObject *self, PyObject *args);
44 static PyObject *M_sys_splitext (PyObject *self, PyObject *args);
45 static PyObject *M_sys_exists (PyObject *self, PyObject *args);
46 static PyObject *M_sys_time (PyObject *self);
48 /*****************************************************************************/
49 /* The following string definitions are used for documentation strings. */
50 /* In Python these will be written to the console when doing a */
51 /* Blender.sys.__doc__ */
52 /*****************************************************************************/
53 static char M_sys_doc[] =
54 "The Blender.sys submodule\n\
56 This is a minimal system module to supply simple functionality available\n\
57 in the default Python module os.";
59 static char M_sys_basename_doc[] =
60 "(path) - Split 'path' in dir and filename.\n\
61 Return the filename.";
63 static char M_sys_dirname_doc[] =
64 "(path) - Split 'path' in dir and filename.\n\
67 static char M_sys_splitext_doc[] =
68 "(path) - Split 'path' in root and extension:\n\
69 /this/that/file.ext -> ('/this/that/file','.ext').\n\
70 Return the pair (root, extension).";
72 static char M_sys_time_doc[] =
73 "() - Return a float representing time elapsed in seconds.\n\
74 Each successive call is garanteed to return values greater than or\n\
75 equal to the previous call.";
77 static char M_sys_exists_doc[] =
78 "(path) - Return 1 if given pathname (file or dir) exists, 0 otherwise.";
80 /*****************************************************************************/
81 /* Python method structure definition for Blender.sys module: */
82 /*****************************************************************************/
83 struct PyMethodDef M_sys_methods[] = {
84 {"basename", M_sys_basename, METH_VARARGS, M_sys_basename_doc},
85 {"dirname", M_sys_dirname, METH_VARARGS, M_sys_dirname_doc},
86 {"splitext", M_sys_splitext, METH_VARARGS, M_sys_splitext_doc},
87 {"exists", M_sys_exists, METH_VARARGS, M_sys_exists_doc},
88 {"time", (PyCFunction)M_sys_time, METH_NOARGS, M_sys_time_doc},
92 /* Module Functions */
94 static PyObject *g_sysmodule = NULL; /* pointer to Blender.sys module */
96 PyObject *sys_Init (void)
98 PyObject *submodule, *dict, *sep;
100 submodule = Py_InitModule3("Blender.sys", M_sys_methods, M_sys_doc);
102 g_sysmodule = submodule;
104 dict = PyModule_GetDict(submodule);
107 sep = Py_BuildValue("s", "\\");
109 sep = Py_BuildValue("s", "/");
114 PyDict_SetItemString(dict, "dirsep" , sep);
115 PyDict_SetItemString(dict, "sep" , sep);
121 static PyObject *M_sys_basename (PyObject *self, PyObject *args)
125 char *name, *p, basename[FILE_MAXFILE];
129 if (!PyArg_ParseTuple(args, "s", &name))
130 return EXPP_ReturnPyObjError (PyExc_TypeError,
131 "expected string argument");
135 c = PyObject_GetAttrString (g_sysmodule, "dirsep");
136 sep = PyString_AsString(c)[0];
139 p = strrchr(name, sep);
142 n = name + len - p - 1; /* - 1 because we don't want the sep */
144 if (n > FILE_MAXFILE)
145 return EXPP_ReturnPyObjError(PyExc_RuntimeError, "path too long");
147 strncpy(basename, p+1, n); /* + 1 to skip the sep */
149 return Py_BuildValue("s", basename);
152 return Py_BuildValue("s", name);
155 static PyObject *M_sys_dirname (PyObject *self, PyObject *args)
159 char *name, *p, dirname[FILE_MAXDIR];
163 if (!PyArg_ParseTuple(args, "s", &name))
164 return EXPP_ReturnPyObjError (PyExc_TypeError,
165 "expected string argument");
167 c = PyObject_GetAttrString (g_sysmodule, "dirsep");
168 sep = PyString_AsString(c)[0];
171 p = strrchr(name, sep);
177 return EXPP_ReturnPyObjError (PyExc_RuntimeError, "path too long");
179 strncpy(dirname, name, n);
181 return Py_BuildValue("s", dirname);
184 return Py_BuildValue("s", "."); /* XXX need to fix this? (is crossplatform?)*/
187 static PyObject *M_sys_splitext (PyObject *self, PyObject *args)
191 char *name, *dot, *p, path[FILE_MAXFILE], ext[FILE_MAXFILE];
195 if (!PyArg_ParseTuple(args, "s", &name))
196 return EXPP_ReturnPyObjError (PyExc_TypeError,
197 "expected string argument");
201 c = PyObject_GetAttrString (g_sysmodule, "dirsep");
202 sep = PyString_AsString(c)[0];
205 dot = strrchr(name, '.');
207 if (!dot) return Py_BuildValue("ss", name, "");
209 p = strrchr(name, sep);
212 if (p > dot) return Py_BuildValue("ss", name, "");
215 n = name + len - dot;
217 /* loong extensions are supported -- foolish, but Python's os.path.splitext
218 * supports them, so ... */
219 if (n > FILE_MAXFILE || (len - n ) > FILE_MAXFILE)
220 EXPP_ReturnPyObjError(PyExc_RuntimeError, "path too long");
222 strncpy(ext, dot, n);
224 strncpy(path, name, dot - name);
225 path[dot - name] = 0;
227 return Py_BuildValue("ss", path, ext);
230 static PyObject *M_sys_time (PyObject *self)
232 double t = PIL_check_seconds_timer();
233 return Py_BuildValue("d", t);
236 static PyObject *M_sys_exists (PyObject *self, PyObject *args)
241 if (!PyArg_ParseTuple(args, "s", &fname))
242 return EXPP_ReturnPyObjError (PyExc_TypeError,
243 "expected string (file path) argument");
245 i = BLI_exists(fname);
247 if (i) return Py_BuildValue("i", 1); /* path was found */
249 return Py_BuildValue("i", 0); /* path doesn't exist */