Skip to content
Extraits de code Groupes Projets
Valider b1e85b9b rédigé par Patrick Watrin's avatar Patrick Watrin
Parcourir les fichiers

First IO support (work in progress)

parent 0a622ab9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#include <Python.h>
#include "UnitexLibIO.h"
static char unitex_docstring[] =
"This module provides some usefull C function to work with the Unitex library.";
static char unitex_get_vfs_file_list_docstring[] =
"This function converts a C array of string (char**) into a Python list.";
static PyObject *unitex_get_vfs_file_list(PyObject *self, PyObject *args);
PyObject *unitex_get_vfs_file_list(PyObject *self, PyObject *args) {
char *filename;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
char **array=GetUnitexFileList(filename);
if (array==NULL)
return PyList_New(0);
unsigned int size = 0;
while ((*(array + size))!=NULL) {
size ++;
}
PyObject *list = PyList_New(size);
for (unsigned int i = 0; i != size; ++i) {
PyList_SET_ITEM(list, i, PyUnicode_FromString(array[i]));
}
char **array_walk=array;
while ((*array_walk)!=NULL) {
free(*array_walk);
array_walk++;
}
free(array);
return list;
}
static PyMethodDef unitex_methods[] = {
{"unitex_get_vfs_file_list", unitex_get_vfs_file_list, METH_VARARGS, unitex_get_vfs_file_list_docstring},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef unitexdef = {
PyModuleDef_HEAD_INIT,
"_unitex",
unitex_docstring,
-1,
unitex_methods
};
PyMODINIT_FUNC PyInit__unitex(void) {
PyObject *module = PyModule_Create(&unitexdef);
if (module == NULL)
return NULL;
return module;
}
...@@ -5,7 +5,7 @@ import os ...@@ -5,7 +5,7 @@ import os
import subprocess import subprocess
import sys import sys
from distutils.core import setup from distutils.core import setup, Extension
from distutils.command.build import build from distutils.command.build import build
from distutils.command.clean import clean from distutils.command.clean import clean
from distutils.command.install import install from distutils.command.install import install
...@@ -121,9 +121,15 @@ setup( ...@@ -121,9 +121,15 @@ setup(
data_files = [ data_files = [
], ],
# cmdclass = { ext_modules=[
# "build": CustomBuild, Extension("_unitex",
# "clean": CustomClean, include_dirs = [UNITEX_INC],
# "install": CustomInstall sources = ["extensions/_unitex.c"])
# } ],
cmdclass = {
"build": CustomBuild,
"clean": CustomClean,
"install": CustomInstall
}
) )
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from unitex.io import *
class TestUnitexIO(unittest.TestCase):
def test_01_(self):
pass
if __name__ == '__main__':
unittest.main()
...@@ -11,48 +11,121 @@ def enable_stdout(): ...@@ -11,48 +11,121 @@ def enable_stdout():
"""This function enable Unitex standard output. This should be used """This function enable Unitex standard output. This should be used
for debug purposes only. for debug purposes only.
""" """
pass swk = 0
trashOutput = ctypes.c_int(0)
fnc_stdOutWrite = None
privatePtr = None
ret = LIBUNITEX.SetStdWriteCB(swk, trashOutput, fnc_stdOutWrite, privatePtr)
if ret == 0:
raise UnitexException("Enabling stdout failed!")
def disable_stdout(): def disable_stdout():
"""This function disable Unitex standard output to ensure multithread """This function disable Unitex standard output to ensure multithread
output consistency (i.e. avoid output mixing between threads) and to output consistency (i.e. avoid output mixing between threads) and to
improve performances. improve performances.
""" """
pass swk = 0
trashOutput = ctypes.c_int(1)
fnc_stdOutWrite = None
privatePtr = None
ret = LIBUNITEX.SetStdWriteCB(swk, trashOutput, fnc_stdOutWrite, privatePtr)
if ret == 0:
raise UnitexException("Disabling stdout failed!")
def enable_stderr():
"""This function enable Unitex error output. This should be used
for debug purposes only.
"""
swk = 1
trashOutput = ctypes.c_int(0)
fnc_stdOutWrite = None
privatePtr = None
class UnitexFile(object): ret = LIBUNITEX.SetStdWriteCB(swk, trashOutput, fnc_stdOutWrite, privatePtr)
if ret == 0:
raise UnitexException("Enabling stderr failed!")
def __init__(self): def disable_stderr():
raise NotImplementedError """This function disable Unitex error output to ensure multithread
output consistency (i.e. avoid output mixing between threads) and to
improve performances.
"""
swk = 1
trashOutput = ctypes.c_int(1)
fnc_stdOutWrite = None
privatePtr = None
def open(self, path, mode=None, encoding=None): ret = LIBUNITEX.SetStdWriteCB(swk, trashOutput, fnc_stdOutWrite, privatePtr)
raise NotImplementedError if ret == 0:
raise UnitexException("Disabling stderr failed!")
def close(self):
raise NotImplementedError
def flush(self):
raise NotImplementedError
def seek(self, offset): class UnitexIOConstants:
raise NotImplementedError
VFS_PREFIX = "$:"
def tell(self):
raise NotImplementedError
def write(self, data):
raise NotImplementedError
def writelines(self, lines): def vfs_cp(source_path, target_path):
_source_path = ctypes.c_char_p(bytes(str(source_path), "utf-8"))
_target_path = ctypes.c_char_p(bytes(str(target_path), "utf-8"))
ret = LIBUNITEX.CopyUnitexFile(_source_path, _target_path)
if ret != 0:
raise UnitexException("File copy failed!")
def vfs_rm(path):
_path = ctypes.c_char_p(bytes(str(path), "utf-8"))
ret = LIBUNITEX.RemoveUnitexFile(_path)
if ret != 0:
raise UnitexException("File suppression failed!")
def vfs_mv(old_path, new_path):
_old_path = ctypes.c_char_p(bytes(str(old_path), "utf-8"))
_new_path = ctypes.c_char_p(bytes(str(new_path), "utf-8"))
ret = LIBUNITEX.RenameUnitexFile(_old_path, _new_path)
if ret != 0:
raise UnitexException("File renaming failed!")
def mkdir(path):
_path = ctypes.c_char_p(bytes(str(path), "utf-8"))
ret = LIBUNITEX.CreateUnitexFolder(_path)
if ret != 0:
raise UnitexException("Folder creation failed!")
def rmdir(path):
_path = ctypes.c_char_p(bytes(str(path), "utf-8"))
ret = LIBUNITEX.RemoveUnitexFolder(_path)
if ret != 0:
raise UnitexException("Folder suppression failed!")
class VirtualFile(object):
def __init__(self):
self.__file = None
self.__mode = None
def open(self, file, mode=None):
self.__file = file
self.__mode = mode
raise NotImplementedError raise NotImplementedError
def read(self, size=None): def close(self):
raise NotImplementedError raise NotImplementedError
def readline(self): def write(self, data):
if self.__mode not in ("w", "a"):
raise UnitexException("File '%s' is opened in read mode..." % self.__file)
raise NotImplementedError raise NotImplementedError
def readlines(self): def read(self):
raise NotImplementedError raise NotImplementedError
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter