| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import anvil.tables as tables
- import anvil.tables.query as q
- from anvil.tables import app_tables
- import anvil.server
- import yaml
- import json
- import pprint
- from json.decoder import JSONDecodeError
- # This is a server module. It runs on the Anvil server,
- # rather than in the user's browser.
- #
- # To allow anvil.server.call() to call functions here, we mark
- # them with @anvil.server.callable.
- # Here is an example - you can replace it with your own:
- #
- @anvil.server.callable
- def parse_yaml(yaml_data):
- try:
- return (0,pprint.pformat(yaml.safe_load(yaml_data), indent=2))
- except yaml.YAMLError as exc:
- if hasattr(exc, 'problem_mark'):
- if exc.context != None:
- error_message = ('Error while parsing YAML file:\n'+
- 'parser says\n' + str(exc.problem_mark) + '\n ' +
- str(exc.problem) + ' ' + str(exc.context) +
- '\nPlease correct data and retry.')
- else:
- error_message = ('Error while parsing YAML file:\n'+' parser says\n' + str(exc.problem_mark) + '\n ' +
- str(exc.problem) + '\nPlease correct data and retry.')
- else:
- error_message = ("Something went wrong while parsing yaml file")
- return (1,error_message)
- #
- @anvil.server.callable
- def parse_json(json_data):
- try:
- return pprint.pformat(json.loads(json_data), indent=2)
- except JSONDecodeError as exc:
- print(json_data)
- if hasattr(exc, 'problem_mark'):
- if exc.context != None:
- error_message = ('Error while parsing YAML file:\n'+
- 'parser says\n' + str(exc.problem_mark) + '\n ' +
- str(exc.problem) + ' ' + str(exc.context) +
- '\nPlease correct data and retry.')
- else:
- error_message = ('Error while parsing YAML file:\n'+' parser says\n' + str(exc.problem_mark) + '\n ' +
- str(exc.problem) + '\nPlease correct data and retry.')
- else:
- error_message = ("Something went wrong while parsing yaml file")
- return (1,error_message)
|