aboutsummaryrefslogtreecommitdiff
path: root/pw_env_setup/py/pw_env_setup/json_visitor.py
blob: 8c217bf74553948cf23a4b944ac9973967d65427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright 2021 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Serializes an Environment into a JSON file."""

import json

# Disable super() warnings since this file must be Python 2 compatible.
# pylint: disable=super-with-arguments


class JSONVisitor(object):  # pylint: disable=useless-object-inheritance
    """Serializes an Environment into a JSON file."""

    def __init__(self, *args, **kwargs):
        super(JSONVisitor, self).__init__(*args, **kwargs)
        self._data = {}

    def serialize(self, env, outs):
        self._data = {
            'modify': {},
            'set': {},
        }

        env.accept(self)

        json.dump(self._data, outs, indent=4, separators=(',', ': '))
        outs.write('\n')
        self._data = {}

    def visit_set(self, set):  # pylint: disable=redefined-builtin
        self._data['set'][set.name] = set.value

    def visit_clear(self, clear):
        self._data['set'][clear.name] = None

    def _initialize_path_like_variable(self, name):
        default = {'append': [], 'prepend': [], 'remove': []}
        self._data['modify'].setdefault(name, default)

    def visit_remove(self, remove):
        self._initialize_path_like_variable(remove.name)
        self._data['modify'][remove.name]['remove'].append(remove.value)
        if remove.value in self._data['modify'][remove.name]['append']:
            self._data['modify'][remove.name]['append'].remove(remove.value)
        if remove.value in self._data['modify'][remove.name]['prepend']:
            self._data['modify'][remove.name]['prepend'].remove(remove.value)

    def visit_prepend(self, prepend):
        self._initialize_path_like_variable(prepend.name)
        self._data['modify'][prepend.name]['prepend'].append(prepend.value)
        if prepend.value in self._data['modify'][prepend.name]['remove']:
            self._data['modify'][prepend.name]['remove'].remove(prepend.value)

    def visit_append(self, append):
        self._initialize_path_like_variable(append.name)
        self._data['modify'][append.name]['append'].append(append.value)
        if append.value in self._data['modify'][append.name]['remove']:
            self._data['modify'][append.name]['remove'].remove(append.value)

    def visit_echo(self, echo):
        pass

    def visit_comment(self, comment):
        pass

    def visit_command(self, command):
        pass

    def visit_doctor(self, doctor):
        pass

    def visit_blank_line(self, blank_line):
        pass

    def visit_function(self, function):
        pass

    def visit_hash(self, hash):  # pylint: disable=redefined-builtin
        pass