From c7684dc94df9a604dc16be0119ee707c83349d78 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 15 May 2022 22:06:43 -0700 Subject: [PATCH] allow multiline fields in pip-show --- jc/parsers/pip_show.py | 54 +++++++++++----- .../generic/pip-show-multiline-license.json | 1 + .../generic/pip-show-multiline-license.out | 64 +++++++++++++++++++ tests/test_pip_show.py | 12 ++++ 4 files changed, 116 insertions(+), 15 deletions(-) create mode 100644 tests/fixtures/generic/pip-show-multiline-license.json create mode 100644 tests/fixtures/generic/pip-show-multiline-license.out diff --git a/jc/parsers/pip_show.py b/jc/parsers/pip_show.py index 5d2cdb37..e488292e 100644 --- a/jc/parsers/pip_show.py +++ b/jc/parsers/pip_show.py @@ -60,12 +60,13 @@ Examples: } ] """ +from typing import List, Dict, Optional import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.3' + version = '1.4' description = '`pip show` command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -76,7 +77,7 @@ class info(): __version__ = info.version -def _process(proc_data): +def _process(proc_data: List[Dict]) -> List[Dict]: """ Final processing to conform to the schema. @@ -92,7 +93,11 @@ def _process(proc_data): return proc_data -def parse(data, raw=False, quiet=False): +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> List[Dict]: """ Main text parsing function @@ -109,8 +114,10 @@ def parse(data, raw=False, quiet=False): jc.utils.compatibility(__name__, info.compatible, quiet) jc.utils.input_type_check(data) - raw_output = [] - package = {} + raw_output: List = [] + package: Dict = {} + last_key: str = '' + last_key_data: List = [] # Clear any blank lines cleandata = list(filter(None, data.splitlines())) @@ -119,21 +126,38 @@ def parse(data, raw=False, quiet=False): for row in cleandata: if row.startswith('---'): + if last_key_data: + package[last_key] = '\n'.join(last_key_data) + raw_output.append(package) package = {} + last_key = '' + last_key_data = [] continue - item_key = row.split(': ', maxsplit=1)[0].lower().replace('-', '_') - item_value = row.split(': ', maxsplit=1)[1] + if not row.startswith(' '): + item_key = row.split(': ', maxsplit=1)[0].lower().replace('-', '_') + item_value: Optional[str] = row.split(': ', maxsplit=1)[1] - if item_value == '': - item_value = None + if item_value == '': + item_value = None - package.update({item_key: item_value}) + if last_key_data and last_key != item_key: + package[last_key] = '\n'.join(last_key_data) + last_key_data = [] - raw_output.append(package) + package[item_key] = item_value + last_key = item_key + continue - if raw: - return raw_output - else: - return _process(raw_output) + if row.startswith(' '): + last_key_data.append(row.strip()) + continue + + if package: + if last_key_data: + package[last_key] = '\n'.join(last_key_data) + + raw_output.append(package) + + return raw_output if raw else _process(raw_output) diff --git a/tests/fixtures/generic/pip-show-multiline-license.json b/tests/fixtures/generic/pip-show-multiline-license.json new file mode 100644 index 00000000..6f819186 --- /dev/null +++ b/tests/fixtures/generic/pip-show-multiline-license.json @@ -0,0 +1 @@ +[{"name":"setuptools","version":"39.2.0","summary":"Easily download, build, install, upgrade, and uninstall Python packages","home_page":"https://github.com/pypa/setuptools","author":"Python Packaging Authority","author_email":"distutils-sig@python.org","license":"UNKNOWN","location":"/usr/lib/python3.6/site-packages","requires":null,"required_by":"twine"},{"name":"pkginfo","version":"1.5.0.1","summary":"Query metadatdata from sdists / bdists / installed packages.","home_page":"https://code.launchpad.net/~tseaver/pkginfo/trunk","author":"Tres Seaver, Agendaless Consulting","author_email":"tseaver@agendaless.com","license":"MIT","location":"/home/kbrazil/.local/lib/python3.6/site-packages","requires":null,"required_by":"twine"},{"name":"cppy","version":"1.2.1","summary":"UNKNOWN","home_page":"https://github.com/nucleic/cppy","author":"None","author_email":"The Nucleic Development Team ","license":"All rights reserved.\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n* Redistributions of source code must retain the above copyright notice, this\nlist of conditions and the following disclaimer.\n* Redistributions in binary form must reproduce the above copyright notice,\nthis list of conditions and the following disclaimer in the documentation\nand/or other materials provided with the distribution.\n* Neither the name of the copyright holder nor the names of its\ncontributors may be used to endorse or promote products derived from\nthis software without specific prior written permission.\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.","location":"/usr/home/bryder/.local/lib/python3.7/site-packages","requires":null,"required_by":null},{"name":"six","version":"1.12.0","summary":"Python 2 and 3 compatibility utilities","home_page":"https://github.com/benjaminp/six","author":"Benjamin Peterson","author_email":"benjamin@python.org","license":"MIT","location":"/home/kbrazil/.local/lib/python3.6/site-packages","requires":null,"required_by":"readme-renderer, bleach"}] diff --git a/tests/fixtures/generic/pip-show-multiline-license.out b/tests/fixtures/generic/pip-show-multiline-license.out new file mode 100644 index 00000000..7b25f51c --- /dev/null +++ b/tests/fixtures/generic/pip-show-multiline-license.out @@ -0,0 +1,64 @@ +Name: setuptools +Version: 39.2.0 +Summary: Easily download, build, install, upgrade, and uninstall Python packages +Home-page: https://github.com/pypa/setuptools +Author: Python Packaging Authority +Author-email: distutils-sig@python.org +License: UNKNOWN +Location: /usr/lib/python3.6/site-packages +Requires: +Required-by: twine +--- +Name: pkginfo +Version: 1.5.0.1 +Summary: Query metadatdata from sdists / bdists / installed packages. +Home-page: https://code.launchpad.net/~tseaver/pkginfo/trunk +Author: Tres Seaver, Agendaless Consulting +Author-email: tseaver@agendaless.com +License: MIT +Location: /home/kbrazil/.local/lib/python3.6/site-packages +Requires: +Required-by: twine +--- +Name: cppy +Version: 1.2.1 +Summary: UNKNOWN +Home-page: https://github.com/nucleic/cppy +Author: None +Author-email: The Nucleic Development Team +License: Copyright (c) 2014, Nucleic + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +Location: /usr/home/bryder/.local/lib/python3.7/site-packages +Requires: +Required-by: +--- +Name: six +Version: 1.12.0 +Summary: Python 2 and 3 compatibility utilities +Home-page: https://github.com/benjaminp/six +Author: Benjamin Peterson +Author-email: benjamin@python.org +License: MIT +Location: /home/kbrazil/.local/lib/python3.6/site-packages +Requires: +Required-by: readme-renderer, bleach diff --git a/tests/test_pip_show.py b/tests/test_pip_show.py index 11872ffc..1923980b 100644 --- a/tests/test_pip_show.py +++ b/tests/test_pip_show.py @@ -22,6 +22,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/pip-show.out'), 'r', encoding='utf-8') as f: self.osx_10_14_6_pip_show = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/pip-show-multiline-license.out'), 'r', encoding='utf-8') as f: + self.generic_pip_show_multiline_license = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/pip-show.json'), 'r', encoding='utf-8') as f: self.centos_7_7_pip_show_json = json.loads(f.read()) @@ -35,6 +38,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/pip-show.json'), 'r', encoding='utf-8') as f: self.osx_10_14_6_pip_show_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/pip-show-multiline-license.json'), 'r', encoding='utf-8') as f: + self.generic_pip_show_multiline_license_json = json.loads(f.read()) + def test_pip_show_nodata(self): """ Test 'pip show' with no data @@ -65,6 +71,12 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.pip_show.parse(self.osx_10_14_6_pip_show, quiet=True), self.osx_10_14_6_pip_show_json) + def test_pip_show_multiline_license(self): + """ + Test 'pip show' with a multiline license + """ + self.assertEqual(jc.parsers.pip_show.parse(self.generic_pip_show_multiline_license, quiet=True), self.generic_pip_show_multiline_license_json) + if __name__ == '__main__': unittest.main()