ServerModule1.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import anvil.tables as tables
  2. import anvil.tables.query as q
  3. from anvil.tables import app_tables
  4. import anvil.server
  5. import yaml
  6. import json
  7. import pprint
  8. from json.decoder import JSONDecodeError
  9. # This is a server module. It runs on the Anvil server,
  10. # rather than in the user's browser.
  11. #
  12. # To allow anvil.server.call() to call functions here, we mark
  13. # them with @anvil.server.callable.
  14. # Here is an example - you can replace it with your own:
  15. #
  16. @anvil.server.callable
  17. def parse_yaml(yaml_data):
  18. try:
  19. return (0,pprint.pformat(yaml.safe_load(yaml_data), indent=2))
  20. except yaml.YAMLError as exc:
  21. if hasattr(exc, 'problem_mark'):
  22. if exc.context != None:
  23. error_message = ('Error while parsing YAML file:\n'+
  24. 'parser says\n' + str(exc.problem_mark) + '\n ' +
  25. str(exc.problem) + ' ' + str(exc.context) +
  26. '\nPlease correct data and retry.')
  27. else:
  28. error_message = ('Error while parsing YAML file:\n'+' parser says\n' + str(exc.problem_mark) + '\n ' +
  29. str(exc.problem) + '\nPlease correct data and retry.')
  30. else:
  31. error_message = ("Something went wrong while parsing yaml file")
  32. return (1,error_message)
  33. #
  34. @anvil.server.callable
  35. def parse_json(json_data):
  36. try:
  37. return pprint.pformat(json.loads(json_data), indent=2)
  38. except JSONDecodeError as exc:
  39. print(json_data)
  40. if hasattr(exc, 'problem_mark'):
  41. if exc.context != None:
  42. error_message = ('Error while parsing YAML file:\n'+
  43. 'parser says\n' + str(exc.problem_mark) + '\n ' +
  44. str(exc.problem) + ' ' + str(exc.context) +
  45. '\nPlease correct data and retry.')
  46. else:
  47. error_message = ('Error while parsing YAML file:\n'+' parser says\n' + str(exc.problem_mark) + '\n ' +
  48. str(exc.problem) + '\nPlease correct data and retry.')
  49. else:
  50. error_message = ("Something went wrong while parsing yaml file")
  51. return (1,error_message)