Split the config code to a new module resolving #81

This commit is contained in:
Frisk 2019-05-20 16:51:17 +02:00
parent 966ec64e84
commit e8fc3eb9ad
No known key found for this signature in database
GPG key ID: 0E9A7D3C0A01586C
3 changed files with 15 additions and 13 deletions

12
configloader.py Normal file
View file

@ -0,0 +1,12 @@
import json, sys, logging
try: # load settings
with open("settings.json") 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.6.0.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)

View file

@ -16,6 +16,7 @@
import json, logging, sys, re
from html.parser import HTMLParser
from configloader import settings
# Create a custom logger

View file

@ -25,6 +25,7 @@ from bs4 import BeautifulSoup
from collections import defaultdict, Counter
from urllib.parse import quote_plus
from html.parser import HTMLParser
from configloader import settings
if __name__ != "__main__": # return if called as a module
logging.critical("The file is being executed as a module. Please execute the script using the console.")
@ -32,23 +33,11 @@ if __name__ != "__main__": # return if called as a module
TESTING = True if "--test" in sys.argv else False # debug mode, pipeline testing
try: # load settings
with open("settings.json") 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.6.0.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)
logging.debug("Current settings: {settings}".format(settings=settings))
# Prepare logging
logging.config.dictConfig(settings["logging"])
logger = logging.getLogger("rcgcdw")
logger.debug("Current settings: {settings}".format(settings=settings))
# Setup translation