From 8a3997cd0eaee84a0c1fa40f5d89ac2104a8eb1d Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 2 May 2024 15:09:06 -0700 Subject: [PATCH] fix for pip-show parser with multi-line fields starting with a blank line --- CHANGELOG | 3 +- docs/parsers/pip_show.md | 2 +- jc/parsers/pip_show.py | 4 +- man/jc.1 | 2 +- ...ip-show-multiline-license-first-blank.json | 1 + ...pip-show-multiline-license-first-blank.out | 99 +++++++++++++++++++ tests/test_pip_show.py | 12 +++ 7 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/generic/pip-show-multiline-license-first-blank.json create mode 100644 tests/fixtures/generic/pip-show-multiline-license-first-blank.out diff --git a/CHANGELOG b/CHANGELOG index e08dafdd..6414fd3f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,8 +1,9 @@ jc changelog -20240429 v1.25.3 +20240502 v1.25.3 - Add `battery_percentage` field to `bluetoothctl` parser output - Fix `pci-ids` parser to correctly handle multiple subdevices +- Fix `pip-show` parser to handle multi-line fields with a beginning blank line - Fix `top` parsers to quiet uptime info parsing - Fix `traceroute` parser to correctly handle hops with multiple IPs - Optimize some tests by removing timezone settings and using quiet=True diff --git a/docs/parsers/pip_show.md b/docs/parsers/pip_show.md index 4900f9a0..9da0d7ed 100644 --- a/docs/parsers/pip_show.md +++ b/docs/parsers/pip_show.md @@ -90,4 +90,4 @@ Compatibility: linux, darwin, cygwin, win32, aix, freebsd Source: [`jc/parsers/pip_show.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/pip_show.py) -Version 1.4 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.5 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/parsers/pip_show.py b/jc/parsers/pip_show.py index c3d9d9e8..4ce9a7a7 100644 --- a/jc/parsers/pip_show.py +++ b/jc/parsers/pip_show.py @@ -66,7 +66,7 @@ import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.4' + version = '1.5' description = '`pip show` command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -144,6 +144,8 @@ def parse( item_value = None if last_key_data and last_key != item_key: + if not isinstance(package[last_key], str): + package[last_key] = '' package[last_key] = package[last_key] + '\n' + '\n'.join(last_key_data) last_key_data = [] diff --git a/man/jc.1 b/man/jc.1 index 8aa09b78..35640eca 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2024-04-29 1.25.3 "JSON Convert" +.TH jc 1 2024-05-02 1.25.3 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings diff --git a/tests/fixtures/generic/pip-show-multiline-license-first-blank.json b/tests/fixtures/generic/pip-show-multiline-license-first-blank.json new file mode 100644 index 00000000..748dcc06 --- /dev/null +++ b/tests/fixtures/generic/pip-show-multiline-license-first-blank.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":"\nCopyright (c) 2014, Nucleic\nAll 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"},{"name":"cppy","version":"1.2.1","summary":"UNKNOWN","home_page":"https://github.com/nucleic/cppy","author":"None","author_email":"The Nucleic Development Team ","license":"\n\nCopyright (c) 2014, Nucleic\nAll 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}] diff --git a/tests/fixtures/generic/pip-show-multiline-license-first-blank.out b/tests/fixtures/generic/pip-show-multiline-license-first-blank.out new file mode 100644 index 00000000..c7039e12 --- /dev/null +++ b/tests/fixtures/generic/pip-show-multiline-license-first-blank.out @@ -0,0 +1,99 @@ +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 +--- +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: diff --git a/tests/test_pip_show.py b/tests/test_pip_show.py index 9e69976e..f5607f2b 100644 --- a/tests/test_pip_show.py +++ b/tests/test_pip_show.py @@ -24,6 +24,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/pip-show-multiline-license.out'), 'r', encoding='utf-8') as f: generic_pip_show_multiline_license = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/pip-show-multiline-license-first-blank.out'), 'r', encoding='utf-8') as f: + generic_pip_show_multiline_license_first_blank = 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: centos_7_7_pip_show_json = json.loads(f.read()) @@ -40,6 +43,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/pip-show-multiline-license.json'), 'r', encoding='utf-8') as f: generic_pip_show_multiline_license_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/pip-show-multiline-license-first-blank.json'), 'r', encoding='utf-8') as f: + generic_pip_show_multiline_license_first_blank_json = json.loads(f.read()) + def test_pip_show_nodata(self): """ @@ -77,6 +83,12 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.pip_show.parse(self.generic_pip_show_multiline_license, quiet=True), self.generic_pip_show_multiline_license_json) + def test_pip_show_multiline_license_first_blank(self): + """ + Test 'pip show' with a multiline license where the first line is blank + """ + self.assertEqual(jc.parsers.pip_show.parse(self.generic_pip_show_multiline_license_first_blank, quiet=True), self.generic_pip_show_multiline_license_first_blank_json) + if __name__ == '__main__': unittest.main()