def SourceArgToUri(arg, resolver=Uri.BASIC_RESOLVER):
    """
    Some command-line scripts take an argument that is supposed to be
    either "-" (denoting standard input) or a URI reference that can be
    resolved against the URI equivalent of the current working
    directory. This function processes such an argument, given as a
    string, and returns an appropriate URI.

    Since users tend to expect local OS paths to work as URIs, this
    function will accept and use an OS path argument if does appear to
    point to an existing local file, even though this could interfere
    with catalog-based resolution.

    Raises a ValueError if arg is neither a local file nor a valid URI
    reference nor "-".

    The resolver object must support a normalize() method that
    can resolve a URI reference against a base URI, returning a URI.
    """
    if not isinstance(resolver, Uri.UriResolverBase):
        msg = 'It appears there is a bug in this command-line' \
              ' script. A %s was passed as URI resolver to a function that' \
              ' requires an instance of Ft.Lib.Uri.UriResolverBase (or' \
              ' a subclass thereof).'
        raise TypeError(msg % type(resolver))
    if not isinstance(arg, str) and not isinstance(arg, unicode):
        msg = 'It appears there is a bug in this command-line' \
              ' script. A %s was passed as an argument needing to be' \
              ' converted to a URI. A string must be provided instead.'
        raise TypeError(msg % type(arg))

    if arg == '-':
        return Uri.OsPathToUri('unknown-STDIN', attemptAbsolute=True)
    elif arg:
        if os.path.isfile(arg):
            return Uri.OsPathToUri(arg, attemptAbsolute=True)
        elif not Uri.MatchesUriRefSyntax(arg):
            raise ValueError("'%s' is not a valid URI reference." % arg)
        elif Uri.IsAbsolute(arg):
            return arg

    base = Uri.OsPathToUri(os.getcwd(), attemptAbsolute=True)
    if base[-1] != '/':
        base += '/'
    return resolver.normalize(arg, base)
Exemple #2
0
def Test(tester):
    tester.startGroup('UriDict')
    tester.startGroup('file:/// and file://localhost/ equivalence')
    tester.startTest('equivalent key in UriDict')
    uris = Uri.UriDict()
    uris['file:///path/to/resource'] = 0
    tester.compare(True, 'file://localhost/path/to/resource' in uris,
                   'RFC 1738 localhost support failed')
    tester.testDone()
    tester.startTest('value of 2 equivalent keys')
    uris = Uri.UriDict()
    uris['file:///path/to/resource'] = 1
    uris['file://localhost/path/to/resource'] = 2
    tester.compare(2, uris['file:///path/to/resource'],
                   'RFC 1738 localhost support failed')
    tester.testDone()
    tester.groupDone()
    tester.startGroup('case equivalence')
    for uri, expected, junk in caseNormalizationTests:
        tester.startTest('%s and %s equivalence' % (uri, expected))
        uris[uri] = 1
        uris[expected] = 2
        tester.compare(2, uris[uri])
        tester.testDone()
    tester.groupDone()
    tester.startGroup('percent-encoding equivalence')
    for uri, expected in pctEncNormalizationTests:
        tester.startTest('%s and %s equivalence' % (uri, expected))
        uris[uri] = 1
        uris[expected] = 2
        tester.compare(2, uris[uri])
        tester.testDone()
    tester.groupDone()
    tester.groupDone()

    tester.startGroup("PercentEncode and PercentDecode")
    for unencoded, encoded in percentEncodeTests:
        if len(unencoded) > 10:
            test_title = unencoded[:11] + '...'
        else:
            test_title = unencoded
        tester.startTest(repr(test_title))
        tester.compare(encoded, Uri.PercentEncode(unencoded))
        tester.compare(unencoded, Uri.PercentDecode(encoded))
        tester.testDone()

    # non-BMP tests:
    #     a couple of random chars from U+10000 to U+10FFFD.
    #
    # This string will be length 2 or 4 depending on how Python
    # was built. Either way, it should result in the same percent-
    # encoded sequence, which should decode back to the original
    # representation.
    unencoded = u'\U00010000\U0010FFFD'
    encoded = u'%F0%90%80%80%F4%8F%BF%BD'
    tester.startTest("u'\U00010000\U0010FFFD'")
    tester.compare(encoded, Uri.PercentEncode(unencoded))
    tester.compare(unencoded, Uri.PercentDecode(encoded))
    tester.testDone()
    #
    # This string will be length 4, regardless of how Python was
    # built. However, if Python was built with wide (UCS-4) chars,
    # PercentDecode will generate an optimal string (length: 2).
    unencoded_in = u'\ud800\udc00\udbff\udffd'
    encoded = u'%F0%90%80%80%F4%8F%BF%BD'
    unencoded_out = u'\U00010000\U0010FFFD'
    tester.startTest("u'\ud800\udc00\udbff\udffd'")
    tester.compare(encoded, Uri.PercentEncode(unencoded_in))
    tester.compare(unencoded_out, Uri.PercentDecode(encoded))
    tester.testDone()

    # test a few iso-8859-n variations just to make sure
    # iso-8859-1 isn't special
    unencoded = ''.join(map(chr, range(256)))
    encoded = '%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F' \
              '%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F' \
              '%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F' \
              '0123456789%3A%3B%3C%3D%3E%3F%40' \
              'ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60' \
              'abcdefghijklmnopqrstuvwxyz%7B%7C%7D~' \
              '%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F' \
              '%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F' \
              '%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF' \
              '%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF' \
              '%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF' \
              '%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF' \
              '%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF' \
              '%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF'
    for part in (1, 2, 3, 15):
        enc_name = 'iso-8859-%d' % part
        tester.startTest(enc_name)
        try:
            codecs.lookup(enc_name)
        except LookupError:
            tester.warning('Not supported on this platform')
            tester.testDone()
            continue
        tester.compare(encoded, Uri.PercentEncode(unencoded,
                                                  encoding=enc_name))
        tester.compare(unencoded, Uri.PercentDecode(encoded,
                                                    encoding=enc_name))
        tester.testDone()

    # utf-16be: why not?
    unencoded = u'a test string...\x00\xe9...\x20\x22...\xd8\x00\xdc\x00'
    encoded = u'a%20test%20string...\u00e9...%20%22...%D8%00%DC%00'

    tester.groupDone()

    tester.startGroup("PublicIdToUrn")
    for publicid, urn in publicIdTests:
        tester.startTest(publicid)
        tester.compare(urn, Uri.PublicIdToUrn(publicid))
        tester.testDone()
    tester.groupDone()

    tester.startGroup("UrnToPublicId")
    for publicid, urn in publicIdTests:
        tester.startTest(urn)
        tester.compare(publicid, Uri.UrnToPublicId(urn))
        tester.testDone()
    tester.groupDone()

    tester.startGroup("URI reference syntax")
    for testuri in good_URI_references:
        tester.startTest("Good URI ref: %s" % repr(testuri))
        tester.compare(1, Uri.MatchesUriRefSyntax(testuri),
                       "Mistakenly tests as invalid")
        tester.testDone()
    for testuri in bad_URI_references:
        tester.startTest("Bad URI ref: %s" % repr(testuri))
        tester.compare(0, Uri.MatchesUriRefSyntax(testuri),
                       "Mistakenly tests as valid")
        tester.testDone()
    tester.groupDone()

    tester.startGroup('Absolutize')
    for uriRef, baseUri, expectedUri in absolutize_test_cases:
        tester.startTest('base=%r ref=%r' % (baseUri, uriRef))
        res = Uri.Absolutize(uriRef, baseUri)
        # in a couple cases, there's more than one correct result
        if isinstance(expectedUri, tuple):
            tester.compare(1, res in expectedUri, 'Invalid result')
        else:
            tester.compare(expectedUri, res, 'Invalid result')
        tester.testDone()
    tester.groupDone()

    tester.startGroup('Relativize')
    for targetUri, againstUri, relativeUri, subPathUri in relativize_test_cases:
        tester.startTest('target=%r against=%r (subPathOnly=False)' %
                         (targetUri, againstUri))
        res = Uri.Relativize(targetUri, againstUri)
        tester.compare(relativeUri, res, 'Invalid result')
        tester.testDone()
        if res is not None:
            tester.startTest(
                'target=%r against=%r (subPathOnly=False, Absolutize)' %
                (targetUri, againstUri))
            res = Uri.Absolutize(res, againstUri)
            tester.compare(res, targetUri, 'Invalid result')
            tester.testDone()
        tester.startTest('target=%r against=%r (subPathOnly=True)' %
                         (targetUri, againstUri))
        res = Uri.Relativize(targetUri, againstUri, True)
        tester.compare(subPathUri, res, 'Invalid result')
        tester.testDone()
        if res is not None:
            tester.startTest(
                'target=%r against=%r (subPathOnly=True, Absolutize)' %
                (targetUri, againstUri))
            res = Uri.Absolutize(res, againstUri)
            tester.compare(res, targetUri, 'Invalid result')
            tester.testDone()
    tester.groupDone()

    tester.startGroup('BaseJoin')
    for base, relative, expectedUri in basejoin_test_cases:
        tester.startTest('base=%r rel=%r' % (base, relative))
        res = Uri.BaseJoin(base, relative)
        tester.compare(expectedUri, res, 'Invalid result')
        tester.testDone()
    tester.groupDone()

    tester.startGroup('UriToOsPath')
    for osname in ('posix', 'nt'):
        tester.startGroup(osname)
        for subgroupname in ('absolute', 'relative'):
            tester.startGroup(subgroupname)
            for uri, nt_path, posix_path in fileUris:
                if subgroupname == 'relative':
                    if uri[:5] == 'file:':
                        uri = uri[5:]
                    else:
                        break
                if isinstance(uri, unicode):
                    testname = repr(uri)
                else:
                    testname = uri
                tester.startTest(testname)
                if osname == 'nt':
                    path = nt_path
                elif osname == 'posix':
                    path = posix_path
                else:
                    break
                if path is None:
                    tester.testException(Uri.UriToOsPath, (uri, ),
                                         Uri.UriException,
                                         kwargs={
                                             'attemptAbsolute': False,
                                             'osname': osname
                                         })
                else:
                    tester.compare(
                        path,
                        Uri.UriToOsPath(uri,
                                        attemptAbsolute=False,
                                        osname=osname))
                tester.testDone()
            tester.groupDone()
        tester.groupDone()
    tester.groupDone()

    tester.startGroup('OsPathToUri')
    for osname in ('posix', 'nt'):
        tester.startGroup(osname)
        for path, nt_uri, posix_uri in filePaths:
            if isinstance(path, unicode):
                testname = repr(path)
            else:
                testname = path
            tester.startTest(testname)
            if osname == 'nt':
                uri = nt_uri
            elif osname == 'posix':
                uri = posix_uri
            else:
                break
            if uri is None:
                tester.testException(Uri.OsPathToUri, (path, ),
                                     Uri.UriException,
                                     kwargs={
                                         'attemptAbsolute': False,
                                         'osname': osname
                                     })
            else:
                tester.compare(
                    uri,
                    Uri.OsPathToUri(path, attemptAbsolute=False,
                                    osname=osname))
            tester.testDone()
        tester.groupDone()
    tester.groupDone()

    tester.startGroup('NormalizeCase')
    for uri, expected0, expected1 in caseNormalizationTests:
        testname = uri
        uri = Uri.SplitUriRef(uri)
        tester.startTest(testname)
        tester.compare(expected0, Uri.UnsplitUriRef(Uri.NormalizeCase(uri)))
        tester.testDone()
        tester.startTest(testname + ' (host too)')
        tester.compare(expected1,
                       Uri.UnsplitUriRef(Uri.NormalizeCase(uri, doHost=1)))
        tester.testDone()
    tester.groupDone()

    tester.startGroup('NormalizePercentEncoding')
    for uri, expected in pctEncNormalizationTests:
        testname = uri
        tester.startTest(testname)
        tester.compare(expected, Uri.NormalizePercentEncoding(uri))
        tester.testDone()
    tester.groupDone()

    tester.startGroup('NormalizePathSegments')
    for path, expected in pathSegmentNormalizationTests:
        testname = path
        tester.startTest(testname)
        tester.compare(expected, Uri.NormalizePathSegments(path))
        tester.testDone()
    tester.groupDone()

    tester.startGroup('NormalizePathSegmentsInUri')
    for path, expectedpath in pathSegmentNormalizationTests:
        # for non-hierarchical scheme, no change expected in every case
        uri = 'urn:bogus:%s?a=1&b=2#frag' % path
        expected = 'urn:bogus:%s?a=1&b=2#frag' % path
        testname = uri
        tester.startTest(testname)
        tester.compare(expected, Uri.NormalizePathSegmentsInUri(uri))
        tester.testDone()
    for path, expectedpath in pathSegmentNormalizationTests:
        if path[:1] == '/':
            # hierarchical scheme
            uri = 'file://*****:*****@host%s?a=1&b=2#frag' % path
            expected = 'file://*****:*****@host%s?a=1&b=2#frag' % expectedpath
            testname = uri
            tester.startTest(testname)
            tester.compare(expected, Uri.NormalizePathSegmentsInUri(uri))
            tester.testDone()
    tester.groupDone()

    tester.startGroup("MakeUrllibSafe")
    tests = makeUrllibSafeTests
    if os.name == 'nt':
        tests += winMakeUrllibSafeTests
    for uri, expected in makeUrllibSafeTests:
        if isinstance(uri, unicode):
            test_title = repr(uri)
        else:
            test_title = uri
        tester.startTest(test_title)
        res = Uri.MakeUrllibSafe(uri)
        tester.compare(expected, res)
        tester.testDone()
    tester.groupDone()

    tester.startGroup("Basic Uri Resolver")
    data = [
        ('http://foo.com/root/', 'path', 'http://foo.com/root/path'),
        ('http://foo.com/root', 'path', 'http://foo.com/path'),
    ]
    for base, uri, exp in data:
        tester.startTest("normalize: %s %s" % (base, uri))
        res = Uri.BASIC_RESOLVER.normalize(uri, base)
        tester.compare(exp, res)
        tester.testDone()

    base = 'foo:foo.com'
    uri = 'path'
    tester.startTest("normalize: %s %s" % (base, uri))
    tester.testException(Uri.BASIC_RESOLVER.normalize, (uri, base),
                         Uri.UriException)
    tester.testDone()

    tester.startTest('resolve')
    base = os.getcwd()
    if base[-1] != os.sep:
        base += os.sep
    stream = Uri.BASIC_RESOLVER.resolve('test.py', Uri.OsPathToUri(base))
    tester.compare(TEST_DOT_PY_BANGPATH, string.rstrip(stream.readline()))
    stream.close()
    tester.testDone()

    tester.startTest('generate')
    uuid = Uri.BASIC_RESOLVER.generate()
    tester.compare('urn:uuid:', uuid[:9])

    tester.testDone()

    tester.groupDone()

    tester.startGroup("SchemeRegistryResolver")

    def evalSchemeHandler(uri, base=None):
        if base: uri = base + uri
        uri = uri[5:]
        return str(eval(uri))

    def shiftSchemeHandler(uri, base=None):
        if base: uri = base + uri
        uri = uri[6:]
        return ''.join([chr(ord(c) + 1) for c in uri])

    resolver = SchemeRegistryResolver({
        'eval': evalSchemeHandler,
        'shift': shiftSchemeHandler,
    })

    scheme_cases = [
        (None, 'eval:150-50', '100'),
        (None, 'shift:abcde', 'bcdef'),
        ('eval:150-', '50', '100'),
        ('shift:ab', 'cde', 'bcdef'),
    ]

    for base, relative, expected in scheme_cases:
        tester.startTest("URI: base=%s uri=%s" % (base, relative))

        res = resolver.resolve(relative, base)
        tester.compare(expected, res)

        tester.testDone()

    resolver.handlers[None] = shiftSchemeHandler
    del resolver.handlers['shift']

    for base, relative, expected in scheme_cases:
        tester.startTest("URI: base=%s uri=%s" % (base, relative))

        res = resolver.resolve(relative, base)
        tester.compare(expected, res)

        tester.testDone()

    tester.groupDone()

    return
def checkValidity(candidate):
    print "* validUri(\"" + candidate + "\") == " \
            + str(Uri.MatchesUriRefSyntax(candidate)) + "<br>"