Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jc --pip-show does not handle -f option #642

Open
oliverkurth opened this issue Feb 19, 2025 · 2 comments
Open

jc --pip-show does not handle -f option #642

oliverkurth opened this issue Feb 19, 2025 · 2 comments

Comments

@oliverkurth
Copy link

pip show supports the -f option to show a list of installed files, see https://pip.pypa.io/en/stable/cli/pip_show/#pip-show , example:

$ pip3 show jc -f
Name: jc
Version: 1.25.4
Summary: Converts the output of popular command-line tools and file-types to JSON.
Home-page: https://github.com/kellyjonbrazil/jc
Author: Kelly Brazil
Author-email: [email protected]
License: MIT
Location: /home/pi/.local/lib/python3.11/site-packages
Requires: Pygments, ruamel.yaml, xmltodict
Required-by: pypiwifi
Files:
  ../../../bin/jc
  jc-1.25.4.dist-info/INSTALLER
  jc-1.25.4.dist-info/LICENSE.md
  jc-1.25.4.dist-info/METADATA
  jc-1.25.4.dist-info/RECORD
...

jc does not like it:

pi@photon-ed60ce58636e [ ~ ]$ pip3 show jc | jc --pip-show
[{"name":"jc","version":"1.25.4","summary":"Converts the output of popular command-line tools and file-types to JSON.","home_page":"https://github.com/kellyjonbrazil/jc","author":"Kelly Brazil","author_email":"[email protected]","license":"MIT","location":"/home/pi/.local/lib/python3.11/site-packages","requires":"Pygments, ruamel.yaml, xmltodict","required_by":"pypiwifi"}]
pi@photon-ed60ce58636e [ ~ ]$ pip3 show jc -f | jc --pip-show
jc:  Error - pip-show parser could not parse the input data.
             If this is the correct parser, try setting the locale to C (LC_ALL=C).
             For details use the -d or -dd option. Use "jc -h --pip-show" for help.
pi@photon-ed60ce58636e [ ~ ]$ 

It would be great if this was supported.

@oliverkurth
Copy link
Author

Looks like 'jc does not deal well with multi lines if there is no content after the field name. This works:

pi@photon-ed60ce58636e [ ~ ]$ cat | jc --pip-show
Name: jc
Version: 1.25.4
Summary: Converts the output of popular command-line tools and file-types to JSON.
Home-page: https://github.com/kellyjonbrazil/jc
Author: Kelly Brazil
Author-email: [email protected]
License: MIT
Location: /home/pi/.local/lib/python3.11/site-packages
Requires: Pygments, ruamel.yaml, xmltodict
Required-by: pypiwifi
Files: bla
 blabla
[{"name":"jc","version":"1.25.4","summary":"Converts the output of popular command-line tools and file-types to JSON.","home_page":"https://github.com/kellyjonbrazil/jc","author":"Kelly Brazil","author_email":"[email protected]","license":"MIT","location":"/home/pi/.local/lib/python3.11/site-packages","requires":"Pygments, ruamel.yaml, xmltodict","required_by":"pypiwifi","files":"bla\nblabla"}]

This does not:

pi@photon-ed60ce58636e [ ~ ]$ cat | jc --pip-show
Name: jc
Version: 1.25.4
Summary: Converts the output of popular command-line tools and file-types to JSON.
Home-page: https://github.com/kellyjonbrazil/jc
Author: Kelly Brazil
Author-email: [email protected]
License: MIT
Location: /home/pi/.local/lib/python3.11/site-packages
Requires: Pygments, ruamel.yaml, xmltodict
Required-by: pypiwifi
Files: 
 bla
Foo: bar
jc:  Error - pip-show parser could not parse the input data.
             If this is the correct parser, try setting the locale to C (LC_ALL=C).
             For details use the -d or -dd option. Use "jc -h --pip-show" for help.

@oliverkurth
Copy link
Author

Here is a fix - it can probably be improved:

okurth@ubuntu-server:~/source/jc (master)$ git diff
diff --git a/jc/parsers/pip_show.py b/jc/parsers/pip_show.py
index 4ce9a7a7..a2ffff13 100644
--- a/jc/parsers/pip_show.py
+++ b/jc/parsers/pip_show.py
@@ -138,7 +138,7 @@ def parse(
 
             if not row.startswith(' '):
                 item_key = row.split(': ', maxsplit=1)[0].lower().replace('-', '_')
-                item_value: Optional[str] = row.split(': ', maxsplit=1)[1]
+                item_value: Optional[str] = row.split(':', maxsplit=1)[1].lstrip()
 
                 if item_value == '':
                     item_value = None
@@ -159,7 +159,10 @@ def parse(
 
         if package:
             if last_key_data:
-                package[last_key] = package[last_key] + '\n' + '\n'.join(last_key_data)
+                if package[last_key] is not None:
+                    package[last_key] = package[last_key] + '\n' + '\n'.join(last_key_data)
+                else:
+                    package[last_key] = '\n'.join(last_key_data)
 
             raw_output.append(package)

Verified tests pass:

okurth@ubuntu-server:~/source/jc (master)$ pytest ./tests/test_pip_show.py 
================================================================================================================= test session starts ==================================================================================================================
platform linux -- Python 3.12.3, pytest-7.4.4, pluggy-1.4.0
rootdir: /home/okurth/source/jc
collected 7 items                                                                                                                                                                                                                                      

tests/test_pip_show.py .......                                                                                                                                                                                                                   [100%]

================================================================================================================== 7 passed in 0.01s ===================================================================================================================

Verified it fixes the issue:

okurth@ubuntu-server:~/source/jc (master)$ cat | python -m jc --pip-show
Name: jc
Version: 1.25.4
Summary: Converts the output of popular command-line tools and file-types to JSON.
Home-page: https://github.com/kellyjonbrazil/jc
Author: Kelly Brazil
Author-email: [email protected]
License: MIT
Location: /home/pi/.local/lib/python3.11/site-packages
Requires: Pygments, ruamel.yaml, xmltodict
Required-by: pypiwifi
Files:
 bla
 blabla
[{"name":"jc","version":"1.25.4","summary":"Converts the output of popular command-line tools and file-types to JSON.","home_page":"https://github.com/kellyjonbrazil/jc","author":"Kelly Brazil","author_email":"[email protected]","license":"MIT","location":"/home/pi/.local/lib/python3.11/site-packages","requires":"Pygments, ruamel.yaml, xmltodict","required_by":"pypiwifi","files:":"bla\nblabla"}]

I'd prefer if Files: were parsed into a list, but with this approach it wouldn't work with fields where you'd expect a long text, like for the License:.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant