xref: /aosp_15_r20/external/pigweed/pw_env_setup/py/pw_env_setup/json_visitor.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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
19class JSONVisitor:
20    """Serializes an Environment into a JSON file."""
21
22    def __init__(self, *args, **kwargs):
23        super().__init__(*args, **kwargs)
24        self._data = {}
25
26    def serialize(self, env, outs):
27        """Write a json file based on the given environment.
28
29        Args:
30            env (environment.Environment): Environment variables to use.
31            outs (file): JSON file to write.
32        """
33        self._data = {
34            'modify': {},
35            'set': {},
36        }
37
38        env.accept(self)
39
40        json.dump(self._data, outs, indent=4, separators=(',', ': '))
41        outs.write('\n')
42        self._data = {}
43
44    def visit_set(self, set):  # pylint: disable=redefined-builtin
45        self._data['set'][set.name] = set.value
46
47    def visit_clear(self, clear):
48        self._data['set'][clear.name] = None
49
50    def _initialize_path_like_variable(self, name):
51        default = {'append': [], 'prepend': [], 'remove': []}
52        self._data['modify'].setdefault(name, default)
53
54    def visit_remove(self, remove):
55        self._initialize_path_like_variable(remove.name)
56        self._data['modify'][remove.name]['remove'].append(remove.value)
57        if remove.value in self._data['modify'][remove.name]['append']:
58            self._data['modify'][remove.name]['append'].remove(remove.value)
59        if remove.value in self._data['modify'][remove.name]['prepend']:
60            self._data['modify'][remove.name]['prepend'].remove(remove.value)
61
62    def visit_prepend(self, prepend):
63        self._initialize_path_like_variable(prepend.name)
64        self._data['modify'][prepend.name]['prepend'].append(prepend.value)
65        if prepend.value in self._data['modify'][prepend.name]['remove']:
66            self._data['modify'][prepend.name]['remove'].remove(prepend.value)
67
68    def visit_append(self, append):
69        self._initialize_path_like_variable(append.name)
70        self._data['modify'][append.name]['append'].append(append.value)
71        if append.value in self._data['modify'][append.name]['remove']:
72            self._data['modify'][append.name]['remove'].remove(append.value)
73
74    def visit_echo(self, echo):
75        pass
76
77    def visit_comment(self, comment):
78        pass
79
80    def visit_command(self, command):
81        pass
82
83    def visit_doctor(self, doctor):
84        pass
85
86    def visit_blank_line(self, blank_line):
87        pass
88
89    def visit_function(self, function):
90        pass
91
92    def visit_hash(self, hash):  # pylint: disable=redefined-builtin
93        pass
94