Skip to content

Commit 853ccb7

Browse files
authored
refactor!(url): Simplify naming conventions (#417)
URLs: Rename match to rule (#417) - Matcher -> Rule, MatcherRegistry -> Rules - matches -> rule_map - default_patterns -> patterns - MATCHERS -> RULES
2 parents 5834604 + a038571 commit 853ccb7

File tree

9 files changed

+147
-146
lines changed

9 files changed

+147
-146
lines changed

CHANGES

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ $ pip install --user --upgrade --pre libvcs
1313

1414
- _Add your latest changes from PRs here_
1515

16+
### Breaking changes
17+
18+
URL renamings (#417):
19+
20+
- `Matcher` -> `Rule`, `MatcherRegistry` -> `Rules`
21+
- `matches` -> `rule_map`
22+
- `default_patterns` -> `patterns`
23+
- `MATCHERS` -> `RULES`
24+
1625
## libvcs 0.16.5 (2022-09-21)
1726

1827
### Bug fixes

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ GitURL([email protected]:vcs-python/libvcs.git,
4646
hostname=github.com,
4747
path=vcs-python/libvcs,
4848
suffix=.git,
49-
matcher=core-git-scp)
49+
rule=core-git-scp)
5050
```
5151

5252
Switch repo libvcs -> vcspull:

src/libvcs/url/base.py

+34-34
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@ def is_valid(self, url: str, is_explicit: Optional[bool] = None) -> bool:
2323

2424

2525
@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."""
2828

2929
label: str
3030
"""Computer readable name / ID"""
3131
description: str
3232
"""Human readable description"""
3333
pattern: Pattern[str]
3434
"""Regex pattern"""
35-
pattern_defaults: dict[str, str] = dataclasses.field(default_factory=dict)
35+
defaults: dict[str, str] = dataclasses.field(default_factory=dict)
3636
"""Is the match unambiguous with other VCS systems? e.g. git+ prefix"""
3737
is_explicit: bool = False
3838

3939

4040
@dataclasses.dataclass(repr=False)
41-
class MatcherRegistry(SkipDefaultFieldsReprMixin):
41+
class RuleMap(SkipDefaultFieldsReprMixin):
4242
"""Pattern matching and parsing capabilities for URL parsers, e.g. GitURL"""
4343

44-
_matchers: dict[str, Matcher] = dataclasses.field(default_factory=dict)
44+
_rule_map: dict[str, Rule] = dataclasses.field(default_factory=dict)
4545

46-
def register(self, cls: Matcher) -> None:
46+
def register(self, cls: Rule) -> None:
4747
r"""
4848
4949
.. currentmodule:: libvcs.url.git
@@ -72,7 +72,7 @@ def register(self, cls: Matcher) -> None:
7272
GitURL(url=github:org/repo,
7373
hostname=github,
7474
path=org/repo,
75-
matcher=core-git-scp)
75+
rule=core-git-scp)
7676
7777
>>> GitURL(url="github:org/repo").to_url()
7878
'git@github:org/repo'
@@ -84,11 +84,11 @@ def register(self, cls: Matcher) -> None:
8484
8585
**Extending matching capability:**
8686
87-
>>> class GitHubPrefix(Matcher):
87+
>>> class GitHubPrefix(Rule):
8888
... label = 'gh-prefix'
8989
... description ='Matches prefixes like github:org/repo'
9090
... pattern = r'^github:(?P<path>.*)$'
91-
... pattern_defaults = {
91+
... defaults = {
9292
... 'hostname': 'github.com',
9393
... 'scheme': 'https'
9494
... }
@@ -97,8 +97,8 @@ def register(self, cls: Matcher) -> None:
9797
9898
>>> @dataclasses.dataclass(repr=False)
9999
... class GitHubURL(GitURL):
100-
... matchers: MatcherRegistry = MatcherRegistry(
101-
... _matchers={'github_prefix': GitHubPrefix}
100+
... rule_map: RuleMap = RuleMap(
101+
... _rule_map={'github_prefix': GitHubPrefix}
102102
... )
103103
104104
>>> GitHubURL.is_valid(url='github:vcs-python/libvcs')
@@ -107,48 +107,48 @@ def register(self, cls: Matcher) -> None:
107107
>>> GitHubURL.is_valid(url='github:vcs-python/libvcs', is_explicit=True)
108108
True
109109
110-
Notice how ``pattern_defaults`` neatly fills the values for us.
110+
Notice how ``defaults`` neatly fills the values for us.
111111
112112
>>> GitHubURL(url='github:vcs-python/libvcs')
113113
GitHubURL(url=github:vcs-python/libvcs,
114114
scheme=https,
115115
hostname=github.com,
116116
path=vcs-python/libvcs,
117-
matcher=gh-prefix)
117+
rule=gh-prefix)
118118
119119
>>> GitHubURL(url='github:vcs-python/libvcs').to_url()
120120
'https://github.com/vcs-python/libvcs'
121121
122122
>>> GitHubURL.is_valid(url='gitlab:vcs-python/libvcs')
123123
False
124124
125-
``GitHubURL`` sees this as invalid since it only has one matcher,
125+
``GitHubURL`` sees this as invalid since it only has one rule,
126126
``GitHubPrefix``.
127127
128128
>>> GitURL.is_valid(url='gitlab:vcs-python/libvcs')
129129
True
130130
131131
Same story, getting caught in ``git(1)``'s own liberal scp-style URL:
132132
133-
>>> GitURL(url='gitlab:vcs-python/libvcs').matcher
133+
>>> GitURL(url='gitlab:vcs-python/libvcs').rule
134134
'core-git-scp'
135135
136-
>>> class GitLabPrefix(Matcher):
136+
>>> class GitLabPrefix(Rule):
137137
... label = 'gl-prefix'
138138
... description ='Matches prefixes like gitlab:org/repo'
139139
... pattern = r'^gitlab:(?P<path>)'
140-
... pattern_defaults = {
140+
... defaults = {
141141
... 'hostname': 'gitlab.com',
142142
... 'scheme': 'https',
143143
... 'suffix': '.git'
144144
... }
145145
146-
Option 1: Create a brand new matcher
146+
Option 1: Create a brand new rule
147147
148148
>>> @dataclasses.dataclass(repr=False)
149149
... class GitLabURL(GitURL):
150-
... matchers: MatcherRegistry = MatcherRegistry(
151-
... _matchers={'gitlab_prefix': GitLabPrefix}
150+
... rule_map: RuleMap = RuleMap(
151+
... _rule_map={'gitlab_prefix': GitLabPrefix}
152152
... )
153153
154154
>>> GitLabURL.is_valid(url='gitlab:vcs-python/libvcs')
@@ -161,30 +161,30 @@ def register(self, cls: Matcher) -> None:
161161
162162
Are we home free, though? Remember our issue with vague matches.
163163
164-
>>> GitURL(url='gitlab:vcs-python/libvcs').matcher
164+
>>> GitURL(url='gitlab:vcs-python/libvcs').rule
165165
'core-git-scp'
166166
167167
Register:
168168
169-
>>> GitURL.matchers.register(GitLabPrefix)
169+
>>> GitURL.rule_map.register(GitLabPrefix)
170170
171171
>>> GitURL.is_valid(url='gitlab:vcs-python/libvcs')
172172
True
173173
174174
**Example: git URLs + pip-style git URLs:**
175175
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
177177
sake of showing how extensibility works, here is a recreation based on
178178
:class:`GitBaseURL`:
179179
180180
>>> from libvcs.url.git import GitBaseURL
181181
182-
>>> from libvcs.url.git import DEFAULT_MATCHERS, PIP_DEFAULT_MATCHERS
182+
>>> from libvcs.url.git import DEFAULT_RULES, PIP_DEFAULT_RULES
183183
184184
>>> @dataclasses.dataclass(repr=False)
185185
... 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]}
188188
... )
189189
190190
>>> GitURLWithPip.is_valid(url="git+ssh://[email protected]/tony/AlgoXY.git")
@@ -197,19 +197,19 @@ def register(self, cls: Matcher) -> None:
197197
hostname=github.com,
198198
path=tony/AlgoXY,
199199
suffix=.git,
200-
matcher=pip-url)
200+
rule=pip-url)
201201
""" # 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
204204

205205
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]
208208

209209
def __iter__(self) -> Iterator[str]:
210-
return self._matchers.__iter__()
210+
return self._rule_map.__iter__()
211211

212212
def values(
213213
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

Comments
 (0)