blob: 4aeedb765f0aae3972eb705c80f1da93d54298ae [file] [log] [blame]
Patrick Williamsce8a4672016-10-15 10:38:54 -05001import inflection
2
Patrick Williamsf5c3cde2016-10-21 11:49:01 -05003
Patrick Williamsa8972b02016-10-09 16:09:57 -05004class NamedElement(object):
5 def __init__(self, **kwargs):
William A. Kennington III66ef0992019-05-17 14:16:34 -07006 super(NamedElement, self).__init__()
Patrick Williamsa8972b02016-10-09 16:09:57 -05007 self.name = kwargs.pop('name', "unnamed")
8 self.description = kwargs.pop('description', "")
Patrick Williamsce8a4672016-10-15 10:38:54 -05009
Patrick Williams18ce8032020-01-31 18:47:38 -080010 def __getattribute__(self, name):
Patrick Williams9f77a472020-07-16 14:23:51 -050011 lam = {'CamelCase': lambda: inflection.camelize(self.name),
12 'camelCase': lambda: inflection.camelize(self.name, False),
13 'snake_case': lambda: inflection.underscore(self.name)}\
Patrick Williamsce8a4672016-10-15 10:38:54 -050014 .get(name)
15
Patrick Williams9f77a472020-07-16 14:23:51 -050016 if lam:
17 return NamedElement.__fixup_name(lam())
Patrick Williamsce8a4672016-10-15 10:38:54 -050018 try:
Patrick Williams18ce8032020-01-31 18:47:38 -080019 return super(NamedElement, self).__getattribute__(name)
Patrick Williamsc6a5bd82020-07-27 12:07:48 -050020 except Exception:
Patrick Williamsce8a4672016-10-15 10:38:54 -050021 raise AttributeError("Attribute '%s' not found in %s.NamedElement"
22 % (name, self.__module__))
Patrick Williamscf078c42016-12-12 12:13:48 -060023
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 Williamsfcff6762021-04-30 20:18:09 -050033 "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 Williamscf078c42016-12-12 12:13:48 -060038 "friend", "goto", "if", "inline", "int", "long", "mutable",
39 "namespace", "new", "noexcept", "not", "not_eq", "nullptr",
40 "operator", "or", "or_eq", "private", "protected", "public",
Patrick Williamsfcff6762021-04-30 20:18:09 -050041 "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 Williamscf078c42016-12-12 12:13:48 -060047
48 while(name in cppReserved):
49 name = name + "_"
50
51 return name