8000 docs(registry): Add example of test from scratch · vcs-python/libvcs@69919fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 69919fe

Browse files
committed
docs(registry): Add example of test from scratch
1 parent 0ecb06c commit 69919fe

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

docs/url/registry.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Detect VCS from `git`, `hg`, and `svn` URLs.
44

5+
**Basic example:**
6+
57
```python
68
>>> from libvcs.url.registry import registry, ParserMatch
79
>>> from libvcs.url.git import GitURL
@@ -22,6 +24,87 @@ Detect VCS from `git`, `hg`, and `svn` URLs.
2224
[ParserMatch(vcs='git', match=GitURL(...))]
2325
```
2426

27+
**From the ground up:**
28+
29+
```python
30+
>>> import dataclasses
31+
>>> from libvcs.url.base import Rule, RuleMap
32+
>>> from libvcs.url.registry import ParserMatch, VCSRegistry
33+
>>> from libvcs.url.git import GitURL
34+
35+
This will match `github:org/repo`:
36+
37+
>>> class GitHubPrefix(Rule):
38+
... label = 'gh-prefix'
39+
... description ='Matches prefixes like github:org/repo'
40+
... pattern = r'^github:(?P<path>.*)$'
41+
... defaults = {
42+
... 'hostname': 'github.com',
43+
... 'scheme': 'https'
44+
... }
45+
... is_explicit = True # We know it's git, not any other VCS
46+
47+
Prefix for KDE infrastructure, `kde:group/repository`:
48+
49+
>>> class KDEPrefix(Rule): # https://community.kde.org/Infrastructure/Git
50+
... label = 'kde-prefix'
51+
... description ='Matches prefixes like kde:org/repo'
52+
... pattern = r'^kde:(?P<path>\w[^:]+)$'
53+
... defaults = {
54+
... 'hostname': 'invent.kde.org',
55+
... 'scheme': 'https'
56+
... }
57+
... is_explicit = True
58+
59+
>>> @dataclasses.dataclass(repr=False)
60+
... class MyGitURLParser(GitURL):
61+
... rule_map: RuleMap = RuleMap(
62+
... _rule_map={
63+
... **GitURL.rule_map._rule_map,
64+
... 'github_prefix': GitHubPrefix,
65+
... 'kde_prefix': KDEPrefix,
66+
... }
67+
... )
68+
69+
>>> my_parsers: "ParserLazyMap" = {
70+
... "git": MyGitURLParser,
71+
... "hg": "libvcs.url.hg.HgURL",
72+
... "svn": "libvcs.url.svn.SvnURL",
73+
... }
74+
75+
>>> vcs_matcher = VCSRegistry(parsers=my_parsers)
76+
77+
>>> vcs_matcher.match('git@invent.kde.org:plasma/plasma-sdk.git')
78+
[ParserMatch(vcs='git', match=MyGitURLParser(...)),
79+
ParserMatch(vcs='hg', match=HgURL(...)),
80+
ParserMatch(vcs='svn', match=SvnURL(...))]
81+
82+
Still works with everything GitURL does:
8000 83+
84+
>>> vcs_matcher.match('git+ssh://git@invent.kde.org:plasma/plasma-sdk.git', is_explicit=True)
85+
[ParserMatch(vcs='git', match=MyGitURLParser(...))]
86+
87+
>>> vcs_matcher.match('github:webpack/webpack', is_explicit=True)
88+
[ParserMatch(vcs='git',
89+
match=MyGitURLParser(url=github:webpack/webpack,
90+
hostname=github.com,
91+
path=webpack/webpack,
92+
rule=gh-prefix))]
93+
94+
>>> vcs_matcher.match('github:webpack/webpack', is_explicit=True)[0].match.to_url()
95+
'git@github.com:webpack/webpack'
96+
97+
>>> vcs_matcher.match('kde:frameworks/kirigami', is_explicit=True)
98+
[ParserMatch(vcs='git',
99+
match=MyGitURLParser(url=kde:frameworks/kirigami,
100+
hostname=invent.kde.org,
101+
path=frameworks/kirigami,
102+
rule=kde-prefix))]
103+
104+
>>> vcs_matcher.match('kde:frameworks/kirigami', is_explicit=True)[0].match.to_url()
105+
'git@invent.kde.org:frameworks/kirigami'
106+
```
107+
25108
```{eval-rst}
26109
.. automodule:: libvcs.url.registry
27110
:members:

0 commit comments

Comments
 (0)
0