From 47b5788a09e37c246ebc640c7c1223c3b78e7294 Mon Sep 17 00:00:00 2001 From: Astrid Date: Wed, 6 Jan 2021 11:53:51 +0100 Subject: [PATCH 1/3] Store bot data in a data/ directory --- database.py | 3 ++- games.py | 11 ++++++++--- the_prestige.py | 10 +++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/database.py b/database.py index a53da43..edccb3b 100644 --- a/database.py +++ b/database.py @@ -2,12 +2,13 @@ import os, json, datetime, re import sqlite3 as sql +data_dir = "data" def create_connection(): #create connection, create db if doesn't exist conn = None try: - conn = sql.connect("matteo.db") + conn = sql.connect(os.path.join(data_dir, "matteo.db")) return conn except: print("oops, db connection no work") diff --git a/games.py b/games.py index bc7a716..8ca356d 100644 --- a/games.py +++ b/games.py @@ -2,8 +2,13 @@ import json, random, os, math, jsonpickle from enum import Enum import database as db +data_dir = "data" +games_config_file = os.path.join(data_dir, "games_config.json") + def config(): - if not os.path.exists("games_config.json"): + if not os.path.exists(os.path.dirname(games_config_file)): + os.makedirs(os.path.dirname(games_config_file)) + if not os.path.exists(games_config_file): #generate default config config_dic = { "default_length" : 3, @@ -16,11 +21,11 @@ def config(): "stolen_base_chance_mod" : 1, "stolen_base_success_mod" : 1 } - with open("games_config.json", "w") as config_file: + with open(games_config_file, "w") as config_file: json.dump(config_dic, config_file, indent=4) return config_dic else: - with open("games_config.json") as config_file: + with open(games_config_file) as config_file: return json.load(config_file) def all_weathers(): diff --git a/the_prestige.py b/the_prestige.py index a324c19..87a1959 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -4,6 +4,8 @@ import onomancer as ono from flask import Flask from uuid import uuid4 +data_dir = "data" +config_filename = os.path.join(data_dir, "config.json") class Command: def isauthorized(self, user): @@ -605,7 +607,9 @@ thread1 = threading.Thread(target=main_controller.update_loop) thread1.start() def config(): - if not os.path.exists("config.json"): + if not os.path.exists(os.path.dirname(config_filename)): + os.makedirs(os.path.dirname(config_filename)) + if not os.path.exists(config_filename): #generate default config config_dic = { "token" : "", @@ -617,12 +621,12 @@ def config(): "soulscream channel id" : 0, "game_freeze" : 0 } - with open("config.json", "w") as config_file: + with open(config_filename, "w") as config_file: json.dump(config_dic, config_file, indent=4) print("please fill in bot token and any bot admin discord ids to the new config.json file!") quit() else: - with open("config.json") as config_file: + with open(config_filename) as config_file: return json.load(config_file) @client.event From e44ce70a1c0cb4a042e52d9ed6d1539b83feb436 Mon Sep 17 00:00:00 2001 From: Astrid Date: Wed, 6 Jan 2021 12:03:52 +0100 Subject: [PATCH 2/3] Use sqlite3 write-ahead log --- database.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/database.py b/database.py index edccb3b..d10a056 100644 --- a/database.py +++ b/database.py @@ -9,6 +9,10 @@ def create_connection(): conn = None try: conn = sql.connect(os.path.join(data_dir, "matteo.db")) + + # enable write-ahead log for performance and resilience + conn.execute('pragma journal_mode=wal') + return conn except: print("oops, db connection no work") From 1d3ac175e31ec334dadb9427dc0f2219748d3d3e Mon Sep 17 00:00:00 2001 From: Astrid Date: Wed, 6 Jan 2021 12:04:44 +0100 Subject: [PATCH 3/3] Add WAL files to .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 1722461..72644a6 100644 --- a/.gitignore +++ b/.gitignore @@ -347,4 +347,6 @@ ids # database matteo.db +matteo.db-wal +matteo.db-shm /matteo_env/Lib/site-packages/flask_socketio/__init__.py