/usr/share/pyshared/mx/URL/URL.py is in python-egenix-mxurl 3.2.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | """ mx.URL -- A URL datatype.
Relies on mx.TextTools and mx.Tools.
Copyright (c) 1998-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2011, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
"""
import mimetypes
from mx import Tools,TextTools
#
# Import the C extension module
#
from mxURL import *
from mxURL import __version__
#
# Registry for the URL object provided by mxURL:
#
def register_scheme(scheme,uses_netloc, uses_params, uses_query, uses_fragment,
uses_relative):
""" Adds a new scheme to the URL objects scheme registry. The uses_*
fields must be 0 or 1 according to the schemes possibilities.
"""
schemes[scheme] = (uses_netloc, uses_params, uses_query, uses_fragment,
uses_relative)
#
# Registry for the URL object provided by mxURL:
#
def register_mimetype(extension,major='*',minor='*'):
""" Adds a new mimetype to the registry used by mxURL.
extension must be a file name extension including the
delimiting dot (e.g. ".html"). The function will overwrite any
existing entry for the given extension.
"""
assert extension[0] == '.'
mimemap[extension] = '%s/%s' % (major,minor)
#
# Add some extra MIME types that are not included in the distribution
# version of mimetypes.py (not until Python 1.5 at least).
#
mimemap.update({
'.shtml': 'text/html',
'.phtml': 'text/html',
'.pcx': 'image/pcx',
'.txt': 'text/plain',
'.css': 'text/css',
'.pyo': 'application/x-python-code',
'.c': 'text/x-c',
'.h': 'text/x-c',
'.cpp': 'text/x-c',
'.cxx': 'text/x-c',
'.hxx': 'text/x-c',
'.xml': 'text/xml',
'.xsl': 'application/xml',
'.dll': 'application/octet-stream',
'.pyd': 'application/octet-stream',
})
### Module init stuff
class _modinit:
# Reserved URL chars as defined by RFC2396
unsafe_charset = TextTools.set(\
'abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789'
'-_.!~*\'()',0)
# Modified version of the above set which includes even fewer
# characters (esp. dots and quotes are not included)
rpc_unsafe_charset = TextTools.set(\
'abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789'
'-_()',0)
def escape(urltext,unsafe_charset=_modinit.unsafe_charset,
ord=ord,len=len,
setsplitx=TextTools.setsplitx,join=TextTools.join):
""" Escape all special chars in a URL text using the %xx-encoding.
The urltext is considered not being part of the URL itself but
only a subpart.
XXX Recode in C for speed.
"""
hl = setsplitx(urltext,unsafe_charset)
if len(hl) > 1:
for i in range(1,len(hl),2):
text = ''
for c in hl[i]:
text = text + '%%%02X' % ord(c)
hl[i] = text
return join(hl,'')
return urltext
def unescape(urltext,
charsplit=TextTools.charsplit,
join=TextTools.join,chr=chr,int=int,
irange=Tools.irange,len=len,trange=Tools.trange):
""" Unescape a URL text part containing %xx-character encodings
XXX Recode in C for speed.
"""
hl = charsplit(urltext,'%')
if len(hl) > 1:
for i,text in irange(hl,trange(1,len(hl))):
hl[i] = chr(int(text[:2],16)) + text[2:]
return join(hl,'')
return urltext
# Aliases
urlencode = escape
urldecode = unescape
quote = escape
unquote = unescape
def rpcencode(name,args=(),kws=None,prefix='',encode=1,
urlencode=urlencode,join=TextTools.join,
rpc_unsafe_charset=_modinit.rpc_unsafe_charset,
irange=Tools.irange,tuple=tuple):
""" Return a string encoding a call of name with the given string
arguments given in tuple args and dict kws (may be None).
kws may also be None if no keywords are needed.
Arguments and keywords are converted to strings (if not
already given as strings) and may *not* contain ',' or '='
characters, since these are used to separate the arguments in
the argument list.
The resulting string is prefixed with prefix and then
urlencoded if encode is true (default).
The encoding scheme used looks like this:
* no arguments: 'name()'
* one argument: 'name(arg0)'
* >1 argument: 'name(arg0,arg1,arg2,...)'
* arguments and keywords: 'name(arg0,arg1,kw0=val0,kw1=val1,...)'
"""
if args:
arguments = map(str,args)
else:
arguments = []
if kws:
l = kws.items()
for i,item in irange(l):
l[i] = '%s=%s' % item
arguments[len(arguments):] = l
if not arguments:
rpc = prefix+name+'()'
else:
rpc = '%s%s(%s)' % (prefix,name,join(arguments,','))
if encode:
return urlencode(rpc,unsafe_charset=rpc_unsafe_charset)
else:
return rpc
def rpcdecode(url,prefix='',decode=1,
splitat=TextTools.splitat,charsplit=TextTools.charsplit,
len=len,tuple=tuple,urldecode=urldecode):
""" Decode a RPC encoded function/method call.
Returns a tuple (name,args,kws) where args is a tuple of
string arguments and kws is a dictionary containing the given
keyword parameters or None. All parameters are returned as
strings; it is up to the caller to decode them into
e.g. integers, etc.
If prefix is given and found it is removed from the name prior
to returning it. decode can be set to false to prevent the url
from being urldecoded prior to processing.
The decode function also supports the syntax 'method' instead
of 'method()' for calls without arguments.
"""
if decode:
url = urldecode(url)
# Decode the method: method[(arg0,arg1,...,kw0=val0,kw1=val1,...)]
name,rawargs = splitat(url,'(')
if rawargs:
# Cut out the pure argument part, ignoring any character after
# the final ')'
rawargs,rest = splitat(rawargs,')',-1)
# Argument list: split at ','
args = charsplit(rawargs,',')
if '=' in rawargs:
kws = {}
for i,arg in Tools.reverse(Tools.irange(args)):
if '=' in arg:
k,v = splitat(arg,'=')
kws[k] = v
del args[i]
else:
kws = None
args = tuple(args)
else:
args = ()
kws = None
if prefix:
if name[:len(prefix)] == prefix:
name = name[len(prefix):]
return name,args,kws
else:
return name,args,kws
def queryencode(items, prefix='?',
urlencode=urlencode,join=TextTools.join,str=str):
""" Takes a sequence of key,value items and formats a URL encoded
query part out of it.
Keys and values are converted to string prior to URL
conversion.
prefix is prepended to the resulting string. It defaults to
'?' so that the returned value can directly be concatenated to
a URL.
"""
l = []
append = l.append
for k,v in items:
append(urlencode(str(k)) + '=' + urlencode(str(v)))
return prefix + join(l, '&')
def querydecode(query,
charsplit=TextTools.charsplit,splitat=TextTools.splitat,
urldecode=urldecode):
""" Decodes a query string and returns a list of items (key, value).
If query is prefixed with a question mark ('?'), this is
silently ignored.
Query parts which don't provide a value will get None assigned
as value in the items list.
"""
if query and query[0] == '?':
query = query[1:]
if not query:
return []
pairs = charsplit(query, '&')
items = []
for pair in pairs:
if '=' in pair:
key, value = splitat(pair, '=')
key = urldecode(key)
value = urldecode(value)
else:
key = urldecode(pair)
value = None
items.append((key, value))
return items
def addscheme(url,
URL=URL):
""" Returns the URL url with scheme added according to common
usage.
If the url already provides a scheme, nothing is changed.
Strings are turned into URL object by the function.
These conventions are used:
www. -> http://www.
ftp. -> ftp://ftp.
[/.] -> file:[/.]
none of these -> http://
"""
url = URL(url)
if not url.scheme:
if url.string[:4] == 'www.':
return url.recode(scheme='http')
elif url.string[:4] == 'ftp.':
return url.recode(scheme='ftp')
elif url.string[:1] in ('/','.'):
return url.recode(scheme='file')
else:
return url.recode(scheme='http')
return url
|