Commits

Gregory Petukhov committed 473de95

Add runtime_body property which links to live(dynamic) body of response. That makes sense only for javascript-eanbled transports like Kit transport.

  • Participants
  • Parent commits 5f4189e

Comments (0)

Files changed (3)

File grab/ext/lxml.py

         from lxml.etree import ParserError
 
         if self._lxml_tree is None:
-            body = self.response.unicode_body(
+            body = self.response.unicode_runtime_body(
                 fix_special_entities=self.config['fix_special_entities']
             ).strip()
             #if self.config['tidy']:

File grab/kit/__init__.py

     def build_response(self, resource):
         response = Response()
         response.head = ''
-        #response.runtime_body = unicode(self.page.mainFrame().toHtml()).encode('utf-8')
+        response.runtime_body = unicode(self.page.mainFrame().toHtml()).encode('utf-8')
         response.body = str(resource.reply.data)
         response.code = resource.status_code
         response.url = str(resource.reply.url().toString())
     logging.basicConfig(level=logging.DEBUG)
 
     br = Kit(gui=False)
-    resp = br.request('http://httpbin.org/post', method='post', cookies={'foo': 'bar'},
-                      data='foo=bar')
-    print resp.body
+    #resp = br.request('http://httpbin.org/post', method='post', cookies={'foo': 'bar'},
+                      #data='foo=bar')
+    #print resp.body
+    resp = br.request('http://ya.ru/')
+    print unicode(br.page.mainFrame().documentElement().findFirst('title').toPlainText())

File grab/response.py

         self.code = None
         self.head = None
         self._body = None
+        self._runtime_body = None
         #self.runtime_body = None
         self.body_path = None
         self.headers =None
         #self.cookiejar = None
         self.charset = 'utf-8'
         self._unicode_body = None
+        self._unicode_runtime_body = None
         self.bom = None
         self.done_time = None
 
             else:
                 self.charset = charset
 
+    def process_unicode_body(self, body, bom, charset, ignore_errors, fix_special_entities):
+        if ignore_errors:
+            errors = 'ignore'
+        else:
+            errors = 'strict'
+        if bom:
+            body = body[len(self.bom):]
+        else:
+            body = body
+        if fix_special_entities:
+            body = encoding_tools.fix_special_entities(body)
+        return body.decode(charset, errors).strip()
+
     def unicode_body(self, ignore_errors=True, fix_special_entities=True):
         """
         Return response body as unicode string.
         """
 
         if not self._unicode_body:
-            if ignore_errors:
-                errors = 'ignore'
-            else:
-                errors = 'strict'
-            if self.bom:
-                body = self.body[len(self.bom):]
-            else:
-                body = self.body
-            if fix_special_entities:
-                body = encoding_tools.fix_special_entities(body)
-            ubody = body.decode(self.charset, errors).strip()
-            self._unicode_body = ubody
+            self._unicode_body = self.process_unicode_body(
+                self.body, self.bom, self.charset,
+                ignore_errors, fix_special_entities)
         return self._unicode_body
 
+    def unicode_runtime_body(self, ignore_errors=True, fix_special_entities=True):
+        """
+        Return response body as unicode string.
+        """
+
+        if not self._unicode_runtime_body:
+            self._unicode_runtime_body = self.process_unicode_body(
+                self.runtime_body, None, self.charset,
+                ignore_errors, fix_special_entities)
+        return self._unicode_runtime_body
+
     def copy(self):
         """
         Clone the Response object.
 
     body = property(_read_body, _write_body)
 
-    #def _read_runtime_body(self):
-        #if self._runtime_body is None:
-            #return self.body
-        #else:
-            #return self._runtime_body
+    def _read_runtime_body(self):
+        if self._runtime_body is None:
+            return self.body
+        else:
+            return self._runtime_body
 
-    #def _write_runtime_body(self, body):
-        #self._runtime_body = body
+    def _write_runtime_body(self, body):
+        self._runtime_body = body
 
-    #runtime_body = property(_read_runtime_body, _write_runtime_body)
+    runtime_body = property(_read_runtime_body, _write_runtime_body)