From 97ea8ee2fa43d345cf3f1013c87569155680625b Mon Sep 17 00:00:00 2001 From: antialias Date: Wed, 22 Aug 2012 14:01:22 -0700 Subject: moved help functions from eip/config.py to base/configuration.py. (cherry picked from get-definition.json branch) solve merge conflict since antialias was working in a version in which baseconfig was still at `configuration` file. Conflicts: src/leap/base/configuration.py --- src/leap/base/config.py | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ src/leap/eip/config.py | 89 +++------------------------------------ 2 files changed, 114 insertions(+), 84 deletions(-) (limited to 'src/leap') diff --git a/src/leap/base/config.py b/src/leap/base/config.py index ccbf3c89..63e643a8 100644 --- a/src/leap/base/config.py +++ b/src/leap/base/config.py @@ -2,9 +2,18 @@ Configuration Base Class """ import configuration # python configuration module, not local! +import grp +import json +import logging +import requests +import socket import os +logger = logging.getLogger(name=__name__) +logger.setLevel('DEBUG') + from leap.eip import config as eip_config +from leap.util.fileutil import (mkdir_p) class BaseLeapConfig(object): @@ -79,3 +88,103 @@ class JSONLeapConfig(BaseLeapConfig): # XXX fix import config_file = eip_config.get_config_file(filename, folder) return config_file + +# +# utility functions +# +# (might be moved to some class as we see fit, but +# let's remain functional for a while) +# + + +def get_config_dir(): + """ + get the base dir for all leap config + @rparam: config path + @rtype: string + """ + # TODO + # check for $XDG_CONFIG_HOME var? + # get a more sensible path for win/mac + # kclair: opinion? ^^ + return os.path.expanduser( + os.path.join('~', + '.config', + 'leap')) + + +def get_config_file(filename, folder=None): + """ + concatenates the given filename + with leap config dir. + @param filename: name of the file + @type filename: string + @rparam: full path to config file + """ + path = [] + path.append(get_config_dir()) + if folder is not None: + path.append(folder) + path.append(filename) + return os.path.join(*path) + + +def get_default_provider_path(): + default_subpath = os.path.join("providers", + "default") + default_provider_path = get_config_file( + '', + folder=default_subpath) + return default_provider_path + + +def validate_ip(ip_str): + """ + raises exception if the ip_str is + not a valid representation of an ip + """ + socket.inet_aton(ip_str) + + +def get_username(): + return os.getlogin() + + +def get_groupname(): + gid = os.getgroups()[-1] + return grp.getgrgid(gid).gr_name + + +# json stuff + +# XXX merge with JSONConfig +def get_config_json(config_file=None): + """ + will replace get_config function be developing them + in parralel for branch purposes. + @param: configuration file + @type: file + @rparam: configuration turples + @rtype: dictionary + """ + if not config_file: + fpath = get_config_file('eip.json') + if not os.path.isfile(fpath): + dpath, cfile = os.path.split(fpath) + if not os.path.isdir(dpath): + mkdir_p(dpath) + with open(fpath, 'wb') as configfile: + configfile.flush() + config_file = open(fpath) + + config = json.load(config_file) + + return config + + +def get_definition_file(url=None): + """ + """ + #TODO: determine good default location of definition file. + r = requests.get(url) + return r.json diff --git a/src/leap/eip/config.py b/src/leap/eip/config.py index f38268e2..8d5c19da 100644 --- a/src/leap/eip/config.py +++ b/src/leap/eip/config.py @@ -1,13 +1,16 @@ import ConfigParser -import grp import logging import os -import json import platform import socket from leap.util.fileutil import (which, mkdir_p, check_and_fix_urw_only) +from leap.base.config import (get_default_provider_path, + get_config_file, + get_username, + get_groupname, + validate_ip) from leap.baseapp.permcheck import (is_pkexec_in_system, is_auth_agent_running) from leap.eip import exceptions as eip_exceptions @@ -32,55 +35,6 @@ ca {LEAP_EIP_KEYS} """ -def get_config_dir(): - """ - get the base dir for all leap config - @rparam: config path - @rtype: string - """ - # TODO - # check for $XDG_CONFIG_HOME var? - # get a more sensible path for win/mac - # kclair: opinion? ^^ - return os.path.expanduser( - os.path.join('~', - '.config', - 'leap')) - - -def get_config_file(filename, folder=None): - """ - concatenates the given filename - with leap config dir. - @param filename: name of the file - @type filename: string - @rparam: full path to config file - """ - path = [] - path.append(get_config_dir()) - if folder is not None: - path.append(folder) - path.append(filename) - return os.path.join(*path) - - -def get_default_provider_path(): - default_subpath = os.path.join("providers", - "default") - default_provider_path = get_config_file( - '', - folder=default_subpath) - return default_provider_path - - -def validate_ip(ip_str): - """ - raises exception if the ip_str is - not a valid representation of an ip - """ - socket.inet_aton(ip_str) - - def check_or_create_default_vpnconf(config): """ checks that a vpn config file @@ -158,15 +112,6 @@ def check_or_create_default_vpnconf(config): f.write(ovpn_config) -def get_username(): - return os.getlogin() - - -def get_groupname(): - gid = os.getgroups()[-1] - return grp.getgrgid(gid).gr_name - - def build_ovpn_options(daemon=False): """ build a list of options @@ -412,27 +357,3 @@ def check_vpn_keys(config): check_and_fix_urw_only(keyfile) except OSError: raise eip_exceptions.EIPInitBadKeyFilePermError - - -def get_config_json(config_file=None): - """ - will replace get_config function be developing them - in parralel for branch purposes. - @param: configuration file - @type: file - @rparam: configuration turples - @rtype: dictionary - """ - if not config_file: - fpath = get_config_file('eip.json') - if not os.path.isfile(fpath): - dpath, cfile = os.path.split(fpath) - if not os.path.isdir(dpath): - mkdir_p(dpath) - with open(fpath, 'wb') as configfile: - configfile.flush() - config_file = open(fpath) - - config = json.load(config_file) - - return config -- cgit v1.2.3