From: normanwinn <nor...@on...> - 2004-09-16 19:53:40
|
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, 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 |
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 |
From: Alex T. <al...@tw...> - 2004-09-16 22:12:12
|
At 14:46 16/09/2004 -0700, Kevin Altis wrote: >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'). Well, os.path goes to all the trouble of giving us a splitext function, so it may be a good idea to use it. Some day, someone may find a file system that uses something other than "." as the separator .... > 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. You could go one step further and do name,ext = os.path.splitext(filename) if ext in ['py', 'pyw]]: if name.endswith(".rsrc") then resource editor else: code editor else: text editor (Hmmm - or get really carried away and put in a table of file types and programs to use to open them with - with a menu option to create/edit associations, and .... :) Which brings me to a real question ... why .rsrc.py rather than .rsrc ? That way you could have associated .rsrc with the resource editor in the OS. -- Alex. |
From: Kevin A. <al...@se...> - 2004-09-16 22:38:14
|
On Sep 16, 2004, at 3:23 PM, Alex Tweedly wrote: > At 14:46 16/09/2004 -0700, Kevin Altis wrote: > > >> 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'). > > Well, os.path goes to all the trouble of giving us a splitext > function, so it may be a good idea to use it. Some day, someone may > find a file system that uses something other than "." as the separator > .... > >> 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. > > You could go one step further and do > name,ext = os.path.splitext(filename) > if ext in ['py', 'pyw]]: > if name.endswith(".rsrc") then > resource editor > else: > code editor > else: > text editor > > (Hmmm - or get really carried away and put in a table of file types > and programs to use to open them with - with a menu option to > create/edit associations, and .... :) Actually, that was the plan at one point, but it seemed like overkill. Sort of like having a built-in codeEditor (childWindow) in findfiles, though that is actually easy to do and sort of appealing, hmm... > Which brings me to a real question ... why .rsrc.py rather than > .rsrc ? > That way you could have associated .rsrc with the resource editor in > the OS. Historical decision. There is a .py extension because the resource files are valid python files, so if you open them in a Python-aware editor you get syntax highlighting, syntax checks should work, etc. I also needed a way of separating attributes of the filename to support automatic international resource support like minimal.fr.rsrc.py so dots seemed like the way to go. What we need to decide is whether the current import methodology makes sense or whether it would actually be better to make the resources into real modules so they could just be imported. To do that, the extra dots as separators have to be dropped they would be misinterpreted by import. Switching to underscores would solve the problem there. The resource file would need one tiny change, something like: RESOURCE_DICT = { 'application':{ 'type':'Application', instead of just { 'application':{ 'type':'Application', The reason for the uppercase name is that if something obvious and unique like that is used, then it would be relatively simple to identify it as a global in any file. Conceivably, you could then inherit attributes from other resource files which would reduce duplication of common menus, or layouts. I don't know how the resourceEditor would be able to parse that, but it has some interesting uses. Anyway, we have to decide about changes like that pretty soon. I've been putting it off since it means changing so many files, but it needs to be done before say release 0.9. To minimize hassles it would be a good time to decide whether the CustomDialog resource format should really be different than Background as well as whether the name Background should finally be changed to just plain Window or Frame or something before we get to 1.0 ka |