def test_font_gets_updated(): fixedfont = Font(name="TkFixedFont", exists=True) settings.set_("font_family", "Helvetica") assert fixedfont["family"] == "Helvetica" settings.set_("font_size", 123) assert fixedfont["size"] == 123
def test_add_option_and_get_and_set(cleared_global_settings): settings.add_option("how_many_foos", 123) settings.add_option("bar_message", "hello") assert settings.get("how_many_foos", int) == 123 assert settings.get("bar_message", str) == "hello" settings.set_("how_many_foos", 456) settings.set_("bar_message", "bla") assert settings.get("how_many_foos", int) == 456 assert settings.get("bar_message", str) == "bla"
def test_no_json_file(cleared_global_settings): assert not settings._get_json_path().exists() settings._load_from_file() settings.add_option("foo", "default") settings.set_("foo", "custom") assert not settings._get_json_path().exists() settings.reset_all() assert settings.get("foo", str) == "default"
def test_unknown_option_in_settings_file(cleared_global_settings): load_from_json_string('{"foo": "custom", "unknown": "hello"}') with pytest.raises(KeyError): settings.get("foo", str) settings.add_option("foo", "default") assert settings.get("foo", str) == "custom" settings.set_("foo", "default") assert settings.get("foo", str) == "default" assert save_and_read_file() == {"unknown": "hello"}
def test_save(cleared_global_settings): load_from_json_string('{"bar": "custom bar"}') settings.add_option("foo", "default") settings.add_option("bar", "default") settings.add_option("baz", "default") settings.set_("foo", "custom foo") settings.save() with settings._get_json_path().open("r") as file: assert json.load(file) == {"foo": "custom foo", "bar": "custom bar"}
def test_wrong_type(cleared_global_settings): settings.add_option("magic_message", "bla") with pytest.raises( dacite.exceptions.WrongTypeError, match=r'wrong value type .* should be "int" instead of .* "str"', ): settings.get("magic_message", int) with pytest.raises( dacite.exceptions.WrongTypeError, match=r'wrong value type .* should be "str" instead of .* "int"', ): settings.set_("magic_message", 123)
def change_font_size(how: Literal["bigger", "smaller", "reset"]) -> None: if how == "reset": settings.reset("font_size") return size = settings.get("font_size", int) if how == "bigger": size += 1 else: size -= 1 if size < 3: return settings.set_("font_size", size)
def hide_old_projects(self, junk: object = None) -> None: for project_id in self.get_children(""): if not self.get_path(project_id).is_dir(): self.delete(project_id) # type: ignore[no-untyped-call] # To avoid getting rid of existing projects when not necessary, we do # shortening after deleting non-existent projects for project_id in reversed(self.get_children("")): if len(self.get_children("")) > MAX_PROJECTS and not any( isinstance(tab, tabs.FileTab) and tab.path is not None and self.get_path(project_id) in tab.path.parents for tab in get_tab_manager().tabs()): self.delete(project_id) # type: ignore[no-untyped-call] # Settings is a weird place for this, but easier than e.g. using a cache file. settings.set_("directory_tree_projects", [str(self.get_path(id)) for id in self.get_children()])
def _set_enabled(self, they_become_enabled: bool) -> None: infos = self._get_selected_infos() disabled = set(settings.get("disabled_plugins", List[str])) if they_become_enabled: disabled -= {info.name for info in infos} else: disabled |= {info.name for info in infos} settings.set_("disabled_plugins", list(disabled)) for info in infos: if info.name not in disabled and pluginloader.can_setup_while_running( info): pluginloader.setup_while_running(info) self._update_row(info) self._on_select() self._update_plz_restart_label()
def stop_completing(self, *, withdraw: bool = True) -> Optional[Completion]: # putting this here avoids some bugs if self.is_showing(): settings.set_("autocomplete_popup_width", self.toplevel.winfo_width()) settings.set_("autocomplete_popup_height", self.toplevel.winfo_height()) selected = self._get_selected_completion() if withdraw: self.toplevel.withdraw() self.treeview.delete( *self.treeview.get_children()) # type: ignore[no-untyped-call] self._completion_list = None return selected
def setup() -> None: style = ttkthemes.ThemedStyle() settings.add_option("ttk_theme", style.theme_use()) var = tkinter.StringVar() for name in sorted(style.get_themes()): menubar.get_menu("Ttk Themes").add_radiobutton(label=name, value=name, variable=var) # Connect style and var var.trace_add("write", lambda *junk: style.set_theme(var.get())) # Connect var and settings get_main_window().bind( "<<SettingChanged:ttk_theme>>", lambda event: var.set(settings.get("ttk_theme", str)), add=True, ) var.set(settings.get("ttk_theme", str)) var.trace_add("write", lambda *junk: settings.set_("ttk_theme", var.get()))
def var2settings(*junk: str) -> None: settings.set_("pygments_style", var.get())
def save_geometry(event: tkinter.Event[tkinter.Misc]) -> None: assert isinstance(event.widget, (tkinter.Tk, tkinter.Toplevel)) settings.set_("default_geometry", event.widget.geometry())