Support for profil settings

This commit is contained in:
Julien Duponchelle 2016-08-22 17:21:03 +02:00
parent cde28c849e
commit 5bb1abb2a9
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
3 changed files with 38 additions and 12 deletions

View File

@ -30,18 +30,19 @@ import logging
log = logging.getLogger(__name__)
class Config(object):
class Config:
"""
Configuration file management using configparser.
:params files: Array of configuration files (optional)
:params config_directory: Path of the configuration directory. If None default OS directory
:param files: Array of configuration files (optional)
:param profil: Profil settings (default use standard settings file)
"""
def __init__(self, files=None, config_directory=None):
def __init__(self, files=None, profil=None):
self._files = files
self._profil = profil
# Monitor configuration files for changes
self._watched_files = {}
@ -61,10 +62,16 @@ class Config(object):
appdata = os.path.expandvars("%APPDATA%")
common_appdata = os.path.expandvars("%COMMON_APPDATA%")
if self._profil:
user_dir = os.path.join(appdata, appname, "profiles", self._profil)
else:
user_dir = os.path.join(appdata, appname)
filename = "gns3_server.ini"
if self._files is None:
self._files = [os.path.join(os.getcwd(), filename),
os.path.join(appdata, appname, filename),
os.path.join(user_dir, filename),
os.path.join(appdata, appname + ".ini"),
os.path.join(common_appdata, appname, filename),
os.path.join(common_appdata, appname + ".ini")]
@ -80,9 +87,15 @@ class Config(object):
appname = "GNS3"
home = os.path.expanduser("~")
filename = "gns3_server.conf"
if self._profil:
user_dir = os.path.join(home, ".config", appname, "profiles", self._profil)
else:
user_dir = os.path.join(home, ".config", appname)
if self._files is None:
self._files = [os.path.join(os.getcwd(), filename),
os.path.join(home, ".config", appname, filename),
os.path.join(user_dir, filename),
os.path.join(home, ".config", appname + ".conf"),
os.path.join("/etc/gns3", filename),
os.path.join("/etc/xdg", appname, filename),
@ -93,6 +106,13 @@ class Config(object):
self.clear()
self._watch_config_file()
@property
def profil(self):
"""
Settings profil
"""
return self._profil
def clear(self):
"""Restart with a clean config"""
self._config = configparser.RawConfigParser()
@ -193,16 +213,15 @@ class Config(object):
self.set_section_config(section, conf)
@staticmethod
def instance(files=None):
def instance(*args, **kwargs):
"""
Singleton to return only one instance of Config.
:params files: Array of configuration files (optional)
:returns: instance of Config
"""
if not hasattr(Config, "_instance") or Config._instance is None:
Config._instance = Config(files)
Config._instance = Config(*args, **kwargs)
return Config._instance
@staticmethod

View File

@ -48,9 +48,13 @@ class Controller:
config_path = os.path.join(os.path.expandvars("%APPDATA%"), "GNS3")
else:
config_path = os.path.join(os.path.expanduser("~"), ".config", "GNS3")
self._config_file = os.path.join(config_path, "gns3_controller.conf")
server_config = Config.instance().get_section_config("Server")
if Config.instance().profil:
config_path = os.path.join(config_path, "profiles", Config.instance().profil)
self._config_file = os.path.join(config_path, "gns3_controller.conf")
log.info("Load controller configuration file {}".format(self._config_file))
if server_config.getboolean("local", False) is True:
self._computes["local"] = Compute(compute_id="local",
controller=self,

View File

@ -106,10 +106,13 @@ def parse_arguments(argv):
parser.add_argument("--log", help="send output to logfile instead of console")
parser.add_argument("--daemon", action="store_true", help="start as a daemon")
parser.add_argument("--pid", help="store process pid")
parser.add_argument("--profil", help="Settings profil (blank will use default settings files)")
args = parser.parse_args(argv)
if args.config:
Config.instance(files=[args.config])
Config.instance(files=[args.config], profil=args.profil)
else:
Config.instance(profil=args.profil)
config = Config.instance().get_section_config("Server")
defaults = {
@ -126,7 +129,7 @@ def parse_arguments(argv):
"quiet": config.getboolean("quiet", False),
"debug": config.getboolean("debug", False),
"logfile": config.getboolean("logfile", ""),
"server_discovery": config.getboolean("server_discovery", False),
"server_discovery": config.getboolean("server_discovery", False)
}
parser.set_defaults(**defaults)