pylint fixes: remove redundant parens, fix comparison order
[yardstick.git] / api / utils / common.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10 import collections
11 import logging
12
13 from flask import jsonify
14
15 from api.utils.daemonthread import DaemonThread
16 from yardstick.cmd.cli import YardstickCLI
17 import six
18
19 logger = logging.getLogger(__name__)
20
21
22 def translate_to_str(obj):
23     if isinstance(obj, collections.Mapping):
24         return {str(k): translate_to_str(v) for k, v in obj.items()}
25     elif isinstance(obj, list):
26         return [translate_to_str(ele) for ele in obj]
27     elif isinstance(obj, six.text_type):
28         return str(obj)
29     return obj
30
31
32 def get_command_list(command_list, opts, args):
33
34     command_list.append(args)
35
36     command_list.extend(('--{}'.format(k) for k in opts if k != 'task-args'))
37
38     task_args = opts.get('task-args', '')
39     if task_args:
40         command_list.extend(['--task-args', str(task_args)])
41
42     return command_list
43
44
45 def exec_command_task(command_list, task_dict):   # pragma: no cover
46     daemonthread = DaemonThread(YardstickCLI().api, (command_list, task_dict))
47     daemonthread.start()
48
49
50 def error_handler(message):
51     logger.debug(message)
52     result = {
53         'status': 'error',
54         'message': message
55     }
56     return jsonify(result)
57
58
59 def result_handler(status, data):
60     result = {
61         'status': status,
62         'result': data
63     }
64     return jsonify(result)
65
66
67 class Url(object):
68
69     def __init__(self, url, resource, endpoint):
70         super(Url, self).__init__()
71         self.url = url
72         self.resource = resource
73         self.endpoint = endpoint