]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/pythonmod/examples/dict.py
c8088a89c2f1297e9458b5a22aba92e28b36f94a
1 # -*- coding: utf-8 -*-
3 calc.py: DNS-based czech-english dictionary
5 Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz)
6 Marek Vavrusa (xvavru00 AT stud.fit.vutbr.cz)
8 This software is open source.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
21 * Neither the name of the organization nor the names of its
22 contributors may be used to endorse or promote products derived from this
23 software without specific prior written permission.
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
42 log_info("pythonmod: dict init")
43 f
= open("examples/dict_data.txt", "r")
46 if line
.startswith('#'):
48 itm
= line
.split("\t", 3)
53 if not (cs
in cz_dict
):
54 cz_dict
[cs
] = [en
] # [cs] = en
56 cz_dict
[cs
].append(en
) # [cs] = en
58 if not (en
in en_dict
):
59 en_dict
[en
] = [cs
] # [en] = cs
61 en_dict
[en
].append(cs
) # [en] = cs
68 log_info("pythonmod: dict deinit")
71 def operate(id, event
, qstate
, qdata
):
72 if (event
== MODULE_EVENT_NEW
) or (event
== MODULE_EVENT_PASS
):
74 if qstate
.qinfo
.qname_str
.endswith("._dict_.cz."):
76 aword
= ' '.join(qstate
.qinfo
.qname_list
[0:-4])
77 adict
= qstate
.qinfo
.qname_list
[-4]
79 log_info("pythonmod: dictionary look up; word:%s dict:%s" % (aword
,adict
))
82 if (adict
== "en") and (aword
in en_dict
):
83 words
= en_dict
[aword
] # EN -> CS
84 if (adict
== "cs") and (aword
in cz_dict
):
85 words
= cz_dict
[aword
] # CS -> EN
87 if len(words
) and ((qstate
.qinfo
.qtype
== RR_TYPE_TXT
) or (qstate
.qinfo
.qtype
== RR_TYPE_ANY
)):
89 msg
= DNSMessage(qstate
.qinfo
.qname_str
, RR_TYPE_TXT
, RR_CLASS_IN
, PKT_RD | PKT_RA | PKT_AA
)
91 msg
.answer
.append("%s 300 IN TXT \"%s\"" % (qstate
.qinfo
.qname_str
,w
.replace("\"","\\\"")))
93 if not msg
.set_return_msg(qstate
):
94 qstate
.ext_state
[id] = MODULE_ERROR
97 qstate
.return_rcode
= RCODE_NOERROR
98 qstate
.ext_state
[id] = MODULE_FINISHED
102 qstate
.return_rcode
= RCODE_SERVFAIL
103 qstate
.ext_state
[id] = MODULE_FINISHED
106 else: #Pass on the unknown query to the iterator
107 qstate
.ext_state
[id] = MODULE_WAIT_MODULE
110 elif event
== MODULE_EVENT_MODDONE
: #the iterator has finished
111 #we don't need modify result
112 qstate
.ext_state
[id] = MODULE_FINISHED
115 log_err("pythonmod: Unknown event")
116 qstate
.ext_state
[id] = MODULE_ERROR
119 def inform_super(id, qstate
, superqstate
, qdata
):