Package shttp :: Module uri
[hide private]
[frames] | no frames]

Source Code for Module shttp.uri

 1  #coding: iso-8859-1; 
 2  ############################################################################ 
 3  #                                                                          # 
 4  #    This file 'shttp/uri.py'                                         # 
 5  #    is part of 'SPyRO: Simple Python Remote Objects'                      # 
 6  #    Copyright (C) 2004-2005 by Eric Sadit Téllez Avila                    # 
 7  #    sadit@lsc.fie.umich.mx or donsadit@gmail.com                          # 
 8  #                                                                          # 
 9  #    This program is free software; you can redistribute it and#or modify  # 
10  #    it under the terms of the GNU General Public License as published by  # 
11  #    the Free Software Foundation; either version 2 of the License, or     # 
12  #    (at your option) any later version.                                   # 
13  #                                                                          # 
14  #    This program is distributed in the hope that it will be useful,       # 
15  #    but WITHOUT ANY WARRANTY; without even the implied warranty of        # 
16  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         # 
17  #    GNU General Public License for more details.                          # 
18  #                                                                          # 
19  #    You should have received a copy of the GNU General Public License     # 
20  #    along with this program; if not, write to the                         # 
21  #    Free Software Foundation, Inc.,                                       # 
22  #    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             # 
23  ############################################################################ 
24  """ A couple of functions to interpret and manage URIs.""" 
25  import re 
26  import os 
27  URI = re.compile(r'(?P<protocol> [\d\w]+)://(?P<host> [^:/]+):?(?P<port> \d*)(?P<path> /[^\#\?]*)? (?P<query> \?[^\#]+)? (?P<sharp> \#.+)?',re.I|re.X) 
28  QUERYSEP = re.compile(r'(?P<path> [^#]+)\?(?P<querysep> [^\?]+)$',re.I|re.X) 
29   
30 -def query_string(uri):
31 s = QUERYSEP.search(uri) 32 if s == None or not s.groupdict().has_key('querysep'): 33 return "" 34 else: 35 return s.groupdict()['querysep']
36 37 first_slash = re.compile(r"^/+")
38 -def split_query(uri):
39 s=QUERYSEP.search(uri) 40 if s == None: 41 return uri 42 else: 43 return s.groups()
44
45 -def normlocation(dictmatch):
46 path = dictmatch['path'] 47 if not (path is None) and path != '' and path != '/': 48 path = os.path.normpath(first_slash.sub("/",path)) 49 else: 50 path = '/' 51 return "%s%s%s"%(path, dictmatch['query'] or '', dictmatch['sharp'] or '')
52
53 -def normurldict(d, host):
54 url = d['protocol'] + "://" + host 55 port = d['port'] 56 if port: url = url + ":" + port 57 url = url + normlocation(d) 58 return url
59
60 -def normurl(urlarg):
61 m = URI.match(urlarg) 62 if m is None: return urlarg 63 d = m.groupdict() 64 return normurldict(d, d['host'])
65