~ricardokirkner/click-toolbelt/release-0.4.0

« back to all changes in this revision

Viewing changes to click_toolbelt/common.py

  • Committer: Tarmac
  • Author(s): Ricardo Kirkner
  • Date: 2015-12-10 11:38:34 UTC
  • mfrom: (42.3.9 split-api-cli-4)
  • Revision ID: tarmac-20151210113834-ii54jbq9b4wzkmvm
[r=fgallina] various cleanups

- move inline import from command to get_oauth_session function
- removed unnecessary code
- improved test coverage
- added missing docstrings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2014 Canonical Ltd.  This software is licensed under the
2
2
# GNU General Public License version 3 (see the file LICENSE).
3
3
from __future__ import absolute_import, unicode_literals
4
 
import os
5
4
import time
6
5
from functools import wraps
7
6
 
8
7
from cliff.command import Command
9
 
from xdg.BaseDirectory import load_first_config
10
 
 
11
 
from click_toolbelt import __namespace__
12
 
from click_toolbelt.compat import ConfigParser, urlparse
13
 
from click_toolbelt.constants import (
14
 
    CLICK_TOOLBELT_PROJECT_NAME,
15
 
    UBUNTU_SSO_API_ROOT_URL,
16
 
)
17
 
 
18
 
 
19
 
def get_config():
20
 
    config_dir = load_first_config(CLICK_TOOLBELT_PROJECT_NAME)
21
 
    filename = os.path.join(config_dir, "%s.cfg" % __namespace__)
22
 
    parser = ConfigParser()
23
 
    if os.path.exists(filename):
24
 
        parser.read(filename)
25
 
 
26
 
    api_endpoint = os.environ.get(
27
 
        'UBUNTU_SSO_API_ROOT_URL', UBUNTU_SSO_API_ROOT_URL)
28
 
    location = urlparse(api_endpoint).netloc
29
 
 
30
 
    config = {}
31
 
    if parser.has_section(location):
32
 
        config.update(dict(parser.items(location)))
33
 
    return config
 
8
 
 
9
from click_toolbelt.api.common import get_oauth_session
 
10
from click_toolbelt.config import get_config
34
11
 
35
12
 
36
13
class CommandError(Exception):
43
20
        return get_config()
44
21
 
45
22
    def get_oauth_session(self):
46
 
        # FIXME: keep refactoring until this import can be made at module level
47
 
        # without triggering circular import issues
48
 
        from click_toolbelt.api.common import get_oauth_session
49
23
        return get_oauth_session()
50
24
 
51
25
    def take_action(self, parsed_args):
53
27
 
54
28
 
55
29
def is_scan_completed(response):
 
30
    """Return True if the response indicates the scan process completed."""
56
31
    if response.ok:
57
32
        return response.json().get('completed', False)
58
33
    return False