1#!/usr/bin/env python3
 2
 3"""Widget information viewer."""
 4
 5
 6
 7import logging
 8
 9from chart.reports.widget import widget_classes
10from chart.common.args import ArgumentParser
11from chart.common.prettyprint import Table
12from chart.project import settings
13
14logger = logging.getLogger()
15
16
17def list_widgets():
18    """Show list of all core and project widgets."""
19
20    for widget_dir_details in settings.WIDGET_DIRS:
21        if widget_dir_details['show-in-gallery']:
22            t = Table(title='Widget classes : ' + str(widget_dir_details['dir']),
23                  headings=('Widget', 'Class'))
24
25            widgets = widget_classes(widget_dir_details['dir'])
26            for widget, wclass in widgets.items():
27                t.append((widget, wclass))
28
29            t.write()
30
31    # legacy code, old style widget dirs...
32    try:
33        for name, path in (('core widget dir', settings.CORE_WIDGET_DIR),
34                  ('project widget dir', settings.PROJ_WIDGET_DIR),
35                  ('digest widget dir', settings.DIGEST_WIDGET_DIR)):
36            t = Table(title='Widget in {name} {dir}:'.format(name=name, dir=path),
37                  headings=('Widget', 'Class'))
38            widgets = widget_classes(path)
39            for widget, wclass in widgets.items():
40                t.append((widget, wclass))
41
42            t.write()
43
44
45    except AttributeError:
46        pass
47
48def main():
49    """Command line entry point."""
50    parser = ArgumentParser(__doc__)
51    parser.add_argument('--list-widgets', '-l', '--list', '--widgets', '-w',
52                        action='store_true',
53                        help='Show available widgets')
54    args = parser.parse_args()
55
56    if args.list_widgets:
57        list_widgets()
58        parser.exit()
59
60    parser.error('No actions specified')
61
62if __name__ == '__main__':
63    main()