Passed
Push — master ( 61e6a6...28b40d )
by Emmanuel
07:42
created

stakkr.aliases.get_aliases()   A

Complexity

Conditions 4

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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