# -*- coding: utf-8 -*-
from setuphelpers import *
import json
import re
import os
import sys
r"""
Usable WAPT package functions: install(), uninstall(), session_setup(), audit(), update_package()
Configuration procedure: https://supunkavinda.blog/vscode-editing-settings-json
File -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json"
Or in these paths in your OS :
Windows : %APPDATA%\Code\User\settings.json
macOS : $HOME/Library/Application Support/Code/User/settings.json
Linux : $HOME/.config/Code/User/settings.json
https://code.visualstudio.com/docs/editor/extension-marketplace#_workspace-recommended-extensions
"""
def install():
pass
def session_setup():
print("Applying: VSCode configuration")
# Initializing variables
if get_os_name() == "Windows":
user_conf_dir = makepath(user_appdata, "Code", "User")
elif get_os_name() == "Linux":
user_conf_dir = makepath(user_home_directory(), ".config", "Code", "User")
elif get_os_name() == "Darwin":
user_conf_dir = makepath(user_home_directory(), "Library", "Application Support", "Code", "User")
user_conf_file = makepath(user_conf_dir, "settings.json")
user_conf_content = r"""{
"update.mode": "none",
"update.enableWindowsBackgroundUpdates": false,
"update.showReleaseNotes": false,
"powershell.promptToUpdatePowerShell": false,
"extensions.autoCheckUpdates": false,
"extensions.autoUpdate": false,
"extensions.ignoreRecommendations": true,
"telemetry.telemetryLevel": "off",
"redhat.telemetry.enabled": false,
"npm.fetchOnlinePackageInfo": false,
"json.schemaDownload.enable": false,
"editor.wordWrap": "on",
"editor.minimap.enabled": false,
"workbench.activityBar.visible": true,
"workbench.startupEditor": "none",
"restructuredtext.preview.scrollEditorWithPreview": false,
"restructuredtext.preview.scrollPreviewWithEditor": false,
"markdown.preview.scrollEditorWithPreview": false,
"markdown.preview.scrollPreviewWithEditor": false,
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--line-length",
"150"
],
"python.formatting.blackPath": "{black_path}",
"python.languageServer": "Jedi",
"git.openRepositoryInParentFolders": "always",
"git.autofetch": true,
"files.associations": {
"control": "yaml"
}
}"""
black_path = makepath(sys.executable.rsplit(os.path.sep, 2)[0], "libdev", "site-packages", "bin", "black")
# user_conf_content = user_conf_content.replace("{black_path}", black_path.replace("\\", "\\\\"))
user_conf_content = user_conf_content.replace("{black_path}", black_path.replace("\\", "/"))
# For now black stay in lib and not libdev
user_conf_data = json.loads(user_conf_content)
del user_conf_data["python.formatting.blackPath"]
if not isdir(user_conf_dir):
mkdirs(user_conf_dir)
if not isfile(user_conf_file):
print("Creating: %s" % user_conf_file)
json_write_file(user_conf_file, user_conf_data)
else:
new_user_conf_data = json_load_file(user_conf_file)
new_user_conf_data.update(user_conf_data)
if new_user_conf_data.get("python.formatting.blackPath"):
del new_user_conf_data["python.formatting.blackPath"]
print("Updating: %s" % user_conf_file)
json_write_file(user_conf_file, new_user_conf_data)
def json_load_file(json_file, encoding="utf-8"):
r"""Get content of a json file
Args:
path (str): path to the file
Returns:
dictionary (dict)
"""
with open(json_file, encoding=encoding) as read_file:
return json.load(read_file, cls=TrailingCommaJSONDecoder)
class TrailingCommaJSONDecoder(json.JSONDecoder):
def decode(self, s, *args, **kwargs):
# Remove trailing commas before decoding
s = self._remove_trailing_commas(s)
return super().decode(s, *args, **kwargs)
def _remove_trailing_commas(self, s):
# A simple implementation to remove trailing commas
# This might not handle all edge cases, but it works for most common cases
return s.replace(",}", "}").replace(",]", "]")