Skip to content

Commit c89b2ac

Browse files
author
Clark Perkins
committed
Fixed a couple issues with a config file not existing
1 parent 45deaea commit c89b2ac

File tree

4 files changed

+53
-33
lines changed

4 files changed

+53
-33
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_python_version():
4848
'Jinja2==2.7.3',
4949
'PyYAML>=3.10',
5050
'click>=6.0,<7.0',
51-
'click-shell==0.3',
51+
'click-shell>=0.4',
5252
'colorama>=0.3,<0.4',
5353
'keyring==3.7',
5454
'requests>=2.4.0',

stackdio/client/__init__.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,19 @@ class StackdioClient(BlueprintMixin, FormulaMixin, AccountMixin, ImageMixin,
4444
def __init__(self, url=None, username=None, password=None, verify=True, cfg_file=None):
4545
self.config = StackdioConfig(cfg_file)
4646

47-
self.url = None
48-
self.username = None
49-
self.password = None
50-
self.verify = None
51-
52-
if self.config.usable_config:
53-
# Grab stuff from the config
54-
self.url = self.config.get('url')
55-
self.username = self.config.get('username')
56-
self.password = self.config.get_password()
57-
self.verify = self.config.get('verify', True)
47+
self._password = self.config.get_password()
5848

5949
if url is not None:
60-
self.url = url
50+
self.config['url'] = url
6151

6252
if username is not None and password is not None:
63-
self.username = username
64-
self.password = password
53+
self.config['username'] = username
54+
self._password = password
6555

6656
if verify is not None:
67-
self.verify = verify
57+
self.config['verify'] = verify
6858

69-
super(StackdioClient, self).__init__(url=self.url,
70-
auth=(self.username, self.password),
71-
verify=self.verify)
59+
super(StackdioClient, self).__init__()
7260

7361
if self.usable():
7462
try:
@@ -80,6 +68,22 @@ def __init__(self, url=None, username=None, password=None, verify=True, cfg_file
8068
raise IncompatibleVersionException('Server version {0}.{1}.{2} not '
8169
'supported.'.format(**self.version))
8270

71+
@property
72+
def url(self):
73+
return self.config.get('url')
74+
75+
@property
76+
def username(self):
77+
return self.config.get('username')
78+
79+
@property
80+
def password(self):
81+
return self._password or self.config.get_password()
82+
83+
@property
84+
def verify(self):
85+
return self.config.get('verify', True)
86+
8387
def usable(self):
8488
return self.url and self.username and self.password
8589

stackdio/client/config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#
1717

1818
import os
19+
import shutil
1920

2021
import click
2122
import keyring
@@ -51,7 +52,7 @@ def __init__(self, config_file=None, section='stackdio'):
5152

5253
self.section = section
5354

54-
self._cfg_file = config_file or CFG_FILE
55+
self._cfg_file = os.path.abspath(config_file or CFG_FILE)
5556

5657
self._config = ConfigParser()
5758

@@ -67,6 +68,11 @@ def __init__(self, config_file=None, section='stackdio'):
6768
self._config.add_section(section)
6869

6970
def save(self):
71+
full_path = os.path.dirname(self._cfg_file)
72+
73+
if not os.path.isdir(full_path):
74+
os.makedirs(full_path)
75+
7076
with open(self._cfg_file, 'w') as f:
7177
self._config.write(f)
7278

stackdio/client/http.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,11 @@ class HttpMixin(object):
4444
'xml': {'content-type': 'application/xml'},
4545
}
4646

47-
def __init__(self, url, auth=None, verify=True):
47+
def __init__(self):
4848
super(HttpMixin, self).__init__()
49-
50-
self.url = url
51-
self.http_options = {
52-
'auth': auth,
53-
'verify': verify,
54-
}
5549
self._http_log = logger
5650

57-
if not verify:
51+
if not self.verify:
5852
if self._http_log.handlers:
5953
self._http_log.warn(HTTP_INSECURE_MESSAGE)
6054
else:
@@ -63,6 +57,22 @@ def __init__(self, url, auth=None, verify=True):
6357
from requests.packages.urllib3 import disable_warnings
6458
disable_warnings()
6559

60+
@property
61+
def url(self):
62+
raise NotImplementedError()
63+
64+
@property
65+
def username(self):
66+
raise NotImplementedError()
67+
68+
@property
69+
def password(self):
70+
raise NotImplementedError()
71+
72+
@property
73+
def verify(self):
74+
raise NotImplementedError()
75+
6676
def usable(self):
6777
raise NotImplementedError()
6878

@@ -124,7 +134,7 @@ def __call__(self, *args, **kwargs):
124134
assert isinstance(self.obj, HttpMixin)
125135

126136
if not self.obj.usable():
127-
raise MissingUrlException('No url is set')
137+
raise MissingUrlException('No url is set. Please run `configure`.')
128138

129139
none_on_404 = kwargs.pop('none_on_404', False)
130140
raise_for_status = kwargs.pop('raise_for_status', True)
@@ -149,10 +159,10 @@ def __call__(self, *args, **kwargs):
149159
result = requests.request(method,
150160
url,
151161
data=data,
152-
auth=self.obj.http_options['auth'],
162+
auth=(self.obj.username, self.obj.password),
153163
headers=self.headers,
154164
params=kwargs,
155-
verify=self.obj.http_options['verify'])
165+
verify=self.obj.verify)
156166

157167
# Handle special conditions
158168
if none_on_404 and result.status_code == 404:
@@ -182,10 +192,10 @@ def __call__(self, *args, **kwargs):
182192
next_page = requests.request(method,
183193
next_url,
184194
data=data,
185-
auth=self.obj.http_options['auth'],
195+
auth=(self.obj.username, self.obj.password),
186196
headers=self.headers,
187197
params=kwargs,
188-
verify=self.obj.http_options['verify']).json()
198+
verify=self.obj.verify).json()
189199
res.extend(next_page['results'])
190200
next_url = next_page.get('next')
191201

0 commit comments

Comments
 (0)