stakkr.aliases.get_aliases()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
cc 4
eloc 9
nop 0
dl 0
loc 11
ccs 7
cts 9
cp 0.7778
crap 4.1755
rs 9.95
c 0
b 0
f 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