Passed
Push — master ( 0c68df...e3ee72 )
by Dongxin
01:27
created

dumpAST()   F

Complexity

Conditions 20

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
c 1
b 0
f 0
dl 0
loc 53
rs 3.309

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like dumpAST() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from __future__ import absolute_import, unicode_literals
2
3
from builtins import str
4
import json
5
from CommonMark.node import is_container
6
7
8
def prepare(obj, topnode=False):
9
    """Walk the complete AST, only returning needed data.
10
11
    This removes circular references and allows us to output
12
    JSON.
13
    """
14
    a = []
15
    for subnode, entered in obj.walker():
16
        rep = {
17
            'type': subnode.t,
18
        }
19
        if subnode.literal:
20
            rep['literal'] = subnode.literal
21
22
        if subnode.string_content:
23
            rep['string_content'] = subnode.string_content
24
25
        if subnode.title:
26
            rep['title'] = subnode.title
27
28
        if subnode.info:
29
            rep['info'] = subnode.info
30
31
        if subnode.destination:
32
            rep['destination'] = subnode.destination
33
34
        if subnode.list_data:
35
            rep['list_data'] = subnode.list_data
36
37
        if is_container(subnode):
38
            rep['children'] = []
39
40
        if entered and len(a) > 0:
41
            if a[-1]['children']:
42
                a[-1]['children'].append(rep)
43
            else:
44
                a[-1]['children'] = [rep]
45
        else:
46
            a.append(rep)
47
    return a
48
49
50
def dumpJSON(obj):
51
    """Output AST in JSON form, this is destructive of block."""
52
    prepared = prepare(obj)
53
    return json.dumps(prepared, indent=4, sort_keys=True)
54
55
56
def dumpAST(obj, ind=0, topnode=False):
57
    """Print out a block/entire AST."""
58
    indChar = ("\t" * ind) + "-> " if ind else ""
59
    print(indChar + "[" + obj.t + "]")
60
    if not obj.title == "":
61
        print("\t" + indChar + "Title: " + (obj.title or ''))
62
    if not obj.info == "":
63
        print("\t" + indChar + "Info: " + (obj.info or ''))
64
    if not obj.destination == "":
65
        print("\t" + indChar + "Destination: " + (obj.destination or ''))
66
    if obj.is_open:
67
        print("\t" + indChar + "Open: " + str(obj.is_open))
68
    if obj.last_line_blank:
69
        print(
70
            "\t" + indChar + "Last line blank: " + str(obj.last_line_blank))
71
    if obj.sourcepos:
72
        print("\t" + indChar + "Sourcepos: " + str(obj.sourcepos))
73
    if not obj.string_content == "":
74
        print("\t" + indChar + "String content: " + (obj.string_content or ''))
75
    if not obj.info == "":
76
        print("\t" + indChar + "Info: " + (obj.info or ''))
77
    if not obj.literal == "":
78
        print("\t" + indChar + "Literal: " + (obj.literal or ''))
79
    if obj.list_data.get('type'):
80
        print("\t" + indChar + "List Data: ")
81
        print("\t\t" + indChar + "[type] = " + obj.list_data.get('type'))
82
        if obj.list_data.get('bullet_char'):
83
            print(
84
                "\t\t" + indChar + "[bullet_char] = " +
85
                obj.list_data['bullet_char'])
86
        if obj.list_data.get('start'):
87
            print(
88
                "\t\t" + indChar + "[start] = " +
89
                str(obj.list_data.get('start')))
90
        if obj.list_data.get('delimiter'):
91
            print(
92
                "\t\t" + indChar + "[delimiter] = " +
93
                obj.list_data.get('delimiter'))
94
        if obj.list_data.get('padding'):
95
            print(
96
                "\t\t" + indChar + "[padding] = " +
97
                str(obj.list_data.get('padding')))
98
        if obj.list_data.get('marker_offset'):
99
            print(
100
                "\t\t" + indChar + "[marker_offset] = " +
101
                str(obj.list_data.get('marker_offset')))
102
    if obj.walker:
103
        print("\t" + indChar + "Children:")
104
        walker = obj.walker()
105
        nxt = walker.nxt()
106
        while nxt is not None and topnode is False:
107
            dumpAST(nxt['node'], ind + 2, topnode=True)
108
            nxt = walker.nxt()
109