New features covered.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 21 Sep 2008 15:16:38 +0000 (16:16 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 21 Sep 2008 15:16:38 +0000 (16:16 +0100)
  * Included fdpass, fwatch and fdflags.
  * Picked up new daemonize and versioncmp functions.

defs.pxi
fdutils.pyx [new file with mode: 0644]
fwatch.pyx [new file with mode: 0644]
mLib.pyx
str.pyx
stuff.pyx [new file with mode: 0644]

index 4b1ecde..f452b28 100644 (file)
--- a/defs.pxi
+++ b/defs.pxi
@@ -88,17 +88,20 @@ cdef extern from 'netdb.h':
 
 cdef extern from 'Python.h':
 
+  ctypedef struct PyObject:
+    pass
+  ctypedef struct PyTypeObject:
+    pass
+
   object PyString_FromStringAndSize(char *p, int len)
   int PyString_AsStringAndSize(obj, char **p, int *len) except -1
   int PyObject_AsReadBuffer(obj, void **buf, int *len) except -1
   int PyObject_TypeCheck(obj, ty)
   object PyInt_FromLong(long i)
   object PyLong_FromUnsignedLong(unsigned long i)
+  char *PyString_AS_STRING(string)
+  int _PyString_Resize(PyObject **string, int size) except -1
 
-  ctypedef struct PyObject:
-    pass
-  ctypedef struct PyTypeObject:
-    pass
   void Py_INCREF(PyObject *obj)
   void Py_DECREF(PyObject *obj)
 
@@ -163,6 +166,9 @@ cdef extern from 'mLib/str.h':
   int str_matchx(char *p, char *s, unsigned f)
   void str_sanitize(char *d, char *p, size_t sz)
 
+cdef extern from 'mLib/versioncmp.h':
+  int _versioncmp "versioncmp"(char *va, char *vb)
+
 #----- Form-urlencoding functions -------------------------------------------
 
 cdef extern from 'mLib/url.h':
@@ -383,9 +389,37 @@ cdef extern from 'mLib/ident.h':
 cdef extern from 'mLib/quis.h':
   void _ego "ego"(char *prog)
   char *_quis "quis"()
+
 cdef extern from 'mLib/report.h':
   void _moan "moan"(char *f, char *msg)
 
+#----- File comparison ------------------------------------------------------
+
+cdef extern from 'mLib/fwatch.h':
+  ctypedef struct fwatch:
+    pass
+  void fwatch_init(fwatch *f, char *name)
+  void fwatch_initfd(fwatch *f, int fd)
+  int fwatch_update(fwatch *f, char *name)
+  int fwatch_updatefd(fwatch *f, int fd)
+
+#----- File descriptor hacking ----------------------------------------------
+
+cdef extern from 'mLib/fdflags.h':
+  int _fdflags "fdflags"(int fd,
+                         unsigned fbic, unsigned fxor,
+                         unsigned fdbic, unsigned fdxor)
+
+cdef extern from 'mLib/fdpass.h':
+  int fdpass_send(int sock, int fd, void *p, size_t sz)
+  int fdpass_recv(int sock, int *fd, void *p, size_t sz)
+
+#----- Daemon utilities -----------------------------------------------------
+
+cdef extern from 'mLib/daemonize.h':
+  int _daemonize "daemonize"()
+  void _detachtty "detachtty"()
+
 #----- Internal utilities ---------------------------------------------------
 
 cdef extern from 'grim.h':
diff --git a/fdutils.pyx b/fdutils.pyx
new file mode 100644 (file)
index 0000000..7923d44
--- /dev/null
@@ -0,0 +1,62 @@
+# -*-pyrex-*-
+#
+# $Id$
+#
+# Messing with file descriptors
+#
+# (c) 2007 Straylight/Edgeware
+#
+
+#----- Licensing notice -----------------------------------------------------
+#
+# This file is part of the Python interface to mLib.
+#
+# mLib/Python is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# mLib/Python is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with mLib/Python; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+def fdflags(file,
+            unsigned fbic = 0, unsigned fxor = 0,
+            unsigned fdbic = 0, unsigned fdxor = 0):
+  cdef int rc
+  rc = _fdflags(_getfd(fd), fbix, fxor, fdbic, fdxor)
+  if rc < 0:
+    _oserror()
+  return rc
+
+def fdsend(sock, file, buffer):
+  cdef void *p
+  cdef int len
+  cdef int rc
+  PyObject_AsReadBuffer(buffer, &p, &len)
+  rc = fdpass_send(_getfd(sock), _getfd(file), p, len)
+  if rc < 0:
+    _oserror()
+  return rc
+
+def fdrecv(sock, unsigned size):
+  cdef void *p
+  cdef buf
+  cdef int len
+  cdef PyObject *obj
+  cdef int fd
+  buf = PyString_FromStringAndSize(NULL, len)
+  p = PyString_AS_STRING(buf)
+  len = fdpass_recv(_getfd(sock), &fd, p, size)
+  if len < 0:
+    _oserror()
+  obj = <PyObject *>buf
+  _PyString_Resize(&obj, len)
+  return fd, <object>obj
+
+#----- That's all, folks ----------------------------------------------------
diff --git a/fwatch.pyx b/fwatch.pyx
new file mode 100644 (file)
index 0000000..a9e4b97
--- /dev/null
@@ -0,0 +1,47 @@
+# -*-pyrex-*-
+#
+# $Id$
+#
+# Watching files for changes
+#
+# (c) 2007 Straylight/Edgeware
+#
+
+#----- Licensing notice -----------------------------------------------------
+#
+# This file is part of the Python interface to mLib.
+#
+# mLib/Python is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# mLib/Python is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with mLib/Python; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+cdef class FWatch:
+  cdef fwatch fw
+  cdef file
+  def __new__(me, file):
+    _init(me, file)
+  def __init__(me, file):
+    _init(me, file)
+  cdef _init(me, file):
+    if isinstance(file, str):
+      fwatch_init(&me.fw, file)
+    else:
+      fwatch_initfd(&me.fw, _getfd(file))
+    me.file = file
+  def update(me):
+    if isinstance(me.file, str):
+      fwatch_update(&me.fw, me.file)
+    else:
+      fwatch_initfd(&me.fw, _getfd(me.file))
+
+#----- That's all, folks ----------------------------------------------------
index 61ae890..2a43cbd 100644 (file)
--- a/mLib.pyx
+++ b/mLib.pyx
@@ -62,6 +62,15 @@ include 'url.pyx'
 
 include 'report.pyx'
 
+# --- File utilities ---
+
+include 'fwatch.pyx'
+include 'fdutils.pyx'
+
+# --- Other useful stuff ---
+
+include 'stuff.pyx'
+
 # --- Buffering ---
 
 include 'lbuf.pyx'
diff --git a/str.pyx b/str.pyx
index b7d80e6..bbde1b0 100644 (file)
--- a/str.pyx
+++ b/str.pyx
@@ -93,6 +93,8 @@ def sanitize(char *p, int n = -1):
   d = buf
   xfree(buf)
   return d
-  
+
+def versioncmp(char *va, char *vb):
+  return _versioncmp(va, vb)
 
 #----- That's all, folks ----------------------------------------------------
diff --git a/stuff.pyx b/stuff.pyx
new file mode 100644 (file)
index 0000000..45efd49
--- /dev/null
+++ b/stuff.pyx
@@ -0,0 +1,35 @@
+# -*-pyrex-*-
+#
+# $Id$
+#
+# Various small things
+#
+# (c) 2007 Straylight/Edgeware
+#
+
+#----- Licensing notice -----------------------------------------------------
+#
+# This file is part of the Python interface to mLib.
+#
+# mLib/Python is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# mLib/Python is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with mLib/Python; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+def detachtty():
+  _detachtty()
+
+def daemonize():
+  if _daemonize():
+    _oserror()
+
+#----- That's all, folks ----------------------------------------------------