Added migration for config

This commit is contained in:
Frisk 2021-04-17 16:13:24 +02:00
parent 065d67c90c
commit cab2d86481
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
6 changed files with 82 additions and 45 deletions

View file

@ -1,22 +1,33 @@
import json, sys, logging import json
import logging
import sys
global settings
def load_settings():
global settings
try: # load settings
with open("settings.json", encoding="utf8") as sfile:
settings = json.load(sfile)
if settings["limitrefetch"] < settings["limit"] and settings["limitrefetch"] != -1:
settings["limitrefetch"] = settings["limit"]
if "user-agent" in settings["header"]:
settings["header"]["user-agent"] = settings["header"]["user-agent"].format(version="1.13.1") # set the version in the useragent
except FileNotFoundError:
logging.critical("No config file could be found. Please make sure settings.json is in the directory.")
sys.exit(1)
# Set the cooldown to 15 seconds if it's a wiki farm like Fandom or Gamepedia and the cooldown is even lower than that.
# Look, it's unreasonable to have even higher refresh rate than that, seriously. Setting it even lower can cause issues
# for all users of the script for high usage of farm's servers. So please, do not remove this code unless you absolutely
# know what you are doing <3
if any(("fandom.com" in settings["wiki_url"], "gamepedia.com" in settings["wiki_url"])):
if settings["cooldown"] < 15:
settings["cooldown"] = 15
if settings["fandom_discussions"]["cooldown"] < 15:
settings["fandom_discussions"]["cooldown"] = 15
load_settings()
try: # load settings
with open("settings.json", encoding="utf8") as sfile:
settings = json.load(sfile)
if settings["limitrefetch"] < settings["limit"] and settings["limitrefetch"] != -1:
settings["limitrefetch"] = settings["limit"]
if "user-agent" in settings["header"]:
settings["header"]["user-agent"] = settings["header"]["user-agent"].format(version="1.13.1") # set the version in the useragent
except FileNotFoundError:
logging.critical("No config file could be found. Please make sure settings.json is in the directory.")
sys.exit(1)
# Set the cooldown to 15 seconds if it's a wiki farm like Fandom or Gamepedia and the cooldown is even lower than that.
# Look, it's unreasonable to have even higher refresh rate than that, seriously. Setting it even lower can cause issues
# for all users of the script for high usage of farm's servers. So please, do not remove this code unless you absolutely
# know what you are doing <3
if any(("fandom.com" in settings["wiki_url"], "gamepedia.com" in settings["wiki_url"])):
if settings["cooldown"] < 15:
settings["cooldown"] = 15
if settings["fandom_discussions"]["cooldown"] < 15:
settings["fandom_discussions"]["cooldown"] = 15

View file

@ -1,23 +0,0 @@
from src.configloader import settings
import logging
from src.migrations.utils import return_example_file
logger = logging.getLogger("rcgcdw.migrations.1.13.1.1")
base_file = return_example_file()
new_settings = settings.copy()
def run():
if "event_appearance" not in settings:
try:
settings["event_appearance"] = {}
struct = settings['appearance']['embed']
for key, value in struct.items():
settings["event_appearance"][key] = value
settings["event_appearance"][key]["emoji"] = base_file["event_appearance"]
except KeyError:
logger.error("Failed to migrate appearance embed.")
else: # Don't do migrations
return

46
src/migrations/11311.py Normal file
View file

@ -0,0 +1,46 @@
from src.configloader import settings, load_settings
import logging
import shutil
import time
import json
import sys
from src.migrations.utils import return_example_file
logger = logging.getLogger("rcgcdw.migrations.1.13.1.1")
base_file = return_example_file()
new_settings = settings.copy()
def run():
global base_file
if "event_appearance" not in settings:
logger.info("Running migration 1.13.1.1")
if "event_appearance" not in base_file: # if local base file is outdated, download from repo
base_file = return_example_file(force=True)
try:
struct = settings['appearance']['embed']
new_settings["event_appearance"] = {}
keys = []
for key, value in struct.items():
if key not in ("show_edit_changes", "show_footer", "embed_images"):
new_settings["event_appearance"][key] = value
try:
new_settings["event_appearance"][key]["emoji"] = base_file["event_appearance"][key]["emoji"]
except KeyError:
new_settings["event_appearance"][key]["emoji"] = ""
keys.append(key)
for item in keys:
del new_settings['appearance']['embed'][item]
except KeyError:
logger.exception("Failed to migrate appearance embed.")
sys.exit(1)
shutil.copy("settings.json", "settings.json.{}.bak".format(int(time.time())))
with open("settings.json", "w") as new_write:
new_write.write(json.dumps(new_settings, indent=4))
load_settings()
logger.info("Migration 1.13.1.1 has been successful.")
else:
logger.debug("Ignoring migration 1.13.1.1")
run()

View file

@ -0,0 +1 @@
__all__ = ["11311"]

View file

@ -5,8 +5,10 @@ import json
discussion_logger = logging.getLogger("rcgcdw.migrations.utils") discussion_logger = logging.getLogger("rcgcdw.migrations.utils")
def return_example_file() -> dict: def return_example_file(force=False) -> dict:
try: try:
if force:
raise FileNotFoundError
with open('settings.json.example', 'r') as example_file: with open('settings.json.example', 'r') as example_file:
return json.loads(example_file.read()) return json.loads(example_file.read())
except FileNotFoundError: except FileNotFoundError:

View file

@ -46,7 +46,7 @@ TESTING = True if "--test" in sys.argv else False # debug mode, pipeline testin
logging.config.dictConfig(settings["logging"]) logging.config.dictConfig(settings["logging"])
logger = logging.getLogger("rcgcdw") logger = logging.getLogger("rcgcdw")
logger.debug("Current settings: {settings}".format(settings=settings)) logger.debug("Current settings: {settings}".format(settings=settings))
from src.migrations import * # migrations after logging
storage = datafile storage = datafile
# Remove previous data holding file if exists and limitfetch allows # Remove previous data holding file if exists and limitfetch allows