def get_server_conf(url): is_cached = json_is_cached() try: if is_cached: fp = open(__JSON_CACHE__, 'r') else: try: url = parse_url(url) user, password = validate_credentials(url) fp = urllib2.urlopen(url, timeout=__URLOPEN_TIMEOUT__) except urllib2.URLError as e: if hasattr(e, 'code') and e.code == 401: user, password = validate_credentials(url) fp = urllib2.urlopen(url, timeout=__URLOPEN_TIMEOUT__) else: raise e fp_cached = open(__JSON_CACHE__, 'w') for line in fp: fp_cached.write(line) fp_cached.close() fp.close() fp = open(__JSON_CACHE__, 'r') content = fp.read() fp.close() conf = json.loads(content) server_conf = ServerConf() server_conf.load_data(conf) return server_conf except urllib2.URLError as e: raise ServerConfException(e) except ValueError as e: raise ServerConfException(_('Configuration file is not valid.'))
def get_server_conf(url): try: user = '' password = '' try: url = parse_url(url) fp = urllib2.urlopen(url, timeout=__URLOPEN_TIMEOUT__) except urllib2.URLError as e: if hasattr(e, 'code') and e.code == 401: user, password = auth_dialog() _install_opener(url, user, password) fp = urllib2.urlopen(url, timeout=__URLOPEN_TIMEOUT__) else: raise e content = fp.read() conf = json.loads(content) if 'version' in conf: version = conf['version'] if version != __CONFIG_FILE_VERSION__: raise Exception(_('Incorrect version of the configuration file.')) server_conf = ServerConf() server_conf.load_data(conf) return server_conf else: raise ValueError() except urllib2.URLError as e: raise ServerConfException(e) except ValueError as e: raise ServerConfException(_('Configuration file is not valid.'))