config file location overwrite

+ add environment toggle to overwrite the config directory

setting ejabberd_metrics_dev to 1 / true  -> set the config path inside the dev directory
This commit is contained in:
nico 2020-02-17 02:05:33 +01:00
parent 89176536ad
commit cf0357197e
Signed by: mightyBroccoli
GPG Key ID: EA7C31AAB1BDC1A2
2 changed files with 11 additions and 7 deletions

View File

@ -2,15 +2,19 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import json import json
import sys import sys
from os import environ
from pathlib import Path from pathlib import Path
class Config: class Config:
def __init__(self): def __init__(self):
# global config path # class variables
conf_path = '/etc/ejabberd-metrics.conf'
self.file = Path(conf_path)
self.content = None self.content = None
self.conf_file = Path('/etc/ejabberd-metrics.conf')
# dev config overwrite
if environ.get('ejabberd_metrics_dev'):
self.conf_file = Path('config.json')
# read config file # read config file
self._read() self._read()
@ -20,7 +24,7 @@ class Config:
self._check() self._check()
# open and load json content from config # open and load json content from config
with open(self.file, 'r', encoding='utf-8') as f: with open(self.conf_file, 'r', encoding='utf-8') as f:
try: try:
self.content = json.load(f) self.content = json.load(f)
@ -32,13 +36,13 @@ class Config:
def _check(self): def _check(self):
"""internal method to check if the config file exists""" """internal method to check if the config file exists"""
try: try:
# if file is present try to read it's contents # if file is present continue
if self.file.exists(): if self.conf_file.exists():
return return
# if not create a blank file # if not create a blank file
else: else:
Path.touch(self.file) self.conf_file.touch(mode=0o640)
# catch permission exceptions as this tries to write to /etc/ # catch permission exceptions as this tries to write to /etc/
except PermissionError as err: except PermissionError as err: