stakkr.aliases.get_config_from_argv()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.972

Importance

Changes 0
Metric Value
cc 6
eloc 10
nop 1
dl 0
loc 15
ccs 7
cts 10
cp 0.7
crap 6.972
rs 8.6666
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