Skip to content

Commit

Permalink
Drop 'inflection' and make transitive dependencies versions explicit (#…
Browse files Browse the repository at this point in the history
…174)

* Add version range for the zipp and MarkupSafe dependencies

These are not direct dependencies but rather ones from
Jinja2 and jsonschema. We need them to be within specific
version range as we are required to support Python 3.5 in
mozilla-central.

* Drop the 'inflection' dependency

This implements the to_camel_case function and
adds basic test coverage for it.

* Update HISTORY.rst
  • Loading branch information
Dexterp37 authored Feb 21, 2020
1 parent 4eb0d46 commit 1349c86
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ History
Unreleased
----------

* Dropped the 'inflection' dependency.
* Constrained the 'zipp' and 'MarkupSafe' transitive dependencies to versions that
support Python 3.5.

1.18.2 (2020-02-14)
-------------------

Expand Down
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ is installed by `pip`.
- appdirs
- Click
- diskcache
- inflection
- Jinja2
- jsonschema
- PyYAML
Expand Down
23 changes: 20 additions & 3 deletions glean_parser/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import appdirs
import diskcache
import inflection
import jinja2
import jsonschema
from jsonschema import _utils
Expand Down Expand Up @@ -136,14 +135,32 @@ def ensure_list(value):
return value


def to_camel_case(input, capitalize_first_letter):
"""
Convert the value to camelCase.
This additionally replaces any '.' with '_'. The first letter is capitalized
depending on `capitalize_first_letter`.
"""
sanitized_input = input.replace(".", "_").replace("-", "_")
# Filter out any empty token. This could happen due to leading '_' or
# consecutive '__'.
tokens = [s.capitalize() for s in sanitized_input.split("_") if len(s) != 0]
# If we're not meant to capitalize the first letter, then lowercase it.
if not capitalize_first_letter:
tokens[0] = tokens[0].lower()
# Finally join the tokens and capitalize.
return ''.join(tokens)


def camelize(value):
"""
Convert the value to camelCase (with a lower case first letter).
This is a thin wrapper around inflection.camelize that handles dots in
addition to underscores.
"""
return inflection.camelize(value.replace(".", "_").replace("-", "_"), False)
return to_camel_case(value, False)


def Camelize(value):
Expand All @@ -153,7 +170,7 @@ def Camelize(value):
This is a thin wrapper around inflection.camelize that handles dots in
addition to underscores.
"""
return inflection.camelize(value.replace(".", "_").replace("-", "_"), True)
return to_camel_case(value, True)


@functools.lru_cache()
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@
"appdirs>=1.4.3",
"Click>=7.0",
"diskcache>=4.0.0",
"inflection>=0.3.1",
"iso8601>=0.1.12",
"Jinja2>=2.10.1,<3.0",
"jsonschema>=3.0.2",
# 'markupsafe' is required by Jinja2. From version 2.0.0 on
# py3.5 support is dropped.
"markupsafe>=1.1,<2.0.0",
"pep487==1.0.1",
"PyYAML>=3.13",
"yamllint>=1.18.0",
# 'zipp' is required by jsonschema->importlib_metadata,
# it drops py3.5 in newer versions.
"zipp>=0.5,<2.0",
]

setup_requirements = ["pytest-runner", "setuptools-scm"]
Expand Down
31 changes: 31 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/

from glean_parser.util import to_camel_case


def test_camel_case_first_lowercase():
assert "testMe" == to_camel_case("test_me", False)


def test_camel_case_first_uppercase():
assert "TestMe" == to_camel_case("test_me", True)


def test_camel_case_empty_tokens():
assert "testMe" == to_camel_case("__test____me", False)


def test_camel_case_dots_sanitized():
assert "testMeYeah" == to_camel_case("__test..me.yeah", False)


def test_camel_case_numbers():
assert "g33kS4n1t1z3d" == to_camel_case("g33k_s4n1t1z3d", False)


def test_camel_case_expected():
assert "easyOne" == to_camel_case("easy_one", False)
assert "moreInvolved1" == to_camel_case("more_involved_1", False)

0 comments on commit 1349c86

Please sign in to comment.