From 7897308e024704982f253434acbb7c8d61330e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Koz=C3=A1k?= Date: Sun, 24 Jun 2018 22:09:10 +0200 Subject: [PATCH] Support termius groups expansion from config file --- termius/porting/providers/ssh/adapter.py | 6 ++++-- termius/porting/providers/ssh/parser.py | 6 ++++++ termius/porting/providers/ssh/provider.py | 16 +++++++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/termius/porting/providers/ssh/adapter.py b/termius/porting/providers/ssh/adapter.py index 0416d52..0e2b034 100644 --- a/termius/porting/providers/ssh/adapter.py +++ b/termius/porting/providers/ssh/adapter.py @@ -5,7 +5,7 @@ from pathlib2 import Path from termius.core.commands.mixins import SshConfigMergerMixin -from termius.core.models.terminal import Host, SshConfig, Identity, SshKey +from termius.core.models.terminal import Host, SshConfig, Identity, SshKey, Group class SSHConfigHostAdapter(SshConfigMergerMixin): @@ -56,7 +56,7 @@ def adapt_instance_to_ssh_config_host(self, host_instance): return adapted - def adapt_ssh_config_host_to_instance(self, alias, parsed_host): + def adapt_ssh_config_host_to_instance(self, alias, parsed_host, parsed_group=None): """Convert parsed host to application host.""" app_host = Host( label=alias, @@ -80,5 +80,7 @@ def adapt_ssh_config_host_to_instance(self, alias, parsed_host): ) app_host.ssh_config = ssh_config + if parsed_group: + app_host.group = parsed_group return app_host diff --git a/termius/porting/providers/ssh/parser.py b/termius/porting/providers/ssh/parser.py index 8b6a274..668e889 100644 --- a/termius/porting/providers/ssh/parser.py +++ b/termius/porting/providers/ssh/parser.py @@ -14,7 +14,9 @@ def parse(self, file_obj): # noqa :param file_obj: a file-like object to read the config file from """ + TERMIUS_GROUPS = "# termius:group" termius_ignore_regexp = re.compile(r'# termius:ignore') + termius_groups_regexp = re.compile(r'{}'.format(TERMIUS_GROUPS)) host = {'host': ['*'], 'config': {}} @@ -30,6 +32,10 @@ def parse(self, file_obj): # noqa if ignore_comment: host['config']['ignore'] = '' + groups_comment = termius_groups_regexp.match(line) + if groups_comment: + host['group'] = line.replace(TERMIUS_GROUPS, "").strip() + continue match = re.match(self.SETTINGS_REGEX, line) diff --git a/termius/porting/providers/ssh/provider.py b/termius/porting/providers/ssh/provider.py index 6ca2bd0..d2b091b 100644 --- a/termius/porting/providers/ssh/provider.py +++ b/termius/porting/providers/ssh/provider.py @@ -5,7 +5,7 @@ from os.path import expanduser from pathlib2 import Path -from termius.core.models.terminal import Host +from termius.core.models.terminal import Host, Group from ..base import BasePortingProvider from .parser import SSHConfigParser @@ -50,8 +50,18 @@ def provider_hosts(self): ] to_import = [] - + parsed_group = None for alias in parsed_hosts: + parsed_group_name = parser._config[parsed_hosts.index(alias) + 1].get("group", None) + if parsed_group_name: + matching_groups = [x for x in self.storage.get_all(Group) if x['label'] == parsed_group_name] + if len(matching_groups) == 0: + g = Group(label=parsed_group_name) + self.storage.create(g) + matching_groups = [g] + + item = matching_groups.pop() + parsed_group = Group(**item) parsed_host = parser.lookup(alias) if 'ignore' in parsed_host: @@ -59,7 +69,7 @@ def provider_hosts(self): to_import.append( self.adapter.adapt_ssh_config_host_to_instance( - alias, parsed_host + alias, parsed_host, parsed_group ) )