async def manual_login(self): client = await Client.create(headless=False) page = await client.new_page() await client.stealth(page) await client.goto("/login", page) await page.waitForSelector(".profile", options={"timeout": 0}) await page.hover(".profile") await page.waitFor(".profile-actions > li:first-child") # going to "View profile" page await page.click(".profile-actions > li:first-child") await page.waitForSelector(".share-title", options={"timeout": 0}) username = await page.Jeval(".share-title", pageFunction="element => element.textContent") username = username.strip() sub_title = await page.Jeval( ".share-sub-title", pageFunction="element => element.textContent", ) logger.info(f"🔑 Logged as @{username} aka {sub_title}") cookies = await page.cookies() loaders.write( f"{settings.HOME_DIR}/settings.toml", {**BASE_SETTINGS, **{"COOKIES": json.dumps(cookies), "USERNAME": username}}, env="default", ) await client.browser.close()
def save_config(): """Persist configuration settings to disk.""" # ref: https://dynaconf.readthedocs.io/en/docs_223/guides/advanced_usage.html#exporting # ref: https://dynaconf.readthedocs.io/en/docs_223/reference/dynaconf.loaders.html#module-dynaconf.loaders.yaml_loader file_to_save = get_config_file() data = config.as_dict() loaders.write(file_to_save, DynaBox(data).to_dict())
def export_to_file(self, file_path): """ Export the settings to a file :param file_path: path and name of the export file. The extensions be any out of .yaml, .toml, .ini, .json, .py """ # generates a dict with all the keys for `development` env data = self.as_dict() loaders.write(file_path, DynaBox(data).to_dict(), env=self.current_env)
def load_or_create_settings(path: Optional[str]): path = path or DEFAULT_PATH print(Path(path).exists()) print(path) if not Path(path).exists(): default_settings_path = str(Path.cwd() / Path(DEFAULT_PATH)) logger.info( f'没有发现配置文件 "{Path(path).absolute()}". ' f"创建默认配置文件: {default_settings_path}", ) Path(path).parent.mkdir(parents=True, exist_ok=True) loaders.write(default_settings_path, BASE_SETTINGS, env="default") settings.load_file(path=path) logger.info("配置文件加载成功")
def load_or_create_settings(path: Optional[str]): path = path or DEFAULT_PATH if not Path(path).exists(): default_settings_path = str(Path.cwd() / Path(DEFAULT_PATH)) logger.info( f'🔧 Settings in path directory not found "{Path(path).absolute()}". ' f"I'll create default settings here: {default_settings_path}", ) Path(path).parent.mkdir(parents=True, exist_ok=True) loaders.write(default_settings_path, BASE_SETTINGS, env="default") settings.load_file(path=path) logger.info("🔧 Settings successfully loaded")
def to_toml(s: Settings) -> str: """convert dynaconf settings to a toml str""" # we'll use the current public dynaconf api so that we don't need to import our own # toml... that means we need to go via a temporary file to dump the current config, # because dynaconf doesn't support it otherwise... # github.com/rochacbruno/dynaconf/blob/68df27d2/dynaconf/loaders/toml_loader.py#L56 data = DynaBox(s.as_dict(internal=False)) # we create a temporary dir and write to a toml file # note: this is to workaround the fact that using NamedTemporaryFile will # error under windows due to loaders.write calling open on the file with tempfile.TemporaryDirectory() as tmpdir: fn = str(Path(tmpdir) / ".paquo.temporary.toml") # suffix determines loader loaders.write(fn, to_kwargs(data)) with open(fn) as f: output = f.read() return output
def _list(env, key, more, loader, _all=False, output=None, flat=False): """Lists all user defined config values and if `--all` is passed it also shows dynaconf internal variables. """ if env: env = env.strip() if key: key = key.strip() if loader: loader = loader.strip() if env: settings.setenv(env) cur_env = settings.current_env.lower() click.echo( click.style( "Working in %s environment " % cur_env, bold=True, bg="bright_blue", fg="bright_white", )) if not loader: data = settings.as_dict(env=env, internal=_all) else: identifier = "{}_{}".format(loader, cur_env) data = settings._loaded_by_loaders.get(identifier, {}) data = data or settings._loaded_by_loaders.get(loader, {}) # remove to avoid displaying twice data.pop("SETTINGS_MODULE", None) def color(_k): if _k in dir(default_settings): return "blue" return "green" def format_setting(_k, _v): return "{key}{data_type} {value}".format( key=click.style(_k, bg=color(_k), fg="white"), data_type=click.style("<{}>".format(type(_v).__name__), bg="bright_black", fg="white"), value=pprint.pformat(_v), ) if not key: datalines = "\n".join( format_setting(k, v) for k, v in data.items() if k not in data.get("RENAMED_VARS", [])) (click.echo_via_pager if more else click.echo)(datalines) if output: loaders.write(output, data, env=not flat and cur_env) else: key = upperfy(key) value = data.get(key) if not value: click.echo(click.style("Key not found", bg="red", fg="white")) return click.echo(format_setting(key, value)) if output: loaders.write(output, {key: value}, env=not flat and cur_env) if env: settings.setenv()