|
3 | 3 |
|
4 | 4 | @author: hsorby |
5 | 5 | """ |
6 | | -import io |
7 | 6 | import json |
8 | 7 | import os |
9 | 8 | import sys |
|
16 | 15 | import types |
17 | 16 | import importlib |
18 | 17 |
|
19 | | -from contextlib import redirect_stdout |
20 | | - |
21 | | - |
22 | | -from mapclient.core.utils import which, FileTypeObject, grep, is_frozen, determine_step_name, determine_step_class_name |
| 18 | +from mapclient.application import get_app_path |
| 19 | +from mapclient.core.utils import which, FileTypeObject, grep, is_frozen, determine_step_name, determine_step_class_name, \ |
| 20 | + stable_hash |
23 | 21 | from mapclient.settings.definitions import VIRTUAL_ENV_PATH, \ |
24 | 22 | PLUGINS_PACKAGE_NAME, PLUGINS_PTH |
25 | 23 | from mapclient.core.checks import getPipExecutable |
26 | 24 |
|
27 | 25 | from importlib import import_module |
28 | 26 |
|
29 | | -from mapclient.settings.general import get_virtualenv_site_packages_directory |
| 27 | +from mapclient.settings.general import get_virtualenv_site_packages_directory, get_settings |
30 | 28 |
|
31 | 29 | logger = logging.getLogger(__name__) |
32 | 30 |
|
| 31 | +CONST_DEFAULT_PROFILE = 'Default' |
| 32 | + |
33 | 33 |
|
34 | 34 | def getVirtualEnvCandidates(): |
35 | 35 | """Return a list of strings which contains possible names |
36 | 36 | of the virtualenv program for this environment. |
37 | 37 | """ |
38 | | - if sys.version_info < (3, 0): |
39 | | - virtualenv_candidates = [which('virtualenv'), which('virtualenv2')] |
40 | | - else: |
41 | | - virtualenv_candidates = [which('virtualenv'), which('virtualenv3')] |
| 38 | + return [which('virtualenv'), which('virtualenv3')] |
| 39 | + |
42 | 40 |
|
43 | | - return virtualenv_candidates |
| 41 | +def _get_app_profile_key(): |
| 42 | + return f'current_profile_{stable_hash(get_app_path())}' |
44 | 43 |
|
45 | 44 |
|
46 | 45 | class PluginManager: |
47 | 46 |
|
48 | 47 | def __init__(self): |
49 | | - self._directories = [] |
| 48 | + self._current_profile = CONST_DEFAULT_PROFILE |
| 49 | + self._profile_directories = {} |
50 | 50 | self._virtualenv_enabled = True |
51 | 51 | self._virtualenv_dir = None |
52 | 52 | self._virtualenv_setup_attempted = False |
@@ -74,7 +74,13 @@ def getVirtualEnvDirectory(self): |
74 | 74 | return self._virtualenv_dir |
75 | 75 |
|
76 | 76 | def directories(self): |
77 | | - return self._directories |
| 77 | + return self._profile_directories.get(self._current_profile, []) |
| 78 | + |
| 79 | + def current_profile(self): |
| 80 | + return self._current_profile |
| 81 | + |
| 82 | + def set_current_profile(self, profile_name): |
| 83 | + self._current_profile = profile_name |
78 | 84 |
|
79 | 85 | def setReloadPlugins(self, state=True): |
80 | 86 | self._reload_plugins = state |
@@ -105,14 +111,15 @@ def list(self): |
105 | 111 | def setOptions(self, options): |
106 | 112 | self._virtualenv_dir = options[VIRTUAL_ENV_PATH] |
107 | 113 |
|
108 | | - def setDirectories(self, directories): |
| 114 | + def set_directories(self, directories): |
109 | 115 | """ |
110 | 116 | Set the list of directories to be searched for |
111 | 117 | plugins. Returns true if the directories listing |
112 | 118 | was updated and false otherwise. |
113 | 119 | """ |
114 | | - if self._directories != directories: |
115 | | - self._directories = directories |
| 120 | + current_directories = self._profile_directories.get(self._current_profile, []) |
| 121 | + if current_directories != directories: |
| 122 | + self._profile_directories[self._current_profile] = directories |
116 | 123 | self._reload_plugins = True |
117 | 124 |
|
118 | 125 | def virtualenvSetupAttempted(self): |
@@ -165,11 +172,6 @@ def setupVirtualEnv(self): |
165 | 172 | def getPluginDatabase(self): |
166 | 173 | return self._plugin_database |
167 | 174 |
|
168 | | - def allDirectories(self): |
169 | | - plugin_dirs = self._directories[:] |
170 | | - |
171 | | - return plugin_dirs |
172 | | - |
173 | 175 | def _addPluginDir(self, directory): |
174 | 176 | added = False |
175 | 177 | if isMapClientPluginsDir(directory): |
@@ -223,7 +225,7 @@ def load(self): |
223 | 225 |
|
224 | 226 | len_package_modules_prior = len(sys.modules[PLUGINS_PACKAGE_NAME].__path__) if PLUGINS_PACKAGE_NAME in sys.modules else 0 |
225 | 227 | new_plugin_directories = [] |
226 | | - for directory in self.allDirectories(): |
| 228 | + for directory in self.directories(): |
227 | 229 | if self._addPluginDir(directory): |
228 | 230 | new_plugin_directories.append(directory) |
229 | 231 | else: |
@@ -328,17 +330,38 @@ def getPluginErrors(self): |
328 | 330 | return {'ImportError': self._import_errors, 'TypeError': self._type_errors, 'SyntaxError': self._syntax_errors, 'TabError': self._tab_errors, |
329 | 331 | 'directories': self._plugin_error_directories} |
330 | 332 |
|
| 333 | + def set_profile_directories(self, profile_directories): |
| 334 | + """ |
| 335 | + Set the directories for the given profile name. |
| 336 | + """ |
| 337 | + self._profile_directories = profile_directories |
| 338 | + |
| 339 | + def profile_directories(self): |
| 340 | + """ |
| 341 | + Get the profile directories information. |
| 342 | + """ |
| 343 | + return self._profile_directories |
| 344 | + |
331 | 345 | def readSettings(self, settings): |
332 | | - self._directories = [] |
| 346 | + self._profile_directories = {} |
333 | 347 | settings.beginGroup('Plugins') |
334 | 348 | self._doNotShowPluginErrors = settings.value('donot_show_plugin_errors', 'true') == 'true' |
335 | 349 | self._virtualenv_setup_attempted = settings.value('virtualenv_setup_attempted', 'false') == 'true' |
336 | | - directory_count = settings.beginReadArray('directories') |
337 | | - for i in range(directory_count): |
| 350 | + self._current_profile = settings.value(_get_app_profile_key(), CONST_DEFAULT_PROFILE) |
| 351 | + profiles_count = settings.beginReadArray('profiles') |
| 352 | + for i in range(profiles_count): |
338 | 353 | settings.setArrayIndex(i) |
339 | | - self._directories.append(settings.value('directory')) |
| 354 | + profile_name = settings.value('name') |
| 355 | + directory_count = settings.beginReadArray('directories') |
| 356 | + directories = [] |
| 357 | + for j in range(directory_count): |
| 358 | + settings.setArrayIndex(j) |
| 359 | + directories.append(settings.value('directory')) |
| 360 | + settings.endArray() |
| 361 | + self._profile_directories[profile_name] = directories |
340 | 362 | settings.endArray() |
341 | 363 | settings.endGroup() |
| 364 | + self._profile_directories[CONST_DEFAULT_PROFILE] = self._profile_directories.get(CONST_DEFAULT_PROFILE, []) |
342 | 365 | settings.beginGroup('Ignored Plugins') |
343 | 366 | plugin_count = settings.beginReadArray('plugins') |
344 | 367 | for i in range(plugin_count): |
@@ -371,12 +394,18 @@ def writeSettings(self, settings): |
371 | 394 | settings.beginGroup('Plugins') |
372 | 395 | settings.setValue('donot_show_plugin_errors', self._doNotShowPluginErrors) |
373 | 396 | settings.setValue('virtualenv_setup_attempted', self._virtualenv_setup_attempted) |
374 | | - settings.beginWriteArray('directories') |
375 | | - directory_index = 0 |
376 | | - for directory in self._directories: |
377 | | - settings.setArrayIndex(directory_index) |
378 | | - settings.setValue('directory', directory) |
379 | | - directory_index += 1 |
| 397 | + settings.setValue(_get_app_profile_key(), self._current_profile) |
| 398 | + settings.beginWriteArray('profiles') |
| 399 | + profile_index = 0 |
| 400 | + for profile_name, profile_directories in self._profile_directories.items(): |
| 401 | + settings.setArrayIndex(profile_index) |
| 402 | + settings.setValue('name', profile_name) |
| 403 | + settings.beginWriteArray('directories') |
| 404 | + for directory_index, directory in enumerate(profile_directories): |
| 405 | + settings.setArrayIndex(directory_index) |
| 406 | + settings.setValue('directory', directory) |
| 407 | + settings.endArray() |
| 408 | + profile_index += 1 |
380 | 409 | settings.endArray() |
381 | 410 | settings.endGroup() |
382 | 411 | settings.beginGroup('Ignored Plugins') |
@@ -557,8 +586,8 @@ def checkForMissingPlugins(self, to_check): |
557 | 586 | """ |
558 | 587 | missing_plugins = {} |
559 | 588 | for plugin in to_check: |
560 | | - if not (plugin in self._database and \ |
561 | | - to_check[plugin]['author'] == self._database[plugin]['author'] and \ |
| 589 | + if not (plugin in self._database and |
| 590 | + to_check[plugin]['author'] == self._database[plugin]['author'] and |
562 | 591 | to_check[plugin]['version'] == self._database[plugin]['version']): |
563 | 592 | missing_plugins[plugin] = to_check[plugin] |
564 | 593 |
|
|
0 commit comments