Last active
July 22, 2020 13:52
-
-
Save abompard/4faf0afa1991aa2b29d56dc500e59633 to your computer and use it in GitHub Desktop.
A python script to setup VS Code with Poetry's virtualenv and some other defaults
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import json | |
from subprocess import run, PIPE | |
GLOBAL_SETTINGS_VALUES = { | |
"python.venvPath": "~/.cache/pypoetry/virtualenvs/", | |
"python.linting.flake8Enabled": True, | |
"python.linting.flake8Path": "flake8", | |
"python.linting.pylintEnabled": False, | |
"python.formatting.provider": "black", | |
"autoDocstring.docstringFormat": "google", | |
"files.trimTrailingWhitespace": True, | |
"files.insertFinalNewline": True, | |
"files.trimFinalNewlines": True, | |
"git.alwaysSignOff": True, | |
"python.venvFolders": ["~/.virtualenvs", "~/.cache/pypoetry/virtualenvs/"], | |
"editor.codeActionsOnSave": { | |
"source.organizeImports": True, | |
}, | |
} | |
def global_config(): | |
"""Setup the general VSCode configuration""" | |
settings_path = os.path.expanduser("~/.config/Code/User/settings.json") | |
with open(settings_path, "r") as f: | |
config = json.load(f) | |
config.update(GLOBAL_SETTINGS_VALUES) | |
with open(settings_path, "w") as f: | |
json.dump(config, f, indent=2) | |
print("Global configuration set.") | |
def local_config(): | |
"""Setup the local configuration""" | |
if not os.path.exists("pyproject.toml"): | |
print("This does not look like a Poetry project, skipping local config") | |
return | |
settings_path = ".vscode/settings.json" | |
try: | |
os.makedirs(os.path.dirname(settings_path)) | |
except OSError: | |
pass | |
python_path = run( | |
["poetry", "env", "info", "-p"], check=True, stdout=PIPE, text=True | |
).stdout.strip() | |
settings_value = f"{python_path}/bin/python" | |
if os.path.exists(settings_path): | |
with open(settings_path, "r") as f: | |
config = json.load(f) | |
if config.get("python.pythonPath") == settings_value: | |
print("Local configuration already set") | |
return | |
else: | |
config = {} | |
config["python.pythonPath"] = settings_value | |
with open(settings_path, "w") as f: | |
json.dump(config, f, indent=4) | |
print("Local configuration set.") | |
if __name__ == "__main__": | |
global_config() | |
local_config() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment