def parse_from_pyproject_toml(command: str, file_path: Path) -> Optional[Configs]: """ Parse nbqa configuration from TOML file. Parameters ---------- command : str Third party tool to run file_path : Path Configuration file path Returns ------- Optional[Configs] Config object parsed from the configuration file(if present) """ config: Optional[Configs] = None toml_config: Mapping[str, Any] = toml.load(str(file_path)) nbqa_toml_config: Optional[Mapping[str, Mapping[str, Any]]] = toml_config.get( _ROOT_CONFIG_KEY, {} ).get(_NBQA_CONFIG_KEY, None) if nbqa_toml_config is not None: config = Configs() for section in CONFIG_SECTIONS: if section in nbqa_toml_config: config.set_config(section, nbqa_toml_config[section].get(command, None)) return config
def _get_configs(cli_args: CLIArgs, project_root: Path) -> Configs: """ Deal with extra configs for 3rd party tool. Parameters ---------- cli_args Commandline arguments passed to nbqa project_root Root of repository, where .git / .hg / .nbqa.ini file is. Returns ------- Configs Taken from CLI (if given), else from .nbqa.ini. """ cli_config: Configs = Configs.parse_from_cli_args(cli_args) file_config: Optional[Configs] = config_parser.parse_config_from_file( cli_args, project_root ) if file_config is not None: cli_config = cli_config.merge(file_config) return cli_config.merge(Configs.get_default_config(cli_args.command))
def _parse_nbqa_ini_config(command: str, file_path: Path) -> Optional[Configs]: """ Parse configuration from .nbqa.ini file. Parameters ---------- command : str Third party tool to run file_path : Path Configuration file path Returns ------- Optional[Configs] Config object parsed from the configuration file(if present) """ config_parser = ConfigParser() config_parser.read(file_path) config: Configs = Configs() section: str = command for option in config_parser.options(section): config.set_config(option, config_parser.get(section, option, fallback=None)) return config
def _parse_setupcfg_or_toxini_config( command: str, file_path: Path ) -> Optional[Configs]: """ Parse nbqa configuration from setup.cfg or tox.ini. Parameters ---------- command : str Third party tool to run file_path : Path Configuration file path Returns ------- Optional[Configs] Config object parsed from the configuration file(if present) """ config_parser = ConfigParser() config_parser.read(file_path) config: Optional[Configs] = None option: str = command # Check if this file contains the nbqa configuration for section in CONFIG_SECTIONS: section_name = f"{CONFIG_PREFIX}{section}" if config_parser.has_section(section_name): config = Configs() break if config is not None: for section in CONFIG_SECTIONS: section_name = f"{CONFIG_PREFIX}{section}" # We might not find the setting for a particular tool in any of the sections if config_parser.has_option(section_name, option): config.set_config( section, config_parser.get(section_name, option, fallback=None) ) return config