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