File: prompt.py

package info (click to toggle)
sketch 0.6.15-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,692 kB
  • ctags: 8,625
  • sloc: python: 36,557; ansic: 16,579; makefile: 83; sh: 28
file content (105 lines) | stat: -rw-r--r-- 3,407 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Sketch - A Python-based interactive drawing program
# Copyright (C) 1997, 1998, 2002 by Bernhard Herzog
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307	USA

import sys, os, string
import Sketch


def get_sketch_modules():
    # return the sketch specific modules that are already imported.
    # Sketch specific means that the module comes from a file below the
    # sketch directory or that it has a name starting with "Sketch.".
    sketch_dir = Sketch.config.sketch_dir
    if sketch_dir[-1] != '/':
        sketch_dir = sketch_dir + '/'
    sketch_dir = os.path.normpath(os.path.abspath(sketch_dir))
    length = len(sketch_dir)
    result = []
    for module in sys.modules.values():
        try:
            mod_file = module.__file__
        except AttributeError:
            continue
        mod_file = os.path.normpath(os.path.abspath(mod_file))
        if mod_file[:length] == sketch_dir or module.__name__[:7] == "Sketch.":
            result.append(module)
    return result



user_functions = {}
def add_sketch_objects(dict):
    from Sketch import main, connector
    # some useful variables and functions
    dict['app'] = main.application
    dict['canv'] = main.application.main_window.canvas
    dict['doc'] = main.application.main_window.document
    dict['connections'] = connector._the_connector.print_connections
    dict['get_sketch_modules'] = get_sketch_modules
    for key, val in user_functions.items():
	dict[key] = val




locals = {}

def PythonPrompt(prompt = '>>>', prompt2 = '...'):
    # try to import readline in Python 1.5
    have_readline = 0
    try:
	import readline
	have_readline = 1
	Sketch.Issue(None, Sketch.const.INIT_READLINE)
    except ImportError:
	pass
    globals = {}
    # put all of Sketch.main and Sketch into the globals
    exec 'from Sketch.main import *' in globals
    exec 'from Sketch import *' in globals
    # put all sketch specific modules into the globals
    for module in get_sketch_modules():
	globals[module.__name__] = module
    add_sketch_objects(globals)
    if have_readline:
	from Sketch.Lib import skcompleter
	skcompleter.install(globals, locals)
    while 1:
	try:
	    cmd = raw_input(prompt)
	    #cmd = string.strip(cmd)
	    if cmd:
		if cmd[-1] == ':':
		    # a compound statement
		    lines = []
		    while string.strip(cmd):
			lines.append(cmd)
			cmd = raw_input(prompt2)
		    cmd = string.join(lines + [''], '\n')
		    kind = 'exec'
		else:
		    kind = 'single'

		c = compile(cmd, '<string>', kind)
		exec c in globals, locals
	except EOFError:
	    print '----- returning to Sketch'
	    return
	except:
	    import traceback
	    traceback.print_tb(sys.exc_traceback)
	    print 'Exception %s: %s' % (sys.exc_type, sys.exc_value)