def set(self, section, option, value, verbose=False, save=True): """ Set an option. section=None: attribute a default section name """ section = self.__check_section_option(section, option) default_value = self.get_default(section, option) if default_value is NoDefault: # This let us save correctly string value options with # no config default that contain non-ascii chars in # Python 2 if PY2 and is_text_string(value): value = repr(value) default_value = value self.set_default(section, option, default_value) if isinstance(default_value, bool): value = bool(value) elif isinstance(default_value, float): value = float(value) elif isinstance(default_value, int): value = int(value) elif not is_text_string(default_value): value = repr(value) self._set(section, option, value, verbose) if save: self._save()
def __check_section_option(self, section, option): """Private method to check section and option types.""" if section is None: section = self.DEFAULT_SECTION_NAME elif not is_text_string(section): raise RuntimeError("Argument 'section' must be a string") if not is_text_string(option): raise RuntimeError("Argument 'option' must be a string") return section
def add_pathlist_to_PYTHONPATH( env, pathlist, drop_env=False, ipyconsole=False ): """Add pathlist to Python path.""" # PyQt API 1/2 compatibility-related tests: assert isinstance(env, list) assert all([is_text_string(path) for path in env]) pypath = "PYTHONPATH" pathstr = os.pathsep.join(pathlist) if os.environ.get(pypath) is not None and not drop_env: old_pypath = os.environ[pypath] if not ipyconsole: for index, var in enumerate(env[:]): if var.startswith(pypath + '='): env[index] = var.replace( pypath + '=', pypath + '=' + pathstr + os.pathsep ) env.append('OLD_PYTHONPATH=' + old_pypath) else: pypath = { 'PYTHONPATH': pathstr + os.pathsep + old_pypath, 'OLD_PYTHONPATH': old_pypath } return pypath else: if not ipyconsole: env.append(pypath + '=' + pathstr) else: return {'PYTHONPATH': pathstr}
def _get_api_info(self, url, proxy_servers=None, verify=True): """Callback.""" proxy_servers = proxy_servers or {} data = { "api_url": url, "api_docs_url": "https://api.anaconda.org/docs", "brand": DEFAULT_BRAND, "conda_url": "https://conda.anaconda.org", "main_url": "https://anaconda.org", "pypi_url": "https://pypi.anaconda.org", "swagger_url": "https://api.anaconda.org/swagger.json", } try: r = requests.get( url, proxies=proxy_servers, verify=verify, timeout=self.DEFAULT_TIMEOUT, ) content = to_text_string(r.content, encoding='utf-8') new_data = json.loads(content) # Enforce no trailing slash for key, value in new_data.items(): if is_text_string(value): data[key] = value[:-1] if value[-1] == '/' else value except Exception as error: logger.error(str(error)) return data
def _set(self, section, option, value, verbose): """Private set method.""" if not self.has_section(section): self.add_section(section) if not is_text_string(value): value = repr(value) if verbose: print('{section}[ {option} ] = {value}'.format( section, option, value)) cp.ConfigParser.set(self, section, option, value)
def _set(self, section, option, value, verbose): """ Private set method """ if not self.has_section(section): self.add_section(section) if not is_text_string(value): value = repr(value) if verbose: print('%s[ %s ] = %s' % (section, option, value)) cp.ConfigParser.set(self, section, option, value)
def _get_user_licenses(self, products=None): """Get user trial/paid licenses from anaconda.org.""" license_data = [] try: res = self._anaconda_client_api.user_licenses() license_data = res.get('data', []) # This should be returning a dict or list not a json string! if is_text_string(license_data): license_data = json.loads(license_data) except Exception: time.sleep(0.3) return license_data
def shell_split(text): """ Split the string `text` using shell-like syntax This avoids breaking single/double-quoted strings (e.g. containing strings with spaces). This function is almost equivalent to the shlex.split function (see standard library `shlex`) except that it is supporting unicode strings (shlex does not support unicode until Python 2.7.3). """ assert is_text_string(text) # in case a QString is passed... pattern = r'(\s+|(?<!\\)".*?(?<!\\)"|(?<!\\)\'.*?(?<!\\)\')' out = [] for token in re.split(pattern, text): if token.strip(): out.append(token.strip('"').strip("'")) return out
def get(self, section, option, default=NoDefault): """ Get an option. section=None: attribute a default section name default: default value (if not specified, an exception will be raised if option doesn't exist) """ section = self.__check_section_option(section, option) if not self.has_section(section): if default is NoDefault: raise cp.NoSectionError(section) else: self.add_section(section) if not self.has_option(section, option): if default is NoDefault: raise cp.NoOptionError(option, section) else: self.set(section, option, default) return default value = cp.ConfigParser.get(self, section, option, raw=self.raw) default_value = self.get_default(section, option) if isinstance(default_value, bool): value = ast.literal_eval(value) elif isinstance(default_value, float): value = float(value) elif isinstance(default_value, int): value = int(value) else: if PY2 and is_text_string(default_value): try: value = value.decode('utf-8') except (UnicodeEncodeError, UnicodeDecodeError): pass try: # lists, tuples, ... value = ast.literal_eval(value) except Exception: pass return value