From 4d30bf280dbdb68ad72e01e911bf039b48ccb869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Thu, 2 Aug 2012 18:06:23 -0300 Subject: Completed XML parsing --- core.py | 20 +++++++++------ examples/networks.xml | 40 ++++++++++++++++++++++------- model.py | 22 ++++++++-------- xml.py | 69 +++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 115 insertions(+), 36 deletions(-) diff --git a/core.py b/core.py index 98284e1..a68d1e5 100644 --- a/core.py +++ b/core.py @@ -55,25 +55,31 @@ class Daemon(): def show_help(): pass -class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): +class WhoisHandler(socketserver.BaseRequestHandler): def handle(self): + daemon = self.server.daemon data = self.request.recv(100) cur_thread = threading.current_thread() - response = self.get_header() + "\n" + self.get_answer(data) + "\n" + self.get_footer() + response = daemon.get_header() + "\n" + daemon.query(data) + "\n" + daemon.get_footer() + self.request.sendall(response) -class ClassicServer(socketserver.TCPServer): - def __init__(self, config): +class ClassicServer(socketserver.ThreadingTCPServer): + def __init__(self, config, daemon): host = config['Servers']['classic_host'] port = config['Servers']['classic_port'] + self.daemon = daemon - TCPServer.__init__(self, (host, port), ThreadedTCPRequestHandler) + ThreadingTCPServer.__init__(self, (host, port), WhoisHandler) def start(self): pass -class WebServer(socketserver.TCPServer): - def __init__(self, config): +class WebServer(socketserver.ThreadingTCPServer): + def __init__(self, config, daemon): self.host = config['Servers']['web_host'] self.port = config['Servers']['web_port'] + self.daemon = daemon + + ThreadingTCPServer.__init__(self, (host, port), WhoisHandler) diff --git a/examples/networks.xml b/examples/networks.xml index 641071d..7efe017 100644 --- a/examples/networks.xml +++ b/examples/networks.xml @@ -1,13 +1,35 @@ - - - one.network.example + + + Network one 10.10.0.0/24 - Admin Istrator <admin@one.network.example> + 1 + 1 - - two.network.example - 10.11.0.0/24 - Admin Ister <admin@two.network.example> + + + Network one + 10.11.0.0/24 + 2 + 2 - \ No newline at end of file + + + Admin + Istrator + admin@one.network.example + + + + Admin + Ister + admin@two.network.example + + + + one.network.example + + + two.network.example + + \ No newline at end of file diff --git a/model.py b/model.py index f63cbd6..fbba248 100644 --- a/model.py +++ b/model.py @@ -1,19 +1,20 @@ class Network(): """A simple network definition""" - def __init__(self, name): - self.name = name - self.domain = [] + def __init__(self): + self.name = "" + self.domains = [] + self.admins = [] self.ip_blocks = [] self.data = {} class Domain(): """A simple domain definition""" - def __init__(self, name, domain): - self.name = name - self.domain = domain + def __init__(self): + self.name = "" self.admins = [] + self.data = {} def add_admin(self, admin): """Add an administrator for this network""" @@ -23,10 +24,11 @@ class Domain(): class Person(): """A simple person definition""" - def __init__(self, name, surname, email): - self.name = name - self.surname = surname - self.email = email + def __init__(self): + self.name = "" + self.surname = "" + self.email = "" + self.data = {} class Data(): """Abstract class for storing and getting information""" diff --git a/xml.py b/xml.py index c49e01a..e01a338 100644 --- a/xml.py +++ b/xml.py @@ -4,20 +4,69 @@ import model class DataXML(model.Data): """Reads network information from a XML file""" - def parse_config(): + def parse_config(self): """Reads and sets up XML config file fields""" self.data_file = self.config['Storage']['xml_file'] def load_data(self): - """Parse XML for getting network information""" # Ugly implementation. Beautify. + """Parse XML for getting network information""" + root = ElementTree(file=self.data_file).getroot() + for elem in root: - network = model.Network(elem.attrib['name']) - for e in elem: - if e.tag == 'ip_block': - network.ip_blocks.append(e.text) - else: - network.data[e.tag] = e.text - - self.networks.append(network) + if elem.tag == 'person': + self.add_person(elem) + elif elem.tag == 'domain': + self.add_domain(elem) + elif elem.tag == 'network': + self.add_network(elem): + else: + pass # raise TagNotFoundError + + + def add_person(self, elem): + """Adds a new person""" + + person = model.Person() + for e in elem: + if e.tag == 'name': + person.name = e.text + elif e.tag == 'surname': + person.surname = e.text + elif e.tag == 'email': + person.email = e.text + else: + person.data[e.tag] = e.text + + self.persons[elem.attrib['id']] = person + + def add_domain(self, elem): + """Adds a new domain""" + + domain = model.Domain() + for e in elem: + if e.tag == 'name': + domain.name == e.text + else: + domain.data[e.tag] = e.text + + self.domains[elem.attrib['id']] = domain + + def add_network(self, elem): + """Adds a new network""" + + network = model.Network() + for e in elem: + if e.tag == 'name': + network.name = e.text + elif e.tag == 'domain': + network.domains.append(e.text) + elif e.tag == 'ip_block': + network.ip_blocks.append(e.text) + elif e.tag == 'admin': + network.admins.append(e.text) + else: + network.data[e.tag] = e.text + + self.networks[elem.attrib['id']] = network -- cgit v1.2.3