Skip to content

Instantly share code, notes, and snippets.

@notmyname
Last active June 10, 2019 21:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notmyname/3c3a697db927656c79350db147935bb1 to your computer and use it in GitHub Desktop.
Save notmyname/3c3a697db927656c79350db147935bb1 to your computer and use it in GitHub Desktop.
rst table printer
#!/usr/bin/env python3.7
# pretty-print columns of text
import sys
def table_print(lines_to_print, padding=3):
"""Given a list of tuples/lists, print a nicely formatted .rst table"""
one_cell = "%*s%%-*s" % ((padding, " "))
line_template = []
column_widths = []
for line in lines_to_print:
cell_count = len(line)
while cell_count > len(line_template):
line_template.append(one_cell)
column_widths.append(0)
for i, cell in enumerate(line):
cell = str(cell) # just in case it's not a string already
len_cell = len(cell)
target_width = len_cell + padding
if target_width > column_widths[i]:
column_widths[i] = target_width
out = []
col_count = len(column_widths)
line_template = "|".join(line_template)
break_line = []
for i, w in enumerate(column_widths):
break_line.append("-" * padding + "-" * w)
break_line = "+" + "+".join(break_line) + "+"
out.append(break_line)
for line in lines_to_print:
while col_count > len(line):
line += ("",)
params = tuple(item for sublist in zip(column_widths, line) for item in sublist)
out_line = line_template % params
if out_line.replace(" ", "") == "":
out_line = ""
out_line = "|" + out_line + "|"
out.append(out_line)
out.append(break_line)
return "\n".join(out)
if __name__ == "__main__":
raw_data = []
for line in sys.stdin:
line = line.strip()
parts = [x.strip() for x in line.split("|")]
raw_data.append(parts)
print(table_print(raw_data, padding=1))
GET Object | https://developer.openstack.org/api-ref/object-store/#delete-container
HEAD | https://developer.openstack.org/api-ref/object-store/#show-object-metadata
PUT | https://developer.openstack.org/api-ref/object-store/#create-or-replace-object
POST | https://developer.openstack.org/api-ref/object-store/#create-or-update-object-metadata
COPY | https://developer.openstack.org/api-ref/object-store/#copy-object
DELETE | https://developer.openstack.org/api-ref/object-store/#delete-object
OPTIONS
DLO | :ref:`dynamic-large-objects` | api/large_objects.rst
SLO | :ref:`static-large-objects` | api/large_objects.rst
TempURLs | api/temporary_url_middleware.rst
Form POST | api/form_post_middleware.rst
Bulk operations | api/bulk-delete.rst
Symlinks | :ref:`symlink`
Encryption | :ref:`overview_encryption`
Versioning | api/object_versioning.rst
Expiring objects | :ref:`overview_expiring_objects` | api/object-expiration.rst
GET Container | https://developer.openstack.org/api-ref/object-store/#show-account-details-and-list-containers | api/pagination.rst
HEAD | https://developer.openstack.org/api-ref/object-store/#show-container-metadata
PUT | https://developer.openstack.org/api-ref/object-store/#show-container-metadata
POST | https://developer.openstack.org/api-ref/object-store/#show-container-metadata
DELETE | https://developer.openstack.org/api-ref/object-store/#delete-container
OPTIONS
TempURLs | api/temporary_url_middleware.rst
Domain remap | :ref:`domainremap`
Static websites | api/static-website.rst
Quotas | api/container_quotas.rst
ACLs | :ref:`container_acls`
GET Account | https://developer.openstack.org/api-ref/object-store/#show-account-details-and-list-containers | api/pagination.rst
HEAD | https://developer.openstack.org/api-ref/object-store/#show-account-details-and-list-containers
PUT
POST | https://developer.openstack.org/api-ref/object-store/#show-account-details-and-list-containers
DELETE
OPTIONS
Quotas
ACLs | :ref:`account_acls`
Read-only | :ref:`read_only`
Discoverability | https://developer.openstack.org/api-ref/object-store/#list-activated-capabilities
Read-only | :ref:`read_only`
Crossdomain xml | :ref:`crossdomain`
CORS | :ref:`cors_overview`
list endpoints | https://developer.openstack.org/api-ref/object-store/#create-or-update-object-metadata
container sync | :ref:`container-sync`
GET Object https://developer.openstack.org/api-ref/object-store/#delete-container
HEAD https://developer.openstack.org/api-ref/object-store/#show-object-metadata
PUT https://developer.openstack.org/api-ref/object-store/#create-or-replace-object
POST https://developer.openstack.org/api-ref/object-store/#create-or-update-object-metadata
COPY https://developer.openstack.org/api-ref/object-store/#copy-object
DELETE https://developer.openstack.org/api-ref/object-store/#delete-object
OPTIONS
DLO dynamic-large-objects api/large_objects.rst
SLO static-large-objects api/large_objects.rst
TempURLs api/temporary_url_middleware.rst
Form POST api/form_post_middleware.rst
Bulk operations api/bulk-delete.rst
Symlinks symlink
Encryption overview_encryption
Versioning api/object_versioning.rst
Expiring objects overview_expiring_objects api/object-expiration.rst
GET Container https://developer.openstack.org/api-ref/object-store/#show-account-details-and-list-containers api/pagination.rst
HEAD https://developer.openstack.org/api-ref/object-store/#show-container-metadata
PUT https://developer.openstack.org/api-ref/object-store/#show-container-metadata
POST https://developer.openstack.org/api-ref/object-store/#show-container-metadata
DELETE https://developer.openstack.org/api-ref/object-store/#delete-container
OPTIONS
TempURLs api/temporary_url_middleware.rst
Domain remap domainremap
Static websites api/static-website.rst
Quotas api/container_quotas.rst
ACLs container_acls
GET Account https://developer.openstack.org/api-ref/object-store/#show-account-details-and-list-containers api/pagination.rst
HEAD https://developer.openstack.org/api-ref/object-store/#show-account-details-and-list-containers
PUT
POST https://developer.openstack.org/api-ref/object-store/#show-account-details-and-list-containers
DELETE
OPTIONS
Quotas
ACLs account_acls
Read-only read_only
Discoverability https://developer.openstack.org/api-ref/object-store/#list-activated-capabilities
Read-only read_only
Crossdomain xml crossdomain
CORS cors_overview
list endpoints https://developer.openstack.org/api-ref/object-store/#create-or-update-object-metadata
container sync container-sync
@notmyname
Copy link
Author

especially note the raw version of the sample output

@clayg
Copy link

clayg commented Jun 7, 2019

so pretty! ⭐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment