weixin_39682301
weixin_39682301
2020-12-01 21:31

Invoking tool from command line for VSCode Extension

Hello,

I'm in the process of writing a VSCode extension that adds support for reorder-python-imports. As part of this process, I need to be able to provide a file that may not yet have been saved to your tool, so that it can sort its imports. Ideally, I would like to provide the text of the file via stdin, and get the file output via stdout. However, to get this to work, I have the following questions:

  1. Is reorder-python-imports /dev/stdin still the recommended method of reading from stdin? ~It might be better if this was reorder-python-imports -- instead to indicate that the file to be read will be from stdin, as that is a more platform agnostic way of doing things.~
  2. --print-only is marked as deprecated, but I don't see another way of getting the output of the file that is easy to parse for a command line tool (writing a regex to parse the difflib output seems tedious). Is there a better way that I'm not aware of to solve this, or is there any chance that option could be un-deprecated?
  3. My understanding is that reorder-python-imports uses static analysis to determine which imports are third party and which are first party. Will providing the file via stdin to this tool mess that up? What impact does the current working directory have on this static analysis?

Looking forward to getting this tool better supported in my favorite IDE! I strongly prefer your approach over isort :)

该提问来源于开源项目:asottile/reorder_python_imports

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • weixin_39682301 weixin_39682301 5月前

    EDIT: I didn't realize that providing - as an argument works already - when I first posted this issue, I tried -- thinking that that was the standard way of indicating the file was stdin.

    Now I see you're already several steps ahead of me - the tool prints to stdout and reads from stdin when - is provided, enabling the following:

    
    cat my_file.py | reorder-python-imports -
    
    点赞 评论 复制链接分享
  • weixin_39955149 weixin_39955149 5月前

    for (1.) and (2.) reorder-python-imports - does exactly what you want (reads from stdin, writes to stdout)

    for 3. the filename itself shouldn't matter unless the script is importing from itself (but that seems super rare???). the working directory is pretty sensitive as that's used to find the local modules -- there's also the --application-directories option which is commonly used when using a src layout (for example)

    hope that helps! some other people were looking for a vs code extension in #117 so maybe once it's completed you can resolve that one as well!

    点赞 评论 复制链接分享
  • weixin_39682301 weixin_39682301 5月前

    Thanks! Any chance you could add a quick line in the readme about reorder-python-imports -?

    点赞 评论 复制链接分享
  • weixin_39955149 weixin_39955149 5月前

    sure, it can probably also be added to the help string for filenames and the deprecation message for --print-only -- would you be interested in sending a patch for those three things?

    点赞 评论 复制链接分享
  • weixin_39682301 weixin_39682301 5月前

    Sure

    点赞 评论 复制链接分享
  • weixin_39682301 weixin_39682301 5月前

    By the way, the first working version is available at this location: https://github.com/TheButlah/vscode-reorder-python-imports

    There's still a few issues to iron out, namely figuring out how to avoid isort from microsoft's python extension overwriting reorder-python-imports changes on filesaves, but there ya go.

    点赞 评论 复制链接分享

相关推荐