Ticket #39: urlquote.patch

File urlquote.patch, 2.6 kB (added by John Hampton <pacopablo@pacopablo.com>, 2 years ago)

Patch to handle URL encoded strings for File Source?

  • pyndexter/sources/file.py

    old new  
    2222import os 
    2323from stat import * 
    2424from urlparse import urlsplit, urlunsplit 
     25from urllib import quote, unquote 
    2526from pyndexter import * 
    2627 
    2728 
     
    5657 
    5758    def matches(self, uri): 
    5859        scheme, netloc, path, query, fragment = urlsplit(uri, 'file') 
    59         path = os.path.normpath(path) 
     60        path = os.path.normpath(unquote(path)) 
     61        print "matches: path = %s" % path 
    6062        return scheme == 'file' and \ 
    6163               path.startswith(self.path) and \ 
    6264               self.predicate(path) 
     
    6466 
    6567    def fetch(self, uri): 
    6668        path = self._uri2file(uri) 
     69        print "fetch: path = %s" % path 
    6770        try: 
    6871            stat = os.stat(path) 
    6972        except Exception, e: 
     
    8588        return codecs.open(path, encoding='utf-8', errors='replace').read() 
    8689 
    8790    def _file2uri(self, file): 
    88         return urlunsplit(('file', '', file, '', '')) 
     91        return urlunsplit(('file', '', quote(file), '', '')) 
    8992 
    9093    def _uri2file(self, uri): 
    9194        scheme, location, path, query, fragment = urlsplit(uri, 'file') 
    9295        if scheme != 'file': 
    9396            raise InvalidURI("URI scheme in '%s' not supported by FileSource" 
    9497                             % scheme) 
    95         path = os.path.normpath(path
     98        path = os.path.normpath(unquote(path)
    9699        if not path.startswith(self.path): 
    97100            raise InvalidURI("Requested URI '%s' is not from this FileSource" 
    98101                             % uri) 
  • pyndexter/util.py

    old new  
    77# 
    88 
    99import re 
     10from StringIO import StringIO 
    1011try: 
    1112    set = set 
    1213except: 
     
    3536    def __init__(self, uri=None): 
    3637        if uri is not None: 
    3738            from cgi import parse_qs 
     39            from urllib import unquote_plus 
    3840 
    3941            match = self._pattern.match(uri) 
    4042            if match is None: 
    4143                raise ValueError('Invalid URI') 
    42             groups = match.groups() 
    43             groups = groups[0:5] + (parse_qs(groups[5] or ''),) + groups[6:] 
    44             groups = [group or '' for group in groups] 
     44            groups = [g or '' for g in match.groups()] 
     45            def uqc(gs): 
     46                return [unquote_plus(g) for g in gs] 
     47            groups = uqc(groups[0:5]) + [parse_qs(groups[5] or '')] + \ 
     48                     uqc(groups[6:]) 
    4549        else: 
    4650            groups = [''] * 7 
    4751