当前位置: 首页>>代码示例>>Python>>正文


Python StringWriter.getBuffer方法代码示例

本文整理汇总了Python中java.io.StringWriter.getBuffer方法的典型用法代码示例。如果您正苦于以下问题:Python StringWriter.getBuffer方法的具体用法?Python StringWriter.getBuffer怎么用?Python StringWriter.getBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.io.StringWriter的用法示例。


在下文中一共展示了StringWriter.getBuffer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: doPost

# 需要导入模块: from java.io import StringWriter [as 别名]
# 或者: from java.io.StringWriter import getBuffer [as 别名]
  def doPost(self, request, response):
    c = NomjycContainer()
    c.parameters = request.getParameterMap()

    log = open("/var/log/nomjyc/sandbox.log","a")
    safelog = dict(c.parameters)
    if "pass" in safelog:
      safelog["pass"]=len(safelog["pass"])
    log.write(("[[%s]] %s %s\n" % (request.getRemoteAddr(), datetime.utcnow(), self.yaml.dump(safelog))).encode("utf-8"))
    log.close()

    output = "<div class=\"infobox\"><span class=\"gh\">nomjyc 0.1</span>\n"
    c.session    = {}
    if len(c.parameters)==0:
      output += "<pre>%s</pre>" % self.__doc__
    output += "</div>"

    c.salt="dckx"

    try:
      c.data = self.yaml.load(FileInputStream("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.yaml"))
    except:
      output += self.explainException("Error while initiating game state")

    # Print some debug information - for now.
    output += self.divHideCode("infobox", "Request", "reqi", self.dumpYaml(c.parameters), visible=True)
    output += self.divHideCode("infobox", "Data read", "dri", self.dumpYaml(c.data))

    # Add a final rule, if the test parameter is set
    if "test" in c.parameters:
      for code in c.parameters["test"]:
        c.data["rules"].add({"author":"impromptu", "code":code, "creation":datetime.utcnow(), "title":"test rule"})

    cycles = 1
    if "cycles" in c.parameters:
      try: 
        cycles = int(c.parameters["cycles"][0])
      except:
        pass
    if cycles<0 or cycles>3:
      cycles = 1

    # Execute all rules against the user input
    brain = PythonInterpreter()
    c.sandbox = True # a flag that gives away that we are in a sandbox
    for i in range(cycles):
      c.cycle = i    # a counter for the cycle we are in
      for rule in c.data["rules"][:]: # we are going to modify the rules. a lot. this prevents concurrent modification.
        try:
          output += self.divHideCode("rulebox", "Executing rule '%s'" % rule["title"], "id"+str(random()), self.dumpPython(rule["code"]), openend=True)
   	  err = StringWriter()
  	  out = StringWriter()
          checksum = hashlib.md5(self.yaml.dump(c.data)).hexdigest()
          brain.set("self", c)
          brain.setErr(err)
          brain.setOut(out)
          before = time.time()
          timeout (brain.exec,(rule["code"],),timeout_duration=30)
          runtime = int((time.time()-before) * 1000)
          changes = (checksum != hashlib.md5(self.yaml.dump(c.data)).hexdigest())
          output += "<div class=\"ruleoutput\">"
          if changes: output += "<div class=\"erroroutput\">This rule changed the game data.</div>"
          if (err.getBuffer().length()): output += "<div class=\"erroroutput\">Err:<br />%s</div>" % self.dumpPythonTB(err.toString().strip())
          if (out.getBuffer().length()): output += "<div class=\"gu\">Out:</div>"+out.toString().strip()
          output += "<div>(runtime: %sms)</div></div></div>" % runtime

        except Exception, ex:
          output += self.explainException("Execution failed") + "</div>"
开发者ID:relet,项目名称:nomjyc,代码行数:70,代码来源:sandbox.py

示例2: doPost

# 需要导入模块: from java.io import StringWriter [as 别名]
# 或者: from java.io.StringWriter import getBuffer [as 别名]
  def doPost(self, request, response):
    c = NomjycContainer() # re-initialized container upon every request    
    c.parameters = request.getParameterMap()
    
    log = open("/var/log/nomjyc/nomjyc.log","a")
    safelog = dict(c.parameters)
    if "pass" in safelog:
      safelog["pass"]=len(safelog["pass"])
    log.write(("[[%s]] %s %s\n" % (request.getRemoteAddr(), datetime.utcnow(), self.yaml.dump(safelog))).encode("utf-8"))
    log.close()

    output = "<div class=\"infobox\"><span class=\"gh\">nomjyc 0.1</span>\n"
    c.session    = {}
    if len(c.parameters)==0:
      output += "<pre>%s</pre>" % self.__doc__
    output += "</div>"

    c.salt="dckx"

    try:
      c.data = self.yaml.load(FileInputStream("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.yaml"))
    except:
      output += self.explainException("Error while initiating game state - trying to load backup.")
      c.data = self.yaml.load(FileInputStream("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.bak"))

    # Print some debug information - for now.
    output += self.divHideCode("infobox", "Request", "reqi", self.dumpYaml(c.parameters), visible=True)
    output += self.divHideCode("infobox", "Data read", "dri", self.dumpYaml(c.data))

    # If we have come so far, we assume that it is safe to write a backup.
    try:
      self.yaml.dump(c.data, FileWriter("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.bak"))    
    except:
      output += self.explainException("Error while storing backup game state")
    
    # Execute all rules against the user input
    brain = PythonInterpreter()
    c.sandbox = False # a flag that tells that we are not in a sandbox
    checksum = hashlib.md5(self.yaml.dump(c.data)).hexdigest()
    for rule in c.data["rules"][:]: # we are going to modify the rules. a lot. this prevents concurrent modification.
      try:
        output += self.divHideCode("rulebox", "Executing rule '%s'" % rule["title"], "id"+str(random()), self.dumpPython(rule["code"]), openend=True)
        err = StringWriter()
        out = StringWriter()
        # Compile the rule into a jython/java class
        brain.set("self", c) # expose the container to the rules
        brain.setErr(err)
        brain.setOut(out)
        before = time.time()
        timeout (brain.exec,(rule["code"],),timeout_duration=30)
        runtime = int((time.time()-before) * 1000)
        newsum   = hashlib.md5(self.yaml.dump(c.data)).hexdigest()
        changes  = (checksum != newsum)
        checksum = newsum
        output += "<div class=\"ruleoutput\">" 
        if changes: output += "<div class=\"erroroutput\">This rule changed the game data.</div>"
        if (err.getBuffer().length()): output += "<div class=\"erroroutput\">Err:<br />%s</div>" % self.dumpPythonTB(err.toString())
        if (out.getBuffer().length()): output += "<div class=\"gu\">Out:</div>"+out.toString()
        output += "<div>(runtime: %sms)</div></div></div>" % runtime

      except Exception, ex:
        output += self.explainException("Execution failed") + "</div>"
开发者ID:relet,项目名称:nomjyc,代码行数:64,代码来源:nomjyc.py


注:本文中的java.io.StringWriter.getBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。