Skip to content

Instantly share code, notes, and snippets.

@MartijnBraam
Created September 9, 2015 09:36
Show Gist options
  • Save MartijnBraam/02dcfb4d895a8f741c69 to your computer and use it in GitHub Desktop.
Save MartijnBraam/02dcfb4d895a8f741c69 to your computer and use it in GitHub Desktop.
Generate content documentation using drush_entity
#!/usr/bin/env python3
"""
Use with: https://www.drupal.org/project/drush_entity
For example::
$ drush entity-type-read --format=json --include-fieldapi | content-type-documentation.py > documentation.md
"""
from argparse import ArgumentParser
import json
try:
import tabulate
except ImportError:
print("Tabulate module not installed")
print("Run: pip3 install tabulate")
exit(1)
parser = ArgumentParser(description="Tool to build documentation from the json output of the drush_entity module")
parser.add_argument('input', type=open, default="/dev/stdin", help="The json file to parse, defaults to stdin")
args = parser.parse_args()
drush_data = json.load(args.input)
if "node" not in drush_data:
print("Json data doesn't contain the 'node' top level element.")
exit(1)
if "field_info_instances" not in drush_data['node']:
print("drush entity-type-read not called with the --include-fieldapi option.")
exit(1)
for bundle in drush_data['node']['bundles']:
bundle_info = drush_data['node']['bundles'][bundle]
if bundle in drush_data['node']['field_info_instances']:
print("## {}\n".format(bundle_info['label']))
print("**Machine name:** {}\n".format(bundle))
table = []
fields = drush_data['node']['field_info_instances'][bundle]
for field_machine_name in fields:
field = drush_data['node']['field_info_instances'][bundle][field_machine_name]
field_label = field['label']
if int(field['required']) > 0:
field_label += " *"
table.append([
field_machine_name,
field['label'],
field['display']['default']['type'],
field['widget']['type'],
field['description']
])
print(tabulate.tabulate(table, headers=["field", "label", "type", "widget", "description"], tablefmt="pipe"))
print("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment