1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2025 Northeastern University 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 19from dataclasses import dataclass, asdict 20from typing import Any, Dict, List, Callable, Optional 21 22from ohos.sbom.data.build_setting import BuildSetting 23from ohos.sbom.data.target import Target 24 25 26@dataclass 27class NinjaJson: 28 _build_setting: 'BuildSetting' 29 _targets: Dict[str, 'Target'] 30 31 @property 32 def build_setting(self) -> 'BuildSetting': 33 return self._build_setting 34 35 @classmethod 36 def from_dict(cls, data: Dict[str, Any]) -> 'NinjaJson': 37 return cls( 38 _build_setting=BuildSetting.from_dict(data.get("build_settings", {})), 39 _targets={name: Target.from_dict(name, tdict) 40 for name, tdict in data.get("targets", {}).items()}, 41 ) 42 43 def get_target(self, name: str) -> Optional['Target']: 44 return self._targets.get(name) 45 46 def all_targets(self) -> List['Target']: 47 return list(self._targets.values()) 48 49 def filter_targets(self, predicate: Callable[['Target'], bool]) -> List['Target']: 50 return [t for t in self._targets.values() if predicate(t)] 51 52 def to_dict(self) -> Dict[str, Any]: 53 return { 54 "build_settings": asdict(self._build_setting), 55 "targets": { 56 name: target.to_dict_without_name() 57 for name, target in self._targets.items() 58 } 59 } 60