Skip to content

Commit

Permalink
Index backends by id, not by name
Browse files Browse the repository at this point in the history
This allows GTG to have multiple backends of one kind at the same time.

* GTG/backends/__init__.py (BackendFactory.get_saved_backends_list): Rename
`backend` to `backend_id` for clarity.  Call `config.rename_backend_section` on
each backend for compatibility with previous versions of GTG.
* GTG/core/config.py (CoreConfig_.rename_backend_section): New function to
rename a config section (from backend name to backend ID).
(CoreConfig_.get_all_backends): Rename `backend` to `backend_id` for clarity.

Fixes getting-things-gnome#930.
  • Loading branch information
cpitclaudel committed Mar 2, 2024
1 parent 8c56d9a commit f56a620
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
10 changes: 7 additions & 3 deletions GTG/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def get_saved_backends_list(self):
config = CoreConfig()
backends = []

for backend in config.get_all_backends():
settings = config.get_backend_config(backend)
for backend_id in config.get_all_backends():
settings = config.get_backend_config(backend_id)
module = self.get_backend(settings.get('module'))

# Skip this backend if it doesn't have a module
Expand Down Expand Up @@ -167,7 +167,11 @@ def get_saved_backends_list(self):
# Parameter not found in config
pass

backend_data['backend'] = module.Backend(backend_data)
backend = backend_data['backend'] = module.Backend(backend_data)

# Rename configuration sections created by older versions of GTG
config.rename_backend_section(backend.get_name(), backend.get_id())

backends.append(backend_data)

return backends
19 changes: 14 additions & 5 deletions GTG/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,25 @@ def get_task_config(self, task_id):
DEFAULTS['task'],
self.save_task_config)

def rename_backend_section(self, backend_name, backend_id):
"""Rename section `backend_name` to `backend_id` if it exists."""
if backend_name in self._backends_conf:
assert backend_id not in self._backends_conf
self._backends_conf.add_section(backend_id)
for (k, v) in self._backends_conf[backend_name].items():
self._backends_conf.set(backend_id, k, v)
self._backends_conf.remove_section(backend_name)

def get_all_backends(self):
return self._backends_conf.sections()

def get_backend_config(self, backend):
if backend not in self._backends_conf:
self._backends_conf.add_section(backend)
def get_backend_config(self, backend_id):
if backend_id not in self._backends_conf:
self._backends_conf.add_section(backend_id)

return SectionConfig(
f'Backend {backend}',
self._backends_conf[backend],
f'Backend {backend_id}',
self._backends_conf[backend_id],
DEFAULTS['backend'],
self.save_backends_config)

Expand Down

0 comments on commit f56a620

Please sign in to comment.