Skip to content

Commit 81593c9

Browse files
committed
fix(parse,hg): Typing fixes and examples
1 parent 1de03d1 commit 81593c9

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

libvcs/parse/hg.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
RE_PATH = r"""
2929
((?P<user>.*)@)?
3030
(?P<hostname>([^/:]+))
31-
(?P<separator>[:,/])?
31+
(:(?P<port>\d{1,4}))?
32+
(?P<separator>/)?
3233
(?P<path>
33-
(\w[^:.]*) # cut the path at . to negate .hg
34+
/?(\w[^:.]*)
3435
)?
3536
"""
3637

@@ -151,9 +152,9 @@ class HgURL(URLProtocol, SkipDefaultFieldsReprMixin):
151152
>>> HgURL(url='ssh://username@machinename/path/to/repo')
152153
HgURL(url=ssh://username@machinename/path/to/repo,
153154
scheme=ssh,
155+
user=username,
154156
hostname=machinename,
155157
path=path/to/repo,
156-
user=username,
157158
matcher=core-hg)
158159
159160
- Compatibility checking: :meth:`HgURL.is_valid()`
@@ -162,9 +163,11 @@ class HgURL(URLProtocol, SkipDefaultFieldsReprMixin):
162163

163164
url: str
164165
scheme: Optional[str] = None
165-
hostname: Optional[str] = None
166-
path: Optional[str] = None
167166
user: Optional[str] = None
167+
hostname: str = dataclasses.field(default="")
168+
port: Optional[int] = None
169+
separator: str = dataclasses.field(default="/")
170+
path: str = dataclasses.field(default="")
168171

169172
#
170173
# commit-ish: tag, branch, ref, revision
@@ -233,23 +236,63 @@ def to_url(self) -> str:
233236
>>> hg_location.to_url()
234237
'https://hg.mozilla.org/mobile-browser'
235238
236-
Switch them to hglab:
239+
Switch them to localhost:
237240
238241
>>> hg_location.hostname = 'localhost'
239242
>>> hg_location.scheme = 'http'
240243
241244
>>> hg_location.to_url()
242245
'http://localhost/mobile-browser'
243246
244-
todo
245-
----
247+
Another example, `hugin <http://hugin.hg.sourceforge.net>`_:
248+
249+
>>> hugin = HgURL(url="http://hugin.hg.sourceforge.net:8000/hgroot/hugin/hugin")
250+
251+
>>> hugin
252+
HgURL(url=http://hugin.hg.sourceforge.net:8000/hgroot/hugin/hugin,
253+
scheme=http,
254+
hostname=hugin.hg.sourceforge.net,
255+
port=8000,
256+
path=hgroot/hugin/hugin,
257+
matcher=core-hg)
258+
259+
>>> hugin.to_url()
260+
'http://hugin.hg.sourceforge.net:8000/hgroot/hugin/hugin'
261+
262+
SSH URL with a username, `graphicsmagic <http://graphicsmagick.org/Hg.html>`_:
263+
264+
>>> graphicsmagick = HgURL(
265+
... url="ssh://yourid@hg.GraphicsMagick.org//hg/GraphicsMagick"
266+
... )
267+
268+
>>> graphicsmagick
269+
HgURL(url=ssh://yourid@hg.GraphicsMagick.org//hg/GraphicsMagick,
270+
scheme=ssh,
271+
user=yourid,
272+
hostname=hg.GraphicsMagick.org,
273+
path=/hg/GraphicsMagick,
274+
matcher=core-hg)
275+
276+
>>> graphicsmagick.to_url()
277+
'ssh://yourid@hg.GraphicsMagick.org//hg/GraphicsMagick'
278+
279+
Switch the username:
280+
281+
>>> graphicsmagick.user = 'lucas'
282+
283+
>>> graphicsmagick.to_url()
284+
'ssh://lucas@hg.GraphicsMagick.org//hg/GraphicsMagick'
246285
247-
- Formats: Show an example converting a hghub url from ssh -> https format,
248-
and the other way around.
249286
"""
250-
if self.scheme is not None:
251-
parts = [self.scheme, "://", self.hostname, "/", self.path]
252-
else:
253-
parts = [self.user or "hg", "@", self.hostname, ":", self.path]
287+
parts = [self.scheme or "ssh", "://"]
288+
if self.user:
289+
parts.extend([self.user, "@"])
290+
291+
parts.append(self.hostname)
292+
293+
if self.port is not None:
294+
parts.extend([":", f"{self.port}"])
295+
296+
parts.extend([self.separator, self.path])
254297

255298
return "".join(part for part in parts if isinstance(part, str))

0 commit comments

Comments
 (0)