-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement name normalization from PEP 503
- Loading branch information
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ API | |
specifiers | ||
markers | ||
requirements | ||
utils | ||
|
||
|
||
Project | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Utilities | ||
========= | ||
|
||
.. currentmodule:: packaging.utils | ||
|
||
|
||
A set of small, helper utilities for dealing with Python packages. | ||
|
||
|
||
Reference | ||
--------- | ||
|
||
.. function:: canonicalize_name(name) | ||
|
||
This function takes a valid Python package name, and returns the normalized | ||
form of it. | ||
|
||
:param str name: The name to normalize. | ||
|
||
.. doctest:: | ||
|
||
>>> from packaging.utils import canonicalize_name | ||
>>> canonicalize_name("Django") | ||
'django' | ||
>>> canonicalize_name("oslo.concurrency") | ||
'oslo-concurrency' | ||
>>> canonicalize_name("requests") | ||
'requests' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# This file is dual licensed under the terms of the Apache License, Version | ||
# 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
# for complete details. | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import re | ||
|
||
|
||
_canonicalize_regex = re.compile(r"[-_.]+") | ||
|
||
|
||
def canonicalize_name(name): | ||
# This is taken from PEP 503. | ||
return _canonicalize_regex.sub("-", name).lower() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# This file is dual licensed under the terms of the Apache License, Version | ||
# 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
# for complete details. | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import pytest | ||
|
||
from packaging.utils import canonicalize_name | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("name", "expected"), | ||
[ | ||
("foo", "foo"), | ||
("Foo", "foo"), | ||
("fOo", "foo"), | ||
("foo.bar", "foo-bar"), | ||
("Foo.Bar", "foo-bar"), | ||
("Foo.....Bar", "foo-bar"), | ||
("foo_bar", "foo-bar"), | ||
("foo___bar", "foo-bar"), | ||
("foo-bar", "foo-bar"), | ||
("foo----bar", "foo-bar"), | ||
], | ||
) | ||
def test_canonicalize_name(name, expected): | ||
assert canonicalize_name(name) == expected |