PageRenderTime 31ms CodeModel.GetById 11ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ADNS.py

http://adns-python.googlecode.com/
Python | 71 lines | 52 code | 17 blank | 2 comment | 18 complexity | 9eed263002f3ac725128dbfb7c690f79 MD5 | raw file
 1#!/usr/bin/python
 2
 3"""High-level interface to adns."""
 4
 5import adns
 6from exceptions import Exception
 7
 8class Error(Exception): pass
 9
10class QueryEngine:
11
12    callback_submit = None
13    callback_submit_reverse = None
14    callback_submit_reverse_any = None
15    
16    def __init__(self, s=None):
17        self._s = s or adns.init(adns.iflags.noautosys)
18        self._queries = {}
19
20    def synchronous(self, qname, rr, flags=0):
21        return self._s.synchronous(qname, rr, flags)
22
23    def submit(self, qname, rr, flags=0, callback=None, extra=None):
24        callback = callback or self.callback_submit
25        if not callback: raise Error, "callback required"
26        q = self._s.submit(qname, rr, flags)
27        self._queries[q] = qname, rr, flags, callback, extra
28
29    def submit_reverse(self, qname, rr, flags=0, callback=None, extra=None):
30        callback = callback or self.callback_submit_reverse
31        if not callback: raise Error, "callback required"
32        q = self._s.submit_reverse(qname, rr, flags)
33        self._queries[q] = qname, rr, flags, callback, extra
34
35    def submit_reverse_any(self, qname, rr, flags=0,
36                           callback=None, extra=None):
37        callback = callback or self.callback_submit_reverse_any
38        if not callback: raise Error, "callback required"
39        q = self._s.submit_reverse_any(qname, rr, flags)
40        self._queries[q] = qname, rr, flags, callback, extra
41
42    def cancel(self, query):
43        query.cancel()
44        try: del self._queries[query]
45        except KeyError: pass
46        
47    def run(self, timeout=0):
48        for q in self._s.completed(timeout):
49            answer = q.check()
50            qname, rr, flags, callback, extra = self._queries[q]
51            del self._queries[q]
52            apply(callback, (answer, qname, rr, flags, extra))
53
54    def finished(self):
55        return not len(self._queries)
56
57    def finish(self):
58        while not self.finished():
59            self.run(1)
60
61    def run_max(self, max):
62        from time import time
63        quittime = time()+max
64        while not self.finished() and time()<=quittime:
65            self.run(1)
66
67    def globalsystemfailure(self):
68        self._s.globalsystemfailure()
69        self._queries.clear()
70
71init = QueryEngine