2016-02-29 10:48:00 +02:00
|
|
|
#!/usr/bin/python3
|
2016-02-08 19:02:31 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
import os
|
|
|
|
from configparser import SafeConfigParser
|
2016-02-08 19:02:31 +02:00
|
|
|
from os import listdir
|
|
|
|
from os.path import isfile, join
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-02-08 19:02:31 +02:00
|
|
|
|
|
|
|
class ConfigManager(object):
|
2016-03-23 17:15:27 +02:00
|
|
|
"""
|
|
|
|
This class written for configuration file management of ahenk and ahenk plugins
|
|
|
|
Sample ahenk configuration file path /etc/ahenk/ahenk.conf and sample ahenk plugins configuration folder path /etc/ahenk/config.d/
|
|
|
|
Usage: Takes two argument, - both of them are optional - one of the is a configuration file path the other one
|
|
|
|
is configuration files folder path
|
|
|
|
"""
|
|
|
|
|
2016-08-18 21:03:36 +03:00
|
|
|
def __init__(self, configuration_file_path=None, configuration_folder_path=None):
|
|
|
|
self.configurationFilePath = configuration_file_path
|
|
|
|
self.configurationFolderPath = configuration_folder_path
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
def read(self):
|
2016-08-18 21:03:36 +03:00
|
|
|
config_files = []
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
# Check if given ahenk configuration file exists
|
|
|
|
# If file exists add it to configFiles array.
|
|
|
|
# TODO must write config file validater !!
|
|
|
|
if self.configurationFilePath:
|
|
|
|
if os.path.exists(self.configurationFilePath):
|
2016-08-18 21:03:36 +03:00
|
|
|
config_files.append(self.configurationFilePath)
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
if self.configurationFolderPath and os.path.exists(self.configurationFolderPath):
|
|
|
|
files = [f for f in listdir(self.configurationFolderPath) if isfile(join(self.configurationFolderPath, f))]
|
|
|
|
for f in files:
|
2016-08-18 21:03:36 +03:00
|
|
|
config_files.append(join(self.configurationFolderPath, f))
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
parser = SafeConfigParser()
|
2016-08-18 21:03:36 +03:00
|
|
|
configValues = parser.read(config_files)
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
return parser
|