1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Serializes an Environment into a JSON file."""
15
16import json
17
18# Disable super() warnings since this file must be Python 2 compatible.
19# pylint: disable=super-with-arguments
20
21
22class JSONVisitor(object):  # pylint: disable=useless-object-inheritance
23    """Serializes an Environment into a JSON file."""
24    def __init__(self, *args, **kwargs):
25        super(JSONVisitor, self).__init__(*args, **kwargs)
26        self._data = {}
27
28    def serialize(self, env, outs):
29        self._data = {
30            'modify': {},
31            'set': {},
32        }
33
34        env.accept(self)
35
36        json.dump(self._data, outs, indent=4, separators=(',', ': '))
37        outs.write('\n')
38        self._data = {}
39
40    def visit_set(self, set):  # pylint: disable=redefined-builtin
41        self._data['set'][set.name] = set.value
42
43    def visit_clear(self, clear):
44        self._data['set'][clear.name] = None
45
46    def _initialize_path_like_variable(self, name):
47        default = {'append': [], 'prepend': [], 'remove': []}
48        self._data['modify'].setdefault(name, default)
49
50    def visit_remove(self, remove):
51        self._initialize_path_like_variable(remove.name)
52        self._data['modify'][remove.name]['remove'].append(remove.value)
53        if remove.value in self._data['modify'][remove.name]['append']:
54            self._data['modify'][remove.name]['append'].remove(remove.value)
55        if remove.value in self._data['modify'][remove.name]['prepend']:
56            self._data['modify'][remove.name]['prepend'].remove(remove.value)
57
58    def visit_prepend(self, prepend):
59        self._initialize_path_like_variable(prepend.name)
60        self._data['modify'][prepend.name]['prepend'].append(prepend.value)
61        if prepend.value in self._data['modify'][prepend.name]['remove']:
62            self._data['modify'][prepend.name]['remove'].remove(prepend.value)
63
64    def visit_append(self, append):
65        self._initialize_path_like_variable(append.name)
66        self._data['modify'][append.name]['append'].append(append.value)
67        if append.value in self._data['modify'][append.name]['remove']:
68            self._data['modify'][append.name]['remove'].remove(append.value)
69
70    def visit_echo(self, echo):
71        pass
72
73    def visit_comment(self, comment):
74        pass
75
76    def visit_command(self, command):
77        pass
78
79    def visit_doctor(self, doctor):
80        pass
81
82    def visit_blank_line(self, blank_line):
83        pass
84
85    def visit_function(self, function):
86        pass
87
88    def visit_hash(self, hash):  # pylint: disable=redefined-builtin
89        pass
90