Skip to content

Commit

Permalink
feat: configurable settings file path
Browse files Browse the repository at this point in the history
- Made settings file configurable via --settings-file launch arg
- Fixed bug with settings not loading properly after latest refactor
  • Loading branch information
moonburnt committed Aug 18, 2021
1 parent 672d1b1 commit 54498d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
24 changes: 14 additions & 10 deletions notashark/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def main():
help="Enable showcase of logs in terminal",
)
# TODO: maybe add arg to override log file location/name?

ap.add_argument(
"--settings-file",
help="Custom path to settings file",
)
args = ap.parse_args()

if args.debug:
Expand All @@ -97,44 +100,45 @@ def main():
# terminal_handler.setLevel(logging.ERROR)
log.addHandler(terminal_handler)

BOT_TOKEN = args.token or environ.get("NOTASHARK_DISCORD_KEY", None)
if not BOT_TOKEN:
bot_token = args.token or environ.get("NOTASHARK_DISCORD_KEY", None)
if not bot_token:
log.critical(
"You didnt specify bot's token! Either set NOTASHARK_DISCORD_KEY "
"environment variable, or pass it via --token launch argument!\nAbort"
)
exit(1)

SERVERS_AUTOUPDATE_TIME = (
servers_autoupdate_time = (
args.serverlist_update_time
if args.serverlist_update_time
and args.serverlist_update_time > fetcher.DEFAULT_AUTOUPDATE_TIME
else fetcher.DEFAULT_AUTOUPDATE_TIME
)
log.info(f"Serverlist will autoupdate each {SERVERS_AUTOUPDATE_TIME} seconds")
log.info(f"Serverlist will autoupdate each {servers_autoupdate_time} seconds")

SETTINGS_AUTOSAVE_TIME = (
settings_autosave_time = (
args.settings_autosave_time
if args.settings_autosave_time
and args.settings_autosave_time > settings.DEFAULT_AUTOSAVE_TIME
else settings.DEFAULT_AUTOSAVE_TIME
)
log.info(f"Settings will autosave each {SETTINGS_AUTOSAVE_TIME} seconds")
log.info(f"Settings will autosave each {settings_autosave_time} seconds")

# Configuring instances of manager and fetcher to use our settings
settings_manager = settings.SettingsManager(
autosave_time=SETTINGS_AUTOSAVE_TIME,
autosave_time=settings_autosave_time,
settings_file=args.settings_file or settings.DEFAULT_SETTINGS_FILE,
)
api_fetcher = fetcher.ApiFetcher(
autoupdate_time=SERVERS_AUTOUPDATE_TIME,
autoupdate_time=servers_autoupdate_time,
)

# Passing our pre-configured instances to bot
bot = discord_bot.make_bot(
settings_manager=settings_manager,
api_fetcher=api_fetcher,
)
bot.run(BOT_TOKEN)
bot.run(bot_token)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion notashark/src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, settings_file: str = None, autosave_time: int = None):
self.settings_file = settings_file or DEFAULT_SETTINGS_FILE
self.autosave_time = autosave_time or DEFAULT_AUTOSAVE_TIME
self.storage = {}
self.load_settings()

def get_settings(self, filepath: str = None) -> dict:
"""Get settings from provided file"""
Expand All @@ -46,7 +47,7 @@ def get_settings(self, filepath: str = None) -> dict:
def load_settings(self, filepath: str = None):
"""Load settings into self.storage"""
try:
settings = get_settings(filepath)
settings = self.get_settings(filepath)
except Exception as e:
log.error(f"Unable to load settings from {filepath}: {e}")
else:
Expand Down

0 comments on commit 54498d1

Please sign in to comment.