From: Kevin A. <al...@se...> - 2004-09-16 21:46:44
|
On Sep 16, 2004, at 12:53 PM, normanwinn wrote: > Hi, > > I was having trouble getting Walkthough 3 to work. Having already > posted I felt too guilty to admit I hadn't even got that far. I > trawled the web for hints but didn't get anywhere. Then I played with > 'FindFiles'. I soon wanted it to open resource files in the resource > editor (no doubt someone will come back and tell me that was already > possible). So I decided to see if I could add this functionality > myself. > > First I had to find the '.rsrc' files. To do this I just searched for: > > { 'application':{ 'type':'Application', > > not elegant, but it worked. > > I first found where the highlighted file was opened which was inside > this function (only showing the part I played with): > >> def editFile(self, filename, lineno=None): >> if filename == '': >> return >> >> log.debug("filename: " + filename + " lineno: " + >> str(lineno)) >> # edit Python scripts with codeEditor >> # everything else with textEditor >> # the list of extensions and associated programs to >> # open with should be user settable >> if os.path.splitext(filename)[-1] in ['.py', '.pyw']: >> program = os.path.join("..", "codeEditor", >> "codeEditor.pyw") >> if not os.path.exists(program): >> program = os.path.join("..", "codeEditor", >> "codeEditor.py") >> else: >> program = os.path.join("..", "textEditor", >> "textEditor.pyw") >> if not os.path.exists(program): >> program = os.path.join("..", "textEditor", >> "textEditor.py") >> # throw an exception if textEditor can't be found? >> log.debug('program: ' + program) > > > I had made a copy of the 'FindFiles' directory content and put it > elsewhere. It took me a while to fathom the FindFiles depends on > hanging off the 'Tools' folder to work. I then moved my copy there and > started to get somewhere. > > I tried adding a condition to so that I could operate on '.rsrc.py' > files. The interactive shell is a great tool for learning. I imported > 'os' and worked out what 'os.path.splitext' does. I copied a path and > assigned it to a variable (still in the shell) and finally found that: > > os.path.splitext(filename)[0][-5:] > > gave me the '.rsrc' I was trying for. > > I was not successful at getting the resource editor to run so I tried > assigning the above to a variable and just to show it with a 'print'. > Evidently, this produced an error. > > I saw all those 'log.debug' lines so went about finding which of the > imported modules it came from. Once I found it I imported this in the > shell and looked for help on how 'log' worked. Nice and simple, so I > put: > > log.enable() > > in the function 'on_initialize' and I had log information pouring out > to me in the shell. > > This told me that I was extracting the right string. I then divined my > error. I was using 'is' instead of '==' to test my string. I changed > it and it worked. I felt real proud of myself when the resource editor > opened up. > > So, if anyone is interested here is what those lines above became: > >> def editFile(self, filename, lineno=None): >> if filename == '': >> return >> >> log.debug("filename: " + filename + " lineno: " + >> str(lineno)) >> # edit Python scripts with codeEditor >> # everything else with textEditor >> # the list of extensions and associated programs to >> # open with should be user settable >> rsrcFile = os.path.splitext(filename)[0][-5:] >> log.debug('rsrcFile: ' + rsrcFile) >> if rsrcFile == '.rsrc': >> program = os.path.join("..", "resourceEditor", >> "resourceEditor.py") >> if not os.path.exists(program): >> program = os.path.join("..", "resourceEditor", >> "resourceEditor.py") >> else: >> if os.path.splitext(filename)[-1] in ['.px', '.pyx']: >> program = os.path.join("..", "codeEditor", >> "codeEditor.pyw") >> if not os.path.exists(program): >> program = os.path.join("..", "codeEditor", >> "codeEditor.py") >> else: >> program = os.path.join("..", "textEditor", >> "textEditor.pyw") >> if not os.path.exists(program): >> program = os.path.join("..", "textEditor", >> "textEditor.py") > > > I realise that using a variable to get my string is not very elegant > but I did it that way to isolate my error. > > I am relating this partly as one test I have set myself is to see if I > can learn PythonCard and Python 'top down'. I learnt Object Pascal in > Delphi that way and had thought it would not be possible. Seems I am > wrong, I'm glad you were able to work this out. I haven't had resourceEditor support in findfiles previously, because usually I want to edit the .rsrc.py files using the codeEditor to make some quick change. However, I think it would be a good option that could be toggled on and off, so I'll look at adding that. In the meantime, all you need to do is a simple string check, in fact, I'm not sure why I'm not doing this in the existing code, except maybe the current way seemed cleaner than filename.endswith('.py) or filename.endswith('.pyw'). if filename.endswith('.rsrc.py'): program = os.path.join("..", "resourceEditor", "resourceEditor.py") elif os.path.splitext(filename)[-1] in ['.py', '.pyw']: ... Obviously, you can put the additional code in to check for a resourceEditor.pyw file, but the main thing is to just use the endswith string method and make sure that check is done before the .py/.pyw check. And just in case it wasn't obvious, in the File types: field of Find Files, just search for *.rsrc.py files. > Norman > > PS I still can't get the child window to open. My script runs OK but > as soon as I hit 'Increment' or 'Decrement' I get > What is the actual error? I haven't looked at that walkthrough in a while, so it hasn't been updated for release 0.8. Make sure to change on_openBackground to on_initialize. ka |