mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-26 10:12:17 +02:00
Get env conf working with tests
This commit is contained in:
@@ -15,7 +15,7 @@ agr = definition.add_group
|
||||
egr = definition.end_group
|
||||
opt = definition.add_option
|
||||
|
||||
agr('global', 'Global') # {{{
|
||||
agr('host', 'Host environment') # {{{
|
||||
|
||||
opt('hostname', '*', option_type='hostname',
|
||||
long_text='''
|
||||
@@ -26,4 +26,23 @@ against is the hostname used by the remote computer, not the name you pass
|
||||
to SSH to connect to it.
|
||||
'''
|
||||
)
|
||||
|
||||
opt('+env', '',
|
||||
option_type='env',
|
||||
add_to_default=False,
|
||||
long_text='''
|
||||
Specify environment variables to set on the remote host. Note that
|
||||
environment variables can refer to each other, so if you use::
|
||||
|
||||
env MYVAR1=a
|
||||
env MYVAR2=$MYVAR1/$HOME/b
|
||||
|
||||
The value of MYVAR2 will be :code:`a/<path to home directory>/b`. Using
|
||||
:code:`VAR=` will set it to the empty string and using just :code:`VAR`
|
||||
will delete the variable from the child process' environment. The definitions
|
||||
are processed alphabetically.
|
||||
'''
|
||||
)
|
||||
|
||||
|
||||
egr() # }}}
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
# generated by gen-config.py DO NOT edit
|
||||
|
||||
import typing
|
||||
from kittens.ssh.options.utils import hostname
|
||||
from kittens.ssh.options.utils import env, hostname
|
||||
from kitty.conf.utils import merge_dicts
|
||||
|
||||
|
||||
class Parser:
|
||||
|
||||
def env(self, val: str, ans: typing.Dict[str, typing.Any]) -> None:
|
||||
for k, v in env(val, ans["env"]):
|
||||
ans["env"][k] = v
|
||||
|
||||
def hostname(self, val: str, ans: typing.Dict[str, typing.Any]) -> None:
|
||||
hostname(val, ans)
|
||||
|
||||
|
||||
def create_result_dict() -> typing.Dict[str, typing.Any]:
|
||||
return {
|
||||
'env': {},
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,11 +4,12 @@ import typing
|
||||
|
||||
|
||||
option_names = ( # {{{
|
||||
'hostname',) # }}}
|
||||
'env', 'hostname') # }}}
|
||||
|
||||
|
||||
class Options:
|
||||
hostname: str = '*'
|
||||
env: typing.Dict[str, str] = {}
|
||||
config_paths: typing.Tuple[str, ...] = ()
|
||||
config_overrides: typing.Tuple[str, ...] = ()
|
||||
|
||||
@@ -59,3 +60,4 @@ class Options:
|
||||
|
||||
|
||||
defaults = Options()
|
||||
defaults.env = {}
|
||||
|
||||
@@ -1,30 +1,51 @@
|
||||
#!/usr/bin/env python
|
||||
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
from typing import Any, Dict, Optional, Iterable, Tuple
|
||||
|
||||
|
||||
DELETE_ENV_VAR = '_delete_this_env_var_'
|
||||
|
||||
|
||||
def env(val: str, current_val: Dict[str, str]) -> Iterable[Tuple[str, str]]:
|
||||
val = val.strip()
|
||||
if val:
|
||||
if '=' in val:
|
||||
key, v = val.split('=', 1)
|
||||
key, v = key.strip(), v.strip()
|
||||
if key:
|
||||
yield key, v
|
||||
else:
|
||||
yield val, DELETE_ENV_VAR
|
||||
|
||||
|
||||
def init_results_dict(ans: Dict[str, Any]) -> Dict[str, Any]:
|
||||
ans['current_hostname'] = '*'
|
||||
ans['current_host_dict'] = chd = {'hostname': '*'}
|
||||
ans['per_host_dicts'] = {'*': chd}
|
||||
ans['hostname'] = '*'
|
||||
ans['per_host_dicts'] = {}
|
||||
return ans
|
||||
|
||||
|
||||
ignored_dict_keys = tuple(init_results_dict({}))
|
||||
def get_per_hosts_dict(results_dict: Dict[str, Any]) -> Dict[str, Dict[str, Any]]:
|
||||
ans: Dict[str, Dict[str, Any]] = results_dict.get('per_host_dicts', {}).copy()
|
||||
h = results_dict['hostname']
|
||||
hd = {k: v for k, v in results_dict.items() if k != 'per_host_dicts'}
|
||||
ans[h] = hd
|
||||
return ans
|
||||
|
||||
|
||||
first_seen_positions: Dict[str, int] = {}
|
||||
|
||||
|
||||
def hostname(val: str, dict_with_parse_results: Optional[Dict[str, Any]] = None) -> str:
|
||||
if dict_with_parse_results is not None:
|
||||
ch = dict_with_parse_results['current_hostname']
|
||||
ch = dict_with_parse_results['hostname']
|
||||
if val != ch:
|
||||
hd = dict_with_parse_results.copy()
|
||||
for k in ignored_dict_keys:
|
||||
del hd[k]
|
||||
phd = dict_with_parse_results['per_host_dicts']
|
||||
phd[ch] = hd
|
||||
from .parse import create_result_dict
|
||||
phd = get_per_hosts_dict(dict_with_parse_results)
|
||||
dict_with_parse_results.clear()
|
||||
dict_with_parse_results.update(phd.pop(val, create_result_dict()))
|
||||
dict_with_parse_results['per_host_dicts'] = phd
|
||||
dict_with_parse_results['current_hostname'] = val
|
||||
dict_with_parse_results['current_host_dict'] = phd.setdefault(val, {'hostname': val})
|
||||
dict_with_parse_results['hostname'] = val
|
||||
if val not in first_seen_positions:
|
||||
first_seen_positions[val] = len(first_seen_positions)
|
||||
return val
|
||||
|
||||
Reference in New Issue
Block a user