Exemple #1
0
def check_href(href, expected):
    ruleset = RuleSet(None, None, None, None)
    html = '<html><body><a href="%s">link</a></body></html>'
    doc = document_fromstring(html % href,
                              base_url='http://localhost/path/theme.html')

    ruleset.make_links_absolute(doc)

    assert_equals(tostring(doc), html % expected)
Exemple #2
0
 def __call__(self, get_resource, app, orig_req):
     url = urllib.parse.urljoin(orig_req.url, self.url)
     doc_resp = get_resource(url)
     if doc_resp.status_int == 304 and self._response is not None:
         doc_resp = self._response
     elif doc_resp.status_int == 200:
         self._response = doc_resp
     else:
         ## FIXME: better error
         assert 0, "Bad response: %r" % doc_resp
     ## FIXME: better content-type detection
     if doc_resp.content_type not in (
             'application/xml',
             'text/xml',
     ):
         ## FIXME: better error
         assert 0, "Bad response content-type: %s (from response %r)" % (
             doc_resp.content_type, doc_resp)
     doc_text = doc_resp.body
     try:
         doc = XML(doc_text, base_url=url)
     except XMLSyntaxError as e:
         raise Exception('Invalid syntax in %s: %s' % (url, e))
     assert doc.tag == 'ruleset', ('Bad rule tag <%s> in document %s' %
                                   (doc.tag, url))
     return RuleSet.parse_xml(doc, url)
Exemple #3
0
 def parse_xml(cls, el, source_location):
     """Parse an instance from an XML/etree element"""
     proxies = []
     for child in el:
         if child.tag == "proxy":
             proxies.append(Proxy.parse_xml(child, source_location))
     ruleset = RuleSet.parse_xml(el, source_location)
     return cls(proxies, ruleset, source_location)
Exemple #4
0
 def parse_xml(cls, el, source_location):
     """Parse an instance from an XML/etree element"""
     proxies = []
     for child in el:
         if child.tag == 'proxy':
             proxies.append(Proxy.parse_xml(child, source_location))
     ruleset = RuleSet.parse_xml(el, source_location)
     return cls(proxies, ruleset, source_location)
Exemple #5
0
 def parse_xml(cls, el, source_location, 
               middleware_factory=None,
               middleware_factory_kwargs=None):
     """Parse an instance from an XML/etree element"""
     proxies = []
     for child in el:
         if child.tag == 'proxy':
             proxies.append(Proxy.parse_xml(child, source_location))
     ruleset = RuleSet.parse_xml(el, source_location)
     return cls(proxies, ruleset, source_location, 
                middleware_factory=middleware_factory,
                middleware_factory_kwargs=middleware_factory_kwargs)
Exemple #6
0
    def load_rules(self):
        filename = self.filename

        try:
            fp = open(filename)
            doc = parse(fp, base_url='file://' +
                        os.path.abspath(filename)).getroot()
            fp.close()
        except XMLSyntaxError as e:
            raise Exception('Invalid syntax in %s: %s' % (filename, e))
        assert doc.tag == 'ruleset', ('Bad rule tag <%s> in document %s' %
                                      (doc.tag, filename))
        assert doc.tag == 'ruleset', ('Bad rule tag <%s> in document %s' %
                                      (doc.tag, filename))
        self.ruleset = RuleSet.parse_xml(doc, filename)
Exemple #7
0
 def parse_xml(cls,
               el,
               source_location,
               middleware_factory=None,
               middleware_factory_kwargs=None):
     """Parse an instance from an XML/etree element"""
     proxies = []
     for child in el:
         if child.tag == 'proxy':
             proxies.append(Proxy.parse_xml(child, source_location))
     ruleset = RuleSet.parse_xml(el, source_location)
     return cls(proxies,
                ruleset,
                source_location,
                middleware_factory=middleware_factory,
                middleware_factory_kwargs=middleware_factory_kwargs)
Exemple #8
0
class FileRuleGetter(object):
    """
    An implementation of `rule_getter` for `DeliveranceMiddleware`.
    This reads the rules from a file.

    If always_reload=True, the file will be re-read on every request.
    """
    def load_rules(self):
        filename = self.filename

        try:
            fp = open(filename)
            doc = parse(fp, base_url='file://' +
                        os.path.abspath(filename)).getroot()
            fp.close()
        except XMLSyntaxError, e:
            raise Exception('Invalid syntax in %s: %s' % (filename, e))
        assert doc.tag == 'ruleset', ('Bad rule tag <%s> in document %s' %
                                      (doc.tag, filename))
        assert doc.tag == 'ruleset', ('Bad rule tag <%s> in document %s' %
                                      (doc.tag, filename))
        self.ruleset = RuleSet.parse_xml(doc, filename)
Exemple #9
0
class SubrequestRuleGetter(object):
    """
    An implementation of `rule_getter` for `DeliveranceMiddleware`.
    This retrieves and instantiates the rules using a subrequest with
    the given url.
    """

    _response = None

    def __init__(self, url):
        self.url = url

    def __call__(self, get_resource, app, orig_req):
        url = urlparse.urljoin(orig_req.url, self.url)
        doc_resp = get_resource(url)
        if doc_resp.status_int == 304 and self._response is not None:
            doc_resp = self._response
        elif doc_resp.status_int == 200:
            self._response = doc_resp
        else:
            ## FIXME: better error
            assert 0, "Bad response: %r" % doc_resp
        ## FIXME: better content-type detection
        if doc_resp.content_type not in (
                'application/xml',
                'text/xml',
        ):
            ## FIXME: better error
            assert 0, "Bad response content-type: %s (from response %r)" % (
                doc_resp.content_type, doc_resp)
        doc_text = doc_resp.body
        try:
            doc = XML(doc_text, base_url=url)
        except XMLSyntaxError, e:
            raise Exception('Invalid syntax in %s: %s' % (url, e))
        assert doc.tag == 'ruleset', ('Bad rule tag <%s> in document %s' %
                                      (doc.tag, url))
        return RuleSet.parse_xml(doc, url)
Exemple #10
0
 def __call__(self, get_resource, app, orig_req):
     return RuleSet.parse_xml(fromstring(self.rules), '')