Ticket #378: trac_378-load_attach_path.2.1.patch

File trac_378-load_attach_path.2.1.patch, 9.0 KB (added by flawrence, 11 years ago)

use os.path.userexpand on load, separate reset function, also use paths for detach. replaces previous.

  • sage/misc/all.py

    # HG changeset patch
    # User Felix Lawrence <felix@physics.usyd.edu.au>
    # Date 1288850675 -39600
    # Node ID 1a53e74525f582bb866594b5075b9b20d824dcf3
    # Parent  514ad55f2b43835bb9b6e401a40e8f9a22c9e52c
    Trac 378: Configurable load + attach path
    
    diff -r 514ad55f2b43 -r 1a53e74525f5 sage/misc/all.py
    a b  
    5858
    5959from defaults import set_default_variable_name
    6060
    61 from preparser import preparse, implicit_multiplication, BackslashOperator, attached_files, detach, load_attach_path
     61from preparser import preparse, implicit_multiplication, BackslashOperator, attached_files, detach, load_attach_path, reset_load_attach_path
    6262
    6363from interpreter import preparser
    6464
  • sage/misc/preparser.py

    diff -r 514ad55f2b43 -r 1a53e74525f5 sage/misc/preparser.py
    a b  
    13911391    return False
    13921392
    13931393
     1394def reset_load_attach_path():
     1395    """
     1396    Resets the current search path for loading and attaching files.
     1397   
     1398    The default path is '.' plus any paths specified in the environment
     1399    variable `SAGE_LOAD_ATTACH_PATH`.
     1400   
     1401    EXAMPLES::
     1402   
     1403        sage: load_attach_path()
     1404        ['.']
     1405        sage: t_dir = tmp_dir()
     1406        sage: load_attach_path(t_dir)
     1407        sage: t_dir in load_attach_path()
     1408        True
     1409        sage: reset_load_attach_path(); load_attach_path()
     1410        ['.']
     1411   
     1412    At startup, Sage adds colon-separated paths in the environment
     1413    variable ``SAGE_LOAD_ATTACH_PATH``::
     1414
     1415        sage: reset_load_attach_path(); load_attach_path()
     1416        ['.']
     1417        sage: os.environ['SAGE_LOAD_ATTACH_PATH'] = '/veni/vidi:vici:'
     1418        sage: reload(sage.misc.preparser)    # Simulate startup
     1419        <module 'sage.misc.preparser' from '...'>
     1420        sage: load_attach_path()
     1421        ['.', '/veni/vidi', 'vici']
     1422        sage: del os.environ['SAGE_LOAD_ATTACH_PATH']
     1423        sage: reload(sage.misc.preparser)    # Simulate startup
     1424        <module 'sage.misc.preparser' from '...'>
     1425        sage: reset_load_attach_path(); load_attach_path()
     1426        ['.']
     1427    """
     1428    global _load_attach_path
     1429    _load_attach_path = ['.']
     1430    if 'SAGE_LOAD_ATTACH_PATH' in os.environ:
     1431        epaths = os.environ['SAGE_LOAD_ATTACH_PATH'].split(':')
     1432        _load_attach_path.extend(epaths)
     1433        while '' in _load_attach_path:
     1434            _load_attach_path.remove('')
     1435
    13941436# Set up the initial search path for loading and attaching files.  A
    13951437# user can modify the path with the function load_attach_path.
    1396 _load_attach_path = ['.']
    1397 if 'SAGE_LOAD_ATTACH_PATH' in os.environ:
    1398     _load_attach_path.extend(os.environ['SAGE_LOAD_ATTACH_PATH'].split(':'))
    1399     if '' in _load_attach_path:
    1400         _load_attach_path.remove('')
    1401 
     1438reset_load_attach_path()
    14021439
    14031440def load(filename, globals, attach=False):
    14041441    """
     
    14891526    current working directory, i.e., ``'.'``.  But you can modify the
    14901527    path with :func:`load_attach_path`::
    14911528
    1492         sage: sage.misc.reset.reset_attached(); load_attach_path(reset=True)
     1529        sage: sage.misc.reset.reset_attached(); reset_load_attach_path()
    14931530        sage: load_attach_path()
    14941531        ['.']
    14951532        sage: t_dir = tmp_dir()
     
    14981535        sage: load_attach_path(t_dir)
    14991536        sage: attach('test.py')
    15001537        111
    1501         sage: sage.misc.reset.reset_attached(); load_attach_path(reset=True) # clean up
     1538        sage: sage.misc.reset.reset_attached(); reset_load_attach_path() # clean up
    15021539
    15031540    or by setting the environment variable ``SAGE_LOAD_ATTACH_PATH``
    15041541    to a colon-separated list before starting Sage::
     
    15341571    if not is_loadable_filename(filename):
    15351572        raise ValueError('argument (=%r) to load or attach must have extension py, pyx, sage, spyx, or m' % filename)
    15361573
    1537     fpath = filename
    1538     if not os.path.isabs(fpath):
     1574    fpath = os.path.expanduser(filename)
     1575    if os.path.isabs(fpath):
     1576        if not os.path.exists(fpath):
     1577            raise IOError('did not find file %r to load or attach' % filename)
     1578    else:
    15391579        global _load_attach_path
    15401580        for path in _load_attach_path:
    15411581            fpath = os.path.join(path, filename)
     1582            fpath = os.path.expanduser(fpath)
    15421583            if os.path.exists(fpath):
    15431584                break
    1544 
    1545     if not os.path.exists(fpath):
    1546         if os.path.isabs(filename):
    1547             raise IOError('did not find file %r to load or attach' % filename)
    15481585        else:
    1549             raise IOError('did not find file %r in load / attach search path' % filename)
     1586            raise IOError('did not find file %r in load / attach search path' \
     1587                % filename)
    15501588
    15511589    if fpath.endswith('.py'):
    15521590        execfile(fpath, globals)
     
    15691607        attached[fpath] = os.path.getmtime(fpath)
    15701608
    15711609
    1572 def load_attach_path(path=None, replace=False, reset=False):
     1610def load_attach_path(path=None, replace=False):
    15731611    """
    15741612    Get or modify the current search path for loading and attaching
    15751613    files.
     
    15831621      whether to *replace* the search path instead of *appending* to
    15841622      it
    15851623
    1586     - ``reset`` - boolean (default: False); whether to reset the
    1587       search path to ``'.'``.  This overrides the other arguments.
    1588 
    15891624    OUTPUT:
    15901625
    15911626    - None or a *reference* to the current list of search paths
     
    15941629
    15951630    First, we extend the example given in :func:`load`'s docstring::
    15961631
    1597         sage: sage.misc.reset.reset_attached(); load_attach_path(reset=True)
     1632        sage: sage.misc.reset.reset_attached(); reset_load_attach_path()
    15981633        sage: load_attach_path()
    15991634        ['.']
    16001635        sage: t_dir = tmp_dir()
     
    16091644        111
    16101645        sage: attached_files() == [fullpath]
    16111646        True       
    1612         sage: sage.misc.reset.reset_attached(); load_attach_path(reset=True)
     1647        sage: sage.misc.reset.reset_attached(); reset_load_attach_path()
    16131648        sage: load_attach_path() == ['.']
    16141649        True
    16151650        sage: load('test.py')
     
    16191654
    16201655    The function returns a reference to the path list::
    16211656
    1622         sage: load_attach_path(reset=True); load_attach_path()
     1657        sage: reset_load_attach_path(); load_attach_path()
    16231658        ['.']
    16241659        sage: load_attach_path('/path/to/my/sage/scripts'); load_attach_path()
    16251660        ['.', '/path/to/my/sage/scripts']
    1626         sage: load_attach_path(['good', 'bad', 'ugly'], replace=True); load_attach_path()
     1661        sage: load_attach_path(['good', 'bad', 'ugly'], replace=True)
     1662        sage: load_attach_path()
    16271663        ['good', 'bad', 'ugly']
    1628         sage: p = load_attach_path(); p.pop(); p[0] = 'weird'; load_attach_path()
     1664        sage: p = load_attach_path(); p.pop()
    16291665        'ugly'
     1666        sage: p[0] = 'weird'; load_attach_path()
    16301667        ['weird', 'bad']
    1631         sage: load_attach_path(reset=True); load_attach_path()
     1668        sage: reset_load_attach_path(); load_attach_path()
    16321669        ['.']
    1633 
    1634     At startup, Sage adds colon-separated paths in the environment
    1635     variable ``SAGE_LOAD_ATTACH_PATH``::
    1636 
    1637         sage: load_attach_path(reset=True); load_attach_path()
    1638         ['.']
    1639         sage: os.environ['SAGE_LOAD_ATTACH_PATH'] = '/veni/vidi:vici:'
    1640         sage: reload(sage.misc.preparser)    # Simulate startup
    1641         <module 'sage.misc.preparser' from '...'>
    1642         sage: load_attach_path()
    1643         ['.', '/veni/vidi', 'vici']
    1644         sage: del os.environ['SAGE_LOAD_ATTACH_PATH']
    1645         sage: load_attach_path(reset=True)
    16461670    """
    16471671    global _load_attach_path
    16481672
    1649     if reset is True:
    1650         _load_attach_path = ['.']
    1651 
    1652     elif path is None:
     1673    if path is None:
    16531674        return _load_attach_path
    1654 
    16551675    else:
    16561676        if isinstance(path, basestring):
    16571677            path = [path]
     
    17721792        sage: detach(t)
    17731793        sage: attached_files()
    17741794        []
     1795   
     1796    ::
     1797   
     1798        sage: sage.misc.reset.reset_attached(); reset_load_attach_path()
     1799        sage: load_attach_path()
     1800        ['.']
     1801        sage: t_dir = tmp_dir()
     1802        sage: fullpath = os.path.join(t_dir, 'test.py')
     1803        sage: open(fullpath, 'w').write("print 37 * 3")
     1804        sage: load_attach_path(t_dir)
     1805        sage: attach('test.py')
     1806        111
     1807        sage: attached_files() == [os.path.normpath(fullpath)]
     1808        True
     1809        sage: detach('test.py')
     1810        sage: attached_files()
     1811        []
     1812        sage: attach('test.py')
     1813        111
     1814        sage: detach(fullpath)
     1815        sage: attached_files()
     1816        []
     1817        sage: sage.misc.reset.reset_attached(); reset_load_attach_path() # clean up
     1818       
    17751819    """
    1776     attached.pop(filename, None)
     1820    fpath = os.path.expanduser(filename)
     1821    if not os.path.isabs(fpath):
     1822        for path in load_attach_path():
     1823            epath = os.path.expanduser(path)
     1824            fpath = os.path.join(epath, filename)
     1825            if fpath in attached:
     1826                break
     1827   
     1828    if fpath in attached:
     1829        attached.pop(fpath)
     1830    else:
     1831        raise ValueError("File '%r' seems not to be attached. To see a list \
     1832            of attached files, call attached_files()" % filename)