]> git.ipfire.org Git - people/stevee/pypdns.git/commitdiff
Initial import of the CLI.
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 27 Sep 2012 17:44:34 +0000 (17:44 +0000)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 27 Sep 2012 17:52:01 +0000 (17:52 +0000)
cli.py [new file with mode: 0644]
i18n.py [new file with mode: 0644]

diff --git a/cli.py b/cli.py
new file mode 100644 (file)
index 0000000..6c92d11
--- /dev/null
+++ b/cli.py
@@ -0,0 +1,188 @@
+#!/usr/bin/python
+#
+###############################################################################
+#                                                                             #
+# pyPDNS - A PDNS administration tool, written in pure python.                #
+# Copyright (C) 2012 Pakfire 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        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import backend
+import argparse
+
+from i18n import _
+
+# Initialize DNS module.
+dns = backend.DNS(backend.DB)
+
+# Create main class for the CLI.
+class Cli(object):
+       def __init__(self):
+               self.parser = argparse.ArgumentParser(
+                       description = _("CLI tool to adminstrate pdns servers."),
+               )
+
+               # Add entry for version displaying.
+               self.parser.add_argument('--version', action='version', version='pdns 1.0')
+
+               # Add sub-commands.
+               self.sub_commands = self.parser.add_subparsers()
+
+               self.parse_command_show()
+               #self.parse_command_add()
+               #self.parse_command_remove()
+
+               # 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.
+               self.action2func = {
+                       "show_all"     : self.handle_show_all,
+                       "show_domains" : self.handle_show_domains,
+                       "show_records" : self.handle_show_records,
+               }
+
+       def parse_command_show(self):
+               # Initialize the "show" command.
+               sub_show = self.sub_commands.add_parser("show",
+                        help=_("Show domains and records."))
+
+               # Initialize subcommands for show.
+               sub_show_subcommands = sub_show.add_subparsers()
+
+               # 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")
+
+               # 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")
+
+               # 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")
+
+       def run(self):
+               action = self.args.action
+
+               try:
+                       func = self.action2func[action]
+               except KeyError:
+                       raise Exception, "Unhandled action: %s" % action
+
+               return func()
+
+       def handle_show_domains(self):
+               # Get all domains and print them
+               domains = dns.get_domains()
+               if domains:
+                       for domain in domains:
+                               print domain.id, 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.
+                       domain = 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"
+
+                                       # Define layout for records table.
+                                       FORMAT = " %-24s %-8s %-34s %-10s"
+
+                                       # 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.
+                                       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 = 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."
+
+
+
+
+cli = Cli()
+cli.run()
diff --git a/i18n.py b/i18n.py
new file mode 100644 (file)
index 0000000..78bbcd1
--- /dev/null
+++ b/i18n.py
@@ -0,0 +1,57 @@
+#!/usr/bin/python
+###############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2011 Pakfire 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        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+"""
+       The translation process of all strings is done in here.
+"""
+
+import  gettext
+
+"""
+       A function that returnes the same string.
+"""
+N_ = lambda x: x
+
+def _(singular, plural=None, n=None):
+       """
+               A function that returnes the translation of a string if available.
+
+               The language is taken from the system environment.
+       """
+       if not plural is None:
+               assert n is not None
+               return gettext.dngettext("pdns-tool", singular, plural, n)
+
+       return gettext.dgettext("pdns-tool", singular)
+
+def list(parts):
+       """
+               Returns a comma-separated list for the given list of parts.
+
+               The format is, e.g., "A, B and C", "A and B" or just "A" for lists
+               of size 1.
+       """
+       if len(parts) == 0: return ""
+       if len(parts) == 1: return parts[0]
+       return _("%(commas)s and %(last)s") % {
+               "commas": u", ".join(parts[:-1]),
+               "last": parts[len(parts) - 1],
+       }