ServerModule1.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 (0,pprint.pformat(json.loads(json_data), indent=2))
  38. except JSONDecodeError as exc:
  39. error_message = ""
  40. if exc.lineno - 1 >= 0:
  41. error_message = error_message.join(json_data.splitlines()[exc.lineno - 3])
  42. if exc.lineno >= 0:
  43. error_message = error_message.join(json_data.splitlines()[exc.lineno - 2])
  44. error_message = error_message.join(json_data.splitlines()[exc.lineno - 1])
  45. if len(json_data.splitlines()) != exc.lineno:
  46. error_message = error_message.join(json_data.splitlines()[exc.lineno])
  47. return (1,error_message)