From 1349c86373e9286c0e08a87c6c37469419e3df46 Mon Sep 17 00:00:00 2001 From: Alessio Placitelli Date: Fri, 21 Feb 2020 15:32:04 +0100 Subject: [PATCH] Drop 'inflection' and make transitive dependencies versions explicit (#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 --- HISTORY.rst | 4 ++++ README.rst | 1 - glean_parser/util.py | 23 ++++++++++++++++++++--- setup.py | 7 ++++++- tests/test_utils.py | 31 +++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 tests/test_utils.py diff --git a/HISTORY.rst b/HISTORY.rst index 3261c9879..3e9083c88 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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) ------------------- diff --git a/README.rst b/README.rst index ef1b2ee2a..1b6ec312b 100644 --- a/README.rst +++ b/README.rst @@ -26,7 +26,6 @@ is installed by `pip`. - appdirs - Click - diskcache -- inflection - Jinja2 - jsonschema - PyYAML diff --git a/glean_parser/util.py b/glean_parser/util.py index fceb0c41f..b8f113b2a 100644 --- a/glean_parser/util.py +++ b/glean_parser/util.py @@ -15,7 +15,6 @@ import appdirs import diskcache -import inflection import jinja2 import jsonschema from jsonschema import _utils @@ -136,6 +135,24 @@ 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). @@ -143,7 +160,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("-", "_"), False) + return to_camel_case(value, False) def Camelize(value): @@ -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() diff --git a/setup.py b/setup.py index 8d1b151cd..85e8d3205 100755 --- a/setup.py +++ b/setup.py @@ -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"] diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 000000000..3fcd73f64 --- /dev/null +++ b/tests/test_utils.py @@ -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)