Completed
Push — master ( 4f4209...7e5a08 )
by Charles
02:37
created

create_parent_dirs()   A

Complexity

Conditions 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 4
rs 9.2
c 1
b 0
f 0
1
# -*- coding: utf-8 -*-
2 1
"""
3
    various tool helpers
4
"""
5 1
import os
6
7
8 1
def create_parent_dirs(path, cwd=None, mode=493):
9
    '''
10
    create parent directories tree for a path
11
12
    mode : 493 in decimal = 0755 in octal
13
    '''
14
15 1
    if not os.path.isabs(path):
16 1
        cwd = cwd if cwd else os.getcwd()
17 1
        path = cwd + '/' + path
18
19 1
    parent_dir = os.path.dirname(path)
20 1
    if not os.path.exists(parent_dir):
21 1
        os.makedirs(parent_dir, mode)
22
23 1
    return path
24
25
26 1
def encode(text, encoding='utf-8'):
27
    '''
28
    encode a string if necessary
29
    '''
30
31
    if isinstance(text, unicode):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'unicode'
Loading history...
32
        return text.encode(encoding)
33
    else:
34
        return text
35
36
37 1
def flatten(items):
38
    '''
39
    transform a list to a string representation
40
    '''
41
42 1
    if isinstance(items, list):
43 1
        flattened_list = ''
44 1
        if len(items):
45 1
            flattened_list = "'{}'".format("', '".join(items))
46 1
        return "[{}]".format(flattened_list)
47
    else:
48
        return items
49