source: trunk/debathena/debathena/python-hesiod/_hesiod.pyx @ 23150

Revision 23150, 2.0 KB checked in by broder, 14 years ago (diff)
Add python-hesiod package for the new version of pyhesiodfs
Line 
1cdef extern from "hesiod.h":
2    int hesiod_init(void **context)
3    void hesiod_end(void *context)
4    char *hesiod_to_bind(void *context, char *name, char *type)
5    char **hesiod_resolve(void *context, char *name, char *type)
6    void hesiod_free_list(void *context, char **list)
7    # This function isn't defined in 3.0.2, which is what Debian/Ubuntu use
8    #void hesiod_free_string(void *context, char *str)
9
10cdef extern from "errno.h":
11    int errno
12
13cdef extern from "string.h":
14    char * strerror(int errnum)
15
16cdef extern from "stdlib.h":
17    void free(void *)
18
19cdef void * __context
20
21cdef class __ContextManager:
22    def __init__(self):
23        if hesiod_init(&__context) == -1:
24            raise IOError(errno, strerror(errno))
25   
26    def __del__(self):
27        hesiod_end(__context)
28
29cdef object __cm
30__cm = __ContextManager()
31
32import threading
33__lookup_lock = threading.Lock()
34
35def bind(hes_name, hes_type):
36    """
37    Convert the provided arguments into a DNS name.
38   
39    The DNS name derived from the name and type provided is used to
40    actually execute the Hesiod lookup.
41    """
42    cdef object py_result
43    cdef char * c_result
44   
45    name_str, type_str = map(str, (hes_name, hes_type))
46   
47    c_result = hesiod_to_bind(__context, name_str, type_str)
48    if c_result is NULL:
49        raise IOError(errno, strerror(errno))
50    py_result = c_result
51   
52    free(c_result)
53    return py_result
54
55def resolve(hes_name, hes_type):
56    """
57    Return a list of records matching the given name and type.
58    """
59    cdef int i
60    cdef object py_result
61    py_result = list()
62    cdef char ** c_result
63   
64    name_str, type_str = map(str, (hes_name, hes_type))
65   
66    __lookup_lock.acquire()
67    c_result = hesiod_resolve(__context, name_str, type_str)
68    err = errno
69    __lookup_lock.release()
70   
71    if c_result is NULL:
72        raise IOError(err, strerror(err))
73    i = 0
74    while True:
75        if c_result[i] is NULL:
76            break
77        py_result.append(c_result[i])
78        i = i + 1
79   
80    hesiod_free_list(__context, c_result)
81    return py_result
Note: See TracBrowser for help on using the repository browser.