Skip to content

Commit 15e9109

Browse files
committed
Add native python and ruby bindings, and a hackish php client script
1 parent 52bffb0 commit 15e9109

File tree

7 files changed

+295
-0
lines changed

7 files changed

+295
-0
lines changed

php_seco_range/range.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?PHP
2+
3+
// This is a crappy hack, don't actually use it
4+
5+
function expand_range($range) {
6+
$ret = array();
7+
$handle = fopen("http://range:9999/range/list?" . urlencode($range), "r");
8+
if ($handle) {
9+
while (!feof($handle)) {
10+
$buffer = fgets($handle, 4096);
11+
$buffer = rtrim($buffer);
12+
$ret[] = $buffer;
13+
}
14+
fclose($handle);
15+
}
16+
return $ret;
17+
}
18+
19+
?>

python_seco_librange/index.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
default:
3+
build: '1'
4+
name: python-secorange
5+
release: '1'
6+
requires:
7+
- librange
8+
summary: Python library for range operations
9+
version: '1.0.2'
10+
11+
deb:
12+
dirtransforms:
13+
- from: /usr/lib
14+
to: /usr/local/lib
15+
x86_64:
16+
nobuild: 1
17+
preferarch: i386

python_seco_librange/scripts/build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
python setup.py build
3+
python setup.py install --prefix $DESTDIR/usr
4+

python_seco_librange/source/MANIFEST

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
secorange.c
2+
setup.py
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#include <Python.h>
2+
#include "range.h"
3+
4+
static PyObject *exception = NULL;
5+
6+
static PyObject*
7+
secorange_sortedexpandrange(PyObject *self, PyObject *args)
8+
{
9+
const char* range;
10+
int i;
11+
PyObject* retval = NULL;
12+
const char** r;
13+
14+
if (!PyArg_ParseTuple(args, "s", &range))
15+
return NULL;
16+
17+
r = range_expand_sorted(range);
18+
if (range_get_exception()) {
19+
PyErr_SetString(exception, range_get_exception());
20+
return NULL;
21+
}
22+
retval = PyList_New(0);
23+
for (i=0; r[i] != NULL; i++)
24+
PyList_Append(retval, PyString_FromString(r[i]));
25+
26+
return retval;
27+
}
28+
29+
static PyObject*
30+
secorange_expandrange(PyObject *self, PyObject *args)
31+
{
32+
const char* range;
33+
int i;
34+
PyObject* retval = NULL;
35+
const char** r;
36+
37+
if (!PyArg_ParseTuple(args, "s", &range))
38+
return NULL;
39+
40+
r = range_expand(range);
41+
if (range_get_exception()) {
42+
PyErr_SetString(exception, range_get_exception());
43+
return NULL;
44+
}
45+
retval = PyList_New(0);
46+
for (i=0; r[i] != NULL; i++)
47+
PyList_Append(retval, PyString_FromString(r[i]));
48+
49+
return retval;
50+
}
51+
52+
static PyObject*
53+
secorange_compressrange(PyObject *self, PyObject *args)
54+
{
55+
int i, length;
56+
PyObject* nodes;
57+
const char** node_lst;
58+
const char* result;
59+
60+
if (!PyArg_ParseTuple(args, "O", &nodes))
61+
return NULL;
62+
63+
length = PyList_Size(nodes);
64+
node_lst = malloc(sizeof(char*) * (length + 1));
65+
for (i=0; i<length; i++)
66+
node_lst[i] = PyString_AsString(PyList_GetItem(nodes, i));
67+
node_lst[length] = NULL;
68+
69+
result = range_compress(node_lst, ",");
70+
free(node_lst);
71+
if (range_get_exception()) {
72+
PyErr_SetString(exception, range_get_exception());
73+
return NULL;
74+
}
75+
76+
return Py_BuildValue("s", result);
77+
}
78+
79+
static PyObject*
80+
secorange_librangeversion(PyObject *self, PyObject *args)
81+
{
82+
return Py_BuildValue("s", range_get_version());
83+
}
84+
85+
static PyObject*
86+
secorange_clearcaches(PyObject *self, PyObject *args)
87+
{
88+
range_clear_caches();
89+
Py_INCREF(Py_None);
90+
return Py_None;
91+
}
92+
93+
static PyObject*
94+
secorange_setaltpath(PyObject *self, PyObject *args)
95+
{
96+
const char* path;
97+
if (!PyArg_ParseTuple(args, "s", &path))
98+
return NULL;
99+
range_set_altpath(path);
100+
if (range_get_exception()) {
101+
PyErr_SetString(exception, range_get_exception());
102+
return NULL;
103+
}
104+
Py_INCREF(Py_None);
105+
return Py_None;
106+
}
107+
108+
static PyObject*
109+
secorange_wantcaching(PyObject *self, PyObject *args)
110+
{
111+
int want;
112+
if (!PyArg_ParseTuple(args, "i", &want))
113+
return NULL;
114+
115+
range_want_caching(want);
116+
if (range_get_exception()) {
117+
PyErr_SetString(exception, range_get_exception());
118+
return NULL;
119+
}
120+
Py_INCREF(Py_None);
121+
return Py_None;
122+
}
123+
124+
static PyObject*
125+
secorange_wantwarnings(PyObject *self, PyObject *args)
126+
{
127+
int want;
128+
if (!PyArg_ParseTuple(args, "i", &want))
129+
return NULL;
130+
131+
range_want_warnings(want);
132+
if (range_get_exception()) {
133+
PyErr_SetString(exception, range_get_exception());
134+
return NULL;
135+
}
136+
Py_INCREF(Py_None);
137+
return Py_None;
138+
}
139+
140+
141+
static PyMethodDef SecorangeMethods[] = {
142+
{"expand_range", secorange_expandrange, METH_VARARGS,
143+
"Expand a seco range returning a list of nodes (not sorted)"},
144+
{"compress_range", secorange_compressrange, METH_VARARGS,
145+
"Compress a list of nodes"},
146+
{"sorted_expand_range", secorange_sortedexpandrange, METH_VARARGS,
147+
"Expand a seco range returning a list of nodes (sorted)"},
148+
{"range_set_altpath", secorange_setaltpath, METH_VARARGS,
149+
"Change the location of where to look for nodes.cf files"},
150+
{"librange_version", secorange_librangeversion, METH_VARARGS,
151+
"Get version of underlying librange"},
152+
{"want_caching", secorange_wantcaching, METH_VARARGS,
153+
"Enable or disable caching"},
154+
{"want_warnings", secorange_wantwarnings, METH_VARARGS,
155+
"Enable or disable warnings"},
156+
{"clear_caches", secorange_clearcaches, METH_VARARGS,
157+
"Clear caches"},
158+
{NULL, NULL, 0, NULL}
159+
};
160+
161+
162+
PyMODINIT_FUNC
163+
initsecorange(void)
164+
{
165+
PyObject *module, *dict;
166+
range_startup();
167+
module = Py_InitModule("secorange", SecorangeMethods);
168+
169+
dict = PyModule_GetDict(module);
170+
exception = PyErr_NewException("secorange.error", NULL, NULL);
171+
PyDict_SetItemString(dict, "error", exception);
172+
173+
}

python_seco_librange/source/setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from distutils.core import setup, Extension
2+
3+
module1 = Extension('secorange',
4+
libraries = ['range', 'pcre'],
5+
library_dirs = ['/usr/local/lib'],
6+
sources = ['secorange.c'])
7+
8+
setup (name = 'SecoRange',
9+
version = '2.0',
10+
description = 'Seco Range package',
11+
author = 'Daniel Muino, Evan Miller',
12+
13+
long_description = '''
14+
Deal with seco ranges
15+
''',
16+
ext_modules = [module1])
17+

ruby_seco_librange/range.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Author Evan Miller [email protected]
2+
# Ruby bindings to librange
3+
4+
require 'dl/import'
5+
6+
class SecoRangeError < RuntimeError
7+
end
8+
9+
module Seco
10+
module Range
11+
extend DL::Importable
12+
dlload "librange.so"
13+
14+
extern "void range_startup()"
15+
extern "const char ** range_expand(const char *)"
16+
extern "const char ** range_expand_sorted(const char *)"
17+
extern "char * range_compress(const char **)"
18+
extern "char * range_parse(const char *)"
19+
extern "int range_set_altpath(const char *)"
20+
extern "void range_clear_caches()"
21+
extern "void range_want_caching(int)"
22+
extern "void range_want_warnings(int)"
23+
extern "char * range_get_exception()"
24+
extern "char * range_get_version()"
25+
26+
range_startup()
27+
28+
# override complex params
29+
class << self
30+
alias range_want_caching_c range_want_caching
31+
alias range_want_warnings_c range_want_warnings
32+
33+
def expand_range(range)
34+
res = range_expand(range)
35+
e = range_get_exception
36+
if e.nil?
37+
res.to_a('S')
38+
else
39+
raise SecoRangeError, e.dup
40+
end
41+
end
42+
def sorted_expand_range(r)
43+
res = range_expand_sorted(r)
44+
e = range_get_exception
45+
if e.nil?
46+
res.to_a('S')
47+
else
48+
raise SecoRangeError, e.dup
49+
end
50+
end
51+
def compress_range(r)
52+
r = r.dup << nil unless r[-1].nil?
53+
res = range_compress(r.to_ptr)
54+
e = range_get_exception
55+
if e.nil?
56+
res
57+
else
58+
raise SecoRangeError, e.dup
59+
end
60+
end
61+
end
62+
end
63+
end

0 commit comments

Comments
 (0)