Procházet zdrojové kódy

Edited ServerModule1 and Form1

florian.residori@gmail.com před 3 roky
rodič
revize
d0d32bb869
2 změnil soubory, kde provedl 40 přidání a 5 odebrání
  1. 5 1
      client_code/Form1/__init__.py
  2. 35 4
      server_code/ServerModule1.py

+ 5 - 1
client_code/Form1/__init__.py

@@ -15,7 +15,11 @@ class Form1(Form1Template):
 
   def check_yaml_click(self, **event_args):
     """This method is called when the button is clicked"""
-    parsed_data = anvil.server.call('parse_yaml',self.input.text)
+    status, parsed_data = anvil.server.call('parse_yaml',self.input.text)
+    if status==1:
+      self.output.foreground = 'red'
+    else:
+      self.output.foreground = 'blue'
     self.output.text = parsed_data
 
   def check_json_click(self, **event_args):

+ 35 - 4
server_code/ServerModule1.py

@@ -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)