Beispiel #1
0
 def tohtml(self):
     html = self.paragraph.tohtml()
     try:
         et.fromstring('<div>' + html + '</div>')
     except:
         if self.parser.zwparser.debug:
             raise
     else:
         html = '<p>' + html + '</p>'
     return html
Beispiel #2
0
 def tohtml( self ):
     html = self.paragraph.tohtml()
     try : 
         et.fromstring( '<div>' + html + '</div>' )
     except :
         if self.parser.zwparser.debug :
             raise
     else :
         html = '<p>' + html + '</p>'
     return html
  def _GetXmlOut(self):
    """Remove banners from outgoing SOAP XML and contstruct XML object.

    Returns:
      Document/Element object generated from string, representing XML message.
    """
    # Remove banners.
    xml_dump = self.GetSoapOut().lstrip('\n').rstrip('\n')
    xml_parts = xml_dump.split('\n')

    # While multiple threads are used, SoapBuffer gets too greedy and tries to
    # capture all traffic that goes to sys.stdout. The parts that we don't need
    # should be redirected back to sys.stdout.
    banner = '%s Outgoing SOAP %s' % ('*' * 3, '*' * 54)
    begin = 1
    for index in range(len(xml_parts)):
      if xml_parts[index] == banner:
        begin = index + 1
        break
    non_xml = '\n'.join(xml_parts[:begin-1])
    # Send data we don't need back to sys.stdout.
    if non_xml: print non_xml

    xml_dump = '\n'.join(xml_parts[begin:len(xml_parts)-1])

    try:
      if self.__xml_parser == PYXML:
        xml_obj = minidom.parseString(xml_dump)
      elif self.__xml_parser == ETREE:
        xml_obj = etree.fromstring(xml_dump)
    except (ExpatError, SyntaxError), e:
      msg = 'Unable to parse SOAP buffer for outgoing messages. %s' % e
      raise MalformedBufferError(msg)
Beispiel #4
0
 def __init__(self, key, name, force_=False):
     #didn't i read somwhere that variable reuse is bad like this? :)
     #sys.stdout.write('Got key: %s\n' % (key,))
     x = 'http://isbndb.com/api/books.xml?access_key=%s&index1=isbn'
     x += '&value1=foo&results=keystats'
     x %=key
     a = urllib.urlopen(x).read()
     sys.stdout.write('Got from isbndb:\n %s\n' % (a,))
     y = ElementTree.fromstring(a)
     if ElementTree.iselement(y.find('ErrorMessage')):
         sys.stdout.write('Found error...')
         if force_:
             sys.stdout.write('forcing\n')
             self.active = False
             self.granted = 0
             self.limit = 0
             return
         else:
             sys.stdout.write('erroring\n')
             raise ValueError, 'either bad key or something'
     else:
         self.active = True
     y = y.find('KeyStats')
     self.granted = int(y.get('granted'))
     self.limit = int(y.get('limit'))
     self.name = name
Beispiel #5
0
    def build_folder(self, raw_folder):

        # Java JMS sends folders with unbound prefixes, i.e. <msgbody xsi:nil="true"></msgbody>
        # which is in no way a valid XML so we have to insert the prefix ourselves
        # in order to avoid parser bailing out with an ExpatError. I can't think
        # of any other way to work around it if we'd like to treat folders as
        # XML(-like) structures.

        if 'xsi:nil="true"' in raw_folder and not 'xmlns' in raw_folder:
            self.logger.log(
                TRACE1,
                "Binding xsi:nil to a dummy namespace [%s]" % raw_folder)
            raw_folder = raw_folder.replace(
                'xsi:nil="true"', 'xmlns:xsi="dummy" xsi:nil="true"')
            self.logger.log(TRACE1,
                            "raw_folder after binding [%s]" % raw_folder)

        folder = etree.fromstring(raw_folder)
        root_name = folder.tag

        root_names = ["jms", "usr"]
        if self.needs_mcd:
            root_names.append("mcd")

        if root_name in root_names:
            self.folders[root_name] = folder
        else:
            self.logger.warn("Ignoring unrecognized JMS folder [%s]=[%s]" %
                             (root_name, raw_folder))
Beispiel #6
0
 def __init__(self, key, name, force_=False):
     #didn't i read somwhere that variable reuse is bad like this? :)
     #sys.stdout.write('Got key: %s\n' % (key,))
     x = 'http://isbndb.com/api/books.xml?access_key=%s&index1=isbn'
     x += '&value1=foo&results=keystats'
     x %= key
     a = urllib.urlopen(x).read()
     sys.stdout.write('Got from isbndb:\n %s\n' % (a, ))
     y = ElementTree.fromstring(a)
     if ElementTree.iselement(y.find('ErrorMessage')):
         sys.stdout.write('Found error...')
         if force_:
             sys.stdout.write('forcing\n')
             self.active = False
             self.granted = 0
             self.limit = 0
             return
         else:
             sys.stdout.write('erroring\n')
             raise ValueError, 'either bad key or something'
     else:
         self.active = True
     y = y.find('KeyStats')
     self.granted = int(y.get('granted'))
     self.limit = int(y.get('limit'))
     self.name = name
Beispiel #7
0
    def __GetXmlOut(self):
        """Remove banners from outgoing SOAP XML and contstruct XML object.

    Returns:
      Document/Element object generated from string, representing XML message.
    """
        # Remove banners.
        xml_dump = self.GetSOAPOut().lstrip('\n').rstrip('\n')
        xml_parts = xml_dump.split('\n')

        # While multiple threads are used, SoapBuffer gets too greedy and tries to
        # capture all traffic that goes to sys.stdout. Part that we don't need
        # should be redirected back to sys.stdout.
        banner = '%s Outgoing SOAP %s' % ('*' * 3, '*' * 54)
        begin = 1
        for index in range(len(xml_parts)):
            if xml_parts[index] == banner:
                begin = index + 1
                break
        non_xml = '\n'.join(xml_parts[:begin - 1])
        # Send data we don't need back to sys.stdout.
        if non_xml:
            print non_xml

        xml_dump = '\n'.join(xml_parts[begin:len(xml_parts) - 1])

        try:
            if self.__xml_parser == PYXML:
                xml_obj = minidom.parseString(xml_dump)
            elif self.__xml_parser == ETREE:
                xml_obj = etree.fromstring(xml_dump)
        except (ExpatError, SyntaxError), e:
            msg = 'Unable to parse SOAP buffer for outgoing messages. %s' % e
            raise MalformedBufferError(msg)
Beispiel #8
0
 def test_badValuePassed(self):
     print "Testing that a bad element passed raises an error...",
     xmlstr = \
     """<FooBar isbn="0000000000"><Title>Stuff</Title></FooBar>"""
     x = ElementTree.fromstring(xmlstr)
     self.assertRaises(ValueError, IsbndbBook, x)
     print "OK"
Beispiel #9
0
 def test_badValuePassed(self):
     print "Testing that a bad element passed raises an error...",
     xmlstr = \
     """<FooBar isbn="0000000000"><Title>Stuff</Title></FooBar>"""
     x = ElementTree.fromstring(xmlstr)
     self.assertRaises(ValueError,IsbndbBook,x)
     print "OK"
Beispiel #10
0
    def call(self, verb, **kwargs):
        """Do some yamlrpc"""
        moniker = "fnord"

        posturi = self.conf.config["site"] + "/__jifty/webservices/xml"
        postargs = {"J:A-%s" % moniker: verb}
        for key, val in kwargs.items():
            postargs["J:A:F-%s-%s" % (key, moniker)] = val
        poststr = urllib.urlencode(postargs)
        if self.debug: print "POSTING:", poststr
        if self.debug: print "TO:", posturi
        postio = StringIO.StringIO(poststr)

        respio = StringIO.StringIO()
        ua = pycurl.Curl()  # perlish "user agent"
        ua.setopt(pycurl.POST, 1)
        ua.setopt(pycurl.POSTFIELDSIZE, len(poststr))
        ua.setopt(pycurl.READFUNCTION, postio.read)
        ua.setopt(pycurl.WRITEFUNCTION, respio.write)
        ua.setopt(pycurl.URL, posturi)
        if self.debug: ua.setopt(pycurl.VERBOSE, 1)
        ua.setopt(pycurl.USERAGENT, self.user_agent)
        # in theory at least:
        if self.last_sid:
            # print "setting scraped sid_cookie:"
            ua.setopt(pycurl.COOKIE, make_sid_cookie(self.last_sid, None))
        if "sid" in self.conf.config:
            # print "setting stored sid_cookie:"
            ua.setopt(pycurl.COOKIE,
                      make_sid_cookie(self.conf.config["sid"], None))
        ua.setopt(pycurl.COOKIELIST, "")  # get cookies *back*
        ua.perform()

        if self.debug: print "SIZE_UPLOAD:", ua.getinfo(pycurl.SIZE_UPLOAD)
        if self.debug:
            print "CONTENT_LENGTH_UPLOAD:", ua.getinfo(
                pycurl.CONTENT_LENGTH_UPLOAD)
        http_status = ua.getinfo(pycurl.HTTP_CODE)
        if self.debug: print "HTTP STATUS:", http_status
        rawcookies = ua.getinfo(pycurl.INFO_COOKIELIST)
        self.last_sid = self.extract_sid_value(rawcookies)

        # res = (urllib or curl).post(posturi, postargs)
        res = respio.getvalue()
        if res:
            if self.debug: print "RAW RES:", repr(res)
            # return yaml.load(res)[moniker]
            parsedres = cElementTree.fromstring(res)
            if self.debug: print "RAW XML:", parsedres
            realresult = parsedres.find("result")
            assert realresult.get(
                "moniker"
            ) == "fnord", "wrong moniker in response %s" % parsedres.items()
            success = realresult.find("success").text
            if success != "1":
                print "unsuccessful!"
            return success == "1", realresult
        print "OOPS, no response to call"
        return None, None
Beispiel #11
0
    def call(self, verb, **kwargs):
        """Do some yamlrpc"""
        moniker = "fnord"

        posturi = self.conf.config["site"] + "/__jifty/webservices/xml"
        postargs = {"J:A-%s" % moniker: verb}
        for key, val in kwargs.items():
            postargs["J:A:F-%s-%s" % (key, moniker)] = val
        poststr = urllib.urlencode(postargs)
        if self.debug: print "POSTING:", poststr
        if self.debug: print "TO:", posturi
        postio = StringIO.StringIO(poststr)

        respio = StringIO.StringIO()
        ua = pycurl.Curl()  # perlish "user agent"
        ua.setopt(pycurl.POST, 1)
        ua.setopt(pycurl.POSTFIELDSIZE, len(poststr))
        ua.setopt(pycurl.READFUNCTION, postio.read)
        ua.setopt(pycurl.WRITEFUNCTION, respio.write)
        ua.setopt(pycurl.URL, posturi)
        if self.debug: ua.setopt(pycurl.VERBOSE, 1)
        ua.setopt(pycurl.USERAGENT, self.user_agent)
        # in theory at least:
        if self.last_sid:
            # print "setting scraped sid_cookie:"
            ua.setopt(pycurl.COOKIE, make_sid_cookie(self.last_sid, None))
        if "sid" in self.conf.config:
            # print "setting stored sid_cookie:"
            ua.setopt(pycurl.COOKIE,
                      make_sid_cookie(self.conf.config["sid"], None))
        ua.setopt(pycurl.COOKIELIST, "")  # get cookies *back*
        ua.perform()

        if self.debug: print "SIZE_UPLOAD:", ua.getinfo(pycurl.SIZE_UPLOAD)
        if self.debug:
            print "CONTENT_LENGTH_UPLOAD:", ua.getinfo(
                pycurl.CONTENT_LENGTH_UPLOAD)
        http_status = ua.getinfo(pycurl.HTTP_CODE)
        if self.debug: print "HTTP STATUS:", http_status
        rawcookies = ua.getinfo(pycurl.INFO_COOKIELIST)
        self.last_sid = self.extract_sid_value(rawcookies)

        # res = (urllib or curl).post(posturi, postargs)
        res = respio.getvalue()
        if res:
            if self.debug: print "RAW RES:", repr(res)
            # return yaml.load(res)[moniker]
            parsedres = cElementTree.fromstring(res)
            if self.debug: print "RAW XML:", parsedres
            realresult = parsedres.find("result")
            assert realresult.get(
                "moniker"
            ) == "fnord", "wrong moniker in response %s" % parsedres.items()
            success = realresult.find("success").text
            if success != "1":
                print "unsuccessful!"
            return success == "1", realresult
        print "OOPS, no response to call"
        return None, None
Beispiel #12
0
 def test_update(self):
     print "Testing the update function...",
     self.key = keys.Key(self.good_key)
     es = '<KeyStats granted="45" access_key="foo"'
     es += ' requests="45" limit="0" />'
     x = ElementTree.fromstring(es)
     self.key.update(x)
     self.assertEqual(self.key.granted, 45, "FAILED (update granted)")
     self.assertEqual(self.key.limit, 0, "FAILED (update limit)")
     print "OK"
Beispiel #13
0
def get_request_fields():
    ctype = request.content_type
    if ctype.startswith('text/xml'):
        vote = etree.fromstring(request.body)
        return dict([(c.tag, c.text) for c in vote])
    elif ctype.startswith('text/javascript'):
        return json.loads(request.body)
    else:
        #  rest will be interpreted as urlencoded form
        return request.params
Beispiel #14
0
 def test_update(self):
     print "Testing the update function...",
     self.key = keys.Key(self.good_key)
     es = '<KeyStats granted="45" access_key="foo"'
     es += ' requests="45" limit="0" />'
     x = ElementTree.fromstring(es)
     self.key.update(x)
     self.assertEqual(self.key.granted, 45, 'FAILED (update granted)')
     self.assertEqual(self.key.limit, 0, 'FAILED (update limit)')
     print "OK"
Beispiel #15
0
def fetch(param):
    a = urllib.urlopen(param.url)
    x = unicode(a.read(),'ascii',errors='ignore')
    tree = ElementTree.fromstring(x)
    err = tree.find('ErrorMessage')
    if ElementTree.iselement(err):
        raise Exception, 'an error occurred'
    stats = tree.find('KeyStats')
    list = tree.find('BookList')
    param.key.update(stats)
    return list
Beispiel #16
0
 def test_titleBehavior(self):
     print "Testing Title and TitleLong match if no TitleLong given...",
     xmlstr =\
     '''<BookData book_id="test" isbn="0000000000">
        <Title>Foo</Title>
        <TitleLong></TitleLong>
        </BookData>'''
     x = ElementTree.fromstring(xmlstr)
     book = IsbndbBook(x)
     self.failUnless(book.Title == book.TitleLong, "FAILED (titles)")
     print "OK"
Beispiel #17
0
    def __init__(self):
        doc = urllib.urlopen(self.url).read()
        tree = cElementTree.fromstring(doc)

        self.data = {}
        for item in tree.findall("weather/item"):
            readout = Readout(item)
            if readout.timestamp:
                if readout.name not in self.data:
                    self.data[readout.name] = []
                self.data[readout.name].append(readout)
Beispiel #18
0
def fetch(param):
    a = urllib.urlopen(param.url)
    x = unicode(a.read(), 'ascii', errors='ignore')
    tree = ElementTree.fromstring(x)
    err = tree.find('ErrorMessage')
    if ElementTree.iselement(err):
        raise Exception, 'an error occurred'
    stats = tree.find('KeyStats')
    list = tree.find('BookList')
    param.key.update(stats)
    return list
Beispiel #19
0
 def test_titleBehavior(self):
     print "Testing Title and TitleLong match if no TitleLong given...",
     xmlstr =\
     '''<BookData book_id="test" isbn="0000000000">
        <Title>Foo</Title>
        <TitleLong></TitleLong>
        </BookData>'''
     x = ElementTree.fromstring(xmlstr)
     book = IsbndbBook(x)
     self.failUnless(book.Title == book.TitleLong, "FAILED (titles)")
     print "OK"
Beispiel #20
0
 def doUpdateXML(self, request):
   data = self.doPost(self.uri + '/update', request, self.xmlheaders)
   if data is None:
     return None
   response = ET.fromstring(data)
   for lst in response.findall('./lst'):
     if lst.get('name') == 'responseHeader':
       for lst_int in lst.findall('./int'):
         if lst_int.get('name') == 'status':
           solr_status = int(lst_int.text)
           if solr_status != 0:
             raise SolrException(rsp.status, solr_status)
           break
       break
   return data
  def __PrettyPrintXml(self, doc, level=0):
    """Return a pretty-printed version of the XML document.

    Args:
      doc: str XML document.
      level: int Level of prettiness, defaults to 0. If -1, remove prettiness.

    Returns:
      str Pretty-printed version of the XML document.
    """
    # Make sure we have a valid doc to work with.
    if Utils.IsHtml(doc):
      return doc

    try:
      if self.__xml_parser == PYXML:
        dom = minidom.parseString(doc)
        pretty_doc = dom.toprettyxml(indent=' ', encoding='UTF-8')
      elif self.__xml_parser == ETREE:
        tree = etree.fromstring(doc)
        self.__Indent(tree)
        pretty_doc = etree.tostring(tree, 'UTF-8')
    except (ExpatError, SyntaxError):
      # If there was a problem with loading XML message into a DOM, return
      # original XML message.
      return doc

    # Adjust prettiness of data values in the XML document.
    #
    # Before:  <operations>
    #            0
    #          </operations>
    #
    # After:   <operations>0</operations>
    pattern = re.compile('\n\s+\n')
    pretty_doc = pattern.sub('\n', pretty_doc)
    groups = re.findall('>(\n\s+(.*?)\n\s+)</', pretty_doc, re.M)
    for old, new in groups:
      if old and new and (new.find('<') > -1 or new.find('>') > -1):
        continue
      pretty_doc = pretty_doc.replace(old, new)

    if level == -1:
      pattern = re.compile('>\s+<')
      pretty_doc = pattern.sub('><', pretty_doc)
    return pretty_doc.strip('\n')
Beispiel #22
0
def get_config_root_node(config_file_name=None, config_file_data=None):
    try:
        # get root
        if config_file_data is None:
            config_file_content = open(config_file_name, "r")
            config = ET.parse(config_file_content)
            root_node = config.getroot()
        else:
            root_node = ET.fromstring(config_file_data)

        # get root data
        root_data = root_node.get("name")
        root_name = np.array(root_data.split(), dtype=str)
    except:
        quit("ERROR: Unable to process config file %s" % config_file_name)

    return root_node, root_name
    def __PrettyPrintXml(self, doc, level=0):
        """Return a pretty-printed version of the XML document.

    Args:
      doc: str XML document.
      level: int Level of prettiness, defaults to 0. If -1, remove prettiness.

    Returns:
      str Pretty-printed version of the XML document.
    """
        # Make sure we have a valid doc to work with.
        if Utils.IsHtml(doc):
            return doc

        try:
            if self.__xml_parser == PYXML:
                dom = minidom.parseString(doc)
                pretty_doc = dom.toprettyxml(indent=' ', encoding='UTF-8')
            elif self.__xml_parser == ETREE:
                tree = etree.fromstring(doc)
                self.__Indent(tree)
                pretty_doc = etree.tostring(tree, 'UTF-8')
        except (ExpatError, SyntaxError):
            # If there was a problem with loading XML message into a DOM, return
            # original XML message.
            return doc

        # Adjust prettiness of data values in the XML document.
        #
        # Before:  <operations>
        #            0
        #          </operations>
        #
        # After:   <operations>0</operations>
        pattern = re.compile('\n\s+\n')
        pretty_doc = pattern.sub('\n', pretty_doc)
        groups = re.findall('>(\n\s+(.*?)\n\s+)</', pretty_doc, re.M)
        for old, new in groups:
            if old and new and (new.find('<') > -1 or new.find('>') > -1):
                continue
            pretty_doc = pretty_doc.replace(old, new)

        if level == -1:
            pattern = re.compile('>\s+<')
            pretty_doc = pattern.sub('><', pretty_doc)
        return pretty_doc.strip('\n')
Beispiel #24
0
def available_weeks(username):
	"""Returns a list of integer tuples, representing the
	available weeks for charts."""
	
	# Get and parse the XML
	root = ET.fromstring(fetch("user/%s/weeklychartlist.xml" % username).read())
	
	# Check the type
	assert root.tag == "weeklychartlist", "This is not a Weekly Chart List"
	
	# For each week, get the times
	weeks = []
	for tag in root.findall("chart"):
		start = float(tag.attrib['from'])
		end = float(tag.attrib['to'])
		weeks.append((start, end))
	
	return weeks
  def _GetXmlIn(self):
    """Remove banners from incoming SOAP XML and construct XML object.

    Returns:
      Document/Element object generated from string, representing XML message.
    """
    # Remove banners.
    xml_dump = self.GetSoapIn().lstrip('\n').rstrip('\n')
    xml_parts = xml_dump.split('\n')
    xml_dump = '\n'.join(xml_parts[1:len(xml_parts)-1])

    try:
      if self.__xml_parser == PYXML:
        xml_obj = minidom.parseString(xml_dump)
      elif self.__xml_parser == ETREE:
        xml_obj = etree.fromstring(xml_dump)
    except (ExpatError, SyntaxError), e:
      msg = 'Unable to parse SOAP buffer for incoming messages. %s' % e
      raise MalformedBufferError(msg)
Beispiel #26
0
def available_weeks(username):
    """Returns a list of integer tuples, representing the
	available weeks for charts."""

    # Get and parse the XML
    root = ET.fromstring(
        fetch("user/%s/weeklychartlist.xml" % username).read())

    # Check the type
    assert root.tag == "weeklychartlist", "This is not a Weekly Chart List"

    # For each week, get the times
    weeks = []
    for tag in root.findall("chart"):
        start = float(tag.attrib['from'])
        end = float(tag.attrib['to'])
        weeks.append((start, end))

    return weeks
Beispiel #27
0
    def __GetXmlIn(self):
        """Remove banners from incoming SOAP XML and construct XML object.

    Returns:
      Document/Element object generated from string, representing XML message.
    """
        # Remove banners.
        xml_dump = self.GetSOAPIn().lstrip('\n').rstrip('\n')
        xml_parts = xml_dump.split('\n')
        xml_dump = '\n'.join(xml_parts[1:len(xml_parts) - 1])

        try:
            if self.__xml_parser == PYXML:
                xml_obj = minidom.parseString(xml_dump)
            elif self.__xml_parser == ETREE:
                xml_obj = etree.fromstring(xml_dump)
        except (ExpatError, SyntaxError), e:
            msg = 'Unable to parse SOAP buffer for incoming messages. %s' % e
            raise MalformedBufferError(msg)
Beispiel #28
0
    def build_folder(self, raw_folder):

        # Java JMS sends folders with unbound prefixes, i.e. <msgbody xsi:nil="true"></msgbody>
        # which is in no way a valid XML so we have to insert the prefix ourselves
        # in order to avoid parser bailing out with an ExpatError. I can't think
        # of any other way to work around it if we'd like to treat folders as
        # XML(-like) structures.

        if 'xsi:nil="true"' in raw_folder and not 'xmlns' in raw_folder:
            self.logger.log(TRACE1, "Binding xsi:nil to a dummy namespace [%s]" % raw_folder)
            raw_folder = raw_folder.replace('xsi:nil="true"', 'xmlns:xsi="dummy" xsi:nil="true"')
            self.logger.log(TRACE1, "raw_folder after binding [%s]" % raw_folder)

        folder = etree.fromstring(raw_folder)
        root_name = folder.tag

        if root_name in("mcd", "jms", "usr"):
            self.folders[root_name] = folder
        else:
            self.logger.warn("Ignoring unrecognized JMS folder [%s]=[%s]" % (root_name, raw_folder))
Beispiel #29
0
class HTML(object):
    example_ctx = ET.fromstring("""
    <html xmlns='http://www.w3.org/1999/xhtml'>
    <body><h1 class="x y">..</h1></body>
    </html>""")

    namespaces = dict(h='http://www.w3.org/1999/xhtml')

    @classmethod
    def by_class(cls, ctx, tagname, classname):
        '''
        >>> found = HTML.by_class(HTML.example_ctx, 'h1', 'x')
        >>> [(e.tag.split('}')[1], e.attrib['class']) for e in found]
        [('h1', 'x y')]
        '''

        w_class = ctx.findall(
            ".//{%s}%s[@class]" % (cls.namespaces['h'], tagname))
        return [e for e in w_class
                if cls.has_class(e, classname)]

    @classmethod
    def has_class(cls, elt, classname):
        '''
        >>> HTML.has_class(HTML.example_ctx[0][0], 'x')
        True
        '''
        return classname in elt.get('class', '').split()

    @classmethod
    def the(cls, ctx, expr):
        '''
        >>> HTML.the(HTML.example_ctx, 'h:body/h:h1').tag.split('}')[1]
        'h1'
        '''
        qexpr = expr.replace('h:', '{' + cls.namespaces['h'] + '}')
        found = ctx.find(qexpr)
        if found is None:
            raise ValueError('cannot find %s (%s) in %s' %
                             (expr, qexpr, ctx.tag))
        return found
Beispiel #30
0
def merge(filename):
    if os.path.exists(filename):
        root = parse(filename).getroot()
    else:
        root = fromstring(filename)
    for elem in root:
        if elem.tag == 'Commands':
            for e in elem:
                comm = _from_element(e)
                commands[comm.id] = comm
        elif elem.tag == 'Images':
            for e in elem:
                if e.tag == 'Image':
                    if 'path' in e.keys():
                        img = Image.open(_attr(e.get('path')))
                    else:
                        img = Image.open(StringIO.StringIO(e.text.strip().decode('base64')))
                    images.register(_attr(e.get('id')), img)
                elif e.tag == 'DirImageProvider':
                    images.register_dir(_attr(e.get('path')))
        elif 'name' in elem.keys():
            registry[eval(elem.get('name'), {})] = elem
        else:
            print >>sys.stderr, "cannot use element", elem
Beispiel #31
0
def load_definition(xmlstr):
    root = ElementTree.fromstring(xmlstr)
    _analyze_tree(root)
    def index(self,**kwargs):
        self.namespace_expr = re.compile(r'^\{.*\}')
        # get request data and produce an ElementTree that we can work with.
        request = cherrypy.request
	if  self.verbose > 1:
	    self.writeLog(request)
        
        response = cherrypy.response
        if  not request.headers.has_key('Content-Length'):
            page  = "<h4>EventSource Meta Data Service</h4>"
            page +="""<pre>
The following operations are supported.

    * GetEnergyBoundaries(Energy_Range_Name)
      Get energy boundaries for a given energy name.

    * GetEnergyRangeNames()
      Query for a list of valid energy range names.

    * GetRuns(Dataset_Name, Energy_Range_Name)
      Query by dataset and energy range to return a list of runs. Both parameters are optional.

    * GetRawRunProperties(Start_Run_Number, End_Run_Number)
      Query and return an array of Raw_Run_Properties.

    * GetDatasetNames()
      Get a list of valid dataset names.

    * GetRunRanges(Dataset_Name, Energy_Range_Name)
      Query by dataset and energy range to return a list of run ranges. Both parameters are optional. 
</pre>
"""
            return page

        clen = int(request.headers.get('Content-Length')) or 0
        data = request.body.read(clen)
	if  self.verbose > 1:
	    self.writeLog(data)
        
        request.soap_start = data[:2048]
        soapreq = et.fromstring(data)
        #self.writeLog(soapreq)

        # find the body of the request and the specific method name that has
        # been requested.
        body = soapreq.find("{http://schemas.xmlsoap.org/soap/envelope/}Body")
        #self.writeLog("body0="+repr(body))
        body = body.getchildren()[0]
        #self.writeLog("body1="+repr(body))

        methodname = body.tag
        methodname = self.namespace_expr.sub("", methodname)
        #self.writeLog(methodname)

        request.soap_method = methodname

        method=getattr(self,methodname)
	if  self.verbose > 1:
	    self.writeLog(method)

        params = {"_ws" : True}
        params["xml_body"] = body
        return method(**params)
 def parse(self, xml):
     """
     Parse xml string to python dict
     """
     tree = etree.fromstring(xml)
     return self._make_dict(tree.tag, self._parse_node(tree), tree.attrib)
Beispiel #34
0
def load_definition(xmlstr):
    root = ElementTree.fromstring(xmlstr)
    _analyze_tree(root)
Beispiel #35
0
def extract_data_from_xmlstring_to_dict(xml_string):
    '''
    Extract raw edit data fields from XML for studio submission on Advanced Editor tab.
    The output will be updated to value of fields Basic Template tab.

        1. problem description
        2. Image url
        3. variables (name, min_value, max_value, type, decimal_places)
        4. answer_template_string

    :param xml_string: string in XML format --

        <problem>
            <description>Given a = [a] and b = [b]. Calculate the sum, difference of a and b. </description>
            <images>
                <image_url link="http://example.com/image1">Image 1</image_url>
            </images>
            <variables>
                <variable name="a" min="1" max="200" type="integer"/>
                <variable name="b" min="1.0" max="20.5" type="float" decimal_places="2"/>
            </variables>
            <answer_templates>
                <answer sum = "[a] + [b]" difference = "[a] - [b]">Teacher's answer</answer>
            </answer_templates>
            <string_variables>
                <string_variable default="car" name="string0" original_text="car" value="car">
                    <context_list>
                        <context name="Synonyms of text 'car' (Default)" select="true">
                            <option>car</option>
                            <option>machine</option>
                            <option>truck</option>
                            <option>auto</option>
                            <option>automobile</option>
                            <option>electric car</option>
                        </context>
                    </context_list>
                </string_variable>
                <string_variable default="house" name="string1" original_text="house" value="house">
                    <context_list>
                        <context name="Synonyms of text 'house' (Default)" select="true">
                            <option>house</option>
                            <option>palace</option>
                            <option>building</option>
                            <option>land</option>
                            <option>island</option>
                        </context>
                    </context_list>
                </string_variable>
            </string_variables>
        </problem>

    :return: raw_editor_data_fields -- a dictionary of raw edit supported fields

    '''
    print("## CALLING FUNCTION extract_data_from_xmlstring_to_dict() ##")

    # Reading the xml data from a string:
    # fromstring() parses XML from a string directly into an Element, which is the root element of the parsed tree.
    problem = ET.fromstring(xml_string)
    problem_fields = problem.getchildren()
    # print(problem_fields)

    # init a dict to store problem field values extracted from the xml string
    raw_editor_data_fields = {}

    for field in problem_fields:
        # print("field.tag: " + field.tag)
        # print("field.attrib: ", field.attrib)
        if field.tag == "description":
            # extract the question template
            raw_editor_data_fields["question_template"] = field.text
        elif field.tag == "images":
            # Extract image url
            #
            # A problem can have many images
            # only get first image_url for now
            # TODO: support multiple images
            image_urls = field.findall(
                'image_url')  # find all direct children only for this field.
            # get first image_url
            raw_editor_data_fields["image_url"] = image_urls[0].get(
                'link')  # get its link attrib
        elif field.tag == "variables":
            # Extract variables info
            raw_editor_data_fields["variables"] = {
            }  # initialize the variables dict
            # find all direct child elements named "variable" under 'variables' element
            variable_list = field.findall("variable")
            for variable in variable_list:
                variable_attributes = variable.attrib
                var_name = variable_attributes["name"]

                # add each variable into the variable dict
                raw_editor_data_fields["variables"][
                    var_name] = variable_attributes
        elif field.tag == "answer_templates":
            # Extract the answers
            raw_editor_data_fields["answer_template"] = {
            }  # initialize the answers dict
            # find all direct childs named "answer" of element 'answer_templates'
            answer_list = field.findall("answer")
            i = 0
            for answer in answer_list:
                i = i + 1
                answer_attributes = answer.attrib
                # add each answer into the raw_editor_data_fields dict
                raw_editor_data_fields["answer_template"][
                    i] = answer_attributes
        elif field.tag == "string_variables":
            # Extract string variables info
            raw_editor_data_fields["string_variables"] = {
            }  # initialize the variables dict
            # find all direct child elements named "string_variable" under 'string_variables' element
            string_variable_list = field.findall("string_variable")
            for string_variable in string_variable_list:
                this_string_var = {}
                string_variable_attributes = string_variable.attrib
                var_name = string_variable_attributes["name"]
                this_string_var['name'] = var_name
                this_string_var['original_text'] = string_variable_attributes[
                    "original_text"]
                this_string_var['value'] = string_variable_attributes["value"]
                this_string_var['default'] = string_variable_attributes[
                    "default"]
                this_string_var['context'] = ''  # TBU as following
                this_string_var['context_list'] = {}

                # find all childs elements named "context" under 'string_variable' element
                context_list = string_variable.iter("context")
                i = 0
                for context in context_list:
                    # print "context.tag: {}".format(context.tag)
                    # print "context.attrib: {}".format(context.attrib)
                    context_id = 'context' + str(i)
                    this_string_var['context_list'][context_id] = {}
                    # set attributes

                    this_string_var['context_list'][context_id][
                        'name'] = context.get("name")
                    # this_string_var['context_list'][context_id]['help'] = context.get("help")
                    if context.get(
                            "select"
                    ) == "true":  # update selected context for this variable
                        this_string_var['context'] = context_id
                    # synonyms list
                    # find all children option elements
                    option_list = context.findall("option")
                    # synonym_list = [option.text for option in option_list]
                    synonym_list = []
                    for option in option_list:
                        print option.tag
                        print option.text
                        synonym_list.append(option.text)

                    this_string_var['context_list'][context_id][
                        'synonyms'] = synonym_list
                    # print i, context_id
                    # print "synonym_list: {}".format(synonym_list)
                    i = i + 1
                # print "this_string_var: {}".format(this_string_var)

                # add each variable into the variable dict
                raw_editor_data_fields["string_variables"][
                    var_name] = this_string_var

    print("## End FUNCTION extract_data_from_xmlstring_to_dict() ##")

    return raw_editor_data_fields