8000 Decouple regexes, make varaibles consistent by tony · Pull Request #463 · vcs-python/libvcs · GitHub
[go: up one dir, main page]

Skip to content

Decouple regexes, make varaibles consistent #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ $ pip install --u 8000 ser --upgrade --pre libvcs

<!-- Maintainers, insert changes / features for the next release here -->

### Breaking changes

#### urls: Variable changes (#463)

- `RE_PIP_REV` moved from `libvcs.url.git` to `libvcs.url.constants`.
- Regex pattern for user (e.g. `git@`) decoupled to `RE_USER`.
- `RE_PATH` and `SCP_REGEX` (now `RE_SCP`) no longer include user regex pattern
- Existing patterns now use `RE_USER` explicitly.
- `REGEX_SCP` renamed to `RE_SCP` for consistency.

### Documentation

- Automatically linkify links that were previously only text.
Expand All @@ -26,6 +36,7 @@ $ pip install --user --upgrade --pre libvcs
- poetry: 1.8.1 -> 1.8.2

See also: https://github.com/python-poetry/poetry/blob/1.8.2/CHANGELOG.md

- Code quality: Use f-strings in more places (#460)

via [ruff 0.4.2](https://github.com/astral-sh/ruff/blob/v0.4.2/CHANGELOG.md).
Expand Down
14 changes: 14 additions & 0 deletions docs/url/constants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
myst:
html_meta:
"property=og:locale": "en_US"
---
(url-parser-constants)=

# Constants - `libvcs.url.constants`

```{eval-rst}
.. automodule:: libvcs.url.constants
:members:
:undoc-members:
```
1 change: 1 addition & 0 deletions docs/url/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,5 @@ svn
hg
base
registry
constants
```
28 changes: 28 additions & 0 deletions src/libvcs/url/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Constants shared across ``libvcs.url``."""

RE_USER = r"""
((?P<user>[^/:@]+)@)?
"""
"""Optional user, e.g. 'git@'"""

# Credit, pip (license: MIT):
# https://github.com/pypa/pip/blob/22.1.2/src/pip/_internal/vcs/git.py#L39-L52
# We modified it to have groupings
RE_SCP = r"""
# Server, e.g. 'github.com'.
(?P<hostname>([^/:]+))
(?P<separator>:)
# The server-side path. e.g. 'user/project.git'. Must start with an
# alphanumeric character so as not to be confusable with a Windows paths
# like 'C:/foo/bar' or 'C:\foo\bar'.
(?P<path>(\w[^:.]+))
"""
"""Regular expression for scp-style of git URLs."""

#
# Third-party URLs, e.g. npm, pip, etc.
#
RE_PIP_REV = r"""
(@(?P<rev>.*))
"""
"""Pip-style revision for branch or revision."""
30 changes: 7 additions & 23 deletions src/libvcs/url/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,9 @@
from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin

from .base import Rule, RuleMap, URLProtocol

# Credit, pip (license: MIT):
# https://github.com/pypa/pip/blob/22.1.2/src/pip/_internal/vcs/git.py#L39-L52
# We modified it to have groupings
SCP_REGEX = r"""
# Optional user, e.g. 'git@'
((?P<user>\w+)@)?
# Server, e.g. 'github.com'.
(?P<hostname>([^/:]+))
(?P<separator>:)
# The server-side path. e.g. 'user/project.git'. Must start with an
# alphanumeric character so as not to be confusable with a Windows paths
# like 'C:/foo/bar' or 'C:\foo\bar'.
(?P<path>(\w[^:.]+))
"""
from .constants import RE_PIP_REV, RE_SCP, RE_USER

RE_PATH = r"""
((?P<user>\w+)@)?
(?P<hostname>([^/:]+))
(:(?P<port>\d{1,5}))?
(?P<separator>[:,/])?
Expand Down Expand Up @@ -69,6 +54,7 @@
rf"""
^{RE_SCHEME}
://
{RE_USER}
{RE_PATH}
{RE_SUFFIX}?
""",
Expand All @@ -83,7 +69,8 @@
pattern=re.compile(
rf"""
^(?P<scheme>ssh)?
{SCP_REGEX}
{RE_USER}
{RE_SCP}
{RE_SUFFIX}?
""",
re.VERBOSE,
Expand Down Expand Up @@ -121,11 +108,6 @@
)
"""

RE_PIP_REV = r"""
(@(?P<rev>.*))
"""


PIP_DEFAULT_RULES: list[Rule] = [
Rule(
label="pip-url",
Expand All @@ -134,6 +116,7 @@
rf"""
{RE_PIP_SCHEME}
://
{RE_USER}
{RE_PATH}
{RE_SUFFIX}?
{RE_PIP_REV}?
Expand All @@ -148,7 +131,8 @@
pattern=re.compile(
rf"""
{RE_PIP_SCP_SCHEME}
{SCP_REGEX}?
{RE_USER}
{RE_SCP}?
{RE_SUFFIX}?
{RE_PIP_REV}?
""",
Expand Down
6D47 9 changes: 6 additions & 3 deletions src/libvcs/url/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
from typing import Optional

from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin
from libvcs.url.git import RE_PIP_REV, RE_SUFFIX, SCP_REGEX
from libvcs.url.git import RE_SUFFIX

from .base import Rule, RuleMap, URLProtocol
from .constants import RE_PIP_REV, RE_SCP, RE_USER

RE_PATH = r"""
((?P<user>\w+)@)?
(?P<hostname>([^/:]+))
(:(?P<port>\d{1,5}))?
(?P<separator>[:,/])?
Expand All @@ -53,6 +53,7 @@
rf"""
^{RE_SCHEME}
://
{RE_USER}
{RE_PATH}
{RE_SUFFIX}?
{RE_PIP_REV}?
Expand All @@ -66,7 +67, 9E81 8 @@
pattern=re.compile(
rf"""
^(?P<scheme>ssh)?
{SCP_REGEX}
{RE_USER}
{RE_SCP}
{RE_SUFFIX}?
""",
re.VERBOSE,
Expand Down Expand Up @@ -100,6 +102,7 @@
rf"""
^{RE_PIP_SCHEME}
://
{RE_USER}
{RE_PATH}
{RE_SUFFIX}?
{RE_PIP_REV}?
Expand Down
8 changes: 5 additions & 3 deletions src/libvcs/url/svn.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
from typing import Optional

from libvcs._internal.dataclasses import SkipDefaultFieldsReprMixin
from libvcs.url.git import RE_PIP_REV, SCP_REGEX

from .base import Rule, RuleMap, URLProtocol
from .constants import RE_PIP_REV, RE_SCP, RE_USER

RE_PATH = r"""
((?P<user>[^/:@]+)@)?
(?P<hostname>([^/:@]+))
(:(?P<port>\d{1,5}))?
(?P<separator>[:,/])?
Expand Down Expand Up @@ -56,6 +55,7 @@
rf"""
^{RE_SCHEME}
://
{RE_USER}
{RE_PATH}
{RE_PIP_REV}?
""",
Expand All @@ -68,7 +68,8 @@
pattern=re.compile(
rf"""
^(?P<scheme>ssh)?
{SCP_REGEX}
{RE_USER}
{RE_SCP}
{RE_PIP_REV}?
""",
re.VERBOSE,
Expand Down Expand Up @@ -101,6 +102,7 @@
rf"""
^{RE_PIP_SCHEME}
://
{RE_USER}
{RE_PATH}
{RE_PIP_REV}?
""",
Expand Down
0