Total Complexity | 10 |
Total Lines | 37 |
Duplicated Lines | 0 % |
Coverage | 78.26% |
Changes | 0 |
1 | # coding: utf-8 |
||
2 | 1 | """Aliases management""" |
|
3 | |||
4 | 1 | from sys import argv |
|
5 | 1 | from yaml import safe_load, error |
|
6 | 1 | from stakkr.configreader import get_config_and_project_dir |
|
7 | |||
8 | |||
9 | 1 | def get_config_from_argv(argv: list): |
|
10 | """Search for a config file option in command line""" |
||
11 | 1 | for index, arg in enumerate(argv): |
|
12 | # manage "=" sign |
||
13 | 1 | if arg.find('=') != -1 and arg.split('=')[0] == '--config': |
|
14 | return arg.split('=')[1] |
||
15 | |||
16 | # else search for -c or --config and get the next value |
||
17 | 1 | if arg in ['-c', '--config']: |
|
18 | 1 | try: |
|
19 | 1 | return argv[index + 1] |
|
20 | except IndexError: |
||
21 | raise ValueError('You must specify the config file name') |
||
22 | |||
23 | 1 | return None |
|
24 | |||
25 | |||
26 | 1 | def get_aliases(): |
|
27 | """Get aliases from config file""" |
||
28 | 1 | config_file, _ = get_config_and_project_dir(get_config_from_argv(argv[1:])) |
|
29 | 1 | config = {} |
|
30 | 1 | try: |
|
31 | 1 | with open(config_file, 'r') as stream: |
|
32 | 1 | config = safe_load(stream) |
|
33 | except (error.YAMLError, FileNotFoundError): |
||
34 | pass |
||
35 | |||
36 | return config['aliases'] if 'aliases' in config else {} |
||
37 |