leoPymacs.py :  » Development » Leo » Leo-4.7.1-final » leo » core » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Leo 
Leo » Leo 4.7.1 final » leo » core » leoPymacs.py
# -*- coding: utf-8 -*-
#@+leo-ver=4-thin
#@+node:ekr.20061024060248.1:@thin leoPymacs.py
#@@first

#@<< docstring>>
#@+node:ekr.20061024060248.2:<< docstring >>
'''A module to allow the Pymacs bridge to access Leo data.

All code in this module must be called *from Emacs import 
calling Pymacs.lisp in other situations will hang Leo.

Notes:

- The init method adds the parent directory of leoPymacs.py to
  Python's sys.path. This is essential to make imports work from  import 
  inside Emacs.

- As of Leo 4.5, the following code, when executed from anEmacsbuffer import 
  will open trunk/leo/test.leo::

      (pymacs-load "c:\\leo.repo\\trunk\\leo\\core\\leoPymacs" "leo-")
      (setq c (leo-open "c:\\leo.repo\\trunk\\leo\\test\\test.leo"))

  Note that full path names are required in each case.

'''
#@-node:ekr.20061024060248.2:<< docstring >>
#@nl

#@@language python
#@@tabwidth -4

# As in leo.py we must be very careful about imports.
g = None # set by init: do *not* import it here!
inited = False
pymacsFile = __file__
# print('leoPymacs:pymacsFile',pymacsFile)

#@+others
#@+node:ekr.20061024131236:dump
def dump (anObject):

    global g

    init()

    return str(g.toEncodedString(repr(anObject),encoding='ascii'))
#@-node:ekr.20061024131236:dump
#@+node:ekr.20061024130957:getters
def get_app ():
    '''Scripts can use g.app.scriptDict for communication with pymacs.'''
    global g
    init()
    return g.app

def get_g():
    global g
    init()
    return g

def script_result():
    global g
    init()
    return g.app.scriptResult
#@nonl
#@-node:ekr.20061024130957:getters
#@+node:ekr.20061024060248.3:hello
def hello():

    init()
    return 'Hello from Leo.  g.app: %s' % g.app
#@nonl
#@-node:ekr.20061024060248.3:hello
#@+node:ekr.20061024075542:init
def init ():

    global inited

    if inited:
        return
    else:
        inited = True # Only try once, no matter what happens.

    # Add the parent path of this file to sys.path
    import os
    import sys

    theDir = os.path.abspath(os.path.join(os.path.dirname(pymacsFile),'..','..'))
    if theDir not in sys.path:
        print ('leoPymacs:adding',theDir,'to sys.path')
        sys.path.append(theDir)

    # Create the dummy app
    try:
        import leo.core.runLeo as leo
    except ImportError:
        print('leoPymacs.init: can not import runLeo')
        print('leoPymacs.init: sys.path:')
        for z in sys.path: print (z)

    leo.run(pymacs=True)

    try:
        import leo.core.leoGlobals as leoGlobals
    except ImportError:
        print('leoPymacs.init: can not import leoGlobals')

    global g ; g = leoGlobals
    # print('leoPymacs:init:g',g)

    if 1: # These traces show up in the pymacs buffer.
        g.trace('app',g.app)
        g.trace('gui',g.app.gui)
#@-node:ekr.20061024075542:init
#@+node:ekr.20061024075542.1:open
def open (fileName=None):

    global g

    init()

    if g.app.unitTesting:
        return

    if not fileName:
        g.es_print('','leoPymacs.open:','no file name')
        return None

    # openWithFileName checks to see if the file is already open.
    ok, frame = g.openWithFileName(
        fileName,
        old_c=None,
        enableLog=False,
        readAtFileNodesFlag=True)

    c = ok and frame.c or None
    if c:
        g.es_print('','leoPymacs.open:',c)
    else:
        g.es_print('','leoPymacs.open:','can not open',fileName)

    return c
#@-node:ekr.20061024075542.1:open
#@+node:ekr.20061024084200:run-script (pymacs)
def run_script(c,script,p=None):

    # It is possible to use script=None, in which case p must be defined.

    global g

    init()

    if c is None:
        c,frame = g.app.newLeoCommanderAndFrame(fileName='dummy script file')

    g.app.scriptResult = None

    c.executeScript(
        event=None,
        p=p,
        script=script,
        useSelectedText=False,
        define_g=True,
        define_name='__main__',
        silent=True,  # Don't write to the log.
    )

    # g.trace('script returns: ',repr(g.app.scriptResult))
    return g.app.scriptResult
#@-node:ekr.20061024084200:run-script (pymacs)
#@-others
#@nonl
#@-node:ekr.20061024060248.1:@thin leoPymacs.py
#@-leo
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.