Passed
Push — develop ( 3ebd01...06d53f )
by Plexxi
07:35 queued 03:53
created

BaseFormatter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 18
rs 10
c 1
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
C format() 0 17 9
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
import json
17
import logging
18
19
import yaml
20
21
from st2client import formatters
22
from st2client.utils import jsutil
23
24
__all__ = [
25
    'JsonFormatter',
26
    'YAMLFormatter'
27
]
28
29
LOG = logging.getLogger(__name__)
30
31
32
class BaseFormatter(formatters.Formatter):
33
    @classmethod
34
    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...
35
        attributes = kwargs.get('attributes', None)
36
        if type(subject) is str:
37
            subject = json.loads(subject)
38
        elif not isinstance(subject, (list, tuple)) and not hasattr(subject, '__iter__'):
39
            doc = subject if isinstance(subject, dict) else subject.__dict__
40
            keys = doc.keys() if not attributes or 'all' in attributes else attributes
41
            docs = jsutil.get_kvps(doc, keys)
42
        else:
43
            docs = []
44
            for item in subject:
45
                doc = item if isinstance(item, dict) else item.__dict__
46
                keys = doc.keys() if not attributes or 'all' in attributes else attributes
47
                docs.append(jsutil.get_kvps(doc, keys))
48
49
        return docs
50
51
52
class JsonFormatter(BaseFormatter):
53
54
    @classmethod
55
    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...
56
        docs = BaseFormatter.format(subject, *args, **kwargs)
57
        return json.dumps(docs, indent=4, sort_keys=True)
58
59
60
class YAMLFormatter(BaseFormatter):
61
62
    @classmethod
63
    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...
64
        docs = BaseFormatter.format(subject, *args, **kwargs)
65
        return yaml.safe_dump(docs, indent=4, default_flow_style=False)
66