Example #1
0
    def find_root_dir() -> Path:
        """Find the root dir of the Python project (the one that has one of the ``ROOT_FILES``).

        Start from the current working dir.
        """
        # pylint: disable=import-outside-toplevel
        from nitpick.files.pyproject_toml import PyProjectTomlFile
        from nitpick.files.setup_cfg import SetupCfgFile

        root_dirs = set()  # type: Set[Path]
        seen = set()  # type: Set[Path]

        all_files = list(Path.cwd().glob("*"))
        # Don't fail if the current dir is empty
        starting_file = str(all_files[0]) if all_files else ""
        starting_dir = Path(starting_file).parent.absolute()
        while True:
            project_files = climb_directory_tree(
                starting_dir, ROOT_FILES +
                (PyProjectTomlFile.file_name, SetupCfgFile.file_name))
            if project_files and project_files & seen:
                break
            seen.update(project_files or [])

            if not project_files:
                # If none of the root files were found, try again with manage.py.
                # On Django projects, it can be in another dir inside the root dir.
                project_files = climb_directory_tree(starting_file,
                                                     [MANAGE_PY])
                if not project_files or project_files & seen:
                    break
                seen.update(project_files)

            for found in project_files or []:
                root_dirs.add(found.parent)

            # Climb up one directory to search for more project files
            starting_dir = starting_dir.parent
            if not starting_dir:
                break

        if not root_dirs:
            LOGGER.error(
                "No files found while climbing directory tree from %s",
                str(starting_file))
            raise NoRootDir()

        # If multiple roots are found, get the top one (grandparent dir)
        return sorted(root_dirs)[0]
Example #2
0
    def find_root_dir(self, starting_file: PathOrStr) -> bool:
        """Find the root dir of the Python project: the dir that has one of the `ROOT_FILES`.

        Also clear the cache dir the first time the root dir is found.
        """
        if hasattr(self, "root_dir"):
            return True

        found_files = climb_directory_tree(
            starting_file,
            ROOT_FILES + (PyProjectTomlFile.file_name, SetupCfgFile.file_name))
        if not found_files:
            # If none of the root files were found, try again with manage.py.
            # On Django projects, it can be in another dir inside the root dir.
            found_files = climb_directory_tree(starting_file, [MANAGE_PY])
            if not found_files:
                LOGGER.error(
                    "No files found while climbing directory tree from %s",
                    str(starting_file))
                return False

        self.root_dir = found_files[0].parent  # pylint: disable=attribute-defined-outside-init
        self.clear_cache_dir()
        return True
Example #3
0
    def find_initial_styles(self, configured_styles: StrOrList):
        """Find the initial style(s) and include them."""
        if configured_styles:
            chosen_styles = configured_styles
            log_message = "Styles configured in {}: %s".format(PyProjectTomlPlugin.file_name)
        else:
            paths = climb_directory_tree(NitpickApp.current().root_dir, [NITPICK_STYLE_TOML])
            if paths:
                chosen_styles = str(sorted(paths)[0])
                log_message = "Found style climbing the directory tree: %s"
            else:
                chosen_styles = self.get_default_style_url()
                log_message = "Loading default Nitpick style %s"
        LOGGER.info(log_message, chosen_styles)

        self.include_multiple_styles(chosen_styles)
Example #4
0
    def find_initial_styles(self, configured_styles: StrOrList):
        """Find the initial style(s) and include them."""
        if configured_styles:
            chosen_styles = configured_styles
            log_message = "Styles configured in {}: %s".format(
                PyProjectTomlFile.file_name)
        else:
            paths = climb_directory_tree(self.config.root_dir,
                                         [NITPICK_STYLE_TOML])
            if paths:
                chosen_styles = str(paths[0])
                log_message = "Found style climbing the directory tree: %s"
            else:
                chosen_styles = DEFAULT_NITPICK_STYLE_URL
                log_message = "Loading default Nitpick style %s"
        LOGGER.info(log_message, chosen_styles)

        self.include_multiple_styles(chosen_styles)