Browse Source

Further changes for python3

String handling changed a lot between Python 2 and 3 which also affected
the return type of `requests` module. To cover this, a check for the
python major version and a conversion of the requests
return type wherever necessary were included.
Alexander Marsteller 4 years ago
parent
commit
684e93a74f
1 changed files with 23 additions and 4 deletions
  1. 23 4
      core.py

+ 23 - 4
core.py

@@ -19,6 +19,8 @@ from tornado.escape import json_decode, json_encode, url_escape
 from threading import Timer
 import subprocess
 
+python_version = sys.version_info.major
+
 root = os.path.dirname(__file__)
 
 
@@ -104,6 +106,10 @@ def fetchDataADEI():
         data = requests.get(url,
                             auth=(os.environ["BORA_ADEI_USERNAME"],
                                   os.environ["BORA_ADEI_PASSWORD"])).content
+                            
+        if python_version == 3:
+            data = data.decode("utf-8")
+                            
         if data == "":
             logger.info(str(param) + ': Empty data!')
             continue
@@ -181,9 +187,9 @@ class DesignerHandler(tornado.web.RequestHandler):
             except yaml.YAMLError as exc:
                 print(exc)
 
-        with open("style.yaml", 'r') as stream:
+        with open("style.yaml", 'r') as stream2:
             try:
-                style_data = yaml.load(stream)
+                style_data = yaml.load(stream2)
             except yaml.YAMLError as exc:
                 print(exc)
 
@@ -192,7 +198,8 @@ class DesignerHandler(tornado.web.RequestHandler):
         else:
             index_data = cache_data
 
-        if index_data is not None:            index_data = sorted(index_data)
+        if index_data is not None:            
+            index_data = sorted(index_data)
 
         data = {
             "cache": cache_data,
@@ -228,7 +235,10 @@ class SaveHandler(tornado.web.RequestHandler):
         json_obj = json_decode(self.request.body)
 
         with open("style.yaml", 'w') as output:
-            output.write(yaml.safe_dump(json_obj,  encoding='utf-8',
+            if python_version == 3:
+                output.write(yaml.safe_dump(json_obj, allow_unicode=True, default_flow_style=False))
+            else:
+                output.write(yaml.safe_dump(json_obj,  encoding='utf-8',
                          allow_unicode=True, default_flow_style=False))
         response = {"success": "Data entry inserted."}
 
@@ -308,6 +318,11 @@ class UpdateHandler(tornado.web.RequestHandler):
             data = requests.get(url, auth=(os.environ["BORA_ADEI_USERNAME"],
                                 os.environ["BORA_ADEI_PASSWORD"]))
             cr = data.content
+            
+            
+            if python_version == 3:
+                cr = cr.decode("utf-8")
+            
             cr = cr.split(",")
 
             match_token = item
@@ -378,6 +393,10 @@ class AdeiKatrinHandler(tornado.web.RequestHandler):
                 os.environ["BORA_ADEI_PASSWORD"])
         )
         cr = data.content
+        
+        if python_version == 3:
+            cr = cr.decode("utf-8")
+        
         cr = cr.splitlines()
         cr = ",".join(cr)
         cr = cr.split(",")