From 29c32b382e3cca9f424020b38b31fee2cf416e00 Mon Sep 17 00:00:00 2001 From: Jeff Quast Date: Tue, 23 Jun 2020 12:08:54 -0400 Subject: [PATCH] Do not depend on pkg_resources module (#42) Generate code for `wcwidth/unicode_versions.py`, delete `wcwidth/version.json`, and all references to it. --- .pylintrc | 2 +- bin/new-wide-by-version.py | 2 +- bin/run_codecov.py | 11 +++-------- bin/update-tables.py | 37 ++++++++++++++++++++++++++++--------- bin/wcwidth-browser.py | 2 +- setup.py | 8 +------- tests/test_ucslevel.py | 14 -------------- wcwidth/__init__.py | 6 +++++- wcwidth/table_wide.py | 2 +- wcwidth/table_zero.py | 2 +- wcwidth/unicode_versions.py | 35 +++++++++++++++++++++++++++++++++++ wcwidth/version.json | 1 - wcwidth/wcwidth.py | 37 +------------------------------------ 13 files changed, 78 insertions(+), 81 deletions(-) create mode 100644 wcwidth/unicode_versions.py delete mode 100644 wcwidth/version.json diff --git a/.pylintrc b/.pylintrc index d1c40dc..074ebb1 100644 --- a/.pylintrc +++ b/.pylintrc @@ -9,7 +9,7 @@ load-plugins= persistent = no jobs = 0 unsafe-load-any-extension = yes -good-names = wc +good-names = wc,fp [MESSAGES CONTROL] disable= diff --git a/bin/new-wide-by-version.py b/bin/new-wide-by-version.py index 7999130..dedf2cd 100755 --- a/bin/new-wide-by-version.py +++ b/bin/new-wide-by-version.py @@ -15,8 +15,8 @@ previous version (4.1.0). """ # std imports -import json import sys +import json # List new WIDE characters at each unicode version. diff --git a/bin/run_codecov.py b/bin/run_codecov.py index 49e7d7f..3c6649a 100644 --- a/bin/run_codecov.py +++ b/bin/run_codecov.py @@ -1,12 +1,10 @@ -""" -Workaround for https://github.com/codecov/codecov-python/issues/158 -""" +"""Workaround for https://github.com/codecov/codecov-python/issues/158.""" # std imports import sys import time -# local +# 3rd party import codecov RETRIES = 5 @@ -14,10 +12,7 @@ def main(): - """ - Run codecov up to RETRIES times - On the final attempt, let it exit normally - """ + """Run codecov up to RETRIES times On the final attempt, let it exit normally.""" # Make a copy of argv and make sure --required is in it args = sys.argv[1:] diff --git a/bin/update-tables.py b/bin/update-tables.py index 49a7d9f..e865ff0 100644 --- a/bin/update-tables.py +++ b/bin/update-tables.py @@ -62,7 +62,7 @@ def main(): do_east_asian(versions) do_zero_width(versions) do_rst_file_update() - do_version_json(versions) + do_unicode_versions(versions) def get_unicode_versions(): @@ -297,18 +297,37 @@ def do_write_table(fname, variable, table): print("complete.") -def do_version_json(versions): - fname = os.path.join(PATH_CODE, 'version.json') +def do_unicode_versions(versions): + """Write unicode_versions.py function list_versions().""" + fname = os.path.join(PATH_CODE, 'unicode_versions.py') print(f"writing {fname} ... ", end='') - with open(fname, 'r') as fp: - version_data = json.load(fp) + utc_now = datetime.datetime.utcnow() + version_tuples_str = '\n '.join( + f'"{ver}",' for ver in versions) + with open(fname, 'w') as fp: + fp.write(f"""\"\"\" +Exports function list_versions() for unicode version level support. - version_data['tables'] = versions +This code generated by {__file__} on {utc_now}. +\"\"\" - with open(fname, 'w') as fp: - json.dump(version_data, fp) - print() + +def list_versions(): + \"\"\" + Return Unicode version levels supported by this module release. + + Any of the version strings returned may be used as keyword argument + ``unicode_version`` to the ``wcwidth()`` family of functions. + + :returns: Supported Unicode version numbers in ascending sorted order. + :rtype: list[str] + \"\"\" + return ( + {version_tuples_str} + ) +""") + print('done.') if __name__ == '__main__': diff --git a/bin/wcwidth-browser.py b/bin/wcwidth-browser.py index 3b3c2b0..4f53960 100755 --- a/bin/wcwidth-browser.py +++ b/bin/wcwidth-browser.py @@ -489,7 +489,7 @@ def _process_keystroke_movement(self, inp, idx, offset): # scroll backward 1 line offset -= self.screen.num_columns elif inp in ('e', 'j') or inp.code in (term.KEY_ENTER, - term.KEY_DOWN,): + term.KEY_DOWN,): # scroll forward 1 line offset = offset + self.screen.num_columns elif inp in ('f', ' ') or inp.code in (term.KEY_PGDOWN,): diff --git a/setup.py b/setup.py index 67982a4..787f8fc 100755 --- a/setup.py +++ b/setup.py @@ -16,11 +16,6 @@ def _get_here(fname): return os.path.join(os.path.dirname(__file__), fname) -def _get_version(fname, key='package'): - import json - return json.load(open(fname, 'r'))[key] - - class _SetupUpdate(setuptools.Command): # This is a compatibility, some downstream distributions might # still call "setup.py update". @@ -49,8 +44,7 @@ def main(): setuptools.setup( name='wcwidth', # NOTE: manually manage __version__ in wcwidth/__init__.py ! - version=_get_version( - _get_here(os.path.join('wcwidth', 'version.json'))), + version='0.2.5', description=( "Measures the displayed width of unicode strings in a terminal"), long_description=codecs.open( diff --git a/tests/test_ucslevel.py b/tests/test_ucslevel.py index f6012e3..0e2ff66 100644 --- a/tests/test_ucslevel.py +++ b/tests/test_ucslevel.py @@ -12,20 +12,6 @@ import wcwidth -def test_list_versions(): - """wcwidth.list_versions() returns expected value.""" - # given, - expected = json.loads( - pkg_resources.resource_string('wcwidth', 'version.json').decode('utf8') - )['tables'] - - # exercise, - result = wcwidth.list_versions() - - # verify, - assert result == expected - - def test_latest(): """wcwidth._wcmatch_version('latest') returns tail item.""" # given, diff --git a/wcwidth/__init__.py b/wcwidth/__init__.py index eb8a96b..a9008f8 100644 --- a/wcwidth/__init__.py +++ b/wcwidth/__init__.py @@ -30,4 +30,8 @@ # these always bit me, too, so I can sympathize -- this version is now manually # kept in sync with version.json to help them out. Shucks, this variable is just # for legacy, from the days before 'pip freeze' was a thing. -__version__ = '0.2.4' +# +# We also used pkg_resources to load unicode version tables from version.json, +# generated by bin/update-tables.py, but some environments are unable to +# import pkg_resources for one reason or another, yikes! +__version__ = '0.2.5' diff --git a/wcwidth/table_wide.py b/wcwidth/table_wide.py index 176bf61..4fe575c 100644 --- a/wcwidth/table_wide.py +++ b/wcwidth/table_wide.py @@ -1,5 +1,5 @@ """Wide_Eastasian table, created by bin/update-tables.py.""" -# Generated: 2020-06-01T15:38:29.249643 +# Generated: 2020-06-23T15:58:41.860748 WIDE_EASTASIAN = { '4.1.0': ( # Source: EastAsianWidth-4.1.0.txt diff --git a/wcwidth/table_zero.py b/wcwidth/table_zero.py index e37201b..73aa689 100644 --- a/wcwidth/table_zero.py +++ b/wcwidth/table_zero.py @@ -1,5 +1,5 @@ """Zero_Width table, created by bin/update-tables.py.""" -# Generated: 2020-06-01T15:38:31.292746 +# Generated: 2020-06-23T15:58:43.900697 ZERO_WIDTH = { '4.1.0': ( # Source: DerivedGeneralCategory-4.1.0.txt diff --git a/wcwidth/unicode_versions.py b/wcwidth/unicode_versions.py new file mode 100644 index 0000000..24119f9 --- /dev/null +++ b/wcwidth/unicode_versions.py @@ -0,0 +1,35 @@ +""" +Exports function list_versions() for unicode version level support. + +This code generated by bin/update-tables.py on 2020-06-23 15:58:44.035540. +""" + + +def list_versions(): + """ + Return Unicode version levels supported by this module release. + + Any of the version strings returned may be used as keyword argument + ``unicode_version`` to the ``wcwidth()`` family of functions. + + :returns: Supported Unicode version numbers in ascending sorted order. + :rtype: list[str] + """ + return ( + "4.1.0", + "5.0.0", + "5.1.0", + "5.2.0", + "6.0.0", + "6.1.0", + "6.2.0", + "6.3.0", + "7.0.0", + "8.0.0", + "9.0.0", + "10.0.0", + "11.0.0", + "12.0.0", + "12.1.0", + "13.0.0", + ) diff --git a/wcwidth/version.json b/wcwidth/version.json deleted file mode 100644 index 59f1aa0..0000000 --- a/wcwidth/version.json +++ /dev/null @@ -1 +0,0 @@ -{"tables": ["4.1.0", "5.0.0", "5.1.0", "5.2.0", "6.0.0", "6.1.0", "6.2.0", "6.3.0", "7.0.0", "8.0.0", "9.0.0", "10.0.0", "11.0.0", "12.0.0", "12.1.0", "13.0.0"], "package": "0.2.4", "default": "8.0.0"} diff --git a/wcwidth/wcwidth.py b/wcwidth/wcwidth.py index 229419e..931bd0b 100644 --- a/wcwidth/wcwidth.py +++ b/wcwidth/wcwidth.py @@ -65,15 +65,12 @@ # std imports import os import sys -import json import warnings -# 3rd party -import pkg_resources - # local from .table_wide import WIDE_EASTASIAN from .table_zero import ZERO_WIDTH +from .unicode_versions import list_versions try: from functools import lru_cache @@ -252,25 +249,6 @@ def wcswidth(pwcs, n=None, unicode_version='auto'): return width -@lru_cache(maxsize=1) -def list_versions(): - """ - Return Unicode version levels supported by this module release. - - Any of the version strings returned may be used as keyword argument - ``unicode_version`` to the ``wcwidth()`` family of functions. - - :returns: Supported Unicode version numbers in ascending sorted order. - :rtype: list[str] - """ - # load from 'version.json', use setuptools to access - # resource string so that the package is zip/wheel-compatible. - return json.loads( - pkg_resources.resource_string( - 'wcwidth', "version.json" - ).decode('utf8'))['tables'] - - @lru_cache(maxsize=128) def _wcversion_value(ver_string): """ @@ -395,16 +373,3 @@ def _wcmatch_version(given_version): if cmp_next_version > cmp_given: return unicode_version assert False, ("Code path unreachable", given_version, unicode_versions) - - -def _get_package_version(): - """ - Version of wcwidth (produces module-level ``__version__`` val). - - :rtype: str - :return: the version of the wcwidth library package. - """ - return json.loads( - pkg_resources.resource_string( - 'wcwidth', "version.json" - ).decode('utf8'))['package']