Completed
Pull Request — master (#33)
by Jace
04:24
created

dtb.find()   C

Complexity

Conditions 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7
Metric Value
cc 7
dl 0
loc 23
ccs 18
cts 18
cp 1
crap 7
rs 5.5
1
"""Classes and functions to interact with sharing services."""
2
3 1
import os
4 1
import logging
5
6
7 1
SERVICES = (
8
    'Dropbox',
9
    'Dropbox (Personal)',
10
)
11 1
SHARE = 'DropTheBeat'
12 1
SHARE_DEPTH = 3  # number of levels to search for share directory
13
14
15 1
def find(home=None):
16
    """Return the path to a sharing location."""
17
18 1
    home = home or os.path.expanduser("~")
19
20 1
    logging.debug("looking for service in {}...".format(home))
21 1
    for directory in os.listdir(home):
22 1
        if directory in SERVICES:
23 1
            service = os.path.join(home, directory)
24 1
            logging.debug("found service: {}".format(service))
25 1
            logging.debug("looking for '{}' in {}...".format(SHARE, service))
26 1
            for dirpath, dirnames, _, in os.walk(service):
27 1
                depth = dirpath.count(os.path.sep) - service.count(os.path.sep)
28 1
                if depth >= SHARE_DEPTH:
29 1
                    del dirnames[:]
30 1
                    continue
31 1
                path = os.path.join(dirpath, SHARE)
32 1
                if os.path.isdir(path) and \
33
                        not os.path.isfile(os.path.join(path, 'setup.py')):
34 1
                    logging.info("found share: {}".format(path))
35 1
                    return path
36
37
    raise EnvironmentError("no '{}' folder found".format(SHARE))
38