Skip to content

Instantly share code, notes, and snippets.

@shuoli84
Created February 29, 2020 04:34
Show Gist options
  • Save shuoli84/93fead0a634d711dc25494acd127c180 to your computer and use it in GitHub Desktop.
Save shuoli84/93fead0a634d711dc25494acd127c180 to your computer and use it in GitHub Desktop.
A sample script to add color customization to vscode setting to wash color for python files.
"""
A sample script to add color customization to vscode setting to wash color for python files.
It loads scopes from python magic.
"""
import json
from pathlib import Path
from jsoncomment import JsonComment
import requests
def main():
user_setting_path = Path("~/Library/Application Support/Code/User/settings.json").expanduser().absolute()
if not user_setting_path.exists():
print(f"Not able to find {user_setting_path}, ensure it exists")
return
print(f"Lets wash out color for python elements")
scopes_url = "https://raw.githubusercontent.com/MagicStack/MagicPython/master/misc/scopes"
print(f"loading scopes from {scopes_url}")
resp = requests.get(scopes_url)
resp.raise_for_status()
scopes_text = resp.text
print(f"loaded: {scopes_text}")
# lets load user settings and find the place holder
editor_color_cust_key = "editor.tokenColorCustomizations"
text_mate_rules_key = "textMateRules"
customize_name = "auto_gen_vscode_py_color"
user_setting_content = user_setting_path.read_text(encoding="utf8")
user_setting_obj = JsonComment().loads(user_setting_content)
if editor_color_cust_key not in user_setting_obj:
user_setting_obj[editor_color_cust_key] = {}
editor_color_cust_obj = user_setting_obj[editor_color_cust_key]
if text_mate_rules_key not in editor_color_cust_obj:
editor_color_cust_obj[text_mate_rules_key] = []
text_mate_rules = editor_color_cust_obj[text_mate_rules_key]
# first strip prev generated keys
text_mate_rules = [rule for rule in text_mate_rules if rule.get("name") != customize_name]
# then generate new rules
for scope in [line.strip() for line in scopes_text.split("\n") if line.strip()]:
scope_rule = {
"name": customize_name,
"scope": scope,
"settings": {
"foreground": "#dbdbdb"
}
}
text_mate_rules.append(scope_rule)
# now save to usersettings
user_setting_obj[editor_color_cust_key][text_mate_rules_key] = text_mate_rules
new_json = json.dumps(user_setting_obj, indent=2)
user_setting_path.write_text(new_json)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment