1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
# Some json parsing functions.
#
# Copyright (C) 2016 Andrew Cagney <cagney@gnu.org>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
import json
from datetime import datetime, timezone
class result:
directory = "directory"
testname = "testname"
expect = "expect"
result = "result"
hosts = "hosts"
issues = "errors" # for historic reasons, "issues" are publicly called "errors"
test_time = "test_time"
boot_time = "boot_time"
hosts = "hosts"
# keep things going
time = "time"
runtime = "runtime"
def load(io):
try:
return json.load(io)
except ValueError:
return None
def loads(s):
try:
return json.loads(s)
except ValueError:
return None
def default(obj):
if hasattr(obj, "__json__") and callable(getattr(obj, "__json__")):
return obj.__json__()
if hasattr(obj, "isoformat") and callable(getattr(obj, "isoformat")):
# date/time objects
if not obj.utcoffset():
# add local timezone to "naive" local time
# https://stackoverflow.com/questions/2720319/python-figure-out-local-timezone
tzinfo = datetime.now(timezone.utc).astimezone().tzinfo
obj = obj.replace(tzinfo=tzinfo)
# convert to UTC
obj = obj.astimezone(timezone.utc)
# strip the UTC offset
obj = obj.replace(tzinfo=None)
return obj.isoformat() + "Z"
elif hasattr(obj, "__str__") and callable(getattr(obj, "__str__")):
return str(obj)
else:
print("obj:", obj)
raise TypeError(obj)
def dump(j, io):
json.dump(j, io, indent=2, default=default)
def dumps(s):
return json.dumps(s, default=default)
def ftime(t):
"""Format the time"""
return t.strftime("%Y-%m-%d %H:%M")
def ptime(s):
"""Tries to parse what is assumed to be local time"""
# print(s)
for hms in ["%H:%M", "%H:%M:%S", "%H:%M:%S.%f"]:
for t in [" ", "T"]:
try:
t = datetime.strptime(s, "%Y-%m-%d" + t + hms)
# print(t)
return t
except:
None
return None
|