- After changing to the file’s directory in order to make any relative filenames in its
code accurate, PyEdit now strips off any directory path prefix in the file’s name
before launching it, because its original directory path may no longer be valid if it
is relative instead of absolute. For instance, paths of files opened manually are
absolute, but file paths in PyDemos’s Code pop ups are all relative to the example
package root and would fail after a chdir. - PyEdit now correctly uses launcher tools that support command-line arguments
for file mode on Windows. - PyEdit inherits a fix in the underlying launchmodes module that changes forward
slashes in script path names to backslashes (though this was later made a moot
point by stripping relative path prefixes). PyEdit gets by with forward slashes on
Windows because open allows them, but some Windows launch tools do not.
Additionally, for both code run from files and code run in memory, this version adds
an update call between pop-up dialogs to ensure that later dialogs appear in all cases
(the second occasionally failed to pop up in rare contexts). Even with these fixes, Run
Code is useful but still not fully robust. For example, if the edited code is not run from
a file, it is run in-process and not spawned off in a thread, and so may block the GUI.
It’s also not clear how best to handle import paths and directories for files run in nonfile
mode, or whether this mode is worth retaining in general. Improve as desired.
Unicode (Internationalized) text support
Finally, because Python 3.X now fully supports Unicode text, this version of PyEdit
does, too—it allows text of arbitrary Unicode encodings and character sets to be opened
and saved in files, viewed and edited in its GUI, and searched by its directory search
utility. This support is reflected in PyMailGUI’s user interface in a variety of ways:
- Opens must ask the user for an encoding (suggesting the platform default) if one
is not provided by the client application or configuration - Saves of new files must ask for an encoding if one is not provided by configuration
- Display and edit must rely on the GUI toolkit’s own support for Unicode text
- Grep directory searches must allow for input of an encoding to apply to all files in
the tree and skip files that fail to decode, as described earlier
The net result is to support Internationalized text which may differ from the platform’s
default encoding. This is particularly useful for text files fetched over the Internet by
email or FTP. Chapter 14’s PyMailGUI, for example, uses an embedded PyEdit object
to view text attachments of arbitrary origin and encoding. The Grep utility’s Unicode
support was described earlier; the remainder of this model essentially reduces to file
opens and saves, as the next section describes.
Because strings are always Unicode code-point strings once
they are created in memory, Unicode support really means supporting arbitrary en-
codings for text files when they are read and written. Recall that text can be stored in
Unicode file and display model.
688 | Chapter 11: Complete GUI Programs