Changeset 3056

Show
Ignore:
Timestamp:
02/16/08 17:06:45 (10 months ago)
Author:
vivainio
Message:

ipy_leo: rename wb..val => wb..v, %leo now opens a file in leo

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ipython/trunk/IPython/Extensions/ipy_leo.py

    r3050 r3056  
    5454 
    5555class TrivialLeoWorkbook: 
    56     """ class to find cells """ 
     56    """ class to find cells with simple syntax 
     57     
     58    """ 
    5759    def __getattr__(self, key): 
    5860        cells = all_cells() 
     
    8082    def get_h(self): return self.p.headString() 
    8183    def set_h(self,val): 
    82         print "set head",val  
    83         c.setHeadString(self.p,val) 
     84        print "set head",val 
     85        c.beginUpdate()  
     86        try: 
     87            c.setHeadString(self.p,val) 
     88        finally: 
     89            c.endUpdate() 
    8490         
    8591    h = property( get_h, set_h)   
     
    8793    def get_b(self): return self.p.bodyString() 
    8894    def set_b(self,val): 
    89         print "set body",val  
    90         c.setBodyString(self.p, val) 
     95        print "set body",val 
     96        c.beginUpdate() 
     97        try:  
     98            c.setBodyString(self.p, val) 
     99        finally: 
     100            c.endUpdate() 
    91101     
    92102    b = property(get_b, set_b) 
     
    95105        self.b = pprint.pformat(val) 
    96106         
    97     val = property(lambda self: ip.ev(self.b.strip()), set_val) 
     107    v = property(lambda self: ip.ev(self.b.strip()), set_val) 
    98108     
    99109    def set_l(self,val): 
     
    131141 
    132142def add_var(varname, value = _dummyval): 
    133     nodename = '@ipy-var ' + varname 
    134     p2 = g.findNodeAnywhere(c,nodename) 
    135     if not c.positionExists(p2): 
    136         p2 = c.currentPosition().insertAfter() 
    137         c.setHeadString(p2,'@ipy ' + varname) 
    138          
    139     c.setCurrentPosition(p2) 
    140     if value is _dummyval: 
    141         val = ip.user_ns[varname] 
    142     else: 
    143         val = value 
    144     if val is not None: 
    145         formatted = format_for_leo(val) 
    146         c.setBodyString(p2,formatted) 
    147     return p2 
     143    c.beginUpdate() 
     144    try: 
     145         
     146        nodename = '@ipy-var ' + varname 
     147        p2 = g.findNodeAnywhere(c,nodename) 
     148        if not c.positionExists(p2): 
     149            p2 = c.currentPosition().insertAfter() 
     150            c.setHeadString(p2,'@ipy ' + varname) 
     151             
     152        c.setCurrentPosition(p2) 
     153        if value is _dummyval: 
     154            val = ip.user_ns[varname] 
     155        else: 
     156            val = value 
     157        if val is not None: 
     158            formatted = format_for_leo(val) 
     159            c.setBodyString(p2,formatted) 
     160        return p2 
     161    finally: 
     162        c.endUpdate() 
    148163 
    149164def add_file(self,fname): 
     
    151166 
    152167def push_script(p): 
    153     ohist = ip.IP.output_hist  
    154     hstart = len(ip.IP.input_hist) 
    155     script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=False,useSentinels=False) 
    156      
    157     script = g.splitLines(script + '\n') 
    158     script = ''.join(z for z in script if z.strip()) 
    159      
    160     ip.runlines(script) 
    161      
    162     has_output = False 
    163     for idx in range(hstart,len(ip.IP.input_hist)): 
    164         val = ohist.get(idx,None) 
    165         if val is None: 
    166             continue 
    167         has_output = True 
    168         inp = ip.IP.input_hist[idx] 
    169         if inp.strip(): 
    170             g.es('In: %s' % (inp[:40], ),  tabName = 'IPython') 
    171              
    172         g.es('<%d> %s' % (idx, pprint.pformat(ohist[idx],width = 40)), tabName = 'IPython') 
    173      
    174     if not has_output: 
    175         g.es('ipy run: %s' %( p.headString(),), tabName = 'IPython') 
     168    c.beginUpdate() 
     169    try: 
     170        ohist = ip.IP.output_hist  
     171        hstart = len(ip.IP.input_hist) 
     172        script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=False,useSentinels=False) 
     173         
     174        script = g.splitLines(script + '\n') 
     175        script = ''.join(z for z in script if z.strip()) 
     176         
     177        ip.runlines(script) 
     178         
     179        has_output = False 
     180        for idx in range(hstart,len(ip.IP.input_hist)): 
     181            val = ohist.get(idx,None) 
     182            if val is None: 
     183                continue 
     184            has_output = True 
     185            inp = ip.IP.input_hist[idx] 
     186            if inp.strip(): 
     187                g.es('In: %s' % (inp[:40], ),  tabName = 'IPython') 
     188                 
     189            g.es('<%d> %s' % (idx, pprint.pformat(ohist[idx],width = 40)), tabName = 'IPython') 
     190         
     191        if not has_output: 
     192            g.es('ipy run: %s' %( p.headString(),), tabName = 'IPython') 
     193    finally: 
     194        c.endUpdate() 
    176195     
    177196     
     
    190209    g.es('ipy var: %s' % (varname,), tabName = "IPython") 
    191210     
    192 def push_from_leo(p): 
    193     # headstring without @ are just scripts 
    194     if not p.headString().startswith('@'): 
    195         push_script(p) 
    196         return 
     211def push_from_leo(p):     
    197212    tup = p.headString().split(None,1) 
    198213    # @ipy foo is variable foo 
     
    201216        push_variable(p,varname) 
    202217        return 
     218 
     219    push_script(p) 
     220    return 
     221     
    203222     
    204223ip.user_ns['leox'].push = push_from_leo     
    205224     
    206225def leo_f(self,s): 
    207     ip = self.getapi() 
    208     s = s.strip() 
    209     if s in ip.user_ns: 
    210         add_var(s) 
    211     elif os.path.isfile(s): 
    212         # todo open file 
    213         pass 
     226    """ open file(s) in Leo 
     227     
     228    Takes an mglob pattern, e.g. '%leo *.cpp' or %leo 'rec:*.cpp'   
     229    """ 
     230    import os 
     231    from IPython.external import mglob 
     232     
     233    files = mglob.expand(s) 
     234    c.beginUpdate() 
     235    try: 
     236        for fname in files: 
     237            p = g.findNodeAnywhere(c,'@auto ' + fname) 
     238            if not p: 
     239                p = c.currentPosition().insertAfter() 
     240             
     241            p.setHeadString('@auto ' + fname) 
     242            if os.path.isfile(fname): 
     243                c.setBodyString(p,open(fname).read()) 
     244            c.selectPosition(p) 
     245    finally: 
     246        c.endUpdate() 
    214247 
    215248ip.expose_magic('leo',leo_f)