Esempio n. 1
0
    def __init__(self, test_uri):
        """
        Constractor
        @param test_uri: Test Uri
        """
        self.test_uri = test_uri
        try:
            self.red = InspectingResourceExpertDroid(self.test_uri)
            self.result = ""
            self.done = False
            self.groups = []
            logger = logging.getLogger()
            logger.setLevel(logging.DEBUG)
#            self.formatter = find_formatter('txt', 'txt', False)(
#                self.test_uri, self.test_uri, [], 'en', None
#            )
#            self.formatter.set_red(self.red.state)
#            self.formatter.start_output()

            self.red.run(self.done)
            nbhttp.run()
            
            if self.red.state.res_complete:
                self.result = self._generate_output_xml(test_uri).toprettyxml()
            else:
                error_string = ""
                if self.red.state.res_error['desc'] == nbhttp.error.ERR_CONNECT['desc']:
                    error_string = "Could not connect to the server (%s)" % self.red.res_error.get('detail', "unknown")
                elif self.red.state.res_error['desc'] == nbhttp.error.ERR_URL['desc']:
                    error_string = self.red.res_error.get('detail', "RED can't fetch that URL.")
                elif self.red.state.res_error['desc'] == nbhttp.error.ERR_READ_TIMEOUT['desc']:
                    error_string = self.red.res_error['desc']
                elif self.red.state.res_error['desc'] == nbhttp.error.ERR_HTTP_VERSION['desc']:
                    error_string = "<code>%s</code> isn't HTTP." % e(self.red.res_error.get('detail', '')[:20])
                else:
                    raise AssertionError, "Unidentified incomplete response error."
                self.result = self._generate_error_xml(error_string).toprettyxml()
        except:
            import traceback
            logging.error(traceback.format_exc())
            self.result = """<?xml version="1.0" ?>
Esempio n. 2
0
 def __init__(self, test_uri):
     """
     Constractor
     @param test_uri: Test Uri
     """
     self.test_uri = test_uri
     try:
         self.red = InspectingResourceExpertDroid(self.test_uri)
         self.result = ""
         self.done = False
         self.groups = []
         logger = logging.getLogger()
         logger.setLevel(logging.DEBUG)
         if self.red.res_complete:
             self.result = self._generate_output_xml(test_uri).toprettyxml()
         else:
             error_string = ""
             if self.red.res_error['desc'] == nbhttp.error.ERR_CONNECT[
                     'desc']:
                 error_string = "Could not connect to the server (%s)" % self.red.res_error.get(
                     'detail', "unknown")
             elif self.red.res_error['desc'] == nbhttp.error.ERR_URL[
                     'desc']:
                 error_string = self.red.res_error.get(
                     'detail', "RED can't fetch that URL.")
             elif self.red.res_error[
                     'desc'] == nbhttp.error.ERR_READ_TIMEOUT['desc']:
                 error_string = self.red.res_error['desc']
             elif self.red.res_error[
                     'desc'] == nbhttp.error.ERR_HTTP_VERSION['desc']:
                 error_string = "<code>%s</code> isn't HTTP." % e(
                     self.red.res_error.get('detail', '')[:20])
             else:
                 raise AssertionError, "Unidentified incomplete response error."
             self.result = self._generate_error_xml(
                 error_string).toprettyxml()
     except:
         import traceback
         logging.error(traceback.format_exc())
         self.result = """<?xml version="1.0" ?>
Esempio n. 3
0
class UnicornUi(object):
    """
    Unicorn Interface of Red Cacheability checker
    """
    def __init__(self, test_uri):
        """
        Constractor
        @param test_uri: Test Uri
        """
        self.test_uri = test_uri
        try:
            self.red = InspectingResourceExpertDroid(self.test_uri)
            self.result = ""
            self.done = False
            self.groups = []
            logger = logging.getLogger()
            logger.setLevel(logging.DEBUG)
#            self.formatter = find_formatter('txt', 'txt', False)(
#                self.test_uri, self.test_uri, [], 'en', None
#            )
#            self.formatter.set_red(self.red.state)
#            self.formatter.start_output()

            self.red.run(self.done)
            nbhttp.run()
            
            if self.red.state.res_complete:
                self.result = self._generate_output_xml(test_uri).toprettyxml()
            else:
                error_string = ""
                if self.red.state.res_error['desc'] == nbhttp.error.ERR_CONNECT['desc']:
                    error_string = "Could not connect to the server (%s)" % self.red.res_error.get('detail', "unknown")
                elif self.red.state.res_error['desc'] == nbhttp.error.ERR_URL['desc']:
                    error_string = self.red.res_error.get('detail', "RED can't fetch that URL.")
                elif self.red.state.res_error['desc'] == nbhttp.error.ERR_READ_TIMEOUT['desc']:
                    error_string = self.red.res_error['desc']
                elif self.red.state.res_error['desc'] == nbhttp.error.ERR_HTTP_VERSION['desc']:
                    error_string = "<code>%s</code> isn't HTTP." % e(self.red.res_error.get('detail', '')[:20])
                else:
                    raise AssertionError, "Unidentified incomplete response error."
                self.result = self._generate_error_xml(error_string).toprettyxml()
        except:
            import traceback
            logging.error(traceback.format_exc())
            self.result = """<?xml version="1.0" ?>
<observationresponse ref="None" xml:lang="en" xmlns="http://www.w3.org/2009/10/unicorn/observationresponse">
    <message type="error">
        <title>
            Internal Server Error
        </title>
        <description>
            Internal Server Error occured.
        </description>
    </message>
</observationresponse>"""

    def done(self):
#        self.formatter.finish_output()
        nbhttp.stop()
        
    def get_result(self):
        """
        Return result if cacheability check was finished.
        If not, return None
        @return: Result of cacheablity checker.
        """
        return str(self.result)
        
    def _get_response_document(self):
        """
        Generate response document
        @return: Root response document DOM object
        """
        doc = minidom.Document()
        rootDoc = doc.createElement("observationresponse")
        rootDoc.setAttribute("xmlns", "http://www.w3.org/2009/10/unicorn/observationresponse")
        rootDoc.setAttribute("xml:lang", "en")
        rootDoc.setAttribute("ref", self.test_uri)
        doc.appendChild(rootDoc)
        return rootDoc, doc
    
    def _output_response_header(self, doc, rootDoc):
        """
        Generate HTTP Response Header to Outputs
        """
        m = doc.createElement("message")
        m.setAttribute("type", "info")
        m.setAttribute("group", "response_header")
        title = doc.createElement("title")
        title.appendChild(doc.createTextNode("HTTP Response Header"))
        description = doc.createElement("description")
        ul = doc.createElement("ul")
        ul.setAttribute("class", "headers")
        description.appendChild(ul)
        for i in self.red.state.res_hdrs:
            li = doc.createElement("li")
            li.appendChild(doc.createTextNode(i[0] + ":" + i[1]))
            ul.appendChild(li)
        m.appendChild(title)
        m.appendChild(description)
        rootDoc.appendChild(m)
    
    def _handle_category(self, category_value):
        """
        Getting Classification key from values
        """
        category = _Classifications.__dict__.keys()[_Classifications.__dict__.values().index(category_value)]
        self.groups.append(category)
        return str(category).lower()
    
    def _add_group_elements(self, doc, rootDoc):
        """
        Getting group informations from _Classifications class
        This implimentation is little a bit hack :)
        """
        #Header group
        h_group_element = doc.createElement("group")
        h_group_element.setAttribute("name", "response_header")
        h_title_element = doc.createElement("title")
        h_title_element.appendChild(doc.createTextNode("HTTP Response Header"))
        h_group_element.appendChild(h_title_element)
        rootDoc.appendChild(h_group_element)
        
        for k in set(self.groups):
            group_element = doc.createElement("group")
            group_element.setAttribute("name", str(k).lower())
            title_element = doc.createElement("title")
            title_text = doc.createTextNode(getattr(_Classifications, k))
            title_element.appendChild(title_text)
            group_element.appendChild(title_element)
            rootDoc.appendChild(group_element)        

    def _generate_output_xml(self, test_uri):
        """
        Generate Output XML Document
        @return: Output XML Document
        """
        rootDoc, doc = self._get_response_document()
        for i in self.red.state.messages:
            m = doc.createElement("message")
            m.setAttribute("type", self._convert_level(i.level))

            """
            Hack
            TODO: clean up this code
            """            
            category = self._handle_category(i.category)
            m.setAttribute("group", category)
            
            title = doc.createElement("title")
            title.appendChild(doc.createTextNode(i.summary['en'] % i.vars))
            text = "<description>" + (i.text['en'] % i.vars) + "</description>"
            try:
                text_dom = parseString(self._convert_html_tags(text))
            except:
                logging.error(text)
                text_dom = parseString("<description>Internal Error</description>")
            text_element = text_dom.getElementsByTagName("description")
            m.appendChild(title)
            m.appendChild(text_element[0])
            rootDoc.appendChild(m)
        
        self._output_response_header(doc, rootDoc)
        self._add_group_elements(doc, rootDoc)
        
        return doc
        
    def _generate_error_xml(self, error_message):
        '''
        Return Error XML Document
        @return: Error XML Document
        '''
        rootDoc, doc = self._get_response_document()
        m = doc.createElement("message")
        m.setAttribute("type", "error")
        title = doc.createElement("title")
        title.appendChild(doc.createTextNode("Checker Error"))
        text = "<description>" + error_message + "</description>"
        try:
            text_dom = parseString(self._convert_html_tags(text))
        except:
            logging.error(text)
            text_dom = parseString("<description>Internal Error</description>")
        text_element = text_dom.getElementsByTagName("description")
        m.appendChild(title)
        m.appendChild(text_element[0])
        rootDoc.appendChild(m)
        return doc
    
    def _convert_level(self, level):
        '''
        Convert verbose level string from Redbot style to unicorn style
        '''
        level = re.sub("good", "info", level)
        level = re.sub("bad", "error", level)
        return level
    
    def _convert_html_tags(self, string):
        string = re.sub("<p>", "<br />", string)
        string = re.sub("</p>", "<br />", string)
        string = re.sub("<br/>", "<br />", string)
        string = re.sub("<br>", "<br />", string)
        return string