Handle relative config paths properly independently of working directory

master
Thor 3 years ago
parent 5e08f59a10
commit 255126b1ee
  1. 19
      bot.py
  2. 3
      cringebot.py

@ -1,4 +1,5 @@
import os
import sys
import time
import copy
import json
@ -41,16 +42,18 @@ class BotClient:
return self.bot.log(obj, infix = "{}: {}".format(self.config["name"], infix))
def setup(self):
if not os.path.exists(self.config["client_file"]):
client_file_path = os.path.join(os.path.dirname(sys.argv[0]), self.config["client_file"])
if not os.path.exists(client_file_path):
Mastodon.create_app(
self.app_name,
api_base_url = self.config["base_url"],
to_file = self.config["client_file"])
to_file = client_file_path)
if not os.path.exists(self.config["user_file"]):
user_file_path = os.path.join(os.path.dirname(sys.argv[0]), self.config["user_file"])
if not os.path.exists(user_file_path):
api = Mastodon(
api_base_url = self.config["base_url"],
client_id = self.config["client_file"])
client_id = client_file_path)
auth_url = api.auth_request_url()
@ -62,10 +65,10 @@ class BotClient:
self.log()
api.log_in(code = auth_code, to_file = self.config["user_file"])
api.log_in(code = auth_code, to_file = user_file_path)
self.api = Mastodon(
access_token = self.config["user_file"],
access_token = user_file_path,
api_base_url = self.config["base_url"])
def start(self):
@ -128,13 +131,15 @@ class BotClient:
def on_load_state(self):
if os.path.exists(self.config["state_file"]):
state_file_path = os.path.join(os.path.dirname(sys.argv[0]), self.config["state_file"])
with open(self.config["state_file"]) as json_file:
return json.load(json_file)
return copy.deepcopy(self.DEFAULT_STATE)
def on_save_state(self, state):
with open(self.config["state_file"], "w") as json_file:
state_file_path = os.path.join(os.path.dirname(sys.argv[0]), self.config["state_file"])
with open(state_file_path, "w") as json_file:
json.dump(state, json_file, indent = 4)
class Bot:

@ -310,7 +310,8 @@ def toot_dict_to_mail(toot_dict):
return bogofilter.Mail(headers = headers, body = body)
bot = Bot(CringeBotClient, toml.load("config.toml"))
config_path = os.path.join(os.path.dirname(sys.argv[0]), "config.toml")
bot = Bot(CringeBotClient, toml.load(config_path))
bot.start()
while True:

Loading…
Cancel
Save