PyDoc launch script

Help with setting up and configuring custom user tools in UltraEdit (based on command line input)

PyDoc launch script

Postby pydoctor » Thu Mar 26, 2009 11:01 am

I made a little script to launch PyDoc from UltraEdit. It might be useful to others. Please read the source code for further instructions.

Code: Select all
""" UltraEdit Pydoc Launcher

This small Python script can be used to launch the PyDoc http server
and display the documentation for either a selected module or the current
python file.

IMPORTANT: Win32api needs to be installed! See http://sourceforge.net/projects/pywin32/

Use:

- In UltraEdit create a (Project) Tool Configuration

    * For the Command Line option use:
        <full path to mydoc.py location>\mydoc.py %p%n %sel%
        or if you want to use a version of Python which is not your default installation:
        <full path to python.exe location>\python.exe <full path to mydoc.py location>\mydoc.py %p%n %sel%

    * For the Working Directory option use:
        <full path to the directory containing your Python code>
        This is needed for Pydoc to find your code if it's not in Python's module search paths.

- Launch the tool configuration from UltraEdit from the Advanced menu or otherwise

  If you have the name selected of a Python module or function like os.path, or os.path.abspath,
  the PyDoc http server will be launched and your default webbrowser will attempt to display
  the documention for that module or function.

  If you have NO text selected, PyDoc will be asked to show the documentation for the
  current Python file in UltraEdit.


This script is free to be used and/or altered by anybody. Please share any modifications
or upgrades you make with others!

- Pydoctor

"""
import sys
import webbrowser
import socket
import os
import os.path
import subprocess

# The host which will run the pydoc server
host = "127.0.0.1"
# The port used by the server
port = 44
# The Working Directory, defined in UltreEdit's Tool Configuration dialog
working_dir = os.path.abspath(os.path.curdir)
# The directory in which this script resides
mydoc_path = os.path.dirname(__file__)
# The location where the pid file will be stored
pid_path = os.path.join(mydoc_path, "mydoc.pid")
# The location of python.exe
python_path = sys.executable
# The command used to launch pydoc.py
pydoc_command = "%s\\Lib\\pydoc.py" % sys.exec_prefix
# The base url for server requests
serv = "http://%s:%s/" % (host, port)

def start_server():
    """ Starts the PyDoc server and stores the present working
    directory and pid in a file. """
    print "Starting server %s with Working Directory '%s'" % (serv, working_dir)
    pid = subprocess.Popen([python_path, pydoc_command, "-p", str(port)]).pid
    f = open(pid_path, 'w')
    f.write("%s\n" % working_dir)
    f.write("%d\n" % pid)
    f.close()

def server_up():
    """ This function checks to see if the pydoc server is up
    and running under the required Working Directory. """

    if os.path.exists(pid_path):
        print "PID file found"
        serverSocket = socket.socket()
        serverSocket.settimeout(0.25)
        try:
            serverSocket.connect((host, port))
            serverSocket.close()
            print "PyDoc server found"
        except socket.error:
            # No PyDoc server is running
            print "PyDoc server not running, removing old PID file"
            os.remove(pid_path)
            return False

        print "Reading PID file content"
        f = open(pid_path, 'r')
        serv_cwd = f.readline()
        serv_pid = int(f.readline())
        f.close()

        if serv_cwd.strip() != working_dir.strip() or True:
            print "Wrong Working Directory (found '%s', we need '%s')" % (serv_cwd, working_dir)
            # PyDoc server is running with wrong Working Directory
            try:
                print "Terminating server..."
                import win32api, win32con
                handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, serv_pid)
                win32api.TerminateProcess(handle, 0)
                win32api.CloseHandle(handle)
                print "Old PyDoc server terminated"
            except BaseException, msg:
                print "Error:", msg

            print "Removing old PID file"
            os.remove(pid_path)
            return False
        else:
            # PyDoc server is running under the needed Working Directory
            print "Server up and running"
            return True
    else:
        print "No PID file found"
        return False

if not server_up(): start_server()


tar = ""

if len(sys.argv) == 3:  # If text was selected in UltraEdit
    tar = sys.argv[2]
elif len(sys.argv) == 2:
    # Parse file path to module path
    modpath = sys.argv[1][len(working_dir) + 1:]
    modpath = modpath.replace('\\','.')
    tar = modpath

if tar != 'mydoc' :
    print "Loading documentation for '%s'" % tar
    webbrowser.open(serv + tar)
else:
    print "mydoc Module can't be shown"
pydoctor
Newbie
 
Posts: 1
Joined: Thu Mar 26, 2009 10:58 am

Return to Custom User Tools/Tool Configuration

cron