Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add native python and ruby bindings, and a hackish php client script

  • Loading branch information...
commit 15e9109b21d0e4975982a5911cee8981c8f4b5ae 1 parent 52bffb0
@eam eam authored
View
19 php_seco_range/range.php
@@ -0,0 +1,19 @@
+<?PHP
+// Author eam@yahoo-inc.com
+// This is a crappy hack, don't actually use it
+
+function expand_range($range) {
+ $ret = array();
+ $handle = fopen("http://range:9999/range/list?" . urlencode($range), "r");
+ if ($handle) {
+ while (!feof($handle)) {
+ $buffer = fgets($handle, 4096);
+ $buffer = rtrim($buffer);
+ $ret[] = $buffer;
+ }
+ fclose($handle);
+ }
+ return $ret;
+}
+
+?>
View
17 python_seco_librange/index.yaml
@@ -0,0 +1,17 @@
+---
+default:
+ build: '1'
+ name: python-secorange
+ release: '1'
+ requires:
+ - librange
+ summary: Python library for range operations
+ version: '1.0.2'
+
+deb:
+ dirtransforms:
+ - from: /usr/lib
+ to: /usr/local/lib
+x86_64:
+ nobuild: 1
+ preferarch: i386
View
4 python_seco_librange/scripts/build
@@ -0,0 +1,4 @@
+#!/bin/sh
+python setup.py build
+python setup.py install --prefix $DESTDIR/usr
+
View
2  python_seco_librange/source/MANIFEST
@@ -0,0 +1,2 @@
+secorange.c
+setup.py
View
173 python_seco_librange/source/secorange.c
@@ -0,0 +1,173 @@
+#include <Python.h>
+#include "range.h"
+
+static PyObject *exception = NULL;
+
+static PyObject*
+secorange_sortedexpandrange(PyObject *self, PyObject *args)
+{
+ const char* range;
+ int i;
+ PyObject* retval = NULL;
+ const char** r;
+
+ if (!PyArg_ParseTuple(args, "s", &range))
+ return NULL;
+
+ r = range_expand_sorted(range);
+ if (range_get_exception()) {
+ PyErr_SetString(exception, range_get_exception());
+ return NULL;
+ }
+ retval = PyList_New(0);
+ for (i=0; r[i] != NULL; i++)
+ PyList_Append(retval, PyString_FromString(r[i]));
+
+ return retval;
+}
+
+static PyObject*
+secorange_expandrange(PyObject *self, PyObject *args)
+{
+ const char* range;
+ int i;
+ PyObject* retval = NULL;
+ const char** r;
+
+ if (!PyArg_ParseTuple(args, "s", &range))
+ return NULL;
+
+ r = range_expand(range);
+ if (range_get_exception()) {
+ PyErr_SetString(exception, range_get_exception());
+ return NULL;
+ }
+ retval = PyList_New(0);
+ for (i=0; r[i] != NULL; i++)
+ PyList_Append(retval, PyString_FromString(r[i]));
+
+ return retval;
+}
+
+static PyObject*
+secorange_compressrange(PyObject *self, PyObject *args)
+{
+ int i, length;
+ PyObject* nodes;
+ const char** node_lst;
+ const char* result;
+
+ if (!PyArg_ParseTuple(args, "O", &nodes))
+ return NULL;
+
+ length = PyList_Size(nodes);
+ node_lst = malloc(sizeof(char*) * (length + 1));
+ for (i=0; i<length; i++)
+ node_lst[i] = PyString_AsString(PyList_GetItem(nodes, i));
+ node_lst[length] = NULL;
+
+ result = range_compress(node_lst, ",");
+ free(node_lst);
+ if (range_get_exception()) {
+ PyErr_SetString(exception, range_get_exception());
+ return NULL;
+ }
+
+ return Py_BuildValue("s", result);
+}
+
+static PyObject*
+secorange_librangeversion(PyObject *self, PyObject *args)
+{
+ return Py_BuildValue("s", range_get_version());
+}
+
+static PyObject*
+secorange_clearcaches(PyObject *self, PyObject *args)
+{
+ range_clear_caches();
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyObject*
+secorange_setaltpath(PyObject *self, PyObject *args)
+{
+ const char* path;
+ if (!PyArg_ParseTuple(args, "s", &path))
+ return NULL;
+ range_set_altpath(path);
+ if (range_get_exception()) {
+ PyErr_SetString(exception, range_get_exception());
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyObject*
+secorange_wantcaching(PyObject *self, PyObject *args)
+{
+ int want;
+ if (!PyArg_ParseTuple(args, "i", &want))
+ return NULL;
+
+ range_want_caching(want);
+ if (range_get_exception()) {
+ PyErr_SetString(exception, range_get_exception());
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyObject*
+secorange_wantwarnings(PyObject *self, PyObject *args)
+{
+ int want;
+ if (!PyArg_ParseTuple(args, "i", &want))
+ return NULL;
+
+ range_want_warnings(want);
+ if (range_get_exception()) {
+ PyErr_SetString(exception, range_get_exception());
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+
+static PyMethodDef SecorangeMethods[] = {
+ {"expand_range", secorange_expandrange, METH_VARARGS,
+ "Expand a seco range returning a list of nodes (not sorted)"},
+ {"compress_range", secorange_compressrange, METH_VARARGS,
+ "Compress a list of nodes"},
+ {"sorted_expand_range", secorange_sortedexpandrange, METH_VARARGS,
+ "Expand a seco range returning a list of nodes (sorted)"},
+ {"range_set_altpath", secorange_setaltpath, METH_VARARGS,
+ "Change the location of where to look for nodes.cf files"},
+ {"librange_version", secorange_librangeversion, METH_VARARGS,
+ "Get version of underlying librange"},
+ {"want_caching", secorange_wantcaching, METH_VARARGS,
+ "Enable or disable caching"},
+ {"want_warnings", secorange_wantwarnings, METH_VARARGS,
+ "Enable or disable warnings"},
+ {"clear_caches", secorange_clearcaches, METH_VARARGS,
+ "Clear caches"},
+ {NULL, NULL, 0, NULL}
+};
+
+
+PyMODINIT_FUNC
+initsecorange(void)
+{
+ PyObject *module, *dict;
+ range_startup();
+ module = Py_InitModule("secorange", SecorangeMethods);
+
+ dict = PyModule_GetDict(module);
+ exception = PyErr_NewException("secorange.error", NULL, NULL);
+ PyDict_SetItemString(dict, "error", exception);
+
+}
View
17 python_seco_librange/source/setup.py
@@ -0,0 +1,17 @@
+from distutils.core import setup, Extension
+
+module1 = Extension('secorange',
+ libraries = ['range', 'pcre'],
+ library_dirs = ['/usr/local/lib'],
+ sources = ['secorange.c'])
+
+setup (name = 'SecoRange',
+ version = '2.0',
+ description = 'Seco Range package',
+ author = 'Daniel Muino, Evan Miller',
+ author_email = 'dmuino@yahoo-inc.com,eam@yahoo-inc.com',
+ long_description = '''
+Deal with seco ranges
+''',
+ ext_modules = [module1])
+
View
63 ruby_seco_librange/range.rb
@@ -0,0 +1,63 @@
+# Author Evan Miller eam@yahoo-inc.com
+# Ruby bindings to librange
+
+require 'dl/import'
+
+class SecoRangeError < RuntimeError
+end
+
+module Seco
+ module Range
+ extend DL::Importable
+ dlload "librange.so"
+
+ extern "void range_startup()"
+ extern "const char ** range_expand(const char *)"
+ extern "const char ** range_expand_sorted(const char *)"
+ extern "char * range_compress(const char **)"
+ extern "char * range_parse(const char *)"
+ extern "int range_set_altpath(const char *)"
+ extern "void range_clear_caches()"
+ extern "void range_want_caching(int)"
+ extern "void range_want_warnings(int)"
+ extern "char * range_get_exception()"
+ extern "char * range_get_version()"
+
+ range_startup()
+
+ # override complex params
+ class << self
+ alias range_want_caching_c range_want_caching
+ alias range_want_warnings_c range_want_warnings
+
+ def expand_range(range)
+ res = range_expand(range)
+ e = range_get_exception
+ if e.nil?
+ res.to_a('S')
+ else
+ raise SecoRangeError, e.dup
+ end
+ end
+ def sorted_expand_range(r)
+ res = range_expand_sorted(r)
+ e = range_get_exception
+ if e.nil?
+ res.to_a('S')
+ else
+ raise SecoRangeError, e.dup
+ end
+ end
+ def compress_range(r)
+ r = r.dup << nil unless r[-1].nil?
+ res = range_compress(r.to_ptr)
+ e = range_get_exception
+ if e.nil?
+ res
+ else
+ raise SecoRangeError, e.dup
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.