Skip to content

Commit

Permalink
Implement name normalization from PEP 503
Browse files Browse the repository at this point in the history
  • Loading branch information
dstufft committed Feb 9, 2016
1 parent 7e07603 commit deb5846
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changelog

.. note:: This version is not yet released and is under active development.

* Add a function that implements the name canonicalization from PEP 503.


16.1 - 2016-02-07
~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ API
specifiers
markers
requirements
utils


Project
Expand Down
28 changes: 28 additions & 0 deletions docs/utils.rst
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'
14 changes: 14 additions & 0 deletions packaging/utils.py
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()
27 changes: 27 additions & 0 deletions tests/test_utils.py
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

0 comments on commit deb5846

Please sign in to comment.