mirror of
https://github.com/arsenetar/send2trash.git
synced 2025-05-08 17:59:50 +00:00
Converted the compiled osx module to ctypes.
--HG-- branch : py3k
This commit is contained in:
parent
b5315cb73d
commit
a6907d57a9
@ -1,58 +0,0 @@
|
|||||||
/* Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
|
||||||
|
|
||||||
This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
|
||||||
which should be included with this package. The terms are also available at
|
|
||||||
http://www.hardcoded.net/licenses/bsd_license
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define PY_SSIZE_T_CLEAN
|
|
||||||
#include "Python.h"
|
|
||||||
#include <CoreServices/CoreServices.h>
|
|
||||||
|
|
||||||
static PyObject* send2trash_osx_send(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
UInt8 *utf8_chars;
|
|
||||||
FSRef fp;
|
|
||||||
OSStatus op_result;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "es", "utf-8", &utf8_chars)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
FSPathMakeRefWithOptions(utf8_chars, kFSPathMakeRefDoNotFollowLeafSymlink, &fp, NULL);
|
|
||||||
op_result = FSMoveObjectToTrashSync(&fp, NULL, kFSFileOperationDefaultOptions);
|
|
||||||
PyMem_Free(utf8_chars);
|
|
||||||
if (op_result != noErr) {
|
|
||||||
PyErr_SetString(PyExc_OSError, GetMacOSStatusCommentString(op_result));
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return Py_None;
|
|
||||||
}
|
|
||||||
|
|
||||||
static PyMethodDef TrashMethods[] = {
|
|
||||||
{"send", send2trash_osx_send, METH_VARARGS, ""},
|
|
||||||
{NULL, NULL, 0, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct PyModuleDef TrashDef = {
|
|
||||||
PyModuleDef_HEAD_INIT,
|
|
||||||
"send2trash_osx",
|
|
||||||
NULL,
|
|
||||||
-1,
|
|
||||||
TrashMethods,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
|
|
||||||
PyObject *
|
|
||||||
PyInit_send2trash_osx(void)
|
|
||||||
{
|
|
||||||
PyObject *m = PyModule_Create(&TrashDef);
|
|
||||||
if (m == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
@ -4,9 +4,41 @@
|
|||||||
# which should be included with this package. The terms are also available at
|
# which should be included with this package. The terms are also available at
|
||||||
# http://www.hardcoded.net/licenses/bsd_license
|
# http://www.hardcoded.net/licenses/bsd_license
|
||||||
|
|
||||||
import send2trash_osx
|
from ctypes import cdll, byref, Structure, c_char, c_char_p
|
||||||
|
from ctypes.util import find_library
|
||||||
|
|
||||||
|
Foundation = cdll.LoadLibrary(find_library('Foundation'))
|
||||||
|
CoreServices = cdll.LoadLibrary(find_library('CoreServices'))
|
||||||
|
|
||||||
|
GetMacOSStatusCommentString = Foundation.GetMacOSStatusCommentString
|
||||||
|
GetMacOSStatusCommentString.restype = c_char_p
|
||||||
|
FSPathMakeRefWithOptions = CoreServices.FSPathMakeRefWithOptions
|
||||||
|
FSMoveObjectToTrashSync = CoreServices.FSMoveObjectToTrashSync
|
||||||
|
|
||||||
|
kFSPathMakeRefDefaultOptions = 0
|
||||||
|
kFSPathMakeRefDoNotFollowLeafSymlink = 0x01
|
||||||
|
|
||||||
|
kFSFileOperationDefaultOptions = 0
|
||||||
|
kFSFileOperationOverwrite = 0x01
|
||||||
|
kFSFileOperationSkipSourcePermissionErrors = 0x02
|
||||||
|
kFSFileOperationDoNotMoveAcrossVolumes = 0x04
|
||||||
|
kFSFileOperationSkipPreflight = 0x08
|
||||||
|
|
||||||
|
class FSRef(Structure):
|
||||||
|
_fields_ = [('hidden', c_char * 80)]
|
||||||
|
|
||||||
|
def check_op_result(op_result):
|
||||||
|
if op_result:
|
||||||
|
msg = GetMacOSStatusCommentString(op_result).decode('utf-8')
|
||||||
|
raise OSError(msg)
|
||||||
|
|
||||||
def send2trash(path):
|
def send2trash(path):
|
||||||
if not isinstance(path, str):
|
if not isinstance(path, bytes):
|
||||||
path = str(path, 'utf-8')
|
path = path.encode('utf-8')
|
||||||
send2trash_osx.send(path)
|
fp = FSRef()
|
||||||
|
opts = kFSPathMakeRefDoNotFollowLeafSymlink
|
||||||
|
op_result = FSPathMakeRefWithOptions(path, opts, byref(fp), None)
|
||||||
|
check_op_result(op_result)
|
||||||
|
opts = kFSFileOperationDefaultOptions
|
||||||
|
op_result = FSMoveObjectToTrashSync(byref(fp), None, opts)
|
||||||
|
check_op_result(op_result)
|
||||||
|
6
setup.py
6
setup.py
@ -6,12 +6,6 @@ from distutils.extension import Extension
|
|||||||
|
|
||||||
exts = []
|
exts = []
|
||||||
|
|
||||||
if sys.platform == 'darwin':
|
|
||||||
exts.append(Extension(
|
|
||||||
'send2trash_osx',
|
|
||||||
[op.join('modules', 'send2trash_osx.c')],
|
|
||||||
extra_link_args=['-framework', 'CoreServices'],
|
|
||||||
))
|
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
exts.append(Extension(
|
exts.append(Extension(
|
||||||
'send2trash_win',
|
'send2trash_win',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user