aboutsummaryrefslogtreecommitdiffstats
path: root/config.py
blob: 117c0169c840633215e716fe6ee6446c7b355b58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -*- coding: utf-8 -*-
import json
import os
import sys


class Config(object):
	def __init__(self):
		self.config = dict()

		# filepath of the config.json in the project directory
		self.path = os.path.dirname(__file__)
		self.filepath = ('/'.join([self.path, 'config.json']))

	def load(self):
		try:
			# try to read config.json
			with open(self.filepath, "r", encoding="utf-8") as f:
				self.config = json.load(f)

		except FileNotFoundError:
			# if file is absent create file
			open(self.filepath, "w").close()
			print("-- config.json is missing.", file=sys.stderr)
			print("-- {file} has been created.".format(file=self.filepath), file=sys.stderr)

		except json.decoder.JSONDecodeError:
			# config file is present but empty
			print("-- JSON parsing error, please check your config.json file.", file=sys.stderr)
			pass

		return self.config