]> git.ipfire.org Git - people/stevee/pypdns.git/blobdiff - backend.py
Add exceptions for database handling and invalid SOA records.
[people/stevee/pypdns.git] / backend.py
index 4acdd204f93076af277ab0be2ccda8692e20bdb0..3911bef87557018deabba485ba9c7b73b2fcfc9b 100644 (file)
@@ -1,7 +1,7 @@
 ###############################################################################
 #                                                                             #
 # pyPDNS - A PDNS administration tool, written in pure python.                #
-# Copyright (C) 2012 Pakfire development team                                 #
+# Copyright (C) 2012 IPFire development team                                  #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
 #                                                                             #
 ###############################################################################
 #                                                                             #
+# Basic information about the database layout can be found here:              #
+# http://doc.powerdns.com/gsqlite.html                                        #
+#                                                                             #
 # More details about the database tables and fields can be found here:        #
 # http://wiki.powerdns.com/trac/wiki/fields                                   #
 #                                                                             #
 ###############################################################################
 
 import database
+import errors
+import sqlite3
 
 DB = "/var/lib/pdns/pdns.db"
 
 # Create the primary DNS class.
-"""Use Database class from imported database module to connect to the PDNS sqlite database."""
 class DNS(object):
+       """
+       Primary DNS class.
+
+       Uses the database class from imported database module.
+       Connects to the PDNS sqlite database.
+       """
        def __init__(self, db):
-               self.db = database.Database(db)
+               # Try to connect to database or raise an exception.
+               try:
+                       self.db = database.Database(db)
+
+               except sqlite3.OperationalError, e:
+                       raise errors.DatabaseException, "Could not open database: %s" % e
+
+
 
        # Get all configured domains.
        def get_domains(self):
+               """
+               Fetch all configured domains.
+               """
+               # Create an empty list.
                domains = []
 
-               """Fetch all configured domains, line by line and add them to the previous created empty list."""
+               # Add fetched domains to the previous created empty list.
                for row in self.db.query("SELECT id FROM domains"):
                        domain = Domain(self, row.id)
                        domains.append(domain)
@@ -46,15 +67,25 @@ class DNS(object):
 
        # Get a domain by it's name.
        def get_domain(self, name):
+               """
+               Get a domain by a given name.
+               """
                row = self.db.get("SELECT id FROM domains WHERE name = ?", name)
-               domain = Domain(self, row.id)
 
-               return domain
+               # Only do anything, if there is an existing domain.
+               if row:
+                       domain = Domain(self, row.id)
+
+                       return domain
 
 # Create Domain class.
-"""Use query method from database module to get all requested information about our domain."""
-"""The domain is specified by it's unique id."""
 class Domain(object):
+       """
+       Domain class.
+
+       Uses query method from database module to get requested information from domain.
+       The domain is specified by it's unique database id.
+       """
        def __init__(self, dns, domain_id):
                self.dns = dns
                self.id = domain_id
@@ -102,9 +133,13 @@ class Domain(object):
        # Get all records from zone.
        @property 
        def records(self):
+               """
+               Get all records from the zone.
+               """
+               # Create an empty list.
                records = []
 
-               """Fetch all records from domain, line by line and add them to the previous created empty list."""
+               # Fetch records from zone and categorize them into their different record types.
                for row in self.db.query("SELECT id, type FROM records WHERE domain_id = ?", self.id):
                        if row.type == "SOA":
                                record = SOARecord(self, row.id)
@@ -134,9 +169,13 @@ class Domain(object):
 
 
 # Create class for domain records.
-"""It is used to get more details about the configured records."""
-"""The domain is specified by it's unique id."""
 class Record(object):
+       """
+       Record class
+
+       It is used to get details about configured records.
+       The domain and record is's are specified by their unique database id's.
+       """
        def __init__(self, domain, record_id):
                self.domain = domain
                self.id = record_id
@@ -197,8 +236,14 @@ class Record(object):
 
 
 # Create an own class to deal with "SOA" records.
-"""Use splitt() to generate a list of the original content string from the database, and return the requested entries.""" 
 class SOARecord(Record):
+       """
+       SOA Record class.
+       This is an own class to deal with "SOA" records.
+
+       Uses splitt() to generate a list of the content string from the database.
+       Returns the requested entries.
+       """
        def __init__(self, domain, record_id):
                Record.__init__(self, domain, record_id)
 
@@ -206,8 +251,8 @@ class SOARecord(Record):
 
                # Check if the content from database is valid (It contains all 7 required information).
                if not len(self.soa_attrs) == 7:
-                       #XXX Add something like an error message or log output.
-                       pass                    
+                       raise InvalidRecordDataException, "Your SOA record \
+                               doesn't contain all required seven elements."
 
        # Primary NS - the domain name of the name server that was the original source of the data.     
        @property
@@ -248,4 +293,3 @@ class SOARecord(Record):
 
 class ARecord(Record):
        pass
-