Skip to content

Commit

Permalink
Change debug command to return string instead of print
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Nov 29, 2024
1 parent 98c4fef commit 4e5e0cb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
39 changes: 17 additions & 22 deletions ThermiaOnlineAPI/model/HeatPump.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
import logging
import sys
from ..utils.utils import pretty_print_except
from ..utils.utils import pretty_json_string_except

from typing import TYPE_CHECKING, Dict, List, Optional

Expand Down Expand Up @@ -940,17 +940,12 @@ def get_historical_data_for_register(
# Print debug data
###########################################################################

def debug(self):
print("Creating debug file")
def debug(self) -> str:
debug_str = "########## DEBUG START ##########\n"

original_stdout = sys.stdout
f = open("debug.txt", "w")
sys.stdout = f
debug_str += "self.__info:\n"

print("########## DEBUG START ##########")

print("self.__info:")
pretty_print_except(
debug_str += pretty_json_string_except(
self.__info,
[
"address",
Expand All @@ -964,11 +959,13 @@ def debug(self):
],
)

print("self.__status:")
pretty_print_except(self.__status)
debug_str += "self.__status:\n"

debug_str += pretty_json_string_except(self.__status)

debug_str += "self.__device_data:\n"

print("self.__device_data:")
pretty_print_except(
debug_str += pretty_json_string_except(
self.__device_data,
["macAddress", "owner", "retailerAccess", "retailerId", "id", "status"],
)
Expand All @@ -982,20 +979,18 @@ def debug(self):
installation_profile_id
)
if all_available_groups is not None:
print("All available groups:")
pretty_print_except(all_available_groups)
debug_str += "All available groups:\n"
debug_str += pretty_json_string_except(all_available_groups)

for group in all_available_groups:
group_name = group.get("name")
if group_name is not None:
print("Group " + group_name + ":")
debug_str += "Group " + group_name + ":\n"
group_data = self.__api_interface.get_register_group_json(
self.__device_id, group_name
)
pretty_print_except(group_data)
debug_str += pretty_json_string_except(group_data)

print("########## DEBUG END ##########")
debug_str += "########## DEBUG END ##########\n"

sys.stdout = original_stdout
f.close()
print("Debug file created")
return debug_str
16 changes: 11 additions & 5 deletions ThermiaOnlineAPI/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ def get_list_value_or_default(list, idx, default: T) -> T:
return default


def pretty_print(json_object):
print(json.dumps(json_object, indent=4, sort_keys=True))
print("\n")
def pretty_json_string(json_object) -> str:
pretty_str = json.dumps(json_object, indent=4, sort_keys=True)
pretty_str += "\n\n"

return pretty_str

def pretty_print_except(json_object, except_keys=[]):

def pretty_json_string_except(json_object, except_keys=[]) -> str:
if json_object is None:
return

json_object_copy = json_object.copy()
for key in except_keys:
if key in json_object_copy:
del json_object_copy[key]
pretty_print(json_object_copy)

pretty_json_str = pretty_json_string(json_object_copy)
pretty_json_str += "\n"

return pretty_json_str


def base64_url_encode(data):
Expand Down
7 changes: 6 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

heat_pump = thermia.heat_pumps[0]

heat_pump.debug()

print("Creating debug file")
with open("debug.txt", "w") as f:
f.write(heat_pump.debug())

print("Debug file created")

print("\n")
print("\n")
Expand Down

0 comments on commit 4e5e0cb

Please sign in to comment.