pvl.dns: quote ZoneDirective arguments only on output if required
authorTero Marttila <tero.marttila@aalto.fi>
Fri, 27 Feb 2015 17:13:18 +0200
changeset 647 90a0790adf8a
parent 646 585fe8171ac6
child 648 8e3e6be9ac70
pvl.dns: quote ZoneDirective arguments only on output if required
pvl/dns/tests.py
pvl/dns/zone.py
--- a/pvl/dns/tests.py	Fri Feb 27 17:06:54 2015 +0200
+++ b/pvl/dns/tests.py	Fri Feb 27 17:13:18 2015 +0200
@@ -215,6 +215,6 @@
             " NS bar",
             "foo A 192.0.2.1",
             "bar A 192.0.2.2",
-            "$INCLUDE \".../includes/test\"",
+            "$INCLUDE .../includes/test",
         ])
 
--- a/pvl/dns/zone.py	Fri Feb 27 17:06:54 2015 +0200
+++ b/pvl/dns/zone.py	Fri Feb 27 17:13:18 2015 +0200
@@ -30,14 +30,17 @@
         Quote a value for inclusion into TXT record.
 
         >>> print zone_quote("foo")
-        "foo"
+        foo
         >>> print zone_quote("foo\\bar")
         "foo\\bar"
         >>> print zone_quote("foo \"bar\" quux")
         "foo \"bar\" quux"
     """
 
-    return u'"' + field.replace('\\', '\\\\').replace('"', '\\"') + u'"'
+    if any(c.isspace() or c == '\\' for c in field):
+        return u'"' + field.replace('\\', '\\\\').replace('"', '\\"') + u'"'
+    else:
+        return field
 
 
 class ZoneLine (object) :
@@ -226,9 +229,9 @@
         """
 
         if origin:
-            return cls.build('INCLUDE', zone_quote(path), origin, **opts)
+            return cls.build('INCLUDE', path, origin, **opts)
         else:
-            return cls.build('INCLUDE', zone_quote(path), **opts)
+            return cls.build('INCLUDE', path, **opts)
 
     def __init__ (self, directive, arguments, comment=None, line=None, origin=None):
         """
@@ -258,7 +261,7 @@
             
         return u"${directive}\t{arguments}{comment}".format(
                 directive   = self.directive,
-                arguments   = '\t'.join(self.arguments),
+                arguments   = '\t'.join(zone_quote(argument) for argument in self.arguments),
                 comment     = comment,
         )