def fix_code(code, directory): """Formats Python code to conform to the PEP 8 style guide. """ if not black: raise Fault("black not installed", code=400) # Get black config from pyproject.toml line_length = black.DEFAULT_LINE_LENGTH string_normalization = True if find_pyproject_toml: pyproject_path = find_pyproject_toml((directory, )) else: pyproject_path = os.path.join(directory, "pyproject.toml") if toml and pyproject_path and os.path.exists(pyproject_path): pyproject_config = toml.load(pyproject_path) black_config = pyproject_config.get("tool", {}).get("black", {}) if "line-length" in black_config: line_length = black_config["line-length"] if "skip-string-normalization" in black_config: string_normalization = not black_config["skip-string-normalization"] try: if parse_version(black.__version__) < parse_version("19.0"): reformatted_source = black.format_file_contents( src_contents=code, line_length=line_length, fast=False) else: fm = black.FileMode(line_length=line_length, string_normalization=string_normalization) reformatted_source = black.format_file_contents(src_contents=code, fast=False, mode=fm) return reformatted_source except black.NothingChanged: return code except Exception as e: raise Fault("Error during formatting: {}".format(e), code=400)
def read_pyproject_toml( ctx: click.Context, param: click.Parameter, value: Optional[str] ) -> Optional[str]: """Inject Black configuration from "pyproject.toml" into defaults in `ctx`. Returns the path to a successfully found and read configuration file, None otherwise. """ if not value: value = find_pyproject_toml(ctx.params.get("src", ())) if value is None: return None try: config = parse_pyproject_toml(value) except (OSError, ValueError) as e: raise click.FileError( filename=value, hint=f"Error reading configuration file: {e}" ) if not config: return None else: # Sanitize the values to be Click friendly. For more information please see: # https://github.com/psf/black/issues/1458 # https://github.com/pallets/click/issues/1567 config = { k: str(v) if not isinstance(v, (list, dict)) else v for k, v in config.items() } target_version = config.get("target_version") if target_version is not None and not isinstance(target_version, list): raise click.BadOptionUsage( "target-version", "Config key target-version must be a list" ) default_map: Dict[str, Any] = {} if ctx.default_map: default_map.update(ctx.default_map) default_map.update(config) ctx.default_map = default_map return value