diff options
author | Raúl Benencia <rul@kalgan.cc> | 2012-10-09 15:58:34 -0300 |
---|---|---|
committer | Raúl Benencia <rul@kalgan.cc> | 2012-10-09 15:58:34 -0300 |
commit | b3fcf3b4e9dc49b0736e8ba1dfdab2e225999c6d (patch) | |
tree | d7e0a40b8554f792137e4ebb166f2be50d379406 | |
parent | 9866dfb204b8bdbb5bf341204371a4e4e458a9c4 (diff) |
Done some refactoring. Now values are printed in order
-rw-r--r-- | doc/pywhoisd.dia | bin | 3770 -> 4520 bytes | |||
-rw-r--r-- | lib/core.py | 87 | ||||
-rw-r--r-- | lib/model.py | 20 | ||||
-rw-r--r-- | res/footer.txt | 1 |
4 files changed, 72 insertions, 36 deletions
diff --git a/doc/pywhoisd.dia b/doc/pywhoisd.dia Binary files differindex df695a8..1ed0a5b 100644 --- a/doc/pywhoisd.dia +++ b/doc/pywhoisd.dia diff --git a/lib/core.py b/lib/core.py index e7ac571..2124763 100644 --- a/lib/core.py +++ b/lib/core.py @@ -18,43 +18,44 @@ class Daemon(): result = self.search_domain(q) else: # Try to find a person with the given query - person = search_person(q) + person = self.search_person(q) if person: result = person else: result = self.get_help() - return self.format_result(result) - - def format_result(self, values): - """Receive a dictionary and return a string""" - result = "" - for k, v in values.items(): - if k == 'domains': - result += '\nAssociated domains\n' - for i, dom in enumerate(v): - result += ' {0}\n'.format(dom.name) - elif k == 'admins': - result += '\nAssociated administrator/s\n ' - for i, adm in enumerate(v): - result += ' {0} {1} <{2}>\n'.format(adm.name, adm.surname, adm.email) - else: - result += '{0}: {1}\n'.format(k, v) + return result.formatted_answer() + +# def format_result(self, values): +# """Receive a dictionary and return a string""" +# result = "" +# for k, v in values.items(): +# if k == 'domains': +# for i, dom in enumerate(v): +# if len(v) == 1: +# result += 'domain: {0}\n'.format(dom.name) +# else: +# result += 'domain-{0}: {1}\n'.format(i, dom.name) +# elif k == 'admins': +# for i, adm in enumerate(v): +# if len(v) == 1: +# result += 'Admin: {0} {1} <{2}>\n'.format(adm.name, adm.surname, adm.email) +# else: +# result += 'Admin-{0}: {1} {2} <{3}>\n'.format(i, adm.name, adm.surname, adm.email) +# else: +# result += '{0}: {1}\n'.format(k, v) - return result +# return result def search_ip(self, ip): - result = {} - # Iterate over all IP block elements networks = self.data.get_networks() for key in networks: for block in networks[key].ip_blocks: if ipaddr.IPAddress(ip) in ipaddr.IPNetwork(block): - return networks[key].as_dict(self.data) + return networks[key].as_answer(self.data) - result['error'] = "Network not found" - return result + return Answer(AnswerValue(1, 'Error', 'Network not found')) def search_domain(self, domain): # Iterate over all network and check its domains @@ -62,9 +63,9 @@ class Daemon(): domains = self.data.get_domains() for network in networks.values(): if any(domains[d].name == domain for d in network.domains): - return network.as_dict(self.data) + return network.as_answer(self.data) - return {'error':'Domain not found'} + return Answer(AnswerValue(1, 'Error', 'Domain not found')) # TODO @@ -107,6 +108,42 @@ class Daemon(): return self.header +class Answer(): + def __init__(self): + self.values = [] + + def add_value(self, av): + self.values.append(av) + + def formatted_answer(self): + res = "" + for av in sorted(self.values, key=lambda v: v.priority): + res += av.formatted_value() + "\n" + + return res + +class AnswerValue(): + def __init__(self, priority, key, val): + self.priority = priority + self.key = key + self.val = val + + def value(self): + return self.val + + def formatted_value(self): + return "{0}: {1}".format(self.key, self.value()) + +class AnswerAdminValue(AnswerValue): + def __init__(self, priority, admin): + self.priority = priority + self.key = 'Admin' + self.adm = admin + + def value(self): + return '{0} {1} <{2}>\n'.format(self.adm.name, self.adm.surname, self.adm.email) + + class WhoisHandler(socketserver.BaseRequestHandler): def setup(self): diff --git a/lib/model.py b/lib/model.py index 933d901..1d1c605 100644 --- a/lib/model.py +++ b/lib/model.py @@ -1,6 +1,7 @@ from xml.etree.ElementTree import ElementTree from lib.config import Config +from lib.core import Answer, AnswerValue, AnswerAdminValue class Network(): """A simple network definition""" @@ -12,26 +13,25 @@ class Network(): self.ip_blocks = [] self.data = {} - def as_dict(self, data): + def as_answer(self, data): # Beautify - result = {} + result = Answer() domains = data.get_domains() persons = data.get_persons() - result['name'] = self.name + result.add_value(AnswerValue(0, 'Name', self.name)) # Domains - result['domains'] = [] for d in self.domains: - result['domains'].append(domains[d]) + result.add_value(AnswerValue(1, 'Domain', domains[d].name)) + + # Networks + for block in self.ip_blocks: + result.add_value(AnswerValue(1, 'Network', block)) # Admins - result['admins'] = [] for a in self.admins: - result['admins'].append(persons[a]) - - # Networks - result['networks'] = self.ip_blocks + result.add_value(AnswerAdminValue(2, persons[a])) return result diff --git a/res/footer.txt b/res/footer.txt index 6139265..33f72e4 100644 --- a/res/footer.txt +++ b/res/footer.txt @@ -1,2 +1 @@ - -----END WHOIS ANSWER----- |