Robot Framework Integrated Development Environment (RIDE)
managesettingsdialog.py
Go to the documentation of this file.
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.
14 
15 import os
16 import wx
17 
18 from wx import Colour
19 from ..widgets import RIDEDialog, VerticalSizer
20 from .configobj import ConfigObj, UnreprError
21 from .settings import ConfigurationError, _Section, initialize_settings
22 
23 from ..context import SETTINGS_DIRECTORY
24 
25 ID_LOAD = 5551
26 ID_SAVE = 5552
27 ID_CANCEL = -1
28 
29 
30 class SaveLoadSettings(RIDEDialog):
31 
32  def __init__(self, parent, settings):
33  self._parent_parent = parent
34  self._settings_settings = settings
35  self._section_section = self._settings_settings._name
36  self._selection_listeners_selection_listeners = []
37  title = "Save or Load Settings"
38  RIDEDialog.__init__(self, parent=parent, title=title, size=(650, 400))
39  # set Left to Right direction (while we don't have localization)
40  self.SetLayoutDirection(wx.Layout_LeftToRight)
41  main_sizer = wx.FlexGridSizer(rows=5, cols=1, vgap=10, hgap=10)
42  buttons_sizer = wx.BoxSizer(orient=wx.HORIZONTAL)
43  load = wx.Button(self, ID_LOAD, 'Load settings from file...')
44  save = wx.Button(self, ID_SAVE, 'Save settings to file...')
45  self.SetSizer(VerticalSizer())
46  self.SetBackgroundColour(Colour(self.color_background))
47  self.SetForegroundColour(Colour(self.color_foreground))
48  self._default_path_default_path = os.path.join(SETTINGS_DIRECTORY, 'settings.cfg')
49  directory = wx.StaticText(self, label=f"Current directory: {SETTINGS_DIRECTORY}")
50  buttons_sizer.Add(load)
51  buttons_sizer.AddSpacer(10)
52  buttons_sizer.Add(save)
53  main_sizer.Add(directory)
54  main_sizer.AddSpacer(10)
55  main_sizer.Add(buttons_sizer)
56  self.SetSizerAndFit(main_sizer)
57  self.Bind(wx.EVT_BUTTON, self.OnLoadOnLoad)
58  self.Bind(wx.EVT_BUTTON, self.OnSaveOnSave)
59  # print(f"DEBUG: SaveLoad init returncode {self.GetReturnCode()}")
60 
61  def OnLoad(self, event):
62  if event.GetId() != ID_LOAD:
63  event.Skip()
64  self.SetReturnCode(ID_CANCEL)
65  return ID_CANCEL
66  load_dlg = wx.FileDialog(self, message="File with Settings to Load",
67  defaultDir=SETTINGS_DIRECTORY, wildcard="*.cfg")
68  if load_dlg.ShowModal() == wx.ID_CANCEL:
69  self.SetReturnCode(ID_CANCEL)
70  return ID_CANCEL
71  file = load_dlg.GetPath()
72  # print(f"DEBUG: SaveLoad returncode {self.GetReturnCode()}")
73  if os.path.isfile(file): # Test validity settings
74  # print(f"DEBUG: Selected file is {file}.")
75  # print(f"DEBUG: load_and_merge BEFORE: {self._settings['background']}")
76  self.Freeze()
77  self.load_and_mergeload_and_merge(file)
78  self.Thaw()
79  # print(f"DEBUG: load_and_merge AFTER: {self._settings['background']}")
80  self._parent_parent.Refresh()
81  self._parent_parent.GetParent().Refresh()
82  self.SetReturnCode(ID_LOAD)
83  self.Close()
84  return ID_LOAD
85 
86  def OnClose(self):
87  self.SetReturnCode(ID_CANCEL)
88  return ID_CANCEL
89 
90  def OnSave(self, event):
91  if event.GetId() != ID_SAVE:
92  event.Skip()
93  self.SetReturnCode(ID_CANCEL)
94  return ID_CANCEL
95 
96  with wx.FileDialog(self, message="Save Settings to file", defaultDir=SETTINGS_DIRECTORY,
97  wildcard="*.cfg", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as save_dlg:
98  if save_dlg.ShowModal() == wx.ID_CANCEL:
99  self.SetReturnCode(ID_CANCEL)
100  return ID_CANCEL
101  # print(f"DEBUG: SaveLoad returncode {self.GetReturnCode()}")
102  pathname = save_dlg.GetPath()
103  filename = os.path.basename(pathname)
104  dirname = os.path.dirname(pathname)
105  try:
106  initialize_settings(self._default_path_default_path, pathname)
107  except IOError:
108  raise RuntimeError('Could not open settings file "%s" for writing' % pathname)
109  self.SetReturnCode(ID_SAVE)
110  self.Close()
111  return ID_SAVE
112 
113  def load_and_merge(self, user_path):
114  try:
115  nnew_settings = ConfigObj(user_path, unrepr=True)
116  mysection=nnew_settings.get(self._section_section)
117  if not mysection:
118  mysection = nnew_settings['Plugins'].get(self._section_section)
119  if not mysection:
120  raise ConfigurationError("Error trying to get '%s' from file %s" % (f"[Plugins][{self._section}]",user_path))
121 
122  for key, value in mysection.items():
123  if self._settings_settings.has_setting(key):
124  if isinstance(value, dict):
125  for k, v in value.items():
126  if isinstance(self._settings_settings.get_without_default(key).get_without_default(k), _Section):
127  self._settings_settings[key][k].set_defaults(None, **v)
128  else:
129  self._settings_settings[key].set(k, v)
130  else:
131  self._settings_settings.set(key,value)
132  self._settings_settings.save()
133  except UnreprError as err: # DEBUG errored file
134  raise ConfigurationError("Invalid config file '%s': %s" % (user_path, err))
An object to read, create, and write config files.
Definition: configobj.py:1098
def initialize_settings(path, dest_file_name=None)
Definition: settings.py:25