diff options
author | Raúl Benencia <rbenencia@linti.unlp.edu.ar> | 2012-08-02 18:06:23 -0300 |
---|---|---|
committer | Raúl Benencia <rbenencia@linti.unlp.edu.ar> | 2012-08-02 18:06:23 -0300 |
commit | 4d30bf280dbdb68ad72e01e911bf039b48ccb869 (patch) | |
tree | e885129fdfecb1327fb50e13ac24d3cca208cbb5 | |
parent | 10e9272d170f5c634e8ca8f7415fb4ad1454bfe6 (diff) |
Completed XML parsing
-rw-r--r-- | core.py | 20 | ||||
-rw-r--r-- | examples/networks.xml | 40 | ||||
-rw-r--r-- | model.py | 22 | ||||
-rw-r--r-- | xml.py | 69 |
4 files changed, 115 insertions, 36 deletions
@@ -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 @@ <?xml version="1.0"?> -<networks> - <network name="Network one"> - <domain>one.network.example</domain> +<data> + <network id="1"> + <name>Network one</name> <ip_block>10.10.0.0/24</ip_block> - <admin_contact>Admin Istrator <admin@one.network.example></admin_contact> + <domain>1</domain> + <admin>1</admin> </network> - <network name="Network two"> - <domain>two.network.example</domain> - <ip_block>10.11.0.0/24</ip_block> - <admin_contact>Admin Ister <admin@two.network.example></admin_contact> + + <network id="2"> + <name>Network one</name> + <ip_block>10.11.0.0/24</ip_block> + <domain>2</domain> + <admin>2</admin> </network> -</networks>
\ No newline at end of file + + <person id="1"> + <name>Admin</name> + <surname>Istrator</surname> + <email>admin@one.network.example</email> + </person> + + <person id="2"> + <name>Admin</name> + <surname>Ister</surname> + <email>admin@two.network.example</email> + </person> + + <domain id="1"> + <name>one.network.example</name> + </domain> + <domain id="2"> + <name>two.network.example</name> + </domain> +</data>
\ No newline at end of file @@ -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""" @@ -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 |