generate.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # read server controller
  2. import re
  3. if __name__ == '__main__':
  4. with open('server_controller.py', 'r') as file:
  5. text = file.read().replace('\n', '')
  6. text = text.split('def ')
  7. del text[0]
  8. documentation = '''# the general syntax is
  9. # {attribute1: value, attribute2: value, ...}
  10. #
  11. # for example a valid request to /register would look like
  12. # {"email": "user123@example.org", "username": "user123", "password": "FILTERED", "preferred_language": "german"}
  13. # while a valid request to /events would be the empty object {}
  14. \n'''
  15. for method in sorted(text):
  16. method = 'def ' + method
  17. method_name = re.search(r"def (.*?)\s*\(", method)[1]
  18. if method_name[0] == '_':
  19. continue
  20. directly_used_arguments = re.findall(r"json_request\['(.*?)'\]", method)
  21. missing_attributes = re.search(r"missing_attributes\(json_request, \[((.|\n)*?)\]\)", method)
  22. if missing_attributes is None:
  23. print('HINT: method', method_name, 'does not require any parameters')
  24. required_arguments = []
  25. else:
  26. required_arguments = re.findall(r"'(.*?)'", missing_attributes[0])
  27. required_arguments = sorted(list(set(required_arguments)))
  28. possible_arguments = sorted(list(set(directly_used_arguments + required_arguments)))
  29. documentation += method_name + '_required_attributes = ' + str(required_arguments) + '\n'
  30. documentation += method_name + '_possible_attributes = ' + str(possible_arguments) + '\n\n'
  31. with open("doc/documentation.py", "w", newline='\n') as file:
  32. file.write(documentation[:-1])
  33. print(documentation)