uripath (version $Id: uripath.py,v 1.21 2007/06/26 02:36:16 syosi Exp $)
index
/Users/yosi/CVSROOT/WWW/2000/10/swap/uripath.py

Uniform Resource Identifier (URI) path manipulation,
above the access layer
 
The name of this module and the functions are somewhat
arbitrary; they hark to other parts of the python
library; e.g. uripath.join() is somewhat like os.path.join().
 
REFERENCES
 
  Uniform Resource Identifiers (URI): Generic Syntax
  http://www.ietf.org/rfc/rfc2396.txt
 
  The Web Model: Information hiding and URI syntax (Jan 98)
  http://www.w3.org/DesignIssues/Model.html
 
  URI API design [was: URI Test Suite] Dan Connolly (Sun, Aug 12 2001)
  http://lists.w3.org/Archives/Public/uri/2001Aug/0021.html

 
Modules
       
os
re
string
unittest

 
Classes
       
unittest.TestCase(__builtin__.object)
Tests

 
class Tests(unittest.TestCase)
    
Method resolution order:
Tests
unittest.TestCase
__builtin__.object

Methods defined here:
testPaths(self)
testRFCCases(self)
testSplit(self)

Methods inherited from unittest.TestCase:
__call__(self, *args, **kwds)
__init__(self, methodName='runTest')
Create an instance of the class that will use the named test
method when executed. Raises a ValueError if the instance does
not have a method with the specified name.
__repr__(self)
__str__(self)
assertAlmostEqual = failUnlessAlmostEqual(self, first, second, places=7, msg=None)
Fail if the two objects are unequal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
 
Note that decimal places (from zero) are usually not the same
as significant digits (measured from the most signficant digit).
assertAlmostEquals = failUnlessAlmostEqual(self, first, second, places=7, msg=None)
Fail if the two objects are unequal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
 
Note that decimal places (from zero) are usually not the same
as significant digits (measured from the most signficant digit).
assertEqual = failUnlessEqual(self, first, second, msg=None)
Fail if the two objects are unequal as determined by the '=='
operator.
assertEquals = failUnlessEqual(self, first, second, msg=None)
Fail if the two objects are unequal as determined by the '=='
operator.
assertFalse = failIf(self, expr, msg=None)
Fail the test if the expression is true.
assertNotAlmostEqual = failIfAlmostEqual(self, first, second, places=7, msg=None)
Fail if the two objects are equal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
 
Note that decimal places (from zero) are usually not the same
as significant digits (measured from the most signficant digit).
assertNotAlmostEquals = failIfAlmostEqual(self, first, second, places=7, msg=None)
Fail if the two objects are equal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
 
Note that decimal places (from zero) are usually not the same
as significant digits (measured from the most signficant digit).
assertNotEqual = failIfEqual(self, first, second, msg=None)
Fail if the two objects are equal as determined by the '=='
operator.
assertNotEquals = failIfEqual(self, first, second, msg=None)
Fail if the two objects are equal as determined by the '=='
operator.
assertRaises = failUnlessRaises(self, excClass, callableObj, *args, **kwargs)
Fail unless an exception of class excClass is thrown
by callableObj when invoked with arguments args and keyword
arguments kwargs. If a different type of exception is
thrown, it will not be caught, and the test case will be
deemed to have suffered an error, exactly as for an
unexpected exception.
assertTrue = failUnless(self, expr, msg=None)
Fail the test unless the expression is true.
assert_ = failUnless(self, expr, msg=None)
Fail the test unless the expression is true.
countTestCases(self)
debug(self)
Run the test without collecting errors in a TestResult
defaultTestResult(self)
fail(self, msg=None)
Fail immediately, with the given message.
failIf(self, expr, msg=None)
Fail the test if the expression is true.
failIfAlmostEqual(self, first, second, places=7, msg=None)
Fail if the two objects are equal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
 
Note that decimal places (from zero) are usually not the same
as significant digits (measured from the most signficant digit).
failIfEqual(self, first, second, msg=None)
Fail if the two objects are equal as determined by the '=='
operator.
failUnless(self, expr, msg=None)
Fail the test unless the expression is true.
failUnlessAlmostEqual(self, first, second, places=7, msg=None)
Fail if the two objects are unequal as determined by their
difference rounded to the given number of decimal places
(default 7) and comparing to zero.
 
Note that decimal places (from zero) are usually not the same
as significant digits (measured from the most signficant digit).
failUnlessEqual(self, first, second, msg=None)
Fail if the two objects are unequal as determined by the '=='
operator.
failUnlessRaises(self, excClass, callableObj, *args, **kwargs)
Fail unless an exception of class excClass is thrown
by callableObj when invoked with arguments args and keyword
arguments kwargs. If a different type of exception is
thrown, it will not be caught, and the test case will be
deemed to have suffered an error, exactly as for an
unexpected exception.
id(self)
run(self, result=None)
setUp(self)
Hook method for setting up the test fixture before exercising it.
shortDescription(self)
Returns a one-line description of the test, or None if no
description has been provided.
 
The default implementation of this method returns the first line of
the specified test method's docstring.
tearDown(self)
Hook method for deconstructing the test fixture after testing it.

Data and other attributes inherited from unittest.TestCase:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'TestCase' objects>
list of weak references to the object (if defined)
failureException = <class exceptions.AssertionError>
Assertion failed.

 
Functions
       
base()
The base URI for this process - the Web equiv of cwd
 
Relative or abolute unix-standard filenames parsed relative to
this yeild the URI of the file.
If we had a reliable way of getting a computer name,
we should put it in the hostname just to prevent ambiguity
canonical(str_in)
Convert equivalent URIs (or parts) to the same string
 
There are many differenet levels of URI canonicalization
which are possible.  See http://www.ietf.org/rfc/rfc3986.txt
Done:
- Converfting unicode IRI to utf-8
- Escaping all non-ASCII
- De-escaping, if escaped, ALPHA (%41-%5A and %61-%7A), DIGIT (%30-%39),
  hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) (Sect 2.4) 
- Making all escapes uppercase hexadecimal
Not done:
- Making URI scheme lowercase
- changing /./ or  /foo/../ to / with care not to change host part
 
 
>>> canonical("foo bar")
'foo%20bar'
 
>>> canonical(u'http:')
'http:'
 
>>> canonical('fran%c3%83%c2%a7ois')
'fran%C3%83%C2%A7ois'
 
>>> canonical('a')
'a'
 
>>> canonical('%4e')
'N'
 
>>> canonical('%9d')
'%9D'
 
>>> canonical('%2f')
'%2F'
 
>>> canonical('%2F')
'%2F'
join(here, there)
join an absolute URI and URI reference
(non-ascii characters are supported/doctested;
haven't checked the details of the IRI spec though)
 
here is assumed to be absolute.
there is URI reference.
 
>>> join('http://example/x/y/z', '../abc')
'http://example/x/abc'
 
Raise ValueError if there uses relative path
syntax but here has no hierarchical path.
 
>>> join('mid:foo@example', '../foo')
Traceback (most recent call last):
    raise ValueError, here
ValueError: Base <mid:foo@example> has no slash after colon - with relative '../foo'.
 
>>> join('http://example/x/y/z', '')
'http://example/x/y/z'
 
>>> join('mid:foo@example', '#foo')
'mid:foo@example#foo'
 
We grok IRIs
 
>>> len(u'Andr\xe9')
5
 
>>> join('http://example.org/', u'#Andr\xe9')
u'http://example.org/#Andr\xe9'
refTo(base, uri)
figure out a relative URI reference from base to uri
 
>>> refTo('http://example/x/y/z', 'http://example/x/abc')
'../abc'
 
>>> refTo('file:/ex/x/y', 'file:/ex/x/q/r#s')
'q/r#s'
 
>>> refTo(None, 'http://ex/x/y')
'http://ex/x/y'
 
>>> refTo('http://ex/x/y', 'http://ex/x/y')
''
 
Note the relationship between refTo and join:
join(x, refTo(x, y)) == y
which points out certain strings which cannot be URIs. e.g.
>>> x='http://ex/x/y';y='http://ex/x/q:r';join(xrefTo(x, y)) == y
0
 
So 'http://ex/x/q:r' is not a URI. Use 'http://ex/x/q%3ar' instead:
>>> x='http://ex/x/y';y='http://ex/x/q%3ar';join(xrefTo(x, y)) == y
1
 
This one checks that it uses a root-realtive one where that is
all they share.  Now uses root-relative where no path is shared.
This is a matter of taste but tends to give more resilience IMHO
-- and shorter paths
 
Note that base may be None, meaning no base.  In some situations, there
just ain't a base. Slife. In these cases, relTo returns the absolute value.
The axiom abs(,rel(b,x))=x still holds.
This saves people having to set the base to "bogus:".
 
>>> refTo('http://ex/x/y/z', 'http://ex/r')
'/r'
splitFrag(uriref)
split a URI reference between the fragment and the rest.
 
Punctuation is thrown away.
 
e.g.
 
>>> splitFrag("abc#def")
('abc', 'def')
 
>>> splitFrag("abcdef")
('abcdef', None)
splitFragP(uriref, punct=0)
split a URI reference before the fragment
 
Punctuation is kept.
 
e.g.
 
>>> splitFragP("abc#def")
('abc', '#def')
 
>>> splitFragP("abcdef")
('abcdef', '')

 
Data
        URI_unreserved = 'ABCDEFGHIJJLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'
__version__ = '$Id: uripath.py,v 1.21 2007/06/26 02:36:16 syosi Exp $'
commonHost = <_sre.SRE_Pattern object>