Patrick Williams | ce8a467 | 2016-10-15 10:38:54 -0500 | [diff] [blame] | 1 | import inflection |
| 2 | |
Patrick Williams | f5c3cde | 2016-10-21 11:49:01 -0500 | [diff] [blame] | 3 | |
Patrick Williams | a8972b0 | 2016-10-09 16:09:57 -0500 | [diff] [blame] | 4 | class NamedElement(object): |
| 5 | def __init__(self, **kwargs): |
William A. Kennington III | 66ef099 | 2019-05-17 14:16:34 -0700 | [diff] [blame] | 6 | super(NamedElement, self).__init__() |
Patrick Williams | a8972b0 | 2016-10-09 16:09:57 -0500 | [diff] [blame] | 7 | self.name = kwargs.pop('name', "unnamed") |
| 8 | self.description = kwargs.pop('description', "") |
Patrick Williams | ce8a467 | 2016-10-15 10:38:54 -0500 | [diff] [blame] | 9 | |
Patrick Williams | 18ce803 | 2020-01-31 18:47:38 -0800 | [diff] [blame] | 10 | def __getattribute__(self, name): |
Patrick Williams | 9f77a47 | 2020-07-16 14:23:51 -0500 | [diff] [blame] | 11 | lam = {'CamelCase': lambda: inflection.camelize(self.name), |
| 12 | 'camelCase': lambda: inflection.camelize(self.name, False), |
| 13 | 'snake_case': lambda: inflection.underscore(self.name)}\ |
Patrick Williams | ce8a467 | 2016-10-15 10:38:54 -0500 | [diff] [blame] | 14 | .get(name) |
| 15 | |
Patrick Williams | 9f77a47 | 2020-07-16 14:23:51 -0500 | [diff] [blame] | 16 | if lam: |
| 17 | return NamedElement.__fixup_name(lam()) |
Patrick Williams | ce8a467 | 2016-10-15 10:38:54 -0500 | [diff] [blame] | 18 | try: |
Patrick Williams | 18ce803 | 2020-01-31 18:47:38 -0800 | [diff] [blame] | 19 | return super(NamedElement, self).__getattribute__(name) |
Patrick Williams | c6a5bd8 | 2020-07-27 12:07:48 -0500 | [diff] [blame] | 20 | except Exception: |
Patrick Williams | ce8a467 | 2016-10-15 10:38:54 -0500 | [diff] [blame] | 21 | raise AttributeError("Attribute '%s' not found in %s.NamedElement" |
| 22 | % (name, self.__module__)) |
Patrick Williams | cf078c4 | 2016-12-12 12:13:48 -0600 | [diff] [blame] | 23 | |
| 24 | """ Some names are reserved in some languages. Fixup names to avoid using |
| 25 | reserved words. |
| 26 | """ |
| 27 | @staticmethod |
| 28 | def __fixup_name(name): |
| 29 | # List of reserved words from http://en.cppreference.com/w/cpp/keyword |
| 30 | cppReserved = frozenset({ |
| 31 | "alignas", "alignof", "and", "and_eq", "asm", "auto", |
| 32 | "bitand", "bitor", "bool", "break", "case", "catch", "char", |
Patrick Williams | fcff676 | 2021-04-30 20:18:09 -0500 | [diff] [blame] | 33 | "char8_t", "char16_t", "char32_t", "class", "compl", "concept", |
| 34 | "const", "consteval", "constexpr", "constinit", "const_cast", |
| 35 | "continue", "co_await", "co_return", "co_yield", "decltype", |
| 36 | "default", "delete", "do", "double", "dynamic_cast", "else", |
| 37 | "enum", "explicit", "export", "extern", "false", "float", "for", |
Patrick Williams | cf078c4 | 2016-12-12 12:13:48 -0600 | [diff] [blame] | 38 | "friend", "goto", "if", "inline", "int", "long", "mutable", |
| 39 | "namespace", "new", "noexcept", "not", "not_eq", "nullptr", |
| 40 | "operator", "or", "or_eq", "private", "protected", "public", |
Patrick Williams | fcff676 | 2021-04-30 20:18:09 -0500 | [diff] [blame] | 41 | "register", "reinterpret_cast", "requires", "return", "short", |
| 42 | "signed", "sizeof", "static", "static_assert", "static_cast", |
| 43 | "struct", "switch", "template", "this", "thread_local", "throw", |
| 44 | "true", "try", "typedef", "typeid", "typename", "union", |
| 45 | "unsigned", "using", "virtual", "void", "volatile", "wchar_t", |
| 46 | "while", "xor", "xor_eq"}) |
Patrick Williams | cf078c4 | 2016-12-12 12:13:48 -0600 | [diff] [blame] | 47 | |
| 48 | while(name in cppReserved): |
| 49 | name = name + "_" |
| 50 | |
| 51 | return name |