| 1 |
""" |
| 2 |
Objective-C runtime wrapper for use by LLDB Python formatters |
| 3 |
|
| 4 |
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 |
See https://llvm.org/LICENSE.txt for license information. |
| 6 |
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 |
""" |
| 8 |
|
| 9 |
|
| 10 |
class AttributesDictionary: |
| 11 |
|
| 12 |
def __init__(self, allow_reset=True): |
| 13 |
# need to do it this way to prevent endless recursion |
| 14 |
self.__dict__['_dictionary'] = {} |
| 15 |
self.__dict__['_allow_reset'] = allow_reset |
| 16 |
|
| 17 |
def __getattr__(self, name): |
| 18 |
if not self._check_exists(name): |
| 19 |
return None |
| 20 |
value = self._dictionary[name] |
| 21 |
return value |
| 22 |
|
| 23 |
def _set_impl(self, name, value): |
| 24 |
self._dictionary[name] = value |
| 25 |
|
| 26 |
def _check_exists(self, name): |
| 27 |
return name in self._dictionary |
| 28 |
|
| 29 |
def __setattr__(self, name, value): |
| 30 |
if self._allow_reset: |
| 31 |
self._set_impl(name, value) |
| 32 |
else: |
| 33 |
self.set_if_necessary(name, value) |
| 34 |
|
| 35 |
def set_if_necessary(self, name, value): |
| 36 |
if not self._check_exists(name): |
| 37 |
self._set_impl(name, value) |
| 38 |
return True |
| 39 |
return False |
| 40 |
|
| 41 |
def __len__(self): |
| 42 |
return len(self._dictionary) |