~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2011-07-23 16:33:38 UTC
  • mto: This revision was merged to the branch mainline in revision 6045.
  • Revision ID: jelmer@samba.org-20110723163338-mjox55g52kt3u922
Add get_transport_from_url and get_transport_from_path functions.

Show diffs side-by-side

added added

removed removed

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