Skip to content

Instantly share code, notes, and snippets.

@imcomking
Last active June 15, 2023 13:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save imcomking/04d8ce30b6ac444e413392ab675b1d2c to your computer and use it in GitHub Desktop.
Save imcomking/04d8ce30b6ac444e413392ab675b1d2c to your computer and use it in GitHub Desktop.
It is partial black in python to be utilized on Windows PyCharm.
"""
It is partial black in python to be utilized in windows PyCharm.
- To see partial black here (https://blog.godatadriven.com/black-formatting-selection)
- This code is conversion of this shell script (https://gist.github.com/BasPH/5e665273d5e4cb8a8eefb6f9d43b0b6d)
If you want to use in windows, you should first complie this python to exe.
- To do that, see (https://pypi.org/project/auto-py-to-exe/)
"""
import os
import sys
import tempfile
print("The arguments are: ", str(sys.argv))
# get the system argv
_partial_black = sys.argv[0]
black = sys.argv[1]
input_file = sys.argv[2]
start_line = int(sys.argv[3])-1
end_line = int(sys.argv[4])
# read input_file
with open(input_file, "rt", encoding="utf-8") as src_file:
src_contents = [line for line in src_file]
selection = src_contents[start_line:end_line]
print("Total file len: ", len(src_contents), "selected lines:", len(selection))
# it is a workaround for escaping windows os permission problem.
tmp_dir = tempfile.TemporaryDirectory()
tmp_file_name = os.path.join(tmp_dir.name, 'tmp_file_for_black')
# write selection on tmp_file
with open(tmp_file_name, "wt", encoding="utf-8") as f:
f.writelines(selection)
# run black on tmp_file
cmd = black+" "+tmp_file_name
print("Run cmd:", cmd)
os.system(cmd)
# apply reformatted selection to origianl source file
with open(tmp_file_name, "rt", encoding="utf-8") as f:
del src_contents[start_line:end_line]
for i, line in enumerate(f):
src_contents.insert(start_line+i, line)
# overwrite it to input_file
with open(input_file, "wt", encoding="utf-8") as f:
f.writelines(src_contents)
@imcomking
Copy link
Author

imcomking commented Feb 4, 2020

It is exe.file which is a complied version of this python code by https://pypi.org/project/auto-py-to-exe/

https://www.dropbox.com/s/eacz7kf28l7q6sr/output.zip?dl=0

@imcomking
Copy link
Author

imcomking commented May 7, 2020

Step by Step

1. Install PyCharm and Black.

If you are Windows 10 Users, you need to install the Black on Anaconda3 as the pip program installed on WSL is not executable by Windows OS.
Then you can run "black test.py" from your Anaconda3 Powershell Prompt. Also you can use this command "pip show black" to check the absolute path of the black.

2. Download the python program above compiled as exe file to be executed by Windows OS.

https://www.dropbox.com/s/eacz7kf28l7q6sr/output.zip?dl=0
Here, you can see "partial_black.exe"
If you don't trust the exe file, you can compile it manually using auto-py-to-exe using python code here.

3. Register external tool on PyCharm.

File-Setting-Tools-External Tools-Add(+button)
Then fill out the form as follows:

Name: partial Black
Description: It is partial formatting version of Black.
Program: <The absolute path of partial_black.exe>
Arguments: <The absolute path of black.exe> "$FilePath$"  $SelectionStartLine$  $SelectionEndLine$
Working directory: <The absolute pah of Anaconda3 Scripts>

In my case the paths are:
C:\Users\User\Dropbox\partial_black\output\partial_black\partial_black.exe
C:\ProgramData\Anaconda3\Scripts\black.exe
C:\ProgramData\Anaconda3\Scripts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment