def test_env_map(): with setenv( {"FOOVAR1": "http://foo.com",} ): assert env_map({"http": "FOOVAR1", "https": "FOOVAR2"}) == { "http": "http://foo.com" }
def http_headers(self) -> dict: http_headers = {} if self._http_headers: http_headers.update(env_map(self._http_headers)) if self.user_config.default_http_headers: for value_dict in self.user_config.default_http_headers: name = value_dict["name"] pattern = value_dict.get("pattern") if name not in http_headers and (pattern is None or pattern.match(self.url)): value = resolve_value_descriptor(value_dict) if value: http_headers[name] = value return http_headers
def __init__( self, config_file: Optional[Path] = None, cache_dir: Optional[Path] = None, remove_cache_dir: Optional[bool] = None, execution_dir: Optional[Path] = None, proxies: Optional[Dict[str, Union[str, Dict[str, str]]]] = None, http_headers: Optional[List[dict]] = None, show_progress: Optional[bool] = None, executor_defaults: Optional[Dict[str, dict]] = None, ): if config_file: with open(config_file, "rt") as inp: defaults = json.load(inp) else: defaults = {} if not cache_dir: cache_dir_str = os.environ.get(ENV_CACHE_DIR, defaults.get(KEY_CACHE_DIR)) if cache_dir_str: cache_dir = ensure_path(cache_dir_str) if cache_dir: self.cache_dir = ensure_path(cache_dir, is_file=False, create=True) if remove_cache_dir is None: remove_cache_dir = False else: self.cache_dir = Path(tempfile.mkdtemp()) if remove_cache_dir is None: remove_cache_dir = True self.remove_cache_dir = remove_cache_dir if not execution_dir: execution_dir_str = os.environ.get(ENV_EXECUTION_DIR, defaults.get(KEY_EXECUTION_DIR)) if execution_dir_str: execution_dir = ensure_path(execution_dir_str) if execution_dir: self.default_execution_dir = ensure_path(execution_dir, is_file=False, create=True) else: self.default_execution_dir = None if not proxies and KEY_PROXIES in defaults: proxies = env_map(defaults[KEY_PROXIES]) self.proxies = proxies or {} if not http_headers and KEY_HTTP_HEADERS in defaults: http_headers = defaults[KEY_HTTP_HEADERS] for d in http_headers: if "pattern" in d: d["pattern"] = re.compile(d.pop("pattern")) self.default_http_headers = http_headers or [] self.show_progress = show_progress if self.show_progress is None: self.show_progress = defaults.get(KEY_SHOW_PROGRESS) self.executor_defaults = executor_defaults or {} if "executors" in defaults: for name, d in defaults["executors"].items(): if name not in self.executor_defaults: self.executor_defaults[name] = d