Skip to content

Commit

Permalink
Move token.json inside data_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxWinterstein committed Dec 3, 2021
1 parent ca3ca8a commit 4de7bae
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions toogoodtogo_ha_mqtt_bridge/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
import watchdog
from config import settings
from tgtg import TgtgClient

# pretty logging is pretty
from watchdog import Watchdog

logger = logging.getLogger(__name__)
coloredlogs.install(level="DEBUG", logger=logger)
coloredlogs.install(level="DEBUG", logger=logger) # pretty logging is pretty

mqtt_client = None
first_run = True
Expand Down Expand Up @@ -136,14 +134,14 @@ def write_token_file():
"last_time_token_refreshed": str(tgtg_client.last_time_token_refreshed),
}

with open("tokens.json", "w") as json_file:
with open(settings.get("data_dir") + "/tokens.json", "w") as json_file:
json.dump(tokens, json_file)

logger.debug("Written tokens.json file to filesystem")


def check_existing_token_file():
if os.path.isfile("tokens.json"):
if os.path.isfile(settings.get("data_dir") + "/tokens.json"):
read_token_file()
return True
else:
Expand All @@ -152,7 +150,7 @@ def check_existing_token_file():


def read_token_file():
with open("tokens.json") as f:
with open(settings.get("data_dir") + "/tokens.json") as f:
tokens = json.load(f)

if tokens:
Expand Down

7 comments on commit 4de7bae

@Dielee
Copy link
Collaborator

@Dielee Dielee commented on 4de7bae Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work if data_dir is not set, right ?
Then, data_dir have to be required in the new version!

@MaxWinterstein
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yes, but it is always set

Thas one/the reasing the settings go to a settings.local.json.

@Dielee
Copy link
Collaborator

@Dielee Dielee commented on 4de7bae Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my config it is not set, as this setting is not found in the readme.

@MaxWinterstein
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in your settings.local.json? or which settings file do you mean?

The user is not meant to mess with the settings.json file. Just with some settings.local.json.

@Dielee
Copy link
Collaborator

@Dielee Dielee commented on 4de7bae Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in the repo readme, there is data_dir marked as optinonal

@MaxWinterstein
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might not be written clearly, but it is definitely there.
Even if you don't set it explicitly, it is there.

Have a look at

Path(settings.get("data_dir")).mkdir(parents=True, exist_ok=True)

where you can see it has already been used for a while.

I use dynaconf to store settings and its layered workflow reads settings.json first, and then your settings.local.json is merged on top of it. So if you do not provide a data_dir at your settings.local.json the one from settings.json is used.

In fact, you can optionally provide a data_dir, which then will replace the one defined in settings.json. Either via settings.local.json, or e.g. environment variable DYNACONF_DATA_DIR.

@Dielee
Copy link
Collaborator

@Dielee Dielee commented on 4de7bae Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh now you got me. Thanks for this explanation!
I thought this will only load settings.local.json but forgot there is also an settings.json.

Please sign in to comment.