-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnvSwitcher.py
executable file
·124 lines (100 loc) · 3.8 KB
/
EnvSwitcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""
Sublime Text plugin to switch between different pre defined user envionments.
"""
import os
import sublime
import sublime_plugin
DEBUG = False
ENV_GROUP = "envs"
SYSTEM_ENVS = dict()
SETTINGS_FILENAME = "EnvSwitcher.sublime-settings"
def trace(message):
"""
print a debug messgae
"""
if DEBUG:
print(message)
def plugin_loaded():
"""
Load the plugin
"""
settings = sublime.load_settings(SETTINGS_FILENAME)
global DEBUG
DEBUG = settings.get("debug")
# store the system environment at startup
EnvSwitcher.store_system_environment(settings)
# load the default environment at startup
EnvSwitcher.load_default_env(settings)
def plugin_unloaded():
"""
Unload the plugin
"""
EnvSwitcher.reset_system_environment()
class EnvSwitcher(sublime_plugin.WindowCommand):
def run(self):
trace("run environment switcher")
names = self.get_environments()
sublime.active_window().show_quick_panel(names, self.on_set_env)
@staticmethod
def reset_system_environment():
trace("overwrite the current envs with the default values")
for key in SYSTEM_ENVS:
os.environ[key] = SYSTEM_ENVS[key]
trace("reset {}: {}".format(key, os.environ[key]))
@staticmethod
def store_system_environment(settings):
trace("store the system defined environments")
if settings is None:
settings = sublime.load_settings(SETTINGS_FILENAME)
if settings.has(ENV_GROUP):
envs = settings.get(ENV_GROUP)
lis = list(envs.values())
for item in lis:
for key in item:
if key not in SYSTEM_ENVS.keys():
if key in os.environ.keys():
SYSTEM_ENVS[key] = os.environ[key]
trace("stored {}: {}".format(key, os.environ[key]))
else:
SYSTEM_ENVS[key] = ""
@staticmethod
def load_default_env(settings):
trace("load default environment")
if settings is None:
settings = sublime.load_settings(SETTINGS_FILENAME)
default_env = settings.get("default_env")
envs = settings.get(ENV_GROUP)
if default_env in envs.keys():
EnvSwitcher.on_set_env(None, list(envs.keys()).index(default_env))
else:
msg = "EnvSwitcher:\nCould not find default environment: '{}'".format(default_env)
sublime.message_dialog(msg)
def get_environments(self):
trace("get all defined environments")
settings = sublime.load_settings(SETTINGS_FILENAME)
ret = list()
envs = settings.get(ENV_GROUP)
if envs:
ret = list(envs.keys())
else:
trace("None envs")
return ret
def on_set_env(self, item):
trace("set the indexed environment")
if -1 == item:
trace("cancel ...")
else:
# reset to th default values
EnvSwitcher.reset_system_environment()
# load the settings to set the new defined Values
settings = sublime.load_settings(SETTINGS_FILENAME)
envs = settings.get(ENV_GROUP)
env_name = list(envs.keys())[item]
lis = list(envs.values())
sublime.status_message("Env: '{}'".format(env_name))
for key, value in lis[item].items():
if ("%{}%".format(key) in value) and (key in SYSTEM_ENVS):
value = value.replace("%{}%".format(key), SYSTEM_ENVS[key])
os.environ[key] = value
trace("{}: {}".format(key, os.environ[key]))