utilize /etc/ directory
+ add config.py to read / touch create the etc config file _read(): tries to open the config files in read only mode to parse its contents. _check(): checks if the config file is present and then calls _read() if that is false it tries to touch create a new file. worst case scenario: file is present and holds content, the method assumes that the file isn't present. Therefore tries to touch-create a new file, if a file is against all odds present, only the mtime would be updated. Hence any file content would not be altered. get(): main method, calling _check() if a key is given return only the assigned value or None or return the whole dictionary
This commit is contained in:
parent
0145458961
commit
eaed97cf4f
|
@ -110,6 +110,7 @@ dmypy.json
|
|||
# Pycharm
|
||||
.idea/
|
||||
.venv/
|
||||
venv
|
||||
|
||||
# config
|
||||
config.json
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class Config:
|
||||
def __init__(self):
|
||||
# global config path
|
||||
conf_path = '/etc/ejabberd-metrics.conf'
|
||||
self.file = Path(conf_path)
|
||||
|
||||
self.content = None
|
||||
|
||||
def _read(self):
|
||||
with open(self.file, 'r', encoding='utf-8') as f:
|
||||
try:
|
||||
self.content = json.load(f)
|
||||
|
||||
# catch json decoding errors
|
||||
except json.JSONDecodeError as err:
|
||||
print(err, file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
def _check(self):
|
||||
try:
|
||||
# if file is present try to read it's contents
|
||||
if self.file.exists():
|
||||
self._read()
|
||||
|
||||
# if not create a blank file
|
||||
else:
|
||||
Path.touch(self.file)
|
||||
|
||||
# catch permission exceptions as this tries to write to /etc/
|
||||
except PermissionError as err:
|
||||
print(err, file=sys.stderr)
|
||||
sys.exit(err.errno)
|
||||
|
||||
def get(self, key: str = None) -> (dict, None):
|
||||
self._check()
|
||||
|
||||
# if a special key is request, return just that
|
||||
if key is not None:
|
||||
|
||||
# safety measure
|
||||
if key in self.content:
|
||||
return self.content[key]
|
||||
|
||||
# if the key isn't part if self.content return None
|
||||
else:
|
||||
return None
|
||||
|
||||
# else return everything
|
||||
return self.content
|
|
@ -1,12 +1,10 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
from influxdb import InfluxDBClient
|
||||
|
||||
from config import Config
|
||||
from ejabberdrpc import EjabberdMetrics
|
||||
|
||||
|
||||
|
@ -79,9 +77,7 @@ class Influx:
|
|||
|
||||
if __name__ == '__main__':
|
||||
# load config
|
||||
path = os.path.dirname(__file__)
|
||||
with open('/'.join([path, 'config.json']), 'r', encoding='utf-8') as f:
|
||||
config = json.load(f)
|
||||
config = Config().get()
|
||||
|
||||
# creds and params
|
||||
url = config['url'] if 'url' in config else 'http://localhost:5280/api'
|
||||
|
|
Loading…
Reference in New Issue