TypeType = type(type)
class dom():
__slots__ = ('Body', 'Button', 'Div')
__ddd__ = None
__body__ = None
def __init__(self, body, slots=None):
self.__body__ = body
if slots:
self.__slots__ = slots
def __getattr__(self, name):
domClasses = [j for (i,j) in globals().items() if isinstance(j, TypeType) and issubclass(j, dom)]
for domClass in domClasses:
# Instead of slots you can parse HTML to guess whether you can map
# $name on any of its elements and instantiate appropriate Class basing
# on it.
# if self.__body__.find(name) != -1
# # Some fancy logic there
# if name == domClass.__name__:
# return domClass(self.__body__.retrieve_this_part_of_the_dom(name))
if name in self.__slots__:
if name == domClass.__name__:
return domClass()
raise ValueError('Can\'t instantiate')
class Body(dom):
__slots__ = ()
def getUrl(self):
print("http://url")
class Button(dom):
__slots__ = ()
def click(self, xpath):
print("Click is triggered on xpath="+xpath)
class Div(dom):
__slots__ = ('Body', 'Button', 'Div')
def setHtml(self, html):
print("Html is set to"+html)
def main():
myDom = dom('HTML to parse')
myDom.Body.getUrl()
myDom.Button.click('qwe')
myDom.Div.setHtml('123')
myDom.Div.Div.setHtml('123')
myDom.Div.Body.Div
if __name__ == "__main__":
main()