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