Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index backends by id, not by name #931

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 27 additions & 6 deletions GTG/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@ def save(self):
self._save_function()


class CoreConfig():
class CoreConfig_():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the underscore here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backwards compatibility: CoreConfig is redefined below:

def CoreConfig():
    return CoreConfig_.get_instance()

""" Class holding configuration to all systems and tasks """

_instance = None

def __init__(self):
assert self._instance is None

self._conf_path = os.path.join(CONFIG_DIR, 'gtg.conf')
self._conf = open_config_file(self._conf_path)

Expand All @@ -194,6 +198,11 @@ def __init__(self):
self._backends_conf_path = os.path.join(CONFIG_DIR, 'backends.conf')
self._backends_conf = open_config_file(self._backends_conf_path)

@classmethod
def get_instance(cls):
cls._instance = cls._instance or cls()
return cls._instance

def save_gtg_config(self):
self._conf.write(open(self._conf_path, 'w'))

Expand All @@ -220,15 +229,27 @@ 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)

def CoreConfig():
return CoreConfig_.get_instance()