class ScriptRequestStrings(ScriptRequest): def __init__(self, instr): self.instream = StringIO(instr) self.outstream = StringIO() self.errstream = StringIO() def fetch_output(self): outstr = self.outstream.get_value() errstr = self.errstream.get_value() self.outstream.close() self.errstream.close() return outstr, errstr
class ScriptRequestStrings(ScriptRequest): """ When a script gets run by our xmlrpc server, we have the input as a string and we also need to catch the output / error output as strings. """ def __init__(self, instr): self.instream = StringIO(instr) self.outstream = StringIO() self.errstream = StringIO() def fetch_output(self): outstr = self.outstream.get_value() errstr = self.errstream.get_value() self.outstream.close() self.errstream.close() return outstr, errstr
def pyobj_from_dom(dom_node): """Converts a DOM tree to a "native" Python object""" # does the tag-named class exist, or should we create it? klass = '_XO_'+py_name(dom_node.nodeName) try: safe_eval(klass) except NameError: exec ('class %s(_XO_): pass' % klass) # create an instance of the tag-named class py_obj = eval('%s()' % klass) # attach any tag attributes as instance attributes attr_dict = dom_node.attributes if attr_dict is None: attr_dict = {} for key, val in attr_dict.items(): setattr(py_obj, py_name(key), val.value) # for nodes with character markup, might want the literal XML xml_stream = StringIO() printer = Printer.PrintVisitor(xml_stream, 'UTF-8') intro_PCDATA, subtag, exit_PCDATA = (0, 0, 0) # now look at the actual tag contents (subtags and PCDATA) for node in dom_node.childNodes: node_name = py_name(node.nodeName) printer.visit(node) # PCDATA is a kind of node, but not a new subtag if node.nodeName == '#text': if hasattr(py_obj, 'PCDATA'): py_obj.PCDATA = py_obj.PCDATA + node.nodeValue elif string.strip(node.nodeValue): # only use "real" node contents py_obj.PCDATA = node.nodeValue # (not bare whitespace) if not subtag: intro_PCDATA = 1 else: exit_PCDATA = 1 # does a py_obj attribute corresponding to the subtag already exist? elif hasattr(py_obj, node_name): # convert a single child object into a list of children if type(getattr(py_obj, node_name)) is not ListType: setattr(py_obj, node_name, [getattr(py_obj, node_name)]) # add the new subtag to the list of children getattr(py_obj, node_name).append(pyobj_from_dom(node)) # start out by creating a child object as attribute value else: setattr(py_obj, node_name, pyobj_from_dom(node)) subtag = 1 # if dom_node appears to contain character markup, save literal XML if subtag and (intro_PCDATA or exit_PCDATA): py_obj._XML = xml_stream.get_value() elif KEEP_CONTAINERS: # ...and if option is specified explicitly py_obj._XML = xml_stream.get_value() return py_obj