From 2773a3d35a2c87100da520b7e947d1d77f52e134 Mon Sep 17 00:00:00 2001 From: Isis Lovecruft Date: Sat, 16 Feb 2013 23:18:57 +0000 Subject: Moved client platform detection code to leap/mx/util/config.py. * Remove extraneous storage.Storage() initializations. * Autosetup calls to config._get_config_location based on whether config.filename has been set elsewhere. --- src/leap/mx/util/config.py | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/leap/mx/util/config.py b/src/leap/mx/util/config.py index 3eb4cdb..9bf90e8 100644 --- a/src/leap/mx/util/config.py +++ b/src/leap/mx/util/config.py @@ -42,11 +42,36 @@ filename = None basic = storage.Storage() advanced = storage.Storage() -def _create_config_file(file): +PLATFORMS = {'LINUX': sys.platform.startswith("linux"), + 'OPENBSD': sys.platform.startswith("openbsd"), + 'FREEBSD': sys.platform.startswith("freebsd"), + 'NETBSD': sys.platform.startswith("netbsd"), + 'DARWIN': sys.platform.startswith("darwin"), + 'SOLARIS': sys.platform.startswith("sunos"), + 'WINDOWS': sys.platform.startswith("win32")} + +def getClientPlatform(platform_name=None): + """ + Determine the client's operating system platform. Optionally, if + :param:`platform_name` is given, check that this is indeed the platform + we're operating on. + + @param platform_name: A string, upper-, lower-, or mixed case, of one + of the keys in the :attr:`leap.util.version.PLATFORMS` + dictionary. E.g. 'Linux' or 'OPENBSD', etc. + @returns: A string specifying the platform name, and the boolean test + used to determine it. + """ + for name, test in PLATFORMS.items(): + if not platform_name or platform_name.upper() == name: + if test: + return name, test + +def _create_config_file(conffile): """ xxx fill me in """ - with open(file, 'w+') as conf: + with open(conffile, 'w+') as conf: conf.write(""" # # mx.conf @@ -72,12 +97,6 @@ advanced: """) conf.flush() - try: - assert os.path.isfile(file), "Config file %s not created!" % file - except AssertionError, ae: - raise SystemExit(ae.message) - else: - return file def _get_config_filename(filename=None, use_dot_config_directory=False): """ @@ -147,14 +166,12 @@ def loadConfig(filename=config_filename): ## from leap.util import config ## config.basic.foo = bar ## - basic = Storage() try: for k, v in configuration['basic'].items(): basic[k] = v except AttributeError: pass - advanced = Storage() try: for k, v in configuration['advanced'].items(): advanced[k] = v @@ -168,8 +185,7 @@ def loadConfig(filename=config_filename): ## This is the name of the config file to use: ## If not set, it defaults to 'leap_mx/leap_mx.conf' -if not config_filename: - config_filename = _get_config_filename() +if not filename: + filename = _get_config_location() else: - config_filename = _get_config_filename(filename=config_filename) - + filename = _get_config_location(config_filename=filename) -- cgit v1.2.3