@@ -23,27 +23,27 @@ def is_valid(self, url: str, is_explicit: Optional[bool] = None) -> bool:
23
23
24
24
25
25
@dataclasses .dataclass (repr = False )
26
- class Matcher (SkipDefaultFieldsReprMixin ):
27
- """Structure for a matcher """
26
+ class Rule (SkipDefaultFieldsReprMixin ):
27
+ """A Rule represents an eligible pattern mapping to URL. """
28
28
29
29
label : str
30
30
"""Computer readable name / ID"""
31
31
description : str
32
32
"""Human readable description"""
33
33
pattern : Pattern [str ]
34
34
"""Regex pattern"""
35
- pattern_defaults : dict [str , str ] = dataclasses .field (default_factory = dict )
35
+ defaults : dict [str , str ] = dataclasses .field (default_factory = dict )
36
36
"""Is the match unambiguous with other VCS systems? e.g. git+ prefix"""
37
37
is_explicit : bool = False
38
38
39
39
40
40
@dataclasses .dataclass (repr = False )
41
- class MatcherRegistry (SkipDefaultFieldsReprMixin ):
41
+ class RuleMap (SkipDefaultFieldsReprMixin ):
42
42
"""Pattern matching and parsing capabilities for URL parsers, e.g. GitURL"""
43
43
44
- _matchers : dict [str , Matcher ] = dataclasses .field (default_factory = dict )
44
+ _rule_map : dict [str , Rule ] = dataclasses .field (default_factory = dict )
45
45
46
- def register (self , cls : Matcher ) -> None :
46
+ def register (self , cls : Rule ) -> None :
47
47
r"""
48
48
49
49
.. currentmodule:: libvcs.url.git
@@ -72,7 +72,7 @@ def register(self, cls: Matcher) -> None:
72
72
GitURL(url=github:org/repo,
73
73
hostname=github,
74
74
path=org/repo,
75
- matcher =core-git-scp)
75
+ rule =core-git-scp)
76
76
77
77
>>> GitURL(url="github:org/repo").to_url()
78
78
'git@github:org/repo'
@@ -84,11 +84,11 @@ def register(self, cls: Matcher) -> None:
84
84
85
85
**Extending matching capability:**
86
86
87
- >>> class GitHubPrefix(Matcher ):
87
+ >>> class GitHubPrefix(Rule ):
88
88
... label = 'gh-prefix'
89
89
... description ='Matches prefixes like github:org/repo'
90
90
... pattern = r'^github:(?P<path>.*)$'
91
- ... pattern_defaults = {
91
+ ... defaults = {
92
92
... 'hostname': 'github.com',
93
93
... 'scheme': 'https'
94
94
... }
@@ -97,8 +97,8 @@ def register(self, cls: Matcher) -> None:
97
97
98
98
>>> @dataclasses.dataclass(repr=False)
99
99
... class GitHubURL(GitURL):
100
- ... matchers: MatcherRegistry = MatcherRegistry (
101
- ... _matchers ={'github_prefix': GitHubPrefix}
100
+ ... rule_map: RuleMap = RuleMap (
101
+ ... _rule_map ={'github_prefix': GitHubPrefix}
102
102
... )
103
103
104
104
>>> GitHubURL.is_valid(url='github:vcs-python/libvcs')
@@ -107,48 +107,48 @@ def register(self, cls: Matcher) -> None:
107
107
>>> GitHubURL.is_valid(url='github:vcs-python/libvcs', is_explicit=True)
108
108
True
109
109
110
- Notice how ``pattern_defaults `` neatly fills the values for us.
110
+ Notice how ``defaults `` neatly fills the values for us.
111
111
112
112
>>> GitHubURL(url='github:vcs-python/libvcs')
113
113
GitHubURL(url=github:vcs-python/libvcs,
114
114
scheme=https,
115
115
hostname=github.com,
116
116
path=vcs-python/libvcs,
117
- matcher =gh-prefix)
117
+ rule =gh-prefix)
118
118
119
119
>>> GitHubURL(url='github:vcs-python/libvcs').to_url()
120
120
'https://github.com/vcs-python/libvcs'
121
121
122
122
>>> GitHubURL.is_valid(url='gitlab:vcs-python/libvcs')
123
123
False
124
124
125
- ``GitHubURL`` sees this as invalid since it only has one matcher ,
125
+ ``GitHubURL`` sees this as invalid since it only has one rule ,
126
126
``GitHubPrefix``.
127
127
128
128
>>> GitURL.is_valid(url='gitlab:vcs-python/libvcs')
129
129
True
130
130
131
131
Same story, getting caught in ``git(1)``'s own liberal scp-style URL:
132
132
133
- >>> GitURL(url='gitlab:vcs-python/libvcs').matcher
133
+ >>> GitURL(url='gitlab:vcs-python/libvcs').rule
134
134
'core-git-scp'
135
135
136
- >>> class GitLabPrefix(Matcher ):
136
+ >>> class GitLabPrefix(Rule ):
137
137
... label = 'gl-prefix'
138
138
... description ='Matches prefixes like gitlab:org/repo'
139
139
... pattern = r'^gitlab:(?P<path>)'
140
- ... pattern_defaults = {
140
+ ... defaults = {
141
141
... 'hostname': 'gitlab.com',
142
142
... 'scheme': 'https',
143
143
... 'suffix': '.git'
144
144
... }
145
145
146
- Option 1: Create a brand new matcher
146
+ Option 1: Create a brand new rule
147
147
148
148
>>> @dataclasses.dataclass(repr=False)
149
149
... class GitLabURL(GitURL):
150
- ... matchers: MatcherRegistry = MatcherRegistry (
151
- ... _matchers ={'gitlab_prefix': GitLabPrefix}
150
+ ... rule_map: RuleMap = RuleMap (
151
+ ... _rule_map ={'gitlab_prefix': GitLabPrefix}
152
152
... )
153
153
154
154
>>> GitLabURL.is_valid(url='gitlab:vcs-python/libvcs')
@@ -161,30 +161,30 @@ def register(self, cls: Matcher) -> None:
161
161
162
162
Are we home free, though? Remember our issue with vague matches.
163
163
164
- >>> GitURL(url='gitlab:vcs-python/libvcs').matcher
164
+ >>> GitURL(url='gitlab:vcs-python/libvcs').rule
165
165
'core-git-scp'
166
166
167
167
Register:
168
168
169
- >>> GitURL.matchers .register(GitLabPrefix)
169
+ >>> GitURL.rule_map .register(GitLabPrefix)
170
170
171
171
>>> GitURL.is_valid(url='gitlab:vcs-python/libvcs')
172
172
True
173
173
174
174
**Example: git URLs + pip-style git URLs:**
175
175
176
- This is already in :class:`GitURL` via :data:`PIP_DEFAULT_MATCHERS `. For the
176
+ This is already in :class:`GitURL` via :data:`PIP_DEFAULT_RULES `. For the
177
177
sake of showing how extensibility works, here is a recreation based on
178
178
:class:`GitBaseURL`:
179
179
180
180
>>> from libvcs.url.git import GitBaseURL
181
181
182
- >>> from libvcs.url.git import DEFAULT_MATCHERS, PIP_DEFAULT_MATCHERS
182
+ >>> from libvcs.url.git import DEFAULT_RULES, PIP_DEFAULT_RULES
183
183
184
184
>>> @dataclasses.dataclass(repr=False)
185
185
... class GitURLWithPip(GitBaseURL):
186
- ... matchers: MatcherRegistry = MatcherRegistry (
187
- ... _matchers ={m.label: m for m in [*DEFAULT_MATCHERS , *PIP_DEFAULT_MATCHERS ]}
186
+ ... rule_map: RuleMap = RuleMap (
187
+ ... _rule_map ={m.label: m for m in [*DEFAULT_RULES , *PIP_DEFAULT_RULES ]}
188
188
... )
189
189
190
190
>>> GitURLWithPip.is_valid(url="git+ssh://[email protected] /tony/AlgoXY.git")
@@ -197,19 +197,19 @@ def register(self, cls: Matcher) -> None:
197
197
hostname=github.com,
198
198
path=tony/AlgoXY,
199
199
suffix=.git,
200
- matcher =pip-url)
200
+ rule =pip-url)
201
201
""" # NOQA: E501
202
- if cls .label not in self ._matchers :
203
- self ._matchers [cls .label ] = cls
202
+ if cls .label not in self ._rule_map :
203
+ self ._rule_map [cls .label ] = cls
204
204
205
205
def unregister (self , label : str ) -> None :
206
- if label in self ._matchers :
207
- del self ._matchers [label ]
206
+ if label in self ._rule_map :
207
+ del self ._rule_map [label ]
208
208
209
209
def __iter__ (self ) -> Iterator [str ]:
210
- return self ._matchers .__iter__ ()
210
+ return self ._rule_map .__iter__ ()
211
211
212
212
def values (
213
213
self , # https://github.com/python/typing/discussions/1033
214
- ) -> "dict_values[str, Matcher ]" :
215
- return self ._matchers .values ()
214
+ ) -> "dict_values[str, Rule ]" :
215
+ return self ._rule_map .values ()
0 commit comments