Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2client/st2client/formatters/doc.py (3 issues)

1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from __future__ import absolute_import
17
18
import json
19
import logging
20
21
import yaml
22
23
from st2client import formatters
24
from st2client.utils import jsutil
25
26
__all__ = [
27
    'JsonFormatter',
28
    'YAMLFormatter'
29
]
30
31
LOG = logging.getLogger(__name__)
32
33
34
class BaseFormatter(formatters.Formatter):
35
    @classmethod
36
    def format(self, subject, *args, **kwargs):
0 ignored issues
show
Coding Style Best Practice introduced by
The first argument of the class method format should be named cls.
Loading history...
37
        attributes = kwargs.get('attributes', None)
38
        if type(subject) is str:
39
            subject = json.loads(subject)
40
        elif not isinstance(subject, (list, tuple)) and not hasattr(subject, '__iter__'):
41
            doc = subject if isinstance(subject, dict) else subject.__dict__
42
            keys = list(doc.keys()) if not attributes or 'all' in attributes else attributes
43
            docs = jsutil.get_kvps(doc, keys)
44
        else:
45
            docs = []
46
            for item in subject:
47
                doc = item if isinstance(item, dict) else item.__dict__
48
                keys = list(doc.keys()) if not attributes or 'all' in attributes else attributes
49
                docs.append(jsutil.get_kvps(doc, keys))
50
51
        return docs
52
53
54
class JsonFormatter(BaseFormatter):
55
56
    @classmethod
57
    def format(self, subject, *args, **kwargs):
0 ignored issues
show
Coding Style Best Practice introduced by
The first argument of the class method format should be named cls.
Loading history...
58
        docs = BaseFormatter.format(subject, *args, **kwargs)
59
        return json.dumps(docs, indent=4, sort_keys=True)
60
61
62
class YAMLFormatter(BaseFormatter):
63
64
    @classmethod
65
    def format(self, subject, *args, **kwargs):
0 ignored issues
show
Coding Style Best Practice introduced by
The first argument of the class method format should be named cls.
Loading history...
66
        docs = BaseFormatter.format(subject, *args, **kwargs)
67
        return yaml.safe_dump(docs, indent=4, default_flow_style=False)
68