~ubuntu-branches/debian/sid/python-cs/sid

« back to all changes in this revision

Viewing changes to cs.py

  • Committer: Package Import Robot
  • Author(s): Vincent Bernat
  • Date: 2015-09-14 22:38:05 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20150914223805-pi1dmhn02ca2wati
Tags: 0.6.10-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
from collections import defaultdict
13
13
 
14
14
try:
15
 
    from configparser import ConfigParser
 
15
    from configparser import ConfigParser, NoSectionError
16
16
except ImportError:  # python 2
17
 
    from ConfigParser import ConfigParser
 
17
    from ConfigParser import ConfigParser, NoSectionError
18
18
 
19
19
try:
20
20
    from urllib.parse import quote
157
157
        return base64.b64encode(digest).decode('utf-8').strip()
158
158
 
159
159
 
160
 
def read_config():
 
160
def read_config(ini_group='cloudstack'):
161
161
    # Try env vars first
162
162
    os.environ.setdefault('CLOUDSTACK_METHOD', 'get')
163
163
    os.environ.setdefault('CLOUDSTACK_TIMEOUT', '10')
186
186
    conf = ConfigParser()
187
187
    conf.read(paths)
188
188
    try:
189
 
        return conf['cloudstack']
 
189
        return conf[ini_group]
190
190
    except AttributeError:  # python 2
191
 
        return dict(conf.items('cloudstack'))
 
191
        return dict(conf.items(ini_group))
192
192
 
193
193
 
194
194
def main():
195
 
    config = read_config()
196
 
 
197
195
    usage = "Usage: {0} <command> [option1=value1 " \
198
 
            "[option2=value2] ...] [--async] [--post]".format(sys.argv[0])
 
196
            "[option2=value2] ...] [--async] [--post] " \
 
197
            "[--region=<region>]".format(sys.argv[0])
199
198
 
200
199
    if len(sys.argv) == 1:
201
200
        raise SystemExit(usage)
203
202
    command = sys.argv[1]
204
203
    kwargs = defaultdict(set)
205
204
    flags = set()
 
205
    args = dict()
206
206
    for option in sys.argv[2:]:
207
207
        if option.startswith('--'):
208
 
            flags.add(option.strip('-'))
 
208
            option = option.strip('-')
 
209
            if '=' in option:
 
210
                key, value = option.split('=', 1)
 
211
                if not value:
 
212
                    raise SystemExit(usage)
 
213
                args[key] = value
 
214
            else:
 
215
                flags.add(option)
209
216
            continue
210
217
        if '=' not in option:
211
218
            raise SystemExit(usage)
213
220
        key, value = option.split('=', 1)
214
221
        kwargs[key].add(value.strip(" \"'"))
215
222
 
 
223
    region = args.get('region', 'cloudstack')
 
224
 
 
225
    try:
 
226
        config = read_config(ini_group=region)
 
227
    except NoSectionError:
 
228
        raise SystemExit("Error: region '%s' not in config" % region)
 
229
 
216
230
    if 'post' in flags:
217
231
        config['method'] = 'post'
218
232
    cs = CloudStack(**config)