#ifdef __cplusplus
extern "C" {
#endif
-
+
namespace xbmcvfs
{
/*****************************************************************
PyObject_HEAD
CFile* pFile;
} File;
-
+
PyDoc_STRVAR(file__doc__,
"File class.\n"
"\n"
- "'w' - opt open for write\n"
+ "'w' - opt open for write\n"
"example:\n"
" f = xbmcvfs.File(file, ['w'])\n");
-
+
PyObject* File_New(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *f_line;
File *self = (File *)type->tp_alloc(type, 0);
if (!self)
return NULL;
-
+
CStdString strSource;
if (!PyXBMCGetUnicodeString(strSource, f_line, 1))
return NULL;
-
+
self->pFile = new CFile();
if (strncmp(cLine, "w", 1) == 0)
{
CPyThreadState pyState;
self->pFile->OpenForWrite(strSource,true);
- pyState.Restore();
+ pyState.Restore();
}
- else
+ else
{
CPyThreadState pyState;
self->pFile->Open(strSource, READ_NO_CACHE);
}
return (PyObject*)self;
}
-
+
void File_Dealloc(File* self)
{
CPyThreadState pyState;
delete self->pFile;
pyState.Restore();
-
+
self->pFile = NULL;
self->ob_type->tp_free((PyObject*)self);
- }
-
+ }
+
// copy() method
PyDoc_STRVAR(copy__doc__,
"copy(source, destination) -- Copy file to destination, returns true/false.\n"
"\n"
"example:\n"
" - success = xbmcvfs.copy(source, destination)\n");
-
+
PyObject* vfs_copy(PyObject *self, PyObject *args)
{
PyObject *f_line;
CStdString strSource;
CStdString strDestnation;
bool bResult = true;
-
+
if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
if (!PyXBMCGetUnicodeString(strDestnation, d_line, 1)) return NULL;
CPyThreadState pyState;
bResult = CFile::Cache(strSource, strDestnation);
pyState.Restore();
-
+
return Py_BuildValue((char*)"b", bResult);
}
PyDoc_STRVAR(delete__doc__,
"\n"
"example:\n"
" - xbmcvfs.delete(file)\n");
-
+
// delete a file
PyObject* vfs_delete(File *self, PyObject *args, PyObject *kwds)
{
}
CStdString strSource;
if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
-
+
CPyThreadState pyState;
self->pFile->Delete(strSource);
pyState.Restore();
-
+
Py_INCREF(Py_None);
return Py_None;
-
+
}
-
+
PyDoc_STRVAR(rename__doc__,
"rename(file, newFileName) -- Rename file, returns true/false.\n"
"\n"
"\n"
"example:\n"
" - success = xbmcvfs.rename(file, newFileName)\n");
-
+
// rename a file
PyObject* vfs_rename(File *self, PyObject *args, PyObject *kwds)
{
CStdString strDestnation;
if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
if (!PyXBMCGetUnicodeString(strDestnation, d_line, 1)) return NULL;
-
+
bool bResult;
CPyThreadState pyState;
bResult = self->pFile->Rename(strSource,strDestnation);
pyState.Restore();
-
+
return Py_BuildValue((char*)"b", bResult);
-
- }
+
+ }
PyDoc_STRVAR(exists__doc__,
"exists(path) -- Check if file exists, returns true/false.\n"
"\n"
"example:\n"
" - success = xbmcvfs.exists(path)\n");
-
+
// check for a file or folder existance, mimics Pythons os.path.exists()
PyObject* vfs_exists(File *self, PyObject *args, PyObject *kwds)
{
}
CStdString strSource;
if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
-
+
bool bResult;
-
+
CPyThreadState pyState;
bResult = self->pFile->Exists(strSource, false);
pyState.Restore();
return Py_BuildValue((char*)"b", bResult);
- }
+ }
PyDoc_STRVAR(mkdir__doc__,
"mkdir(path) -- Create a folder.\n"
}
CStdString strSource;
if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
-
+
bool bResult;
-
+
CPyThreadState pyState;
bResult = CDirectory::Create(strSource);
pyState.Restore();
return Py_BuildValue((char*)"b", bResult);
- }
+ }
PyDoc_STRVAR(mkdirs__doc__,
"mkdirs(path) -- Create folder(s) - it will create all folders in the path.\n"
CStdString strSource;
if (!PyXBMCGetUnicodeString(strSource, f_line, 1))
return NULL;
-
+
CPyThreadState pyState;
bool bResult = CUtil::CreateDirectoryEx(strSource);
pyState.Restore();
-
+
return Py_BuildValue((char*)"b", bResult);
- }
-
+ }
+
PyDoc_STRVAR(rmdir__doc__,
"rmdir(path) -- Remove a folder.\n"
"\n"
}
CStdString strSource;
if (!PyXBMCGetUnicodeString(strSource, f_line, 1)) return NULL;
-
+
bool bResult;
if (bForce)
{
bResult = CFileUtils::DeleteItem(strSource, bForce);
pyState.Restore();
}
- else
+ else
{
CPyThreadState pyState;
bResult = CDirectory::Remove(strSource);
}
return Py_BuildValue((char*)"b", bResult);
- }
-
+ }
+
PyDoc_STRVAR(listdir__doc__,
"listdir(path) -- lists content of a folder.\n"
"\n"
CFileItemList items;
if (!PyXBMCGetUnicodeString(strSource, f_line, 1))
return NULL;
-
+
CPyThreadState pyState;
CDirectory::GetDirectory(strSource, items);
pyState.Restore();
-
+
PyObject *fileList = PyList_New(0);
PyObject *folderList = PyList_New(0);
for (int i=0; i < items.Size(); i++)
- {
+ {
CStdString itemPath = items[i]->GetPath();
PyObject *obj;
if (URIUtils::HasSlashAtEnd(itemPath)) // folder
- {
+ {
URIUtils::RemoveSlashAtEnd(itemPath);
CStdString strFileName = URIUtils::GetFileName(itemPath);
obj = Py_BuildValue((char*)"s", strFileName.c_str());
Py_DECREF(obj); //we have to do this as PyList_Append is known to leak
}
return Py_BuildValue((char*)"O,O", folderList, fileList);
- }
-
+ }
+
// define c functions to be used in python here
PyMethodDef xbmcvfsMethods[] = {
{(char*)"copy", (PyCFunction)vfs_copy, METH_VARARGS, copy__doc__},
" f = xbmcvfs.File(file)\n"
" b = f.read()\n"
" f.close()\n");
-
+
// read a file
PyObject* File_read(File *self, PyObject *args)
{
char* buffer = new char[readBytes + 1];
PyObject* ret = NULL;
if (buffer)
- {
+ {
unsigned int bytesRead;
CPyThreadState pyState;
bytesRead = self->pFile->Read(buffer, readBytes);
" f = xbmcvfs.File(file, 'w', True)\n"
" result = f.write(buffer)\n"
" f.close()\n");
-
+
// read a file
PyObject* File_write(File *self, PyObject *args)
{
CPyThreadState pyState;
bool bResult = self->pFile->Write( (void*) pBuffer, std::strlen( pBuffer ) + 1 );
pyState.Restore();
-
+
return Py_BuildValue((char*)"b", bResult);
}
-
+
PyDoc_STRVAR(size__doc__,
"size()\n"
"\n"
" f = xbmcvfs.File(file)\n"
" s = f.size()\n"
" f.close()\n");
-
+
// size of a file
PyObject* File_size(File *self, PyObject *args)
{
CPyThreadState pyState;
size = self->pFile->GetLength();
pyState.Restore();
-
+
return Py_BuildValue((char*)"L", size);
}
-
+
PyDoc_STRVAR(seek__doc__,
"seek()\n"
"\n"
" f = xbmcvfs.File(file)\n"
" result = f.seek(8129, 0)\n"
" f.close()\n");
-
+
// seek a file
PyObject* File_seek(File *self, PyObject *args)
{
int iWhence;
if (!PyArg_ParseTuple(args, (char*)"Li", &seekBytes, &iWhence ))
return NULL;
-
+
CPyThreadState pyState;
int64_t bResult = self->pFile->Seek(seekBytes,iWhence);
pyState.Restore();
-
+
return Py_BuildValue((char*)"L", bResult);
}
-
+
PyDoc_STRVAR(close__doc__,
"close()\n"
"\n"
"example:\n"
" f = xbmcvfs.File(file)\n"
" f.close()\n");
-
+
// close a file
PyObject* File_close(File *self, PyObject *args, PyObject *kwds)
{
CPyThreadState pyState;
self->pFile->Close();
pyState.Restore();
-
+
Py_INCREF(Py_None);
return Py_None;
}
* end of methods and python objects
* initxbmc(void);
*****************************************************************/
-
+
/*************************************************************
* Meta data for the xbmcvfs.File class
*************************************************************/
{(char*)"close", (PyCFunction)File_close, METH_NOARGS, close__doc__},
{NULL, NULL, 0, NULL}
};
-
+
PyTypeObject File_Type;
-
+
void initFile_Type()
{
PyXBMCInitializeTypeObject(&File_Type);
-
+
File_Type.tp_name = (char*)"xbmcvfs.File";
File_Type.tp_basicsize = sizeof(File);
File_Type.tp_dealloc = (destructor)File_Dealloc;
File_Type.tp_base = 0;
File_Type.tp_new = File_New;
}
-
+
PyMODINIT_FUNC
DeinitVFSModule()
{
// no need to Py_DECREF our objects (see InitXBMCMVFSModule()) as they were created only
// so that they could be added to the module, which steals a reference.
}
-
+
PyMODINIT_FUNC
InitVFSModule()
{
PyObject* pXbmcvfsModule;
pXbmcvfsModule = Py_InitModule((char*)"xbmcvfs", xbmcvfsMethods);
if (pXbmcvfsModule == NULL) return;
-
+
PyModule_AddObject(pXbmcvfsModule, (char*)"File", (PyObject*)&File_Type);
-
+
// constants
PyModule_AddStringConstant(pXbmcvfsModule, (char*)"__author__", (char*)PY_XBMC_AUTHOR);
PyModule_AddStringConstant(pXbmcvfsModule, (char*)"__date__", (char*)"25 May 2012");
PyModule_AddStringConstant(pXbmcvfsModule, (char*)"__version__", (char*)"1.4");
PyModule_AddStringConstant(pXbmcvfsModule, (char*)"__credits__", (char*)PY_XBMC_CREDITS);
PyModule_AddStringConstant(pXbmcvfsModule, (char*)"__platform__", (char*)PY_XBMC_PLATFORM);
- }
-
+ }
+
PyMODINIT_FUNC
InitVFSTypes(bool bInitTypes)
{
if (PyType_Ready(&File_Type)) return;
}
}
-
+
#ifdef __cplusplus
}
#endif