Coverage for src/robotide/preferences/managesettingsdialog.py: 53%
103 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
1# Copyright 2020- Robot Framework Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15import builtins 1gh
16import os 1gh
17import wx 1gh
19from wx import Colour 1gh
20from ..widgets import RIDEDialog, VerticalSizer 1gh
21# from .configobj import ConfigObj, UnreprError
22from . import ConfigObj, UnreprError 1gh
23from .settings import ConfigurationError, _Section, initialize_settings 1gh
25from ..context import SETTINGS_DIRECTORY 1gh
27_ = wx.GetTranslation # To keep linter/code analyser happy 1gh
28builtins.__dict__['_'] = wx.GetTranslation 1gh
30ID_LOAD = 5551 1gh
31ID_SAVE = 5552 1gh
32ID_CANCEL = -1 1gh
35class SaveLoadSettings(RIDEDialog): 1gh
37 def __init__(self, parent, settings): 1gh
38 self._parent = parent 1adebfc
39 self._settings = settings 1adebfc
40 self._section = self._settings._name 1adebfc
41 self._selection_listeners = [] 1adebfc
42 title = _("Save or Load Settings") 1adebfc
43 RIDEDialog.__init__(self, parent=parent, title=title, size=(650, 400)) 1adebfc
44 # set Left to Right direction (while we don't have localization)
45 self.SetLayoutDirection(wx.Layout_LeftToRight) 1adebfc
46 main_sizer = wx.FlexGridSizer(rows=5, cols=1, vgap=10, hgap=10) 1adebfc
47 buttons_sizer = wx.BoxSizer(orient=wx.HORIZONTAL) 1adebfc
48 load = wx.Button(self, ID_LOAD, _('Load settings from file...')) 1adebfc
49 save = wx.Button(self, ID_SAVE, _('Save settings to file...')) 1adebfc
50 self.SetSizer(VerticalSizer()) 1adebfc
51 self.SetBackgroundColour(Colour(self.color_background)) 1adebfc
52 self.SetForegroundColour(Colour(self.color_foreground)) 1adebfc
53 load.SetBackgroundColour(Colour(self.color_secondary_background)) 1adebfc
54 load.SetForegroundColour(Colour(self.color_secondary_foreground)) 1adebfc
55 save.SetBackgroundColour(Colour(self.color_secondary_background)) 1adebfc
56 save.SetForegroundColour(Colour(self.color_secondary_foreground)) 1adebfc
57 self._default_path = os.path.join(SETTINGS_DIRECTORY, 'settings.cfg') 1adebfc
58 directory = wx.StaticText(self, label=f"{_('Current directory:')} {SETTINGS_DIRECTORY}") 1adebfc
59 buttons_sizer.Add(load) 1adebfc
60 buttons_sizer.AddSpacer(10) 1adebfc
61 buttons_sizer.Add(save) 1adebfc
62 main_sizer.Add(directory) 1adebfc
63 main_sizer.AddSpacer(10) 1adebfc
64 main_sizer.Add(buttons_sizer) 1adebfc
65 self.SetSizerAndFit(main_sizer) 1adebfc
66 self.Bind(wx.EVT_BUTTON, self.on_load) 1adebfc
67 self.Bind(wx.EVT_BUTTON, self.on_save) 1adebfc
68 # print(f"DEBUG: SaveLoad init returncode {self.GetReturnCode()}")
70 def on_load(self, event): 1gh
71 if event.GetId() != ID_LOAD: 71 ↛ 75line 71 didn't jump to line 75 because the condition on line 71 was always true1b
72 event.Skip() 1b
73 self.SetReturnCode(ID_CANCEL) 1b
74 return ID_CANCEL 1b
75 load_dlg = wx.FileDialog(self, message=_("File with Settings to Load"),
76 defaultDir=SETTINGS_DIRECTORY, wildcard="*.cfg")
77 if load_dlg.ShowModal() == wx.ID_CANCEL:
78 self.SetReturnCode(ID_CANCEL)
79 return ID_CANCEL
80 file = load_dlg.GetPath()
81 if os.path.isfile(file): # Test validity settings
82 self.Freeze()
83 self.load_and_merge(file)
84 self.Thaw()
85 self._parent.Refresh()
86 self._parent.GetParent().Refresh()
87 self.SetReturnCode(ID_LOAD)
88 self.Close()
89 return ID_LOAD
91 def on_close(self): 1gh
92 self.SetReturnCode(ID_CANCEL) 1d
93 return ID_CANCEL 1d
95 def on_save(self, event): 1gh
96 if event.GetId() != ID_SAVE: 96 ↛ 101line 96 didn't jump to line 101 because the condition on line 96 was always true1c
97 event.Skip() 1c
98 self.SetReturnCode(ID_CANCEL) 1c
99 return ID_CANCEL 1c
101 with wx.FileDialog(self, message=_("Save Settings to file"), defaultDir=SETTINGS_DIRECTORY,
102 wildcard="*.cfg", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as save_dlg:
103 if save_dlg.ShowModal() == wx.ID_CANCEL:
104 self.SetReturnCode(ID_CANCEL)
105 return ID_CANCEL
106 pathname = save_dlg.GetPath()
107 """
108 filename = os.path.basename(pathname)
109 dirname = os.path.dirname(pathname)
110 """
111 try:
112 initialize_settings(self._default_path, pathname)
113 except IOError:
114 raise RuntimeError(_('Could not open settings file "%s" for writing') % pathname)
115 self.SetReturnCode(ID_SAVE)
116 self.Close()
117 return ID_SAVE
119 def load_and_merge(self, user_path): 1gh
120 try: 1a
121 nnew_settings = ConfigObj(user_path, encoding='UTF-8', unrepr=True) 1a
122 mysection = nnew_settings.get(self._section) 1a
123 if not mysection: 123 ↛ 129line 123 didn't jump to line 129 because the condition on line 123 was always true1a
124 mysection = nnew_settings['Plugins'].get(self._section) 1a
125 if not mysection:
126 raise ConfigurationError(_("Error trying to get '%s' from file %s") % (f"[Plugins][{self._section}]",
127 user_path))
129 for key, value in mysection.items():
130 if self._settings.has_setting(key):
131 if isinstance(value, dict):
132 for k, v in value.items():
133 if isinstance(self._settings.get_without_default(key).get_without_default(k), _Section):
134 self._settings[key][k].set_defaults(None, **v)
135 else:
136 self._settings[key].set(k, v)
137 else:
138 self._settings.set(key, value)
139 self._settings.save()
140 except UnreprError as err: # DEBUG errored file 1a
141 raise ConfigurationError(_("Invalid config file '%s': %s") % (user_path, err))