kunit_config.py (e6f61920653925e6fa9aceb5cdb47ecf543986c8) kunit_config.py (0453f984a7b9458f0e469afb039f2841308b1bef)
1# SPDX-License-Identifier: GPL-2.0
2#
3# Builds a .config from a kunitconfig.
4#
5# Copyright (C) 2019, Google LLC.
6# Author: Felix Guo <felixguoxiuping@gmail.com>
7# Author: Brendan Higgins <brendanhiggins@google.com>
8

--- 6 unchanged lines hidden (view full) ---

15
16@dataclass(frozen=True)
17class KconfigEntry:
18 name: str
19 value: str
20
21 def __str__(self) -> str:
22 if self.value == 'n':
1# SPDX-License-Identifier: GPL-2.0
2#
3# Builds a .config from a kunitconfig.
4#
5# Copyright (C) 2019, Google LLC.
6# Author: Felix Guo <felixguoxiuping@gmail.com>
7# Author: Brendan Higgins <brendanhiggins@google.com>
8

--- 6 unchanged lines hidden (view full) ---

15
16@dataclass(frozen=True)
17class KconfigEntry:
18 name: str
19 value: str
20
21 def __str__(self) -> str:
22 if self.value == 'n':
23 return r'# CONFIG_%s is not set' % (self.name)
24 else:
25 return r'CONFIG_%s=%s' % (self.name, self.value)
23 return f'# CONFIG_{self.name} is not set'
24 return f'CONFIG_{self.name}={self.value}'
26
27
28class KconfigParseError(Exception):
29 """Error parsing Kconfig defconfig or .config."""
30
31
25
26
27class KconfigParseError(Exception):
28 """Error parsing Kconfig defconfig or .config."""
29
30
32class Kconfig(object):
31class Kconfig:
33 """Represents defconfig or .config specified using the Kconfig language."""
34
35 def __init__(self) -> None:
36 self._entries = [] # type: List[KconfigEntry]
37
38 def entries(self) -> Set[KconfigEntry]:
39 return set(self._entries)
40
41 def add_entry(self, entry: KconfigEntry) -> None:
42 self._entries.append(entry)
43
44 def is_subset_of(self, other: 'Kconfig') -> bool:
45 other_dict = {e.name: e.value for e in other.entries()}
46 for a in self.entries():
47 b = other_dict.get(a.name)
48 if b is None:
49 if a.value == 'n':
50 continue
51 return False
32 """Represents defconfig or .config specified using the Kconfig language."""
33
34 def __init__(self) -> None:
35 self._entries = [] # type: List[KconfigEntry]
36
37 def entries(self) -> Set[KconfigEntry]:
38 return set(self._entries)
39
40 def add_entry(self, entry: KconfigEntry) -> None:
41 self._entries.append(entry)
42
43 def is_subset_of(self, other: 'Kconfig') -> bool:
44 other_dict = {e.name: e.value for e in other.entries()}
45 for a in self.entries():
46 b = other_dict.get(a.name)
47 if b is None:
48 if a.value == 'n':
49 continue
50 return False
52 elif a.value != b:
51 if a.value != b:
53 return False
54 return True
55
56 def merge_in_entries(self, other: 'Kconfig') -> None:
57 if other.is_subset_of(self):
58 return
59 self._entries = list(self.entries().union(other.entries()))
60

--- 25 unchanged lines hidden (view full) ---

86 empty_match = is_not_set_matcher.match(line)
87 if empty_match:
88 entry = KconfigEntry(empty_match.group(1), 'n')
89 kconfig.add_entry(entry)
90 continue
91
92 if line[0] == '#':
93 continue
52 return False
53 return True
54
55 def merge_in_entries(self, other: 'Kconfig') -> None:
56 if other.is_subset_of(self):
57 return
58 self._entries = list(self.entries().union(other.entries()))
59

--- 25 unchanged lines hidden (view full) ---

85 empty_match = is_not_set_matcher.match(line)
86 if empty_match:
87 entry = KconfigEntry(empty_match.group(1), 'n')
88 kconfig.add_entry(entry)
89 continue
90
91 if line[0] == '#':
92 continue
94 else:
95 raise KconfigParseError('Failed to parse: ' + line)
93 raise KconfigParseError('Failed to parse: ' + line)
96 return kconfig
94 return kconfig