19 from ..context
import SETTINGS_DIRECTORY, LIBRARY_XML_DIRECTORY
20 from .configobj
import ConfigObj, ConfigObjError, Section, UnreprError
21 from .excludes_class
import Excludes
22 from ..publish
import RideSettingsChanged
26 if not os.path.exists(SETTINGS_DIRECTORY):
27 os.makedirs(SETTINGS_DIRECTORY)
29 SETTINGS_DIRECTORY, path, dest_file_name)
45 if not dest_file_name:
46 dest_file_name = os.path.basename(source_path)
47 settings_path = os.path.join(settings_dir, dest_file_name)
48 if not os.path.exists(settings_path):
49 shutil.copyfile(source_path, settings_path)
55 except ConfigObjError
as parsing_error:
56 print(
"WARNING! corrupted configuration file replaced with defaults")
58 m_error = parsing_error
59 shutil.copyfile(settings_path, settings_path +
'_old_broken')
60 print(
"(backed up corrupted configuration file at: %s)" %
61 (settings_path +
'_old_broken'))
62 shutil.copyfile(source_path, settings_path)
67 return os.path.abspath(settings_path), m_error
72 SETTINGS_VERSION =
'settings_version'
80 except UnreprError
as err:
128 if settings.get(
'Colors', {}).get(
'text library keyword') ==
'blue':
129 settings[
'Colors'][
'text library keyword'] =
'#0080C0'
136 pythonpath = settings.get(
'pythonpath', [])
138 settings[
'pythonpath'] = [p.strip()
for p
139 in pythonpath
if p.strip()]
144 old_excludes = os.path.join(SETTINGS_DIRECTORY,
'excludes')
145 if os.path.isfile(old_excludes):
146 with open(old_excludes)
as f:
148 new =
'\n'.join(d
for d
in old.split(
'\n')
if os.path.isdir(d))+
'\n'
149 with open(old_excludes,
'wb')
as f:
150 f.write(new.encode(
'UTF-8'))
155 font_size = settings.get(
'font size',
None)
156 if font_size
and font_size == 11:
157 settings[
'font size'] = 8
163 colors = settings.get(
'Colors',
None)
165 settings[
'Grid Colors'] = colors
166 del settings[
'Colors']
171 grid_colors = settings.get(
'Grid Colors',
None)
173 settings[
'Grid'] = grid_colors
174 del settings[
'Grid Colors']
175 grid_font_size = settings.get(
'font size',
None)
177 settings[
'Grid'][
'font size'] = grid_font_size
178 del settings[
'font size']
179 text_edit_colors = settings.get(
'Text Edit Colors',
None)
181 settings[
'Text Edit'] = text_edit_colors
182 del settings[
'Text Edit Colors']
183 text_font_size = settings.get(
'text edit font size',
None)
185 settings[
'Text Edit'][
'font size'] = text_font_size
186 del settings[
'text edit font size']
190 settings[
'use installed robot libraries'] =
True
194 installed_rf_libs = settings.get(
'use installed robot libraries',
None)
195 if installed_rf_libs:
196 del settings[
'use installed robot libraries']
198 'BuiltIn',
'Collections',
'DateTime',
'Dialogs',
'Easter',
'OperatingSystem',
'Process',
199 'Remote',
'Screenshot',
'String',
'Telnet',
'XML']:
200 lib_xml_path = os.path.join(LIBRARY_XML_DIRECTORY,
'{}.xml'.format(name))
201 if os.path.exists(lib_xml_path):
202 os.remove(lib_xml_path)
206 def migrate_from_8_to_9(self, settings):
207 # mainframe_size = settings.get('mainframe size', (1100, 700))
210 'mainframe_position',
211 'mainframe_maximized',
213 'list_variable_columns',
214 'list_col_min_width',
215 'list_col_max_width',
217 'library_xml_directories',
218 'txt_number_of_spaces',
219 'txt_format_separator',
221 'default_file_format',
226 sec_text = ['font size'] # [Text Edit]
227 sec_grid = [ # [Grid]
234 'text_library_keyword',
236 'text_unknown_variable',
241 'background_keyword',
242 'background_mandatory',
243 'background_optional',
244 'background_must_be_empty',
245 'background_unknown',
247 'background_highlight'
250 for keyname in parameters:
251 self._key_with_underscore(settings, keyname)
252 for keyname in sec_text:
253 self._key_with_underscore(settings, keyname, 'Text Edit')
254 for keyname in sec_grid:
255 self._key_with_underscore(settings, keyname, 'Grid')
256 except AttributeError: # DEBUG Ignore errors
257 # raise ConfigObjError
259 settings[self.SETTINGS_VERSION] = 9
264 keyname_old = keyname.replace(
'_',
' ')
266 value = settings.get(keyname_old,
None)
268 settings[keyname] = value
269 del settings[keyname_old]
271 value = settings.get(section, {}).get(keyname_old,
None)
273 settings[section][keyname] = value
274 del settings[section][keyname_old]
279 with open(path,
'wb')
as outfile:
280 settings.write(outfile)
283 'Could not open settings file "%s" for writing' % path)
305 self.
setset(name, value)
309 if isinstance(value, Section):
327 def get(self, name, default):
331 self.
setset(name, default)
348 def set(self, name, value, autosave=True, override=True):
349 if self.
_is_section_is_section(name)
and not isinstance(value, _Section):
350 raise SectionError(
"Cannot override section with value.")
351 if isinstance(value, _Section):
354 for key, _value
in value._config_obj.items():
355 self[name].
set(key, _value, autosave, override)
356 elif name
not in self.
_config_obj_config_obj
or override:
362 keys=[self.
_name_name, name], old=old, new=value).publish()
368 def set_values(self, settings, autosave=True, override=True):
370 for key, value
in settings.items():
371 self.
setset(key, value, autosave=
False, override=override)
378 settings_dict = settings_dict
or {}
379 settings_dict.update(settings)
380 return self.
set_valuesset_values(settings_dict, override=
False)
385 not isinstance(self.
_config_obj_config_obj[name], Section):
386 raise SectionError(
'Cannot override value with section.')
393 isinstance(self.
_config_obj_config_obj[name], Section)
400 _Section.__init__(self,
ConfigObj(user_path, unrepr=
True))
401 except UnreprError
as error:
415 self.
_default_path_default_path = os.path.join(os.path.dirname(__file__),
'settings.cfg')
418 Settings.__init__(self, user_path)
421 self.
setset(
'install root', os.path.dirname(os.path.dirname(__file__)))
An object to read, create, and write config files.
def get_path(self, *parts)
Returns path which combines settings directory and given parts.
def __init__(self, path=None)
Used when section is tried to replace with normal value or vice versa.
def _write_merged_settings(settings, path)
def migrate_from_6_to_7(self, settings)
def migrate_from_5_to_6(self, settings)
def _key_with_underscore(settings, keyname, section=None)
def migrate_from_3_to_4(self, settings)
def migrate_from_0_to_1(self, settings)
def migrate_from_2_to_3(self, settings)
def migrate_from_1_to_2(self, settings)
def migrate_from_4_to_5(self, settings)
def migrate_from_7_to_8(self, settings)
def __init__(self, default_path, user_path)
def __init__(self, user_path)
def _is_section(self, name)
def __setitem__(self, name, value)
def iteritems(self)
Returns an iterator over the (key,value) items of the section.
def __getitem__(self, name)
def set_defaults(self, settings_dict=None, **settings)
Sets defaults based on dict and kwargs, kwargs having precedence.
def get(self, name, default)
Returns specified setting or (automatically set) default.
def get_without_default(self, name)
Returns specified setting or None if setting is not defined.
def __init__(self, section, parent=None, name='')
def set_values(self, settings, autosave=True, override=True)
Set values from settings.
def has_setting(self, name)
def add_section(self, name, **defaults)
Creates section or updates existing section with defaults.
def set(self, name, value, autosave=True, override=True)
Sets setting 'name' value to 'value'.
Sent when settings are changed.
def write(msg, level='INFO', html=False)
Writes the message to the log file using the given level.
def initialize_settings(path, dest_file_name=None)
def _copy_or_migrate_user_settings(settings_dir, source_path, dest_file_name)
Creates settings directory and copies or merges the source to there.