|
|
@@ -4,6 +4,8 @@ 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.
|
|
|
@@ -13,10 +15,39 @@ import json
|
|
|
# Here is an example - you can replace it with your own:
|
|
|
#
|
|
|
@anvil.server.callable
|
|
|
-def parse_yaml(text):
|
|
|
- return yaml.safe_load(text)
|
|
|
+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_yaml(text):
|
|
|
- return yaml.safe_load(text)
|
|
|
+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)
|