|
1 | 1 | import getpass
|
2 | 2 | from contextlib import suppress
|
3 |
| -from pathlib import Path, PurePath |
4 |
| -from typing import Sequence, Union |
| 3 | +from pathlib import Path |
| 4 | +import asyncssh |
| 5 | + |
5 | 6 | from asyncssh.config import SSHClientConfig
|
6 | 7 |
|
7 | 8 | SSH_CONFIG = Path("~", ".ssh", "config").expanduser()
|
8 |
| -FilePath = Union[str, PurePath] |
9 |
| -def parse_config( |
10 |
| - *, host, user=(), port=(), local_user=None, config_files=None |
11 |
| -): |
| 9 | + |
| 10 | + |
| 11 | +def parse_config(*, host, user=(), port=(), local_user=None, config_files=None): |
12 | 12 | if config_files is None:
|
13 | 13 | config_files = [SSH_CONFIG]
|
14 | 14 |
|
15 | 15 | if local_user is None:
|
16 |
| - with suppress(KeyError): |
| 16 | + with suppress(OSError): # Use OSError as getuser() might raise this. |
17 | 17 | local_user = getpass.getuser()
|
18 | 18 |
|
19 | 19 | last_config = None
|
20 | 20 | reload = False
|
21 |
| - config = SSHClientConfig( |
22 |
| - last_config =last_config, |
23 |
| - reload = reload, |
24 |
| - canonical = False, |
25 |
| - final=False, |
26 |
| - local_user = local_user, |
27 |
| - user = user, |
28 |
| - host = host, |
29 |
| - port = port, |
30 |
| - ) |
31 |
| - |
32 |
| - if config_files: |
33 |
| - if isinstance(config_files, (str, PurePath)): |
34 |
| - paths: Sequence[FilePath] = [config_files] |
35 |
| - else: |
36 |
| - paths = config_files |
37 | 21 |
|
38 |
| - for path in paths: |
39 |
| - config.parse(Path(path)) |
40 |
| - config.loaded = True |
41 |
| - return config |
| 22 | + # Check asyncssh version |
| 23 | + version = tuple(map(int, asyncssh.__version__.split("."))) |
| 24 | + if version <= (2, 18, 0): # Compare version properly |
| 25 | + return SSHClientConfig.load( |
| 26 | + last_config, |
| 27 | + config_files, |
| 28 | + reload, |
| 29 | + local_user, |
| 30 | + user, |
| 31 | + host, |
| 32 | + port, |
| 33 | + ) |
| 34 | + else: |
| 35 | + canonical = False # Fixed typo |
| 36 | + final = False # Fixed typo |
| 37 | + return SSHClientConfig.load( |
| 38 | + last_config, |
| 39 | + config_files, |
| 40 | + reload, |
| 41 | + canonical, # Use correct parameter |
| 42 | + final, # Use correct parameter |
| 43 | + local_user, |
| 44 | + user, |
| 45 | + host, |
| 46 | + port, |
| 47 | + ) |
0 commit comments