Skip to content

Commit b9bd0f5

Browse files
committed
refactor!(url): rules -> rule_map
1 parent ce1b04e commit b9bd0f5

File tree

7 files changed

+50
-50
lines changed

7 files changed

+50
-50
lines changed

src/libvcs/url/base.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class Rule(SkipDefaultFieldsReprMixin):
3838

3939

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

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

4646
def register(self, cls: Rule) -> None:
4747
r"""
@@ -97,8 +97,8 @@ def register(self, cls: Rule) -> None:
9797
9898
>>> @dataclasses.dataclass(repr=False)
9999
... class GitHubURL(GitURL):
100-
... rules: Rules = Rules(
101-
... _rules={'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')
@@ -147,8 +147,8 @@ def register(self, cls: Rule) -> None:
147147
148148
>>> @dataclasses.dataclass(repr=False)
149149
... class GitLabURL(GitURL):
150-
... rules: Rules = Rules(
151-
... _rules={'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')
@@ -166,7 +166,7 @@ def register(self, cls: Rule) -> None:
166166
167167
Register:
168168
169-
>>> GitURL.rules.register(GitLabPrefix)
169+
>>> GitURL.rule_map.register(GitLabPrefix)
170170
171171
>>> GitURL.is_valid(url='gitlab:vcs-python/libvcs')
172172
True
@@ -183,8 +183,8 @@ def register(self, cls: Rule) -> None:
183183
184184
>>> @dataclasses.dataclass(repr=False)
185185
... class GitURLWithPip(GitBaseURL):
186-
... rules: Rules = Rules(
187-
... _rules={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_MATCHERS, *PIP_DEFAULT_MATCHERS]}
188188
... )
189189
190190
>>> GitURLWithPip.is_valid(url="git+ssh://git@github.com/tony/AlgoXY.git")
@@ -199,17 +199,17 @@ def register(self, cls: Rule) -> None:
199199
suffix=.git,
200200
rule=pip-url)
201201
""" # NOQA: E501
202-
if cls.label not in self._rules:
203-
self._rules[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._rules:
207-
del self._rules[label]
206+
if label in self._rule_map:
207+
del self._rule_map[label]
208208

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

212212
def values(
213213
self, # https://github.com/python/typing/discussions/1033
214214
) -> "dict_values[str, Rule]":
215-
return self._rules.values()
215+
return self._rule_map.values()

src/libvcs/url/git.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- Strict ``git(1)`` compatibility: :class:`GitBaseURL`.
1313
1414
- Output ``git(1)`` URL: :meth:`GitBaseURL.to_url()`
15-
- Extendable via :class:`~libvcs.url.base.Rules`,
15+
- Extendable via :class:`~libvcs.url.base.RuleMap`,
1616
:class:`~libvcs.url.base.Rule`
1717
"""
1818

@@ -22,7 +22,7 @@
2222

2323
from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin
2424

25-
from .base import Rule, Rules, URLProtocol
25+
from .base import Rule, RuleMap, URLProtocol
2626

2727
# Credit, pip (license: MIT):
2828
# https://github.com/pypa/pip/blob/22.1.2/src/pip/_internal/vcs/git.py#L39-L52
@@ -262,11 +262,11 @@ class GitBaseURL(URLProtocol, SkipDefaultFieldsReprMixin):
262262
suffix: Optional[str] = None
263263

264264
rule: Optional[str] = None
265-
rules: Rules = Rules(_rules={m.label: m for m in DEFAULT_MATCHERS})
265+
rule_map: RuleMap = RuleMap(_rule_map={m.label: m for m in DEFAULT_MATCHERS})
266266

267267
def __post_init__(self) -> None:
268268
url = self.url
269-
for rule in self.rules.values():
269+
for rule in self.rule_map.values():
270270
match = re.match(rule.pattern, url)
271271
if match is None:
272272
continue
@@ -311,10 +311,10 @@ def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
311311
if is_explicit is not None:
312312
return any(
313313
re.search(rule.pattern, url)
314-
for rule in cls.rules.values()
314+
for rule in cls.rule_map.values()
315315
if rule.is_explicit == is_explicit
316316
)
317-
return any(re.search(rule.pattern, url) for rule in cls.rules.values())
317+
return any(re.search(rule.pattern, url) for rule in cls.rule_map.values())
318318

319319
def to_url(self) -> str:
320320
"""Return a ``git(1)``-compatible URL. Can be used with ``git clone``.
@@ -370,7 +370,7 @@ class GitPipURL(GitBaseURL, URLProtocol, SkipDefaultFieldsReprMixin):
370370
# commit-ish (rev): tag, branch, ref
371371
rev: Optional[str] = None
372372

373-
rules: Rules = Rules(_rules={m.label: m for m in PIP_DEFAULT_MATCHERS})
373+
rule_map: RuleMap = RuleMap(_rule_map={m.label: m for m in PIP_DEFAULT_MATCHERS})
374374

375375
def to_url(self) -> str:
376376
"""Exports a pip-compliant URL.
@@ -452,7 +452,7 @@ def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
452452
453453
**Explicit VCS detection**
454454
455-
Pip-style URLs are prefixed with the VCS name in front, so its rules can
455+
Pip-style URLs are prefixed with the VCS name in front, so its rule_map can
456456
unambigously narrow the type of VCS:
457457
458458
>>> GitPipURL.is_valid(
@@ -478,13 +478,13 @@ class GitURL(GitPipURL, GitBaseURL, URLProtocol, SkipDefaultFieldsReprMixin):
478478
- :meth:`GitBaseURL.to_url`
479479
"""
480480

481-
rules: Rules = Rules(
482-
_rules={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
481+
rule_map: RuleMap = RuleMap(
482+
_rule_map={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
483483
)
484484

485485
@classmethod
486486
def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
487-
r"""Whether URL is compatible included Git URL rules or not.
487+
r"""Whether URL is compatible included Git URL rule_map or not.
488488
489489
Examples
490490
--------
@@ -510,7 +510,7 @@ def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
510510
511511
**Explicit VCS detection**
512512
513-
Pip-style URLs are prefixed with the VCS name in front, so its rules can
513+
Pip-style URLs are prefixed with the VCS name in front, so its rule_map can
514514
unambigously narrow the type of VCS:
515515
516516
>>> GitURL.is_valid(
@@ -549,7 +549,7 @@ def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
549549
... }
550550
... )
551551
552-
>>> GitURL.rules.register(GitHubRule)
552+
>>> GitURL.rule_map.register(GitHubRule)
553553
554554
>>> GitURL.is_valid(
555555
... url='git@github.com:vcs-python/libvcs.git', is_explicit=True
@@ -561,7 +561,7 @@ def is_valid(cls, url: str, is_explicit: Optional[bool] = None) -> bool:
561561
562562
This is just us cleaning up:
563563
564-
>>> GitURL.rules.unregister('gh-rule')
564+
>>> GitURL.rule_map.unregister('gh-rule')
565565
566566
>>> GitURL(url='git@github.com:vcs-python/libvcs.git').rule
567567
'core-git-scp'

src/libvcs/url/hg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
compare to :class:`urllib.parse.ParseResult`
77
88
- Output ``hg(1)`` URL: :meth:`HgURL.to_url()`
9-
- Extendable via :class:`~libvcs.url.base.Rules`,
9+
- Extendable via :class:`~libvcs.url.base.RuleMap`,
1010
:class:`~libvcs.url.base.Rule`
1111
1212
.. Note::
@@ -23,7 +23,7 @@
2323

2424
from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin
2525

26-
from .base import Rule, Rules, URLProtocol
26+
from .base import Rule, RuleMap, URLProtocol
2727

2828
RE_PATH = r"""
2929
((?P<user>\w+)@)?
@@ -176,11 +176,11 @@ class HgURL(URLProtocol, SkipDefaultFieldsReprMixin):
176176

177177
rule: Optional[str] = None
178178
# name of the :class:`Rule`
179-
rules: Rules = Rules(_rules={m.label: m for m in DEFAULT_MATCHERS})
179+
rule_map: RuleMap = RuleMap(_rule_map={m.label: m for m in DEFAULT_MATCHERS})
180180

181181
def __post_init__(self) -> None:
182182
url = self.url
183-
for rule in self.rules.values():
183+
for rule in self.rule_map.values():
184184
match = re.match(rule.pattern, url)
185185
if match is None:
186186
continue
@@ -211,7 +211,7 @@ def is_valid(cls, url: str, is_explicit: Optional[bool] = False) -> bool:
211211
>>> HgURL.is_valid(url='notaurl')
212212
False
213213
"""
214-
return any(re.search(rule.pattern, url) for rule in cls.rules.values())
214+
return any(re.search(rule.pattern, url) for rule in cls.rule_map.values())
215215

216216
def to_url(self) -> str:
217217
"""Return a ``hg(1)``-compatible URL. Can be used with ``hg clone``.

src/libvcs/url/svn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
compare to :class:`urllib.parse.ParseResult`
77
88
- Output ``svn(1)`` URL: :meth:`SvnURL.to_url()`
9-
- Extendable via :class:`~libvcs.url.base.Rules`,
9+
- Extendable via :class:`~libvcs.url.base.RuleMap`,
1010
:class:`~libvcs.url.base.Rule`
1111
1212
.. Note::
@@ -24,7 +24,7 @@
2424

2525
from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin
2626

27-
from .base import Rule, Rules, URLProtocol
27+
from .base import Rule, RuleMap, URLProtocol
2828

2929
RE_PATH = r"""
3030
((?P<user>.*)@)?
@@ -170,11 +170,11 @@ class SvnURL(URLProtocol, SkipDefaultFieldsReprMixin):
170170
ref: Optional[str] = None
171171

172172
rule: Optional[str] = None
173-
rules: Rules = Rules(_rules={m.label: m for m in DEFAULT_MATCHERS})
173+
rule_map: RuleMap = RuleMap(_rule_map={m.label: m for m in DEFAULT_MATCHERS})
174174

175175
def __post_init__(self) -> None:
176176
url = self.url
177-
for rule in self.rules.values():
177+
for rule in self.rule_map.values():
178178
match = re.match(rule.pattern, url)
179179
if match is None:
180180
continue
@@ -202,7 +202,7 @@ def is_valid(cls, url: str, is_explicit: Optional[bool] = False) -> bool:
202202
>>> SvnURL.is_valid(url='notaurl')
203203
False
204204
"""
205-
return any(re.search(rule.pattern, url) for rule in cls.rules.values())
205+
return any(re.search(rule.pattern, url) for rule in cls.rule_map.values())
206206

207207
def to_url(self) -> str:
208208
"""Return a ``svn(1)``-compatible URL. Can be used with ``svn checkout``.

tests/url/test_git.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from libvcs.sync.git import GitSync
6-
from libvcs.url.base import Rules
6+
from libvcs.url.base import RuleMap
77
from libvcs.url.git import DEFAULT_MATCHERS, PIP_DEFAULT_MATCHERS, GitBaseURL, GitURL
88

99

@@ -142,8 +142,8 @@ def test_git_url_extension_pip(
142142
git_repo: GitSync,
143143
) -> None:
144144
class GitURLWithPip(GitBaseURL):
145-
rules: Rules = Rules(
146-
_rules={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
145+
rule_map: RuleMap = RuleMap(
146+
_rule_map={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
147147
)
148148

149149
git_url_kwargs["url"] = git_url_kwargs["url"].format(local_repo=git_repo.dir)
@@ -255,8 +255,8 @@ def test_git_revs(
255255
git_url_kwargs: GitURLKwargs,
256256
) -> None:
257257
class GitURLWithPip(GitURL):
258-
rules: Rules = Rules(
259-
_rules={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
258+
rule_map: RuleMap = RuleMap(
259+
_rule_map={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
260260
)
261261

262262
git_url = GitURLWithPip(**git_url_kwargs)

tests/url/test_hg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from libvcs.sync.hg import HgSync
6-
from libvcs.url.base import Rules
6+
from libvcs.url.base import RuleMap
77
from libvcs.url.hg import DEFAULT_MATCHERS, PIP_DEFAULT_MATCHERS, HgURL
88

99

@@ -107,8 +107,8 @@ def test_hg_url_extension_pip(
107107
hg_repo: HgSync,
108108
) -> None:
109109
class HgURLWithPip(HgURL):
110-
rules: Rules = Rules(
111-
_rules={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
110+
rule_map: RuleMap = RuleMap(
111+
_rule_map={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
112112
)
113113

114114
hg_url_kwargs["url"] = hg_url_kwargs["url"].format(local_repo=hg_repo.dir)

tests/url/test_svn.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from libvcs.sync.svn import SvnSync
6-
from libvcs.url.base import Rules
6+
from libvcs.url.base import RuleMap
77
from libvcs.url.svn import DEFAULT_MATCHERS, PIP_DEFAULT_MATCHERS, SvnURL
88

99

@@ -124,8 +124,8 @@ def test_svn_url_extension_pip(
124124
svn_repo: SvnSync,
125125
) -> None:
126126
class SvnURLWithPip(SvnURL):
127-
rules: Rules = Rules(
128-
_rules={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
127+
rule_map: RuleMap = RuleMap(
128+
_rule_map={m.label: m for m in [*DEFAULT_MATCHERS, *PIP_DEFAULT_MATCHERS]}
129129
)
130130

131131
svn_url_kwargs["url"] = svn_url_kwargs["url"].format(local_repo=svn_repo.dir)

0 commit comments

Comments
 (0)