Rev 48: Simplification. in http://bazaar.launchpad.net/%7Ebzr/bzr.webdav/webdav

Vincent Ladeuil v.ladeuil+lp at free.fr
Sun Jun 8 09:50:47 BST 2008


At http://bazaar.launchpad.net/%7Ebzr/bzr.webdav/webdav

------------------------------------------------------------
revno: 48
revision-id: v.ladeuil+lp at free.fr-20080608085045-ic1og61059r0w44m
parent: v.ladeuil+lp at free.fr-20080607173105-g5au9vt8txid83ee
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: webdav
timestamp: Sun 2008-06-08 10:50:45 +0200
message:
  Simplification.
  
  * test_webdav.py:
  (TestDavSaxParser): Simplified.
  
  * webdav.py:
  (_extract_dir_content): Remove gratuitous class DavResponseParser.
modified:
  test_webdav.py                 test_webdav.py-20060823130244-qvg4wqdodnmf5nhs-1
  webdav.py                      webdav.py-20060816232542-enpjxth2743ttqpq-3
-------------- next part --------------
=== modified file 'test_webdav.py'
--- a/test_webdav.py	2008-06-07 17:31:05 +0000
+++ b/test_webdav.py	2008-06-08 08:50:45 +0000
@@ -37,7 +37,6 @@
 import sys
 import time
 import urlparse
-import xml.sax
 
 
 from bzrlib import (
@@ -423,17 +422,6 @@
 
 class TestDavSaxParser(tests.TestCase):
 
-    def _get_parser(self, handler=None):
-        if handler is None:
-            handler = webdav.DavResponseHandler()
-        return webdav.DavResponseParser(handler)
-
-    def _parse_string(self, str, url):
-        parser = self._get_parser()
-        parser.handler.set_url(url)
-        parser.parse(StringIO(str), url)
-        return parser.handler
-
     def test_apache2_example(self):
         example = """<?xml version="1.0" encoding="utf-8"?>
 <D:multistatus xmlns:D="DAV:" xmlns:ns0="DAV:">
@@ -470,8 +458,9 @@
         </D:propstat>
     </D:response>
 </D:multistatus>"""
-        handler = self._parse_string(example, 'http://localhost/blah')
-        self.assertEqual(['a', 'b', 'c'], handler.get_dir_content())
+        self.assertEqual(['a', 'b', 'c'],
+                         webdav._extract_dir_content('http://localhost/blah',
+                                                     StringIO(example)))
 
     def test_lighttpd_example(self):
         example = """<?xml version="1.0" encoding="utf-8"?>
@@ -486,8 +475,9 @@
 <D:href>http://localhost/toto</D:href>
 </D:response>
 </D:multistatus>"""
-        handler = self._parse_string(example, 'http://localhost/blah')
-        self.assertEqual(['titi', 'toto'], handler.get_dir_content())
+        self.assertEqual(['titi', 'toto'],
+                         webdav._extract_dir_content('http://localhost/blah',
+                                                     StringIO(example)))
 
     def test_malformed_response(self):
         # Invalid xml, neither multistatus nor response are properly closed
@@ -496,15 +486,15 @@
 <D:response>
 <D:href>http://localhost/</D:href>"""
         self.assertRaises(errors.InvalidHttpResponse,
-                          self._parse_string, example,
-                          'http://localhost/blah')
+                          webdav._extract_dir_content,
+                          'http://localhost/blah', StringIO(example))
 
     def test_unkown_format_response(self):
         # Valid but unrelated xml
         example = """<document/>"""
         self.assertRaises(errors.InvalidHttpResponse,
-                          self._parse_string, example,
-                          'http://localhost/blah')
+                          webdav._extract_dir_content,
+                          'http://localhost/blah', StringIO(example))
 
     def test_incomplete_format_response(self):
         # The minimal information is present but doesn't conform to RFC 2518
@@ -523,5 +513,6 @@
 </D:response>
 <D:href>http://localhost/toto</D:href>
 </D:multistatus>"""
-        handler = self._parse_string(example, 'http://localhost/blah')
-        self.assertEqual(['titi'], handler.get_dir_content())
+        self.assertEqual(['titi'],
+                         webdav._extract_dir_content('http://localhost/blah',
+                                                     StringIO(example)))

=== modified file 'webdav.py'
--- a/webdav.py	2008-06-07 17:31:05 +0000
+++ b/webdav.py	2008-06-08 08:50:45 +0000
@@ -120,6 +120,8 @@
 
     def startDocument(self):
         self.elt_stack = []
+        self.dir_content = None
+        self.chars = None
 
     def endDocument(self):
         if self.dir_content is None:
@@ -132,16 +134,16 @@
             self.chars = []
 
     def endElement(self, name):
-        st = self.elt_stack
-        if (len(st) == 3
-            and st[0] == 'D:multistatus'
-            and st[1] == 'D:response'
-            and name == 'D:href'): # sax guarantees that st[2] is also D:href
+        stack = self.elt_stack
+        if (len(stack) == 3
+            and stack[0] == 'D:multistatus'
+            and stack[1] == 'D:response'
+            and name == 'D:href'): # sax guarantees that stack[2] is also D:href
             if self.dir_content is None:
                 self.dir_content = []
             self.dir_content.append(''.join(self.chars))
         self.chars = None
-        self.elt_stack.pop()
+        stack.pop()
 
     def characters(self, chrs):
         if self._current_element() == 'D:href':
@@ -177,33 +179,22 @@
         return elements
 
 
-class DavResponseParser(object):
-    """A parser for DAV responses.
+def _extract_dir_content(url, infile):
+    """Extract the directory content from a DAV PROPFIND response.
 
-    The main aim is to encapsulate sax house keeping and translate exceptions.
+    :param url: The url used for the PROPFIND request.
+    :param infile: A file-like object pointing at the start of the response.
     """
-
-    def __init__(self, handler=None):
-        if handler is None:
-            handler = DavResponseHandler()
-        self.handler = handler
-        self.parser = None
-
-    def parse(self, infile, url):
-        p = self._get_parser()
-        try:
-            p.parse(infile)
-        except xml.sax.SAXParseException, e:
-            raise errors.InvalidHttpResponse(
-                url, msg='Malformed xml response: %s' % e)
-
-    def _get_parser(self):
-        if self.parser is None:
-            parser = xml.sax.make_parser()
-            parser.setContentHandler(self.handler)
-            self.parser = parser
-        return self.parser
-
+    parser = xml.sax.make_parser()
+
+    handler = DavResponseHandler()
+    parser.setContentHandler(handler)
+    try:
+        parser.parse(infile)
+    except xml.sax.SAXParseException, e:
+        raise errors.InvalidHttpResponse(
+            url, msg='Malformed xml response: %s' % e)
+    return handler.get_dir_content()
 
 class PUTRequest(_urllib2_wrappers.Request):
 



More information about the bazaar-commits mailing list