• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2
3#
4# Copyright 2024, The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19"""This is a general JSON Config checker that checks string values.
20"""
21
22import json
23import os
24import subprocess
25import sys
26
27class PixelJSONFieldNameChecker(object):
28  """A object for common JSON configuration checking.
29
30    Takes a json_files = dict(file_path, JSON Object) and
31    field_names_path (list of strings) and checks every field name
32    against the list.
33
34    Typical usage example:
35
36    foo = PixelFieldNameChecker(files, vocabulary_path)
37    success, error = foo.check_json_field_names()
38  """
39  valid_field_names = None
40  json_files = None
41  commit_sha = None
42
43  def __init__(self, json_files, field_names_path):
44    self.valid_field_names = self.load_field_names_from_txt(field_names_path)
45    self.json_files = json_files
46
47  def load_field_names_from_txt(self, file_path):
48   """ Function to load a list of new line separated field names
49   from a file at file_path.
50
51   input:
52    file_path: path to lexicon
53
54   output: Set of strings.
55   """
56   field_names = set()
57   with open(file_path, 'r') as f:
58     for line in f:
59       name = line.strip()
60       if name:
61         field_names.add(name)
62   return field_names
63
64  def _check_json_field_names(self, data):
65    """ Recursive function that traverses the json object
66      checking every field and string value.
67
68    input:
69      data: JSON object
70
71    output:
72      Tuple of Success and name if unknown.
73    """
74    if isinstance(data, dict):
75      for key, value in data.items():
76        if key not in self.valid_field_names:
77          return False, key
78        ok, name = self._check_json_field_names(value)
79        if not ok:
80          return False, name
81
82    if isinstance(data, list):
83      for item in data:
84        ok, name = self._check_json_field_names(item)
85        if not ok:
86          return False, name
87
88    return True, None
89
90  def check_json_field_names(self):
91    """ Entry function to check strings and field names if known.
92
93    output:
94      Tuple of Success and error message.
95    """
96    for file_path, json_object in self.json_files.items():
97      success, message = self._check_json_field_names(json_object)
98      if not success:
99        return False, "File " + file_path +": Unknown string: " + message
100
101    return True, ""
102