[Idle-dev] Newbie Question

Saul Spatz sspatz at kcnet.com
Mon Jan 15 17:58:44 CET 2007

Thanks for all your help.  I'm sure I'll be asking you for some of these 
patches, once I've done enough reading to know what I need, but I wanted 
to express my gratitude immediately.  I seem to be getting my code to 
work, slowly, but I want to follow best practices, if I can.

I've streamlined the functionality a bit, so some of these issues don't 
arise any more, I think.  What I'm doing now is allowing the user to 
specify the directory for the log file either by typing it into an entry 
widget, or selecting it with a tkFileDialog.askdirectory dialog.  Of 
course, if the user selects a directory with the dialog, I also update 
the text in the entry widget. If I understand you right,  I'm going to 
have trouble saving this string in the configuration file and reading it 
back at startup. 

Thanks so much,

Tal Einat wrote:
> You could (and probably should) make this an IDLE extension. This will
> increase the chance that your changes will work with future changes in 
> and allow you to easily implement (and debug!) such changes when they 
> will
> be required. For instance, you could easily switch your extension on 
> or off
> in a config file, instead of running a different version of IDLE.
> For info on writing extensions, read the "extend.txt" file in 
> Lib\idlelib\,
> and check out the code of existing extensions (FormatParagraph and
> CodeContext are good examples).
> For a taste - when writing an extension, you write a class whose 
> contructor
> recieves the EditorWindow instance as an argument. PyShell is a 
> subclass of
> EditorWindow, so your extension will actually be recieving the PyShell
> instance, which you can change you your liking.
> Also, you should use idleConf to save/load configuration; idleConf has an
> 'extensions' section specifically for IDLE extensions. So you could write
> file's default location in config-extensions.def, and each user can 
> choose a
> location of his choice in his config-extensions.cfg.
> You can easily configure an extension to be enabled only for Shell 
> windows
> by writing "enable_editor=0" in config_extensions.def.
> Caveat: Writing paths in IDLE's config files requires reading/writing 
> them
> as "raw" values, which idleConf currently doesn't allow. I have a simple
> patch which allows idleConf to do this, but I sent it along with a 
> patch for
> the Squeezer IDLE extension, which hasn't been accepted yet. I could 
> create
> a patch just for idleConf if you'd like.
> Currently extension config is done by manually editing the above 
> mentioned
> text files, but you mentioned wanting to specifiy the session file inside
> IDLE. I have written a generic configuration dialog for IDLE extensions
> which allows extension configuration inside IDLE, but I haven't posted 
> it as
> a patch yet (not polished enough). Again, I could send you a working 
> version
> if you like.
> You also mentioned wanting IDLE to prompt the user if the session hasn't
> been saved. Notice that IDLE does this when editing files. This is 
> because
> the EditorWindow class defines a maybesave() method which does this, but
> OutputWindow (a subclass of EditorWindow, which PyShell inherits) 
> overrides
> this method. You could override PyShell's maybesave() method with 
> something
> similar to EditorWindow's method.
> EditorWindow.maybesave() actually calls IOBinding.maybesave()... which 
> does
> a lot of work for you (such as remembering the filename, prompting for a
> filename if the buffer has never been saved, etc.). Not sure if this is
> exactly the functionality you want, but it's a starting point.
> I hope all this helps! (and that we have an awesome new IDLE extension 
> soon
> :)
> Good luck,
> - Tal
> On 1/15/07, Saul Spatz <sspatz at kcnet.com> wrote:
>> > On 12/27/06, Saul Spatz <sspatz at kcnet.com> wrote:
>> >>
>> >> I am rather new to both python and IDLE.  The references to IDLE at
>> >> www.python.org seem sadly out-of-date.  I have a couple of simple
>> >> enhancements I would like to make to IDLE that are not likely to 
>> be of
>> >> general interest.  (I want to use IDLE in teaching, and I want to 
>> save
>> >> the sessions, both user input and IDLE output.  I would like IDLE to
>> >> remember where I saved the last session and default to that 
>> directory,
>> >> and I would like it to prompt me if I close the IDLE window without
>> >> having saved the session.)   It seems like these should be easy to
>> >> program, but I can't find the IDLE source.
>> >>
>> Thanks.  I think I've figured out how to modify IDLE to do what I want.
>> Now, I'm having trouble figuring out how best to organize my changes so
>> they don't get clobbered by the next IDLE update.  I'm trying to work
>> out how best to deal with packages and python namespaces.  Here is a
>> sample of what I have worked out:
>> #idle2.pyw
>> import idlelib.PyShell
>> import time
>> from Tkinter import *
>> EditorWindow = idlelib.EditorWindow.EditorWindow
>> class MyPyShell(idlelib.PyShell.PyShell):
>>     def close2(self):
>>         format = '%d%b%Y.%H%M%S.log'
>>         path = "E:/MyPythonProjects/"
>>         filename = path + time.strftime(format)
>>         try:
>>             log = open(filename,'w')
>>             log.write(self.text.get(1.0,END))
>>             log.close()
>>         except IOError:
>>             pass
>>         return EditorWindow.close(self)
>> idlelib.PyShell.PyShell = MyPyShell
>> idlelib.PyShell.main()
>> This seems to work.  However, I don't really want the path to be
>> hard-coded into my program.  I want to be able to select the directory,
>> and to control the logging behavior interactively.  This means that I
>> will have to subclass ConfigDialog.ConfigDialog and perhaps other
>> classes as well.  Am I likely to run into trouble with the approach
>> indicated above, and, in any case, is there a better way?
>> I realize that this question has little to do with IDLE development, but
>> I don't know where else to turn for help.  I'd be grateful if some guru
>> will give of his wisdom to a beginner.
>> Thanks,
>> Saul

More information about the IDLE-dev mailing list