Skip to content

Commit

Permalink
core: configuration loading and logging function
Browse files Browse the repository at this point in the history
  • Loading branch information
Annika committed May 22, 2020
1 parent 3ad30d7 commit c45a028
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
config.py
config.json
__pycache__/**
__pycache__
backup/*
logs/*
data/*
backup/**Ã
backup/**
5 changes: 5 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"username": "",
"password": "",
"loglevel": 2
}
26 changes: 26 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/python3

import json

################## config.py ###################
## loads configuration data from config.json ##
## by Annika ##
################################################

CONFIG_PATH = 'config.json'
CONFIG_VARS = ['username', 'password', 'loglevel']

def loadConfig():
configData = json.load(open(CONFIG_PATH, 'r'))
returnValue = []
# make sure we're not missing anything
for configItem in CONFIG_VARS:
if configItem not in configData:
# We can't use core.log because config.loglevel doesn't exist yet
print("E: {item} not found in config.json".format(item = configItem))
else:
returnValue.append(configData[configItem])
return returnValue

username, password, loglevel = loadConfig()
# the order of these needs to be the same as in CONFIG_VARS
21 changes: 21 additions & 0 deletions core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/python3

import config

################## core.py #####################
## core functionality of Expecto Botronum ##
## by Annika ##
################################################

######################
## Helper Functions ##
######################

def log(message):
if config.loglevel > 2 or message[:2] == 'E:':
# Errors are always logged and everything in debug mode is logged.
print(message)
elif message[:2] == 'W:' and config.loglevel >= 1:
print(message)
elif message[:2] == 'I:' and config.loglevel >= 2:
print(message)

0 comments on commit c45a028

Please sign in to comment.