← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~harlowja/cloud-init/no-prettytable into lp:cloud-init

 

Joshua Harlow has proposed merging lp:~harlowja/cloud-init/no-prettytable into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/no-prettytable/+merge/238491

Remove prettytable

Output the network and route information using yaml
instead so that it can be easily parsed by applications
looking at the console (if they so desire to do this).
-- 
https://code.launchpad.net/~harlowja/cloud-init/no-prettytable/+merge/238491
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/no-prettytable into lp:cloud-init.
=== modified file 'cloudinit/config/cc_debug.py'
--- cloudinit/config/cc_debug.py	2014-01-23 19:28:59 +0000
+++ cloudinit/config/cc_debug.py	2014-10-15 19:31:58 +0000
@@ -15,6 +15,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from cloudinit import type_utils
+from cloudinit import safeyaml
 from cloudinit import util
 import copy
 from StringIO import StringIO
@@ -55,10 +56,10 @@
     # Now dump it...
     to_print = StringIO()
     to_print.write(_make_header("Config"))
-    to_print.write(util.yaml_dumps(dump_cfg))
+    to_print.write(safeyaml.dumps(dump_cfg))
     to_print.write("\n")
     to_print.write(_make_header("MetaData"))
-    to_print.write(util.yaml_dumps(cloud.datasource.metadata))
+    to_print.write(safeyaml.dumps(cloud.datasource.metadata))
     to_print.write("\n")
     to_print.write(_make_header("Misc"))
     to_print.write("Datasource: %s\n" %

=== modified file 'cloudinit/config/cc_salt_minion.py'
--- cloudinit/config/cc_salt_minion.py	2014-02-05 15:36:47 +0000
+++ cloudinit/config/cc_salt_minion.py	2014-10-15 19:31:58 +0000
@@ -16,6 +16,7 @@
 
 import os
 
+from cloudinit import safeyaml
 from cloudinit import util
 
 # Note: see http://saltstack.org/topics/installation/
@@ -41,7 +42,7 @@
     if 'conf' in salt_cfg:
         # Add all sections from the conf object to /etc/salt/minion
         minion_config = os.path.join(config_dir, 'minion')
-        minion_data = util.yaml_dumps(salt_cfg.get('conf'))
+        minion_data = safeyaml.dumps(salt_cfg.get('conf'))
         util.write_file(minion_config, minion_data)
 
     # ... copy the key pair if specified

=== modified file 'cloudinit/handlers/cloud_config.py'
--- cloudinit/handlers/cloud_config.py	2014-08-26 19:53:41 +0000
+++ cloudinit/handlers/cloud_config.py	2014-10-15 19:31:58 +0000
@@ -25,6 +25,7 @@
 from cloudinit import handlers
 from cloudinit import log as logging
 from cloudinit import mergers
+from cloudinit import safeyaml
 from cloudinit import util
 
 from cloudinit.settings import (PER_ALWAYS)
@@ -92,7 +93,7 @@
                 '',
             ]
             lines.extend(file_lines)
-            lines.append(util.yaml_dumps(self.cloud_buf))
+            lines.append(safeyaml.dumps(self.cloud_buf))
         else:
             lines = []
         util.write_file(self.cloud_fn, "\n".join(lines), 0600)

=== modified file 'cloudinit/netinfo.py'
--- cloudinit/netinfo.py	2014-09-12 21:22:29 +0000
+++ cloudinit/netinfo.py	2014-10-15 19:31:58 +0000
@@ -20,12 +20,11 @@
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from cloudinit import safeyaml
 import cloudinit.util as util
 from cloudinit.log import logging
 import re
 
-from prettytable import PrettyTable
-
 LOG = logging.getLogger()
 
 
@@ -156,11 +155,21 @@
         lines.append(util.center("Net device info failed", '!', 80))
         netdev = None
     if netdev is not None:
-        fields = ['Device', 'Up', 'Address', 'Mask', 'Hw-Address']
-        tbl = PrettyTable(fields)
+        fields = {
+            'up': 'Up',
+            'addr': 'Address',
+            'mask': 'Mask',
+            'hwaddr': 'Hw-Address',
+        }
+        rows = []
         for (dev, d) in netdev.iteritems():
-            tbl.add_row([dev, d["up"], d["addr"], d["mask"], d["hwaddr"]])
-        netdev_s = tbl.get_string()
+            row = {
+                'Device': dev,
+            }
+            for (from_key, to_key) in fields.items():
+                row[to_key] = d[from_key]
+            rows.append(row)
+        netdev_s = safeyaml.dumps(rows, explicit_start=False)
         max_len = len(max(netdev_s.splitlines(), key=len))
         header = util.center("Net device info", "+", max_len)
         lines.extend([header, netdev_s])
@@ -176,15 +185,23 @@
         util.logexc(LOG, "Route info failed: %s" % e)
         routes = None
     if routes is not None:
-        fields = ['Route', 'Destination', 'Gateway',
-                  'Genmask', 'Interface', 'Flags']
-        tbl = PrettyTable(fields)
+        fields = {
+            'destination': 'Destination',
+            'gateway': 'Gateway',
+            'genmask': 'Genmask',
+            'iface': 'Interface',
+            'flags': 'Flags',
+        }
+        rows = []
         for (n, r) in enumerate(routes):
             route_id = str(n)
-            tbl.add_row([route_id, r['destination'],
-                        r['gateway'], r['genmask'],
-                        r['iface'], r['flags']])
-        route_s = tbl.get_string()
+            row = {
+                'Route': route_id,
+            }
+            for (from_key, to_key) in fields.items():
+                row[to_key] = r[from_key]
+            rows.append(row)
+        route_s = safeyaml.dumps(rows, explicit_start=False)
         max_len = len(max(route_s.splitlines(), key=len))
         header = util.center("Route info", "+", max_len)
         lines.extend([header, route_s])

=== modified file 'cloudinit/safeyaml.py'
--- cloudinit/safeyaml.py	2012-09-28 20:35:53 +0000
+++ cloudinit/safeyaml.py	2014-10-15 19:31:58 +0000
@@ -30,3 +30,10 @@
 
 def load(blob):
     return(yaml.load(blob, Loader=_CustomSafeLoader))
+
+
+def dumps(obj, explicit_start=True):
+    formatted = yaml.dump(obj, line_break="\n",
+                          indent=4, explicit_start=explicit_start,
+                          explicit_end=True, default_flow_style=False)
+    return formatted

=== modified file 'cloudinit/util.py'
--- cloudinit/util.py	2014-09-30 20:24:54 +0000
+++ cloudinit/util.py	2014-10-15 19:31:58 +0000
@@ -1270,16 +1270,6 @@
             logexc(LOG, "Failed writing url content to %s", target_fn)
 
 
-def yaml_dumps(obj):
-    formatted = yaml.dump(obj,
-                    line_break="\n",
-                    indent=4,
-                    explicit_start=True,
-                    explicit_end=True,
-                    default_flow_style=False)
-    return formatted
-
-
 def ensure_dir(path, mode=None):
     if not os.path.isdir(path):
         # Make the dir and adjust the mode

=== modified file 'requirements.txt'
--- requirements.txt	2014-03-05 23:05:59 +0000
+++ requirements.txt	2014-10-15 19:31:58 +0000
@@ -4,9 +4,6 @@
 cheetah
 jinja2
 
-# This is used for any pretty printing of tabular data.
-PrettyTable
-
 # This one is currently only used by the MAAS datasource. If that
 # datasource is removed, this is no longer needed
 oauth

=== modified file 'tests/unittests/test_runs/test_merge_run.py'
--- tests/unittests/test_runs/test_merge_run.py	2014-09-10 18:32:37 +0000
+++ tests/unittests/test_runs/test_merge_run.py	2014-10-15 19:31:58 +0000
@@ -2,6 +2,7 @@
 
 from .. import helpers
 
+from cloudinit import safeyaml
 from cloudinit.settings import (PER_INSTANCE)
 from cloudinit import stages
 from cloudinit import util
@@ -21,7 +22,7 @@
             'cloud_init_modules': ['write-files'],
         }
         ud = self.readResource('user_data.1.txt')
-        cloud_cfg = util.yaml_dumps(cfg)
+        cloud_cfg = safeyaml.dumps(cfg)
         util.ensure_dir(os.path.join(new_root, 'etc', 'cloud'))
         util.write_file(os.path.join(new_root, 'etc',
                                      'cloud', 'cloud.cfg'), cloud_cfg)

=== modified file 'tests/unittests/test_runs/test_simple_run.py'
--- tests/unittests/test_runs/test_simple_run.py	2014-07-23 16:25:35 +0000
+++ tests/unittests/test_runs/test_simple_run.py	2014-10-15 19:31:58 +0000
@@ -2,6 +2,7 @@
 
 from .. import helpers
 
+from cloudinit import safeyaml
 from cloudinit.settings import (PER_INSTANCE)
 from cloudinit import stages
 from cloudinit import util
@@ -46,7 +47,7 @@
             ],
             'cloud_init_modules': ['write-files'],
         }
-        cloud_cfg = util.yaml_dumps(cfg)
+        cloud_cfg = safeyaml.dumps(cfg)
         util.ensure_dir(os.path.join(new_root, 'etc', 'cloud'))
         util.write_file(os.path.join(new_root, 'etc',
                                      'cloud', 'cloud.cfg'), cloud_cfg)


Follow ups