From: Stefan Schantl Date: Mon, 8 Oct 2012 21:03:45 +0000 (+0000) Subject: Code rework of the CLI. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=HEAD;p=people%2Fstevee%2Fpypdns.git Code rework of the CLI. Improvments on CLI functions. Now we also should fit the projct coding guidelines. --- diff --git a/backend.py b/backend.py index 1679281..e915fd2 100644 --- a/backend.py +++ b/backend.py @@ -73,7 +73,7 @@ class DNS(object): """ row = self.db.get("SELECT id FROM domains WHERE name = ?", name) - # Check if an id has been returned from database, or return None. + # Check if an id has been returned from database or return None. if not row: return None @@ -85,7 +85,9 @@ class Domain(object): """ Domain class. - Uses query method from database module to get requested information from domain. + 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): @@ -102,7 +104,8 @@ class Domain(object): @property def data(self): if self.__data is None: - self.__data = self.db.get("SELECT * FROM domains WHERE id = ?", self.id) + self.__data = self.db.get("SELECT * FROM domains \ + WHERE id = ?", self.id) assert self.__data return self.__data @@ -112,7 +115,8 @@ class Domain(object): def name(self): return self.data.name - # Get information of the master nameserver from which the domain should be slaved. + # Get information of the master nameserver from which the domain should + # be slaved. @property def master(self): return self.data.master @@ -137,14 +141,28 @@ class Domain(object): def account(self): return self.data.account + # Get count of records of a zone. Return true if there is at least one + # or false. + def has_records(self): + count = self.db.get("SELECT COUNT(*) AS num FROM records \ + WHERE domain_id = ?", self.id) + + if count.num > 0: + return True + + return False + # Get all records from zone. @property def records(self): """ Get all records from the zone. """ - # 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): + # 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) elif row.type == "A": @@ -193,7 +211,8 @@ class Record(object): @property def data(self): if self.__data is None: - self.__data = self.db.get("SELECT * FROM records WHERE id = ?", self.id) + self.__data = self.db.get("SELECT * FROM records \ + WHERE id = ?", self.id) assert self.__data return self.__data @@ -246,7 +265,9 @@ 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. + Uses splitt() to generate a list of the content string from the + database. + Returns the requested entries. """ def __init__(self, domain, record_id): @@ -254,12 +275,14 @@ class SOARecord(Record): self.soa_attrs = self.content.split() - # Check if the content from database is valid (It contains all 7 required information). + # Check if the content from database is valid. + # (It contains all 7 required information) if not len(self.soa_attrs) == 7: 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. + # Primary NS - the domain name of the name server that was the + # original source of the data. @property def mname(self): return self.soa_attrs[0] @@ -269,22 +292,26 @@ class SOARecord(Record): def email(self): return self.soa_attrs[1] - # The serial which increases allways after a change on the domain has been made. + # The serial which increases allways after a change on the domain has + # been made. @property def serial(self): return self.soa_attrs[2] - # The number of seconds between the time that a secondary name server gets a copy of the domain. + # The number of seconds between the time that a secondary name server + # gets a copy of the domain. @property def refresh(self): return self.soa_attrs[3] - # The number of seconds during the next refresh attempt if the previous fails. + # The number of seconds during the next refresh attempt if the + # previous fails. @property def retry(self): return self.soa_attrs[4] - # The number of seconds that lets the secondary name server(s) know how long they can hold the information. + # The number of seconds that lets the secondary name server(s) know + # how long they can hold the information. @property def expire(self): return self.soa_attrs[5] diff --git a/cli.py b/cli.py index 8bd1cf4..ad7a7e6 100644 --- a/cli.py +++ b/cli.py @@ -30,11 +30,12 @@ from backend import * class Cli(object): def __init__(self): self.parser = argparse.ArgumentParser( - description = _("CLI tool to adminstrate authoritative PowerDNS servers."), - ) + description = _("CLI tool to adminstrate authoritative \ + PowerDNS servers.")) # Add entry for version displaying. - self.parser.add_argument('--version', action='version', version='pyPDNS 0.1.1') + self.parser.add_argument('--version', action='version', + version='pyPDNS 0.1.1') # Add sub-commands. self.sub_commands = self.parser.add_subparsers() @@ -43,7 +44,8 @@ class Cli(object): #self.parse_command_add() #self.parse_command_remove() - # Finally parse all arguments from the command line and save them. + # Finally parse all arguments from the command line and save + # them. self.args = self.parser.parse_args() # Map return values from action to functions to proceed. @@ -64,19 +66,22 @@ class Cli(object): # Impement "show all". sub_show_records = sub_show_subcommands.add_parser("all", help="Show all domains and records.") - sub_show_records.add_argument("action", action="store_const", const="show_all") + sub_show_records.add_argument("action", action="store_const", + const="show_all") # Implement "show records" and pick up given domain. sub_show_records = sub_show_subcommands.add_parser("records", help="Show records from a given domain.") sub_show_records.add_argument("domain", nargs="?", help="Show records from a given domain.") - sub_show_records.add_argument("action", action="store_const", const="show_records") + sub_show_records.add_argument("action", action="store_const", + const="show_records") # Implement "show domains". sub_show_domains = sub_show_subcommands.add_parser("domains", help="Show all configured domains.") - sub_show_domains.add_argument("action", action="store_const", const="show_domains") + sub_show_domains.add_argument("action", action="store_const", + const="show_domains") def run(self): action = self.args.action @@ -94,96 +99,118 @@ class Cli(object): return func() def handle_show_domains(self): - # Get all domains and print them + """ + Get all domains and print them. + """ domains = self.dns.get_domains() - if domains: + + # Print the domains, if there are any configured. + if not domains: + print "No domain configured, yet." + + else: + print "Currently configured domains:" for domain in domains: - print domain.id, domain.name + print " - %s" % domain.name - # If there hasn't been any domain configured, print an error message. - else: - print "No domain configured, yet." def handle_show_records(self): - if self.args.domain: - # Check if the given domain name really exists. + # Print message if domain doens't exist. + if not self.args.domain: + print " No domain name specified to show \ + records." + print " Use 'pdns show domains' to get a list \ + from all available domains." + + return + + # Print message if domain doens't exist. domain = self.dns.get_domain(self.args.domain) - if domain: - # Get all records and print them. - if domain.records: - print " Showing records for: %s \n" % self.args.domain - - soa = domain.SOA - - if soa: - FORMAT = " %-8s: %s" - print FORMAT % ("MName", soa.mname) - print FORMAT % ("E-Mail",soa.email) - print FORMAT % ("Serial", soa.serial) - print FORMAT % ("Refresh", soa.refresh) - print FORMAT % ("Retry", soa.retry) - print FORMAT % ("Expire", soa.expire) - print FORMAT % ("Minimum", soa.minimum) - - print "" #Just an emtpy line. - - else: - print " No Start of Authority (SOA record) created yet. Your domain will not work correctly. \n" + if not domain: + print " No such domain, use 'pdns show \ + domains' to get a list of all \ + available domains." + + return + + # Print message if no records have been configured yet. + if not domain.has_records(): + print " Domain has no records yet." + + else: + print " Showing records \ + for: %s \n" % self.args.domain + + soa = domain.SOA + + # Check if the domain has a SOA record. + if not soa: + print " No Start of Authority \ + (SOA record) created yet. \ + Your domain will not work \ + correctly. \n" + + else: + # Define table layout for SOA table.. + FORMAT = " %-8s: %s" + print FORMAT % ("MName", soa.mname) + print FORMAT % ("E-Mail",soa.email) + print FORMAT % ("Serial", soa.serial) + print FORMAT % ("Refresh", soa.refresh) + print FORMAT % ("Retry", soa.retry) + print FORMAT % ("Expire", soa.expire) + print FORMAT % ("Minimum", soa.minimum) + + print "" #Just an emtpy line. + # Define layout for records table. FORMAT = " %-24s %-8s %-34s %-10s" - # Define tables headline for records and print it. + # Define tables headline for records + # and print it. title = FORMAT % (_("Name"), _("Type"), _("Content"), _("TTL")) print title print "=" * len(title) # spacing line - # Display all remaining records, except SOA records. + # Display all remaining records, + # except SOA records. for record in domain.records: if not record.type == "SOA": print FORMAT % (record.dnsname, record.type, record.content, record.ttl) - # If there aren't any records yet, print a short notice about that. - else: - print " Domain has no records, yet." - - # If given domain doesn't exist, print an error message. - else: - print " Invalid domain. Use 'pdns show domains' to get a list from all available domains." - # If no domain name has been specified, also print an error message. - else: - print " No domain name specified to show records." - print " Use 'pdns show domains' to get a list from all available domains." - def handle_show_all(self): # Get all domains and print them domains = self.dns.get_domains() - if domains: - for domain in domains: - print domain.name - print "=" * 80 - # Print all records. - if domain.records: - for record in domain.records: - if record.type == "SOA": - FORMAT = ("%-30s %s") - print FORMAT % (record.dnsname, record.type) - else: - FORMAT = ("%-30s %-6s %s") - print FORMAT % (record.dnsname, record.type, record.content) - # Print a notice if the domain hasn't any records yet. - else: - print "Domain has no records, yet." - - # Just an emty line. - print "" - - # If there hasn't been any domain configured, print an error message. - else: - print "No domain configured, yet." + # Print message if no domain has been configured. + if not domains: + print "No domain configured, yet." + + return + + for domain in domains: + print domain.name + + # Spacing line. + print "=" * 80 + + # Print a message if domain has no records. + if not domain.has_records(): + print "Domain has no records yet." + + # Print records. + for record in domain.records: + if record.type == "SOA": + FORMAT = ("%-30s %s") + print FORMAT % (record.dnsname, record.type) + else: + FORMAT = ("%-30s %-6s %s") + print FORMAT % (record.dnsname, record.type, record.content) + # Just an emty line. + print ""