translator.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. class Tag(object):
  4. """Tag class"""
  5. def __init__(self, content=None):
  6. self.content = content
  7. self.attrs = ' '.join(['%s="%s"' % (attr, value)
  8. for attr, value in self.attrs])
  9. def __str__(self):
  10. return '<%s%s>\n %s\n</%s>' % (self.name,
  11. ' ' + self.attrs if self.attrs else '',
  12. self.content,
  13. self.name)
  14. class ScriptTag(Tag):
  15. name = 'script'
  16. attrs = (('type', 'text/javascript'),)
  17. class AnonymousFunction(object):
  18. def __init__(self, arguments, content):
  19. self.arguments = arguments
  20. self.content = content
  21. def __str__(self):
  22. return 'function(%s) { %s }' % (self.arguments, self.content)
  23. class Function(object):
  24. def __init__(self, name):
  25. self.name = name
  26. self._calls = []
  27. def __str__(self):
  28. operations = [self.name]
  29. operations.extend(str(call) for call in self._calls)
  30. return '%s' % ('.'.join(operations),)
  31. def __getattr__(self, attr):
  32. self._calls.append(attr)
  33. return self
  34. def __call__(self, *args):
  35. if not args:
  36. self._calls[-1] = self._calls[-1] + '()'
  37. else:
  38. arguments = ','.join([str(arg) for arg in args])
  39. self._calls[-1] = self._calls[-1] + '(%s)' % (arguments,)
  40. return self
  41. class Assignment(object):
  42. def __init__(self, key, value, scoped=True):
  43. self.key = key
  44. self.value = value
  45. self.scoped = scoped
  46. def __str__(self):
  47. return '%s%s = %s;' % ('var ' if self.scoped else '', self.key, self.value)
  48. def indent(func):
  49. # TODO: Add indents to function str
  50. return str(func)