MCPcopy Index your code
hub / github.com/pre-commit/pre-commit / Classifier

Class Classifier

pre_commit/commands/run.py:73–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

71
72
73class Classifier:
74 def __init__(self, filenames: Iterable[str]) -> None:
75 self.filenames = [f for f in filenames if os.path.lexists(f)]
76
77 @functools.cache
78 def _types_for_file(self, filename: str) -> set[str]:
79 return tags_from_path(filename)
80
81 def by_types(
82 self,
83 names: Iterable[str],
84 types: Iterable[str],
85 types_or: Iterable[str],
86 exclude_types: Iterable[str],
87 ) -> Generator[str]:
88 types = frozenset(types)
89 types_or = frozenset(types_or)
90 exclude_types = frozenset(exclude_types)
91 for filename in names:
92 tags = self._types_for_file(filename)
93 if (
94 tags >= types and
95 (not types_or or tags & types_or) and
96 not tags & exclude_types
97 ):
98 yield filename
99
100 def filenames_for_hook(self, hook: Hook) -> Generator[str]:
101 return self.by_types(
102 filter_by_include_exclude(
103 self.filenames,
104 hook.files,
105 hook.exclude,
106 ),
107 hook.types,
108 hook.types_or,
109 hook.exclude_types,
110 )
111
112 @classmethod
113 def from_config(
114 cls,
115 filenames: Iterable[str],
116 include: str,
117 exclude: str,
118 ) -> Classifier:
119 # on windows we normalize all filenames to use forward slashes
120 # this makes it easier to filter using the `files:` regex
121 # this also makes improperly quoted shell-based hooks work better
122 # see #1173
123 if os.altsep == '/' and os.sep == '\\':
124 filenames = (f.replace(os.sep, os.altsep) for f in filenames)
125 filenames = filter_by_include_exclude(filenames, include, exclude)
126 return Classifier(filenames)
127
128
129def _get_skips(environ: MutableMapping[str, str]) -> set[str]:

Callers 3

from_configMethod · 0.85

Calls

no outgoing calls

Tested by 2