Development.py 2.05 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
# -*- coding: utf-8 -*-
"""
  This simply sucks, hate doing it this way (Ivan)
"""
import sys
from ZODB.POSException import ConflictError
from Products.Formulator.MethodField import Method
from Products.PythonScripts.PythonScript import PythonScript

def getMethodObject(self, method_id):
  return Method(method_id)
  
def editForm(self, form, update_dicts):
  for key, value in update_dicts.items():
    setattr(form, key, value)
    
def Base_runPythonCode(self, code):
  """
    Run python code
  """
  code_lines = code.split('\r\n')
  python_wrapper_code = """class CodeWrapper:
  def runMethod(code_wrapper_self, self):
    context=self
    if 1:
      %s
"""%'\n      '.join(code_lines)
  exec(compile(python_wrapper_code,"-","exec"))
  wrapper = CodeWrapper()
  return wrapper.runMethod(self)
  

def Base_runPythonScript(self, code):
  script = PythonScript('Python Shell Script').__of__(self)
  code_line_list = code.split('\r\n')
  code = '\n'.join(code_line_list)
  script.write(code)
  if script._code is None:
    raise ValueError, repr(script.errors)

  return script()

def getPythonCodeExecutionError(self):
  result = None
  try:
    self.Base_executePython()
  except ConflictError:
    raise
  except :
    result = sys.exc_info()
  
  return result[1]


55
import lxml.html
Jean-Paul Smets's avatar
Jean-Paul Smets committed
56 57 58 59

def updateCodeWithMainContent(self, html_code, div_class):
   main_content = """
       <div class="%s">
60
        __REPLACE_MAIN_CONTENT__
Jean-Paul Smets's avatar
Jean-Paul Smets committed
61 62 63
      </div>      
   """ % (div_class)
   document = lxml.html.fromstring(html_code)
64 65 66 67 68 69 70 71 72
   element_list = document.find_class(div_class)
   if len(element_list) == 0:
     raise ValueError("It was not possible to find div with class=%s" % (div_class))

   element = element_list[0]
   new_element = lxml.html.fromstring(main_content)
   if element.get("id") is not None:
     new_element.set('id', element.get('id'))
   element.getparent().replace(element, new_element)
73 74
   new_html_code = lxml.html.tostring(document, pretty_print=True)
   return new_html_code.replace("__REPLACE_MAIN_CONTENT__", 
75
                                '<tal:block metal:define-slot="main"/>')