Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import argparse
  4. import ast
  5. import os
  6.  
  7. import xlsxwriter
  8.  
  9.  
  10. def parse_drivers_dir(options):
  11. drivers = {}
  12. for driver in os.listdir(options.cloud_dir):
  13. if not driver.endswith('.py') or driver == '__init__.py':
  14. continue
  15. funcs = parse_driver(os.path.join(options.cloud_dir, driver))
  16. drivers[driver] = funcs
  17. return dict(sorted(drivers.items(), key=lambda i: i[0]))
  18.  
  19.  
  20. def parse_driver(driver):
  21. with open(driver, "r") as source:
  22. tree = ast.parse(source.read())
  23. functions = [node for node in tree.body if isinstance(node, ast.FunctionDef)]
  24. func_list = []
  25. for fn in functions:
  26. doc = ast.get_docstring(fn)
  27. doc = doc.splitlines()[0] if doc else ''
  28. if fn.name.startswith('_'):
  29. continue
  30. func_list.append((fn.name, doc))
  31.  
  32. assignments = [
  33. node.targets[0].id for node in tree.body
  34. if isinstance(node, ast.Assign) and
  35. isinstance(node.value, ast.Call) and
  36. isinstance(node.value.func, ast.Name) and
  37. node.value.func.id == 'namespaced_function'
  38. ]
  39.  
  40. for assignment in assignments:
  41. func_list.append((assignment, 'NAMESPACED FUNCTION'))
  42. func_list.sort()
  43. return func_list
  44.  
  45.  
  46. if __name__ == '__main__':
  47. parser = argparse.ArgumentParser(
  48. formatter_class=argparse.ArgumentDefaultsHelpFormatter
  49. )
  50. parser.add_argument(
  51. '--cloud-dir',
  52. type=str,
  53. help='Path to Salt cloud drivers directory',
  54. required=True,
  55. )
  56. parser.add_argument(
  57. '--out',
  58. type=str,
  59. default='salt-cloud-drivers.xlsx',
  60. help='Name of the resulting xlsx file',
  61. )
  62. parser.add_argument(
  63. '--group',
  64. action='store_true',
  65. help='Group functions by name',
  66. )
  67. options = parser.parse_args()
  68. if not os.path.isdir(options.cloud_dir):
  69. parser.exit('Cloud dir should be a directory: {}'.format(options.cloud_dir))
  70.  
  71. drivers = parse_drivers_dir(options)
  72.  
  73. workbook = xlsxwriter.Workbook(options.out)
  74. worksheet = workbook.add_worksheet()
  75.  
  76. if options.group:
  77. all_funcs = sorted(list(set([f[0] for k, v in drivers.items() for f in v])))
  78. for fi, fn in enumerate(all_funcs):
  79. worksheet.write(fi + 1, 0, fn)
  80. for drv_index, (drv_name, functions) in enumerate(drivers.items()):
  81. worksheet.write(0, drv_index + 1, drv_name)
  82. for fi, fn in enumerate(functions):
  83. fn_index = all_funcs.index(fn[0])
  84. worksheet.write(fn_index + 1, drv_index + 1, fn[0])
  85. worksheet.write_comment(fn_index + 1, drv_index + 1, fn[1])
  86. else:
  87. for drv_index, (drv_name, functions) in enumerate(drivers.items()):
  88. worksheet.write(0, drv_index + 1, drv_name)
  89. for fi, fn in enumerate(functions):
  90. worksheet.write(fi + 1, drv_index + 1, fn[0])
  91. worksheet.write_comment(fi + 1, drv_index + 1, fn[1])
  92.  
  93. workbook.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement