← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~ajorgens/cloud-init:simpletable into cloud-init:master

 

Andrew Jorgensen has proposed merging ~ajorgens/cloud-init:simpletable into cloud-init:master.

Commit message:
Remove prettytable dependency, introduce simpletable

The output of simpletable is more simplistic than PrettyTable, but simpletable has no dependencies and very little complexity.

Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~ajorgens/cloud-init/+git/cloud-init/+merge/330525

Remove prettytable dependency, introduce simpletable

The output of simpletable is more simplistic than PrettyTable, but simpletable has no dependencies and very little complexity.
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~ajorgens/cloud-init:simpletable into cloud-init:master.
diff --git a/cloudinit/config/cc_ssh_authkey_fingerprints.py b/cloudinit/config/cc_ssh_authkey_fingerprints.py
index 0066e97..72a2baa 100755
--- a/cloudinit/config/cc_ssh_authkey_fingerprints.py
+++ b/cloudinit/config/cc_ssh_authkey_fingerprints.py
@@ -28,7 +28,7 @@ the keys can be specified, but defaults to ``md5``.
 import base64
 import hashlib
 
-from prettytable import PrettyTable
+from cloudinit.simpletable import SimpleTable as PrettyTable
 
 from cloudinit.distros import ug_util
 from cloudinit import ssh_util
diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py
index 39c79de..6785bba 100644
--- a/cloudinit/netinfo.py
+++ b/cloudinit/netinfo.py
@@ -13,7 +13,7 @@ import re
 from cloudinit import log as logging
 from cloudinit import util
 
-from prettytable import PrettyTable
+from cloudinit.simpletable import SimpleTable as PrettyTable
 
 LOG = logging.getLogger()
 
diff --git a/cloudinit/simpletable.py b/cloudinit/simpletable.py
new file mode 100644
index 0000000..a1825aa
--- /dev/null
+++ b/cloudinit/simpletable.py
@@ -0,0 +1,57 @@
+# Copyright (C) 2017 Amazon.com, Inc. or its affiliates
+#
+# Author: Ethan Faust <efaust@xxxxxxxxxx>
+# Author: Andrew Jorgensen <ajorgens@xxxxxxxxxx>
+#
+# This file is part of cloud-init. See LICENSE file for license information.
+
+try:
+    from StringIO import StringIO
+except ImportError:
+    from io import StringIO
+
+
+class SimpleTable(object):
+    """A minimal implementation of PrettyTable
+    for distribution with cloud-init.
+    """
+
+    def __init__(self, fields):
+        self.fields = fields
+        self.rows = []
+
+        # initialize list of 0s the same length
+        # as the number of fields
+        self.column_widths = [0] * len(self.fields)
+        self.update_column_widths(fields)
+
+    def update_column_widths(self, values):
+        for i, value in enumerate(values):
+            self.column_widths[i] = max(
+                len(value),
+                self.column_widths[i])
+
+    def add_row(self, values):
+        if len(values) > len(self.fields):
+            raise TypeError('too many values')
+        values = [str(value) for value in values]
+        self.rows.append(values)
+        self.update_column_widths(values)
+
+    def __repr__(self):
+        out = StringIO()
+
+        for i, column in enumerate(self.fields):
+            out.write(column.center(self.column_widths[i] + 2))
+
+        for row in self.rows:
+            out.write('\n')
+            for i, column in enumerate(row):
+                out.write(column.center(self.column_widths[i] + 2))
+
+        result = out.getvalue()
+        out.close()
+        return result
+
+    def get_string(self):
+        return repr(self)

Follow ups