~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-07-25 14:01:28 UTC
  • mfrom: (6039.1.9 transport-from-url)
  • Revision ID: pqm@pqm.ubuntu.com-20110725140128-croovh96z0rs57yy
(jelmer) Add get_transport_from_url and get_transport_from_path functions.
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1586
1586
    return location
1587
1587
 
1588
1588
 
1589
 
def get_transport(base, possible_transports=None):
1590
 
    """Open a transport to access a URL or directory.
1591
 
 
1592
 
    :param base: either a URL or a directory name.
1593
 
 
 
1589
def get_transport_from_path(path, possible_transports=None):
 
1590
    """Open a transport for a local path.
 
1591
 
 
1592
    :param path: Local path as byte or unicode string
 
1593
    :return: Transport object for path
 
1594
    """
 
1595
    return get_transport_from_url(urlutils.local_path_to_url(path),
 
1596
        possible_transports)
 
1597
 
 
1598
 
 
1599
def get_transport_from_url(url, possible_transports=None):
 
1600
    """Open a transport to access a URL.
 
1601
    
 
1602
    :param base: a URL
1594
1603
    :param transports: optional reusable transports list. If not None, created
1595
1604
        transports will be added to the list.
1596
1605
 
1597
1606
    :return: A new transport optionally sharing its connection with one of
1598
1607
        possible_transports.
1599
1608
    """
1600
 
    if base is None:
1601
 
        base = '.'
1602
 
    base = location_to_url(base)
1603
 
 
1604
1609
    transport = None
1605
1610
    if possible_transports is not None:
1606
1611
        for t in possible_transports:
1607
 
            t_same_connection = t._reuse_for(base)
 
1612
            t_same_connection = t._reuse_for(url)
1608
1613
            if t_same_connection is not None:
1609
1614
                # Add only new transports
1610
1615
                if t_same_connection not in possible_transports:
1613
1618
 
1614
1619
    last_err = None
1615
1620
    for proto, factory_list in transport_list_registry.items():
1616
 
        if proto is not None and base.startswith(proto):
1617
 
            transport, last_err = _try_transport_factories(base, factory_list)
 
1621
        if proto is not None and url.startswith(proto):
 
1622
            transport, last_err = _try_transport_factories(url, factory_list)
1618
1623
            if transport:
1619
1624
                if possible_transports is not None:
1620
1625
                    if transport in possible_transports:
1621
1626
                        raise AssertionError()
1622
1627
                    possible_transports.append(transport)
1623
1628
                return transport
1624
 
    raise errors.UnsupportedProtocol(base, last_err)
 
1629
    if not urlutils.is_url(url):
 
1630
        raise errors.InvalidURL(path=url)
 
1631
    raise errors.UnsupportedProtocol(url, last_err)
 
1632
 
 
1633
 
 
1634
def get_transport(base, possible_transports=None):
 
1635
    """Open a transport to access a URL or directory.
 
1636
 
 
1637
    :param base: either a URL or a directory name.
 
1638
 
 
1639
    :param transports: optional reusable transports list. If not None, created
 
1640
        transports will be added to the list.
 
1641
 
 
1642
    :return: A new transport optionally sharing its connection with one of
 
1643
        possible_transports.
 
1644
    """
 
1645
    if base is None:
 
1646
        base = '.'
 
1647
    return get_transport_from_url(location_to_url(base), possible_transports)
1625
1648
 
1626
1649
 
1627
1650
def _try_transport_factories(base, factory_list):