• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
25    def __init__(self, *args, **kwargs):
26        super(JSONVisitor, self).__init__(*args, **kwargs)
27        self._data = {}
28
29    def serialize(self, env, outs):
30        self._data = {
31            'modify': {},
32            'set': {},
33        }
34
35        env.accept(self)
36
37        json.dump(self._data, outs, indent=4, separators=(',', ': '))
38        outs.write('\n')
39        self._data = {}
40
41    def visit_set(self, set):  # pylint: disable=redefined-builtin
42        self._data['set'][set.name] = set.value
43
44    def visit_clear(self, clear):
45        self._data['set'][clear.name] = None
46
47    def _initialize_path_like_variable(self, name):
48        default = {'append': [], 'prepend': [], 'remove': []}
49        self._data['modify'].setdefault(name, default)
50
51    def visit_remove(self, remove):
52        self._initialize_path_like_variable(remove.name)
53        self._data['modify'][remove.name]['remove'].append(remove.value)
54        if remove.value in self._data['modify'][remove.name]['append']:
55            self._data['modify'][remove.name]['append'].remove(remove.value)
56        if remove.value in self._data['modify'][remove.name]['prepend']:
57            self._data['modify'][remove.name]['prepend'].remove(remove.value)
58
59    def visit_prepend(self, prepend):
60        self._initialize_path_like_variable(prepend.name)
61        self._data['modify'][prepend.name]['prepend'].append(prepend.value)
62        if prepend.value in self._data['modify'][prepend.name]['remove']:
63            self._data['modify'][prepend.name]['remove'].remove(prepend.value)
64
65    def visit_append(self, append):
66        self._initialize_path_like_variable(append.name)
67        self._data['modify'][append.name]['append'].append(append.value)
68        if append.value in self._data['modify'][append.name]['remove']:
69            self._data['modify'][append.name]['remove'].remove(append.value)
70
71    def visit_echo(self, echo):
72        pass
73
74    def visit_comment(self, comment):
75        pass
76
77    def visit_command(self, command):
78        pass
79
80    def visit_doctor(self, doctor):
81        pass
82
83    def visit_blank_line(self, blank_line):
84        pass
85
86    def visit_function(self, function):
87        pass
88
89    def visit_hash(self, hash):  # pylint: disable=redefined-builtin
90        pass
91