Skip to content

Commit

Permalink
Add a boolean to prefs and use time.monotonic to prevent loading the …
Browse files Browse the repository at this point in the history
…guake.yml file more than once per second
  • Loading branch information
jorgeecardona committed Apr 18, 2022
1 parent 557bcf2 commit bf12a5e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
5 changes: 5 additions & 0 deletions guake/data/org.guake.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<summary>Automatically save tabs session when changed</summary>
<description>If true, when tabs has changed (add / delete ...etc.), it will automatically saved the tabs session</description>
</key>
<key name="load-guake-yml" type="b">
<default>true</default>
<summary>Load settings from guake.yml</summary>
<description>If true, when a change in the cwd is detected settings are changed based on the content of the file `cwd`/.guake.yml</description>
</key>
<key name="use-login-shell" type="b">
<default>false</default>
<summary>Login shell</summary>
Expand Down
13 changes: 12 additions & 1 deletion guake/data/prefs.glade
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,18 @@
</packing>
</child>
<child>
<placeholder/>
<object class="GtkCheckButton" id="load-guake-yml">
<property name="label" translatable="yes">Load settings from `cwd`/.guake.yml</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="draw-indicator">True</property>
<signal name="toggled" handler="on_load_guake_yml_toggled" swapped="no"/>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
<placeholder/>
Expand Down
34 changes: 26 additions & 8 deletions guake/guake_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ def window_event(*args):
Keybinder.init()
self.hotkeys = Keybinder
Keybindings(self)

# Hold a copy of guake_yaml
self._guake_yml = {}
self._guake_yml_load_monotonic = {}
self.load_config()

if self.settings.general.get_boolean("start-fullscreen"):
Expand Down Expand Up @@ -1105,18 +1109,32 @@ def recompute_tabs_titles(self):

def load_cwd_guake_yaml(self, vte) -> dict:
# Read the content of .guake.yml in cwd
if not self.settings.general.get_boolean("load-guake-yml"):
return {}

cwd = Path(vte.get_current_directory())
guake_yaml = cwd.joinpath(".guake.yml")
guake_yml = cwd.joinpath(".guake.yml")
content = {}
try:
if guake_yaml.is_file():
with guake_yaml.open(encoding="utf-8") as fd:
content = yaml.safe_load(fd)
except PermissionError:
log.debug("PermissionError on accessing .guake.yml")

reload_guake_yml = True
if guake_yml in self._guake_yml_load_monotonic:
if pytime.monotonic() < self._guake_yml_load_monotonic[guake_yml] + 1.0:
reload_guake_yml = False

if not reload_guake_yml:
content = self._guake_yml.get(guake_yml, {})
else:
try:
if guake_yml.is_file():
with guake_yml.open(encoding="utf-8") as fd:
content = yaml.safe_load(fd)
self._guake_yml[guake_yml] = content
self._guake_yml_load_monotonic[guake_yml] = pytime.monotonic()
except PermissionError:
log.debug("PermissionError on accessing .guake.yml")

if not isinstance(content, dict):
conent = {}
content = {}
return content

def compute_tab_title(self, vte):
Expand Down
8 changes: 8 additions & 0 deletions guake/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ def on_save_tabs_when_changed_toggled(self, chk):
"""Changes the activity of save-tabs-when-changed in dconf"""
self.settings.general.set_boolean("save-tabs-when-changed", chk.get_active())

def on_load_guake_yml_toggled(self, chk):
"""Changes the activity of load-guake-yml"""
self.settings.general.set_boolean("load-guake-yml", chk.get_active())

def on_default_shell_changed(self, combo):
"""Changes the activity of default_shell in dconf"""
citer = combo.get_active_iter()
Expand Down Expand Up @@ -1008,6 +1012,10 @@ def load_configs(self):
value = self.settings.general.get_boolean("save-tabs-when-changed")
self.get_widget("save-tabs-when-changed").set_active(value)

# save tabs when changed
value = self.settings.general.get_boolean("load-guake-yml")
self.get_widget("load-guake-yml").set_active(value)

# login shell
value = self.settings.general.get_boolean("use-login-shell")
self.get_widget("use_login_shell").set_active(value)
Expand Down

0 comments on commit bf12a5e

Please sign in to comment.