This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
7 changed files
with
269 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import json | ||
import logging | ||
import os | ||
|
||
from cate.conf import USER_PREFERENCES_FILE, DEFAULT_DATA_PATH | ||
|
||
_DEFAULT_USER_PREFS = { | ||
"reopenLastWorkspace": True, | ||
"lastWorkspacePath": "", | ||
"autoUpdateSoftware": True, | ||
"autoShowNewFigures": True, | ||
"offlineMode": False, | ||
"showSelectedVariableLayer": True, | ||
"savedLayers": {}, | ||
"selectedDataStoreId": "", | ||
"selectedDataSourceId": None, | ||
"dataSourceFilterExpr": "", | ||
"selectedOperationName": None, | ||
"operationFilterTags": [], | ||
"operationFilterExpr": "", | ||
"dataSourceListHeight": 200, | ||
"showDataSourceDetails": True, | ||
"resourceListHeight": 100, | ||
"showResourceDetails": True, | ||
"workflowStepListHeight": 100, | ||
"showWorkflowStepDetails": True, | ||
"operationListHeight": 200, | ||
"showOperationDetails": True, | ||
"variableListHeight": 200, | ||
"showVariableDetails": True, | ||
"layerListHeight": 160, | ||
"showLayerDetails": True, | ||
"panelContainerUndockedMode": False, | ||
"leftPanelContainerLayout": { | ||
"horPos": 300, | ||
"verPos": 400 | ||
}, | ||
"rightPanelContainerLayout": { | ||
"horPos": 300, | ||
"verPos": 400 | ||
}, | ||
"selectedLeftTopPanelId": "dataSources", | ||
"selectedLeftBottomPanelId": "operations", | ||
"selectedRightTopPanelId": "workspace", | ||
"selectedRightBottomPanelId": "variables", | ||
"placemarkCollection": { | ||
"type": "FeatureCollection", | ||
"features": [] | ||
}, | ||
"selectedPlacemarkId": None, | ||
"placemarkListHeight": 160, | ||
"showPlacemarkDetails": True, | ||
"defaultPlacemarkStyle": { | ||
"markerSize": "small", | ||
"markerColor": "#ff0000", | ||
"markerSymbol": "", | ||
"fill": "#0000ff", | ||
"fillOpacity": 0.5, | ||
"stroke": "#ffff00", | ||
"strokeOpacity": 0.5, | ||
"strokeWidth": 1 | ||
}, | ||
"workspacePanelMode": "steps", | ||
"showDataStoreDescription": False, | ||
"showDataStoreNotices": True, | ||
"showDataSourceIDs": True, | ||
"showLayerTextOverlay": True, | ||
"debugWorldView": False, | ||
"styleContext": "entity", | ||
"backendConfig": { | ||
"dataStoresPath": "~/.cate/data_stores", | ||
"useWorkspaceImageryCache": False, | ||
"resourceNamePattern": "res_{index}", | ||
"proxyUrl": None | ||
} | ||
} | ||
|
||
_LOG = logging.getLogger('cate') | ||
|
||
|
||
def _write_user_prefs_file(user_prefs_file: str, user_prefs: dict): | ||
try: | ||
with open(user_prefs_file, 'w') as fp: | ||
json.dump(user_prefs, fp) | ||
except Exception as error: | ||
_LOG.warning('failed writing %s: %s' % (user_prefs_file, str(error))) | ||
|
||
|
||
def _read_user_prefs(user_prefs_file: str) -> dict: | ||
if user_prefs_file and os.path.isfile(user_prefs_file): | ||
user_prefs = None | ||
try: | ||
with open(user_prefs_file, 'r') as pfile: | ||
user_prefs = json.load(pfile) | ||
except Exception as error: | ||
_LOG.warning('failed reading %s: %s' % (user_prefs_file, str(error))) | ||
|
||
if user_prefs is not None: | ||
return user_prefs | ||
else: | ||
_LOG.error("Could not load user prefs") | ||
|
||
|
||
def set_user_prefs(prefs: dict, user_prefs_file: str = None): | ||
if not user_prefs_file: | ||
user_prefs_file = os.path.join(DEFAULT_DATA_PATH, USER_PREFERENCES_FILE) | ||
|
||
if not os.path.isfile(user_prefs_file): | ||
_write_user_prefs_file(user_prefs_file, _DEFAULT_USER_PREFS) | ||
|
||
_write_user_prefs_file(user_prefs_file, prefs) | ||
|
||
return get_user_prefs() | ||
|
||
|
||
def get_user_prefs(user_prefs_file: str = None) -> dict: | ||
if not user_prefs_file: | ||
user_prefs_file = os.path.join(DEFAULT_DATA_PATH, USER_PREFERENCES_FILE) | ||
|
||
if not os.path.isfile(user_prefs_file): | ||
_write_user_prefs_file(user_prefs_file, _DEFAULT_USER_PREFS) | ||
|
||
return _read_user_prefs(user_prefs_file) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import os | ||
import tempfile | ||
import unittest | ||
|
||
from cate.conf.userprefs import get_user_prefs, set_user_prefs | ||
|
||
_TEST_USER_PREFS = { | ||
"reopenLastWorkspace": True, | ||
"lastWorkspacePath": "", | ||
"autoUpdateSoftware": True, | ||
"autoShowNewFigures": True, | ||
"offlineMode": False, | ||
"showSelectedVariableLayer": True, | ||
"savedLayers": {}, | ||
"selectedDataStoreId": "", | ||
"selectedDataSourceId": None, | ||
"dataSourceFilterExpr": "", | ||
"selectedOperationName": None, | ||
"operationFilterTags": [], | ||
"operationFilterExpr": "", | ||
"dataSourceListHeight": 200, | ||
"showDataSourceDetails": True, | ||
"resourceListHeight": 100, | ||
"showResourceDetails": True, | ||
"workflowStepListHeight": 100, | ||
"showWorkflowStepDetails": True, | ||
"operationListHeight": 200, | ||
"showOperationDetails": True, | ||
"variableListHeight": 200, | ||
"showVariableDetails": True, | ||
"layerListHeight": 160, | ||
"showLayerDetails": True, | ||
"panelContainerUndockedMode": False, | ||
"leftPanelContainerLayout": { | ||
"horPos": 300, | ||
"verPos": 400 | ||
}, | ||
"rightPanelContainerLayout": { | ||
"horPos": 300, | ||
"verPos": 400 | ||
}, | ||
"selectedLeftTopPanelId": "dataSources", | ||
"selectedLeftBottomPanelId": "operations", | ||
"selectedRightTopPanelId": "workspace", | ||
"selectedRightBottomPanelId": "variables", | ||
"placemarkCollection": { | ||
"type": "FeatureCollection", | ||
"features": [] | ||
}, | ||
"selectedPlacemarkId": None, | ||
"placemarkListHeight": 160, | ||
"showPlacemarkDetails": True, | ||
"defaultPlacemarkStyle": { | ||
"markerSize": "small", | ||
"markerColor": "#ff0000", | ||
"markerSymbol": "", | ||
"fill": "#0000ff", | ||
"fillOpacity": 0.5, | ||
"stroke": "#ffff00", | ||
"strokeOpacity": 0.5, | ||
"strokeWidth": 1 | ||
}, | ||
"workspacePanelMode": "steps", | ||
"showDataStoreDescription": False, | ||
"showDataStoreNotices": True, | ||
"showDataSourceIDs": True, | ||
"showLayerTextOverlay": True, | ||
"debugWorldView": False, | ||
"styleContext": "entity", | ||
"backendConfig": { | ||
"dataStoresPath": "~/.cate/data_stores", | ||
"useWorkspaceImageryCache": False, | ||
"resourceNamePattern": "res_{index}", | ||
"proxyUrl": None | ||
} | ||
} | ||
|
||
|
||
class MyTestCase(unittest.TestCase): | ||
def test_get_user_prefs(self): | ||
tdir = tempfile.gettempdir() | ||
tfile = os.path.join(tdir, 'test_get_prefs.txt') | ||
prefs = get_user_prefs(tfile) | ||
|
||
self.assertDictEqual(_TEST_USER_PREFS, prefs) | ||
|
||
n_prefs = _TEST_USER_PREFS.copy() | ||
n_prefs['autoUpdateSoftware'] = True | ||
|
||
prefs = set_user_prefs(n_prefs, tfile) | ||
|
||
self.assertDictEqual(n_prefs, prefs) | ||
|
||
def test_set_user_prefs(self): | ||
tdir = tempfile.gettempdir() | ||
tfile = os.path.join(tdir, 'test_set_prefs.txt') | ||
prefs = set_user_prefs(_TEST_USER_PREFS, tfile) | ||
|
||
self.assertDictEqual(_TEST_USER_PREFS, prefs) | ||
|
||
n_prefs = _TEST_USER_PREFS.copy() | ||
n_prefs['autoUpdateSoftware'] = True | ||
|
||
prefs = set_user_prefs(n_prefs) | ||
|
||
self.assertDictEqual(n_prefs, prefs) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |