Commit eeaa4007 authored by Kirill Smelkov's avatar Kirill Smelkov

*: No need to decode bytes when fed to json.loads

Starting from py3.6 json.loads accepts all unicode, bytes and bytearray:

    https://docs.python.org/3/library/json.html#:~:text=json.loads(s
    https://github.com/python/cpython/commit/b161562f72a2
    https://bugs.python.org/issue17909
parent b067cc0e
......@@ -22,8 +22,6 @@ import socket
from datetime import datetime
from xml.sax.saxutils import escape
from slapos.util import bytes2str
OPML_START = """<?xml version="1.0" encoding="UTF-8"?>
<!-- OPML generated by SlapOS -->
<opml version="1.1">
......@@ -233,7 +231,7 @@ class Monitoring(object):
print("ERROR: Timeout with %r while downloading monitor config at %s " % (e, url))
else:
try:
monitor_dict = json.loads(bytes2str(response.read()))
monitor_dict = json.loads(response.read())
monitor_title = monitor_dict.get('title', 'Unknown Instance')
success = True
except ValueError as e:
......
......@@ -29,7 +29,7 @@ class RunPromise(JSONPromise):
for f in iter_logrotate_file_handle(self.netconf_log, 'rb'):
for line in iter_reverse_lines(f):
l = json.loads(line.decode())
l = json.loads(line)
alarm_notif = l.get('data', {}).get('notification', {}).get('alarm-notif', None)
if alarm_notif and alarm_notif['fault-id'] == '102':
if alarm_notif['is-cleared'] == 'false':
......
......@@ -29,7 +29,7 @@ class RunPromise(JSONPromise):
for f in iter_logrotate_file_handle(self.netconf_log, 'rb'):
for line in iter_reverse_lines(f):
l = json.loads(line.decode())
l = json.loads(line)
alarm_notif = l.get('data', {}).get('notification', {}).get('alarm-notif', None)
if alarm_notif and alarm_notif['fault-id'] == '103':
if alarm_notif['is-cleared'] == 'false':
......
......@@ -29,7 +29,7 @@ class RunPromise(JSONPromise):
for f in iter_logrotate_file_handle(self.netconf_log, 'rb'):
for line in iter_reverse_lines(f):
l = json.loads(line.decode())
l = json.loads(line)
alarm_notif = l.get('data', {}).get('notification', {}).get('alarm-notif', None)
if alarm_notif and alarm_notif['fault-id'] == '9':
if alarm_notif['is-cleared'] == 'false':
......
......@@ -29,7 +29,7 @@ class RunPromise(JSONPromise):
for f in iter_logrotate_file_handle(self.netconf_log, 'rb'):
for line in iter_reverse_lines(f):
l = json.loads(line.decode())
l = json.loads(line)
alarm_notif = l.get('data', {}).get('notification', {}).get('alarm-notif', None)
if alarm_notif and alarm_notif['fault-id'] == '101':
if alarm_notif['is-cleared'] == 'false':
......
......@@ -29,7 +29,7 @@ class RunPromise(JSONPromise):
for f in iter_logrotate_file_handle(self.netconf_log, 'rb'):
for line in iter_reverse_lines(f):
l = json.loads(line.decode())
l = json.loads(line)
alarm_notif = l.get('data', {}).get('notification', {}).get('alarm-notif', None)
if alarm_notif and alarm_notif['fault-id'] == '18':
if alarm_notif['is-cleared'] == 'false':
......
......@@ -29,7 +29,7 @@ class RunPromise(JSONPromise):
for f in iter_logrotate_file_handle(self.netconf_log, 'rb'):
for line in iter_reverse_lines(f):
l = json.loads(line.decode())
l = json.loads(line)
alarm_notif = l.get('data', {}).get('notification', {}).get('alarm-notif', None)
if alarm_notif and alarm_notif['fault-id'] == '9':
if alarm_notif['is-cleared'] == 'false':
......
......@@ -47,7 +47,7 @@ def get_json_log_data_interval(json_log_file, interval):
data_list = []
for f in iter_logrotate_file_handle(json_log_file, 'rb'):
for line in iter_reverse_lines(f):
l = json.loads(line.decode())
l = json.loads(line)
timestamp = dateparser.parse(l['time'])
if (current_time - timestamp).total_seconds() > interval:
return data_list
......@@ -61,7 +61,7 @@ def get_json_log_latest_timestamp(json_log_file):
"""
for f in iter_logrotate_file_handle(json_log_file, 'rb'):
for line in iter_reverse_lines(f):
l = json.loads(line.decode())
l = json.loads(line)
return dateparser.parse(l['time']).timestamp()
return 0
......
......@@ -92,7 +92,7 @@ ipv4 = 10.0.151.118
pkg_resources.resource_string(
'slapos.monitor',
'doc/monitor_instance.schema.json')
self.monitor_instance_schema = json.loads(monitor_schema_string.decode('utf-8'))
self.monitor_instance_schema = json.loads(monitor_schema_string)
self.monitor_config_dict = dict(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment