async def suggest_typeshed_obsolete(obsolete: Obsolete, session: aiohttp.ClientSession, action_level: ActionLevel) -> None: if action_level <= ActionLevel.nothing: return title = f"[stubsabot] Mark {obsolete.distribution} as obsolete since {obsolete.obsolete_since_version}" async with _repo_lock: branch_name = f"{BRANCH_PREFIX}/{normalize(obsolete.distribution)}" subprocess.check_call( ["git", "checkout", "-B", branch_name, "origin/master"]) with open(obsolete.stub_path / "METADATA.toml", "rb") as f: meta = tomlkit.load(f) obs_string = tomlkit.string(obsolete.obsolete_since_version) obs_string.comment( f"Released on {obsolete.obsolete_since_date.date().isoformat()}") meta["obsolete_since"] = obs_string with open(obsolete.stub_path / "METADATA.toml", "w") as f: tomlkit.dump(meta, f) subprocess.check_call(["git", "commit", "--all", "-m", title]) if action_level <= ActionLevel.local: return somewhat_safe_force_push(branch_name) if action_level <= ActionLevel.fork: return body = "\n".join(f"{k}: {v}" for k, v in obsolete.links.items()) await create_or_update_pull_request(title=title, body=body, branch_name=branch_name, session=session)
def test_load_from_file_object(example_name): with open( os.path.join(os.path.dirname(__file__), "examples", example_name + ".toml"), encoding="utf-8", ) as fp: assert isinstance(load(fp), TOMLDocument)
async def suggest_typeshed_update(update: Update, session: aiohttp.ClientSession, action_level: ActionLevel) -> None: if action_level <= ActionLevel.nothing: return title = f"[stubsabot] Bump {update.distribution} to {update.new_version_spec}" async with _repo_lock: branch_name = f"{BRANCH_PREFIX}/{normalize(update.distribution)}" subprocess.check_call( ["git", "checkout", "-B", branch_name, "origin/master"]) with open(update.stub_path / "METADATA.toml", "rb") as f: meta = tomlkit.load(f) meta["version"] = update.new_version_spec with open(update.stub_path / "METADATA.toml", "w") as f: tomlkit.dump(meta, f) subprocess.check_call(["git", "commit", "--all", "-m", title]) if action_level <= ActionLevel.local: return somewhat_safe_force_push(branch_name) if action_level <= ActionLevel.fork: return body = "\n".join(f"{k}: {v}" for k, v in update.links.items()) body += """ If stubtest fails for this PR: - Leave this PR open (as a reminder, and to prevent stubsabot from opening another PR) - Fix stubtest failures in another PR, then close this PR """ await create_or_update_pull_request(title=title, body=body, branch_name=branch_name, session=session)
def _read_pyproject(self): pyproject = self.path.parent.joinpath("pyproject.toml") if pyproject.exists(): self._pyproject = tomlkit.load(pyproject) build_system = self._pyproject.get("build-system", None) if not os.path.exists(self.path_to("setup.py")): if not build_system or not build_system.get("requires"): build_system = { "requires": ["setuptools>=38.2.5", "wheel"], "build-backend": "setuptools.build_meta", } self._build_system = build_system
def load(file_path: str | Path) -> tomlkit.TOMLDocument: """Loads TOML settings from a given path, or generates a new file if it doesn't exist""" settings_path = Path(file_path) if settings_path.is_file(): _logger.info(f'Loaded configuration from {settings_path.name}') with settings_path.open('r') as f: document = tomlkit.load(f) else: _logger.info(f'Configuration file {settings_path.name} missing, generating from template') document = generate(TEMPLATE) save(document, settings_path) return validate(document, TEMPLATE)
def load_params(ctx, param, filename): if not filename or ctx.resilient_parsing: return ctx.default_map = {} if Path(filename).is_file(): with Path(filename).open("r") as f: params = tomlkit.load(f) if params: ctx.default_map = params.get(ctx.info_name, {}) else: console.print("No config file found. ignoring..") ctx.obj = {"config_file": filename}
def save_params(ctx): cmd = ctx.info_name params = {k: v for k, v in ctx.params.items() if v} save_type = params.pop("save_config") try: config_file = Path(ctx.obj["config_file"]) except TypeError: raise click.BadParameter( "use `--save-config` with a specified `--config`") if config_file.is_file(): console.print(f"Updating current config file at [b cyan]{config_file}") with config_file.open("r") as f: config = tomlkit.load(f) else: console.print(f"Staring a config file at [b cyan]{config_file}") config = tomlkit.document() if save_type == "explicit": params = { k: v for k, v in params.items() if ctx.get_parameter_source(k).value != 3 } # sanitize the path's for writing to toml for k in ["input", "pipeline", "output"]: if k in params.keys(): params[k] = str(params[k]) config[cmd] = params null_hints = { "extract": ["filter_count", "fastp_args"], "merge": ["fastp_args"] } if cmd in null_hints.keys() and save_type == "full": for param in null_hints[cmd]: if param not in params.keys(): config[cmd].add(tomlkit.comment(f"{param} =")) with config_file.open("w") as f: f.write(tomlkit.dumps(config)) console.print("Exiting...") ctx.exit()
def test_invalid_encode(invalid_encode_case): with pytest.raises((TOMLKitError, UnicodeDecodeError)): with open(invalid_encode_case, encoding="utf-8") as f: load(f)