Skip to content

Commit

Permalink
Add parser of gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Nov 6, 2022
1 parent 1b8310c commit 47a6a51
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 8 deletions.
3 changes: 3 additions & 0 deletions norma2/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from norma2.config.config_class import Config
from norma2.config.from_cmdline import from_cmdline
from norma2.config.from_gitignore import from_gitignore
from norma2.config.from_json import from_json


Expand All @@ -13,4 +14,6 @@ def get_config(console: Console, argv: Optional[List[str]] = None) -> Config:
conf = conf + conf_cmdline
conf_json = from_json(console)
conf = conf + conf_json
conf_gitignore = from_gitignore(console)
conf = conf + conf_gitignore
return conf
16 changes: 15 additions & 1 deletion norma2/config/config_class.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import json
from argparse import Namespace
from typing import Any, Union
from contextlib import suppress
from typing import Any, Callable, Optional, Union

from rich.console import Console

GITIGNORE_MATCHES_TYPE = Optional[Callable[[str], bool]]


class __OutputFormat:
__choices = {
Expand Down Expand Up @@ -115,6 +118,8 @@ class __Defaults:
explain_error = ""
list_errors = False
install_completion = False
folder_exclude = [".git"]
file_ext_exclude = []
_options = [
"operators_plugin",
"preview",
Expand All @@ -132,10 +137,14 @@ class __Defaults:
"explain_error",
"list_errors",
"install_completion",
"folder_exclude",
"file_ext_exclude",
]


class Config(__Defaults):
gitignore_matches: GITIGNORE_MATCHES_TYPE = None

def __init__(self, console: Console) -> None:
super().__init__()
self.console: Console = console
Expand Down Expand Up @@ -163,6 +172,11 @@ def __add__(self, other):
self.__add_one(other, attr, conf)
except Exception:
self.console.print_exception()
with suppress(Exception):
if self.gitignore_matches is None:
conf.gitignore_matches = other.gitignore_matches
else:
conf.gitignore_matches = self.gitignore_matches
return conf

def __str__(self):
Expand Down
17 changes: 17 additions & 0 deletions norma2/config/from_gitignore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

from igittigitt import IgnoreParser
from rich.console import Console

from norma2.config.config_class import Config


def from_gitignore(console: Console, conf_path: str = ".") -> Config:
full_path = os.path.join(conf_path, ".gitignore")
full_path = os.path.abspath(full_path)
parser = IgnoreParser()
parser.parse_rule_file(full_path)
matches = parser.match
conf = Config(console)
conf.gitignore_matches = matches
return conf
7 changes: 6 additions & 1 deletion norma2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ def main(config: Config) -> int:
config.console.print(f"[blue]Check: {folder}", justify="center")
list_all_err: List[List[_TemplateNormError]] = []
try:
files_to_check = get_files.get_all_files(folder, [], [])
files_to_check = get_files.get_all_files(
folder,
config.folder_exclude,
config.file_ext_exclude,
config.gitignore_matches,
)
except Exception:
config.console.print(":warning: [red]An Error Occured")
config.console.print_exception()
Expand Down
24 changes: 19 additions & 5 deletions norma2/parser/get_files.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
import os
from typing import List

from norma2.config.config_class import GITIGNORE_MATCHES_TYPE

def _can_add_file(file: str, exts: List[str], folder_out: List[str]):

def _can_add_file(
file: str,
full_file: str,
exts: List[str],
folder_out: List[str],
gitignore_matches: GITIGNORE_MATCHES_TYPE,
):
for ext in exts:
if file.endswith(ext):
return False
splited = file.split(os.path.sep)
splited = full_file.split(os.path.sep)
for dirr in splited:
for fold in folder_out:
if dirr == fold:
return False
return True
if gitignore_matches is None:
return True
return not gitignore_matches(full_file)


def get_all_files(
folder_or_file_path: str,
folder_exclude: List[str],
file_ext_exclude: List[str],
gitignore_matches: GITIGNORE_MATCHES_TYPE,
) -> List[str]:
if os.path.isfile(folder_or_file_path):
return [folder_or_file_path]
if os.path.isdir(folder_or_file_path):
res = []
for root, _, files in os.walk(folder_or_file_path):
for file in files:
if not _can_add_file(file, file_ext_exclude, folder_exclude):
full_file = os.path.join(root, file)
if not _can_add_file(
file, full_file, file_ext_exclude, folder_exclude, gitignore_matches
):
continue
res.append(os.path.join(root, file))
res.append(full_file)
return res
raise os.error(f"Invalid path: {folder_or_file_path}")
80 changes: 79 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies = [
"rich>=12.6.0",
"rich-argparse>=0.3.1",
"shtab>=1.5.7",
"igittigitt>=2.1.2",
]
requires-python = ">=3.10"
readme = "README.md"
Expand Down

0 comments on commit 47a6a51

Please sign in to comment.