textnode.py :  » Development » Leo » Leo-4.7.1-final » leo » plugins » 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 » plugins » textnode.py
#@+leo-ver=4-thin
#@+node:ajones.20070122160142:@thin textnode.py
#@<< docstring >>
#@+node:ajones.20070122160142.1:<< docstring >>
'''The @text node is for embedding text files in a leo node that won't be saved with the leo file, and won't contain any sentinel leo comments.  Children of @text nodes are not saved with the derived file, though they will stay in the outline.  When a outline is first loaded any @text nodes are filled with the contents of the text files on disk.  To refresh the contents of an @text node, double click on the heading icon.
'''
#@nonl
#@-node:ajones.20070122160142.1:<< docstring >>
#@nl

#@@language python
#@@tabwidth -4

import leo.core.leoGlobals as g
import leo.core.leoPlugins as leoPlugins
import os.path
__version__ = "1.1"
    # Terry Brown: support for @path ancestors and uses universal newline mode for opening.

#@+others
#@+node:ajones.20070122160142.2:init
def init():
    leoPlugins.registerHandler(('new','open2'), on_open)
    leoPlugins.registerHandler("save1", on_save)
    leoPlugins.registerHandler("save2", on_open)
    leoPlugins.registerHandler("icondclick1", on_icondclick)

    g.plugin_signon(__name__)

    return 1
#@nonl
#@-node:ajones.20070122160142.2:init
#@+node:ajones.20070122181914:on_icondclick
def on_icondclick(tag, keywords):
    c = keywords['c']
    p = keywords['p']
    h = p.h
    if g.match_word(h,0,"@text"): 
        if p.b != "":
            result = g.app.gui.runAskYesNoDialog(c, "Query", "Read from file "+h[6:]+"?")
            if result == "no":
                return
        readtextnode(c, p)
#@nonl
#@-node:ajones.20070122181914:on_icondclick
#@+node:ajones.20070122160142.3:on_open
def on_open(tag,keywords):
    c = keywords.get("c")
    if not c: return

    for p in c.all_positions():
        h = p.h
        if g.match_word(h,0,"@text"):
            readtextnode(c, p)
    c.redraw()
#@-node:ajones.20070122160142.3:on_open
#@+node:ajones.20070122161942:on_save
def on_save(tag,keywords):
    c = keywords.get("c")
    if not c: return

    for p in c.all_positions():
        h = p.h
        if g.match_word(h,0,"@text") and p.isDirty():
            savetextnode(c, p)
            c.setBodyString(p, "")
#@nonl
#@-node:ajones.20070122161942:on_save
#@+node:tbrown.20080128221824:getPath
def getPath(c,p):
    path = [i.h[6:] for i in p.self_and_parents()
            if i.h[:6] in ('@path ', '@text ')]
    path.append(g.getBaseDirectory(c))
    path.reverse()
    return os.path.join(*path)
#@nonl
#@-node:tbrown.20080128221824:getPath
#@+node:ajones.20070122181914.1:readtextnode
def readtextnode(c, p):
    changed = c.isChanged()

    name = getPath(c,p)

    try:
        file = open(name,"rU")
        g.es("..." + name)
        c.setBodyString(p, file.read())
        p.clearDirty()
        c.setChanged(changed)
        file.close()
    except IOError as msg:
        g.es("error reading %s: %s" % (name, msg))
        g.es("...not found: " + name)
        c.setBodyString(p,"") # Clear the body text.
        p.setDirty()
#@-node:ajones.20070122181914.1:readtextnode
#@+node:ajones.20070122185020:savetextnode
def savetextnode(c, p):
    name = getPath(c,p)
    try:
        file = open(name,"w")
        g.es("writing " + name)
        file.write(p.b)
        file.close()
    except IOError as msg:
        g.es("error writing %s: %s" % (name, msg))
        p.setDirty()
        p.setMarked(1)
#@nonl
#@-node:ajones.20070122185020:savetextnode
#@-others
#@nonl
#@-node:ajones.20070122160142:@thin textnode.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.