blob: 39abe6bda47846365bb1f42482df9de5b7332d4e [file] [log] [blame]
Enrico Granata3f1052b2012-03-13 21:52:00 +00001"""
2Objective-C runtime wrapper for use by LLDB Python formatters
3
4part of The LLVM Compiler Infrastructure
5This file is distributed under the University of Illinois Open Source
6License. See LICENSE.TXT for details.
7"""
Kate Stoneb9c1b512016-09-06 20:57:50 +00008
9
Enrico Granatacfdafa32012-03-05 19:56:33 +000010class AttributesDictionary:
Enrico Granatacfdafa32012-03-05 19:56:33 +000011
Kate Stoneb9c1b512016-09-06 20:57:50 +000012 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
Enrico Granatacfdafa32012-03-05 19:56:33 +000016
Kate Stoneb9c1b512016-09-06 20:57:50 +000017 def __getattr__(self, name):
18 if not self._check_exists(name):
19 return None
20 value = self._dictionary[name]
21 return value
Enrico Granatacfdafa32012-03-05 19:56:33 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023 def _set_impl(self, name, value):
24 self._dictionary[name] = value
Enrico Granatacfdafa32012-03-05 19:56:33 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026 def _check_exists(self, name):
27 return name in self._dictionary
Enrico Granatacfdafa32012-03-05 19:56:33 +000028
Kate Stoneb9c1b512016-09-06 20:57:50 +000029 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)
Enrico Granatacfdafa32012-03-05 19:56:33 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 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)