~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/pygimp/plug-ins/gimpcons.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
from gimpfu import *
21
21
 
22
 
def plug_in_python_fu_console():
 
22
t = gettext.translation('gimp20-python', gimp.locale_directory, fallback=True)
 
23
_ = t.ugettext
 
24
 
 
25
PROC_NAME = 'python-fu-console'
 
26
 
 
27
RESPONSE_BROWSE, RESPONSE_CLEAR, RESPONSE_SAVE = range(3)
 
28
 
 
29
def do_console():
23
30
    import pygtk
24
31
    pygtk.require('2.0')
25
32
 
26
 
    import gtk, gimpenums, gimpshelf
27
 
 
28
 
    gtk.rc_parse(gimp.gtkrc())
 
33
    import sys, gobject, gtk, gimpenums, gimpshelf, gimpui, pyconsole
29
34
 
30
35
    namespace = {'__builtins__': __builtins__,
31
36
                 '__name__': '__main__', '__doc__': None,
32
37
                 'gimp': gimp, 'pdb': gimp.pdb,
33
38
                 'shelf': gimpshelf.shelf}
34
 
 
 
39
    
35
40
    for s in gimpenums.__dict__.keys():
36
41
        if s[0] != '_':
37
42
            namespace[s] = getattr(gimpenums, s)
38
43
 
39
 
    def bye(*args):
40
 
        gtk.main_quit()
41
 
 
42
 
    win = gtk.Window()
43
 
    win.connect("destroy", bye)
44
 
    win.set_title("Gimp-Python Console")
45
 
 
46
 
    import gtkcons
47
 
    cons = gtkcons.Console(namespace=namespace, quit_cb=bye)
48
 
 
49
 
    def browse(button, cons):
50
 
        import gimpprocbrowser
51
 
 
52
 
        def on_apply(proc): 
 
44
    class Console(gimpui.Dialog):
 
45
        def __init__(self):
 
46
            gimpui.Dialog.__init__(self, title=_("Python Console"),
 
47
                                   role=PROC_NAME, help_id=PROC_NAME,
 
48
                                   buttons=(gtk.STOCK_SAVE,  RESPONSE_SAVE,
 
49
                                            gtk.STOCK_CLEAR, RESPONSE_CLEAR,
 
50
                                            _("_Browse..."), RESPONSE_BROWSE,
 
51
                                            gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
 
52
 
 
53
            self.set_alternative_button_order((gtk.RESPONSE_CLOSE,
 
54
                                               RESPONSE_BROWSE,
 
55
                                               RESPONSE_CLEAR,
 
56
                                               RESPONSE_SAVE))
 
57
 
 
58
            banner = ('Gimp %s Python Console\nPython %s\n' %
 
59
                      (gimp.pdb.gimp_version(), sys.version))
 
60
 
 
61
            self.cons = pyconsole.Console(locals=namespace, banner=banner,
 
62
                                          quit_func=lambda: gtk.main_quit())
 
63
 
 
64
            self.connect('response', self.response)
 
65
 
 
66
            self.browse_dlg = None
 
67
            self.save_dlg = None
 
68
 
 
69
            vbox = gtk.VBox(False, 12)
 
70
            vbox.set_border_width(12)
 
71
            self.vbox.pack_start(vbox)
 
72
 
 
73
            scrl_win = gtk.ScrolledWindow()
 
74
            scrl_win.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
 
75
            vbox.pack_start(scrl_win)
 
76
 
 
77
            scrl_win.add(self.cons)
 
78
 
 
79
            self.set_default_size(500, 500)
 
80
 
 
81
        def response(self, dialog, response_id):
 
82
            if response_id == RESPONSE_BROWSE:
 
83
                self.browse()
 
84
            elif response_id == RESPONSE_CLEAR:
 
85
                self.cons.banner = None
 
86
                self.cons.clear()
 
87
            elif response_id == RESPONSE_SAVE:
 
88
                self.save_dialog()
 
89
            else:
 
90
                gtk.main_quit()
 
91
 
 
92
            self.cons.grab_focus()
 
93
 
 
94
        def browse_response(self, dlg, response_id):
 
95
            if response_id != gtk.RESPONSE_APPLY:
 
96
                dlg.hide()
 
97
                return
 
98
 
 
99
            proc_name = dlg.get_selected()
 
100
 
 
101
            if not proc_name:
 
102
                return
 
103
 
 
104
            proc = pdb[proc_name]
 
105
            
53
106
            cmd = ''
54
107
 
55
108
            if len(proc.return_vals) > 0:
56
 
                cmd = ', '.join([x[1] for x in proc.return_vals]) + ' = '
57
 
 
58
 
            if '-' in proc.proc_name:
59
 
                cmd = cmd + "pdb['%s']" % proc.proc_name
60
 
            else:
61
 
                cmd = cmd + "pdb.%s" % proc.proc_name
62
 
 
63
 
            if len(proc.params) > 0 and proc.params[0][1] == 'run_mode':
 
109
                cmd = ', '.join([x[1].replace('-', '_')
 
110
                                for x in proc.return_vals]) + ' = '
 
111
 
 
112
            cmd = cmd + 'pdb.%s' % proc.proc_name.replace('-', '_')
 
113
 
 
114
            if len(proc.params) > 0 and proc.params[0][1] == 'run-mode':
64
115
                params = proc.params[1:]
65
116
            else:
66
117
                params = proc.params
67
118
 
68
 
            cmd = cmd + "(%s)" % ', '.join([x[1] for x in params])
69
 
 
70
 
            cons.line.set_text(cmd)
71
 
    
72
 
        dlg = gimpprocbrowser.dialog_new(on_apply)
73
 
 
74
 
    button = gtk.Button("Browse")
75
 
    button.connect("clicked", browse, cons)
76
 
 
77
 
    cons.inputbox.pack_end(button, expand=FALSE)
78
 
    button.show()
79
 
 
80
 
    win.add(cons)
81
 
    cons.show()
82
 
 
83
 
    win.set_default_size(475, 300)
84
 
    win.show()
85
 
 
86
 
    cons.init()
87
 
 
88
 
    # flush the displays every half second
89
 
    def timeout():
90
 
        gimp.displays_flush()
91
 
        return TRUE
92
 
 
93
 
    gtk.timeout_add(500, timeout)
94
 
    gtk.main()
 
119
            cmd = cmd + '(%s)' % ', '.join([x[1].replace('-', '_')
 
120
                                           for x in params])
 
121
 
 
122
            buffer = self.cons.buffer
 
123
 
 
124
            lines = buffer.get_line_count()
 
125
            iter = buffer.get_iter_at_line_offset(lines - 1, 4)
 
126
            buffer.delete(iter, buffer.get_end_iter())
 
127
            buffer.place_cursor(buffer.get_end_iter())
 
128
            buffer.insert_at_cursor(cmd)
 
129
 
 
130
        def browse(self):
 
131
            if not self.browse_dlg:
 
132
                dlg = gimpui.ProcBrowserDialog(_("Python Procedure Browser"),
 
133
                                               role=PROC_NAME,
 
134
                                               buttons=(gtk.STOCK_APPLY,
 
135
                                                        gtk.RESPONSE_APPLY,
 
136
                                                        gtk.STOCK_CLOSE,
 
137
                                                        gtk.RESPONSE_CLOSE))
 
138
 
 
139
                dlg.set_default_response(gtk.RESPONSE_APPLY)
 
140
                dlg.set_alternative_button_order((gtk.RESPONSE_CLOSE,
 
141
                                                  gtk.RESPONSE_APPLY))
 
142
 
 
143
                dlg.connect('response', self.browse_response)
 
144
                dlg.connect('row-activated',
 
145
                            lambda dlg: dlg.response(gtk.RESPONSE_APPLY))
 
146
 
 
147
                self.browse_dlg = dlg
 
148
 
 
149
            self.browse_dlg.present()
 
150
 
 
151
        def save_response(self, dlg, response_id):
 
152
            if response_id == gtk.RESPONSE_DELETE_EVENT:
 
153
                self.save_dlg = None
 
154
                return
 
155
            elif response_id == gtk.RESPONSE_OK:
 
156
                filename = dlg.get_filename()
 
157
 
 
158
                try:
 
159
                    logfile = open(filename, 'w')
 
160
                except IOError, e:
 
161
                    gimp.message(_("Could not open '%s' for writing: %s") %
 
162
                                 (filename, e.strerror))
 
163
                    return
 
164
 
 
165
                buffer = self.cons.buffer
 
166
 
 
167
                start = buffer.get_start_iter()
 
168
                end = buffer.get_end_iter()
 
169
 
 
170
                log = buffer.get_text(start, end, False)
 
171
 
 
172
                try:
 
173
                    logfile.write(log)
 
174
                    logfile.close()
 
175
                except IOError, e:
 
176
                    gimp.message(_("Could not write to '%s': %s") %
 
177
                                 (filename, e.strerror))
 
178
                    return
 
179
 
 
180
            dlg.hide()
 
181
 
 
182
        def save_dialog(self):
 
183
            if not self.save_dlg:
 
184
                dlg = gtk.FileChooserDialog(_("Save Python-Fu Console Output"),
 
185
                                            parent=self,
 
186
                                            action=gtk.FILE_CHOOSER_ACTION_SAVE,
 
187
                                            buttons=(gtk.STOCK_CANCEL,
 
188
                                                     gtk.RESPONSE_CANCEL,
 
189
                                                     gtk.STOCK_SAVE,
 
190
                                                     gtk.RESPONSE_OK))
 
191
 
 
192
                dlg.set_default_response(gtk.RESPONSE_OK)
 
193
                dlg.set_alternative_button_order((gtk.RESPONSE_OK,
 
194
                                                  gtk.RESPONSE_CANCEL))
 
195
 
 
196
                dlg.connect('response', self.save_response)
 
197
 
 
198
                self.save_dlg = dlg
 
199
 
 
200
            self.save_dlg.present()
 
201
 
 
202
        def run(self):
 
203
            self.show_all()
 
204
 
 
205
            # flush the displays every half second
 
206
            def timeout():
 
207
                gimp.displays_flush()
 
208
                return True
 
209
 
 
210
            gobject.timeout_add(500, timeout)
 
211
            gtk.main()
 
212
 
 
213
    console = Console()
 
214
    console.run()
95
215
 
96
216
register(
97
 
    "python_fu_console",
98
 
    "Python interactive interpreter with gimp extensions",
 
217
    PROC_NAME,
 
218
    N_("Interactive Gimp-Python interpreter"),
99
219
    "Type in commands and see results",
100
220
    "James Henstridge",
101
221
    "James Henstridge",
102
222
    "1997-1999",
103
 
    "<Toolbox>/Xtns/Python-Fu/_Console",
 
223
    N_("_Console"),
104
224
    "",
105
225
    [],
106
226
    [],
107
 
    plug_in_python_fu_console)
 
227
    do_console,
 
228
    menu="<Toolbox>/Xtns/Languages/Python-Fu",
 
229
    domain=("gimp20-python", gimp.locale_directory))
108
230
 
109
231
main()