erlydtl_python_test.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from erlport import Port, Protocol, String, Atom
  2. from erlport.erlterms import decode
  3. from django.template import Context, Template
  4. from django.conf import settings
  5. import types
  6. # Inherit custom protocol from erlport.Protocol
  7. class ErlydtlProtocol(Protocol):
  8. def handle_slice(self, command):
  9. list = command[0]
  10. slice = "%s" % String(command[1])
  11. slice_test1_string = "%s[%s]" % (list,slice)
  12. try:
  13. result_list = eval(slice_test1_string)
  14. except(IndexError):
  15. result_list = Atom("indexError")
  16. except:
  17. result_list = Atom("error")
  18. #print "result_list: %s" % (result_list)
  19. return result_list
  20. #@doc Start list with 'object' to pass in a python term along with import statement in the form:
  21. #@doc object|module to import|term (three strings delimited by "|"
  22. def handle_template(self, command):
  23. file = open("/tmp/debug.txt",'a')
  24. template_text = "%s" % String(command[0])
  25. #value = "%s" % String(command[1])
  26. value = "%s" % String(command[1])
  27. value_split = value.split("|")
  28. if value_split[0] == u"object":
  29. module = value_split[1]
  30. exec "import %s" % module
  31. value = eval(value_split[2])
  32. #term(((term.year, term.month, term.day), (term.hour, term.minute, term.second)))
  33. c = Context({"value":value})
  34. t = Template(template_text)
  35. result = String(t.render(c))
  36. file.close()
  37. return result
  38. if __name__ == "__main__":
  39. settings.configure(DEBUG=True, TEMPLATE_DEBUG=True)
  40. proto = ErlydtlProtocol()
  41. # Run protocol with port open on STDIO
  42. proto.run(Port(use_stdio=True))