2 * Copyright (C) 2005-2011 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 #if (defined USE_EXTERNAL_PYTHON)
24 #if (defined HAVE_LIBPYTHON2_6)
25 #include <python2.6/Python.h>
26 #elif (defined HAVE_LIBPYTHON2_5)
27 #include <python2.5/Python.h>
28 #elif (defined HAVE_LIBPYTHON2_4)
29 #include <python2.4/Python.h>
31 #error "Could not determine version of Python to use."
34 #include "python/Include/Python.h"
36 #include "../XBPythonDll.h"
38 #include "filesystem/File.h"
40 #include "pythreadstate.h"
43 using namespace XFILE;
44 using namespace PYXBMC;
47 #pragma code_seg("PY_TEXT")
48 #pragma data_seg("PY_DATA")
49 #pragma bss_seg("PY_BSS")
50 #pragma const_seg("PY_RDATA")
53 #if defined(__GNUG__) && (__GNUC__>4) || (__GNUC__==4 && __GNUC_MINOR__>=2)
54 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
63 /*****************************************************************
64 * start of xbmcvfs methods
65 *****************************************************************/
72 PyDoc_STRVAR(copy__doc__,
73 "copy(source, destination) -- copy file to destination, returns true/false.\n"
75 "source : file to copy.\n"
76 "destination : destination file"
79 " success = xbmcvfs.copy(source, destination)\n");
81 PyObject* vfs_copy(PyObject *self, PyObject *args)
85 if (!PyArg_ParseTuple(
94 CStdString strDestnation;
97 if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
98 if (!PyXBMCGetUnicodeString(strDestnation, d_line, 1)) return NULL;
100 CPyThreadState pyState;
101 bResult = CFile::Cache(strSource, strDestnation);
104 return Py_BuildValue((char*)"b", bResult);
106 PyDoc_STRVAR(delete__doc__,
109 "file : file to delete"
112 " xbmcvfs.delete(file)\n");
115 PyObject* vfs_delete(File *self, PyObject *args, PyObject *kwds)
118 if (!PyArg_ParseTuple(
125 CStdString strSource;
126 if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
128 CPyThreadState pyState;
129 self->pFile->Delete(strSource);
137 PyDoc_STRVAR(rename__doc__,
138 "rename(file, newFileName)\n"
140 "file : file to reaname"
141 "newFileName : new filename, including the full path"
144 " success = xbmcvfs.rename(file,newFileName)\n");
147 PyObject* vfs_rename(File *self, PyObject *args, PyObject *kwds)
151 if (!PyArg_ParseTuple(
159 CStdString strSource;
160 CStdString strDestnation;
161 if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
162 if (!PyXBMCGetUnicodeString(strDestnation, d_line, 1)) return NULL;
166 CPyThreadState pyState;
167 bResult = self->pFile->Rename(strSource,strDestnation);
170 return Py_BuildValue((char*)"b", bResult);
174 PyDoc_STRVAR(exists__doc__,
177 "path : file or folder"
180 " success = xbmcvfs.exists(path)\n");
182 // check for a file or folder existance, mimics Pythons os.path.exists()
183 PyObject* vfs_exists(File *self, PyObject *args, PyObject *kwds)
186 if (!PyArg_ParseTuple(
193 CStdString strSource;
194 if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
198 CPyThreadState pyState;
199 bResult = self->pFile->Exists(strSource, false);
202 return Py_BuildValue((char*)"b", bResult);
205 // define c functions to be used in python here
206 PyMethodDef xbmcvfsMethods[] = {
207 {(char*)"copy", (PyCFunction)vfs_copy, METH_VARARGS, copy__doc__},
208 {(char*)"delete", (PyCFunction)vfs_delete, METH_VARARGS, delete__doc__},
209 {(char*)"rename", (PyCFunction)vfs_rename, METH_VARARGS, rename__doc__},
210 {(char*)"exists", (PyCFunction)vfs_exists, METH_VARARGS, exists__doc__},
211 {NULL, NULL, 0, NULL}
214 /*****************************************************************
215 * end of methods and python objects
217 *****************************************************************/
223 // no need to Py_DECREF our objects (see InitXBMCMVFSModule()) as they were created only
224 // so that they could be added to the module, which steals a reference.
230 // init general xbmc modules
231 PyObject* pXbmcvfsModule;
232 pXbmcvfsModule = Py_InitModule((char*)"xbmcvfs", xbmcvfsMethods);
233 if (pXbmcvfsModule == NULL) return;