mod_tempfname.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 » mod_tempfname.py
#@+leo-ver=4-thin
#@+node:EKR.20040517075715.1:@thin mod_tempfname.py
"""Replace Commands.openWithTempFilePath to create alternate temporary
directory paths.  Two alternates are supported.  Default method creates temporary
files with a filename that begins with the headline text, and
located in a "username_Leo" subdirectory of the temporary
directory. The "LeoTemp" prefix is omitted.  If 'open_with_clean_filenames' is
set to true then subdirectories mirror the node's hierarchy in Leo. Either method
makes it easier to see which temporary file is related to which outline node."""

#@@language python
#@@tabwidth -4

import leo.core.leoGlobals as g
import leo.core.leoPlugins as leoPlugins

import leo.core.leoCommands as leoCommands
import getpass
import os
import tempfile

__version__ = "1.3"

#@+others
#@+node:ekr.20100128091412.5382:init
def init():

    ok = not g.app.unitTesting
        # Not Ok for unit testing: it changes Leo's core.

    if ok:
        # Register the handlers...
        leoPlugins.registerHandler("start2", onStart)
        g.plugin_signon(__name__)

    return ok
#@nonl
#@-node:ekr.20100128091412.5382:init
#@+node:EKR.20040517075715.2:onStart
def onStart (tag,keywords):

    # g.trace("replacing openWithTempFilePath")

    g.funcToMethod(openWithTempFilePath,leoCommands.Commands,"openWithTempFilePath")
#@-node:EKR.20040517075715.2:onStart
#@+node:EKR.20040517075715.3:openWithTempFilePath
def openWithTempFilePath (self,v,ext):

    """Return the path to the temp file corresponding to v and ext.
       Replaces the Commands method."""

    #TL: Added support creating temporary directory structure based on node's
    #    hierarchy in Leo's outline.
    c = self
    if c.config.getBool('open_with_clean_filenames'):
        atFileFound = False   #Track when first ancestor @file found
        #Build list of all of node's parents
        ancestor = []
        p = c.p
        while p:
            hs = p.isAnyAtFileNode() #Get file name if we're at a @file node
            if not hs:
                hs = p.h  #Otherwise, use the entire header
            else:
#@verbatim
                #@file type node
                if c.config.getBool('open_with_uses_derived_file_extensions'):
                    #Leo configured to use node's derived file's extension
                    if(atFileFound == False):
                        atFileFound = True #no need to look any more.
                        #Found first ancestor @file node in outline
                        atFileBase,atFileExt = g.os_path_splitext(hs)
                        if(p == c.p):
                            #node to edit is an @file, Move ext from hs to ext
                            hs = atFileBase
                        if atFileExt: #It has an extension
                            ext = atFileExt #use it

            #Remove unsupported directory & file name characters
            #if(os.name == "dos" or os.name == "nt"):
            if 1:
                hsClean = ""
                for ch in hs:
                    if ch in g.string.whitespace: #Convert tabs to spaces
                        hsClean += ' '
                    elif ch in ('\\','/',':','|','<','>','*', '"'): #Not allowed in Dos/Windows
                        hsClean += '_'
                    elif ch in ('"'): #Leo code can't handle the "
                        hsClean += '\''   #replace with '
                    else:
                        hsClean += ch
                #Windows directory and file names can't end with a period
                if hsClean.endswith( '.' ):
                    hsClean += '_'
            else:
                hsClean = g.sanitize_filename(hs)
            #Add node's headstring (filtered) to the list of ancestors
            ancestor.append(hsClean)
            p = p.parent()

        #Put temporary directory structure under <tempdir>\Leo<uniqueId> directory
        ancestor.append( "Leo" + str(id(v)))

        #Build temporary directory
        td = os.path.abspath(tempfile.gettempdir())
        #Loop through all of node's ancestors
        while len(ancestor) > 1:
            #Add each ancestor of node from nearest to farthest
            td = os.path.join(td, ancestor.pop()) #Add next subdirectory
            if not os.path.exists(td):
                os.mkdir(td)
        #Add filename with extension to the path (last entry in ancestor list)
        name = ancestor.pop() + ext
    else:
        #Use old method for unsupported operating systems
        try:
            leoTempDir = getpass.getuser() + "_" + "Leo"
        except:
            leoTempDir = "LeoTemp"
            g.es("Could not retrieve your user name.")
            g.es("Temporary files will be stored in: %s" % leoTempDir)
        td = os.path.join(os.path.abspath(tempfile.gettempdir()), leoTempDir)
        if not os.path.exists(td):
            os.mkdir(td)
        name = g.sanitize_filename(v.h) + '_' + str(id(v)) + ext

    path = os.path.join(td,name)

    return path

#@-node:EKR.20040517075715.3:openWithTempFilePath
#@-others
#@-node:EKR.20040517075715.1:@thin mod_tempfname.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.