Generic Conversion Functions
The next couple of modules are used for general data
conversions.
netrc
The netrc module parses, processes, and
encapsulates the .netrc configuration file format used by
UNIX FTP program and other FTP
clients.
import netrc
netrc_filename = "/usr/local/myconfig.netrc"
netrccfg = netrc.netrc(netrc_filename)
l, a, p = netrccfg.authenticators("connection.msg")
print "My Login = %s" % (l)
print "My Password = %s" % (p)
print "My Account= %s" % (a)
mhlib
The mhlib module provides a Python interface to access MH folders/mailboxes and their contents. This module contains three basic classes:
MH ([path[,
profile]])—
Represents a particular collection of MH folders.
Folder (mh, name)—
Represents a single folder and its messages.
Message (folder, number[,
name])—
Represents individual messages in a folder. The Message class is derived from mimetools.Message.
MHObjects
MH instances have the following methods:
error(format[, ...])—
Prints an error message: can be overridden.
getprofile(key)—
Returns a profile entry (None if not set).
getpath()—
Returns the mailbox pathname.
getcontext()—
Return the current folder name.
setcontext(name)—
Sets the current folder name.
listfolders()—
Returns a list of top-level folders.
listallfolders()—
Returns a list of all folders.
listsubfolders(name)—
Returns a list of direct subfolders of the given folder.
listallsubfolders(name)—
Returns a list of all subfolders of the given folder.
makefoldername)—
Creates a new folder.
deletefolder(name)—
Deletes a folder: must have no subfolders.
openfolder(name)—
Returns a new open folder object.
Folder Objects
Folder instances represent open folders and have the following methods:
error(format[, ...])—
Prints an error message; can be overridden.
getfullname()—
Returns the folder's full pathname.
getsequencesfilename()—
Returns the full pathname of the folder's sequences file.
getmessagefilename(n)—
Returns the full pathname of message n of the folder.
listmessages()—
Returns a list of messages in the folder (as numbers).
getcurrent()—
Returns the current message number.
setcurrent(n)—
Sets the current message number to n.
parsesequence(seq)—
Parses msgs syntax into a list of messages.
getlast()—
Gets last message, or 0 if no messages are in the folder.
setlast(n)—
Sets last message (internal use only).
getsequences()—
Returns dictionary of sequences in folder. The sequence names are used as keys, and the values are the lists of message numbers in the sequences.
putsequences(dict)—
Returns dictionary of sequences in folder name: list.
removemessages(list)—
Removes messages in list from folder.
refilemessages(list, tofolder)—
Moves messages in list to other folder.
movemessage(n, tofolder, ton)—
Moves one message to a given destination in another folder.
copymessage(n, tofolder, ton)—
Copies one message to a given destination in another folder.
Message Objects
openmessage(n) returns a new open
message object (costs a file descriptor).
binhex
The binhex module encodes and decodes files in binhex4 format. This format is commonly used to represent files on Macintosh systems.
import binhex, sys
infile = "filename.jpg"
binhex.binhex(infile, sys.stdout)
binhex(inputfile, outputfile) converts a binary file (inputfile) to a binhex file(outputfile).
hexbin(inputfile [, outputfile])
converts a binhex file (inputfile) back to a regular binary
file (outputfile). When the output name is omitted, the
interpreter uses the same one provided in the first argument.
uu
The uu module encodes and decodes files in
uuencode format. This module does its job by transferring binary data over an
ASCII-only connection. Wherever a file argument is expected, the methods accept
a file-like object. For backwards compatibility, a string containing a pathname
is also accepted, and the corresponding file will be opened for reading and
writing; the
pathname - is understood to mean the
standard input or output. However, this interface is deprecated; it's better
for the caller to open the file itself, and be sure that, when required, the
mode is
rb or
wb on Windows or DOS.
The code of this module was contributed by
Lance Ellinghouse and modified by
Jack Jansen.
The uu module defines the following
functions:
encode (in_file, out_file[, name[, mode]])—
This function uuencodes file in_file into file out_file. The uuencoded file will have the header specifying name and mode as the defaults for the results of decoding the file. The default defaults are taken from in_file, or - and 0666, respectively.
decode (in_file[, out_file[, mode]])—
This call decodes uuencoded file in_file placing the result on file out_file. If out_file is a pathname, the mode is also set. Defaults for out_file and mode are taken from the uuencode header.
Note that in the previous functions, both arguments can be either
filenames or file objects.
This format used to be popular on the Usenet, but nowadays, it is being superceded by base64 encoding.
Each encoded data stream starts with a begin line, which also includes the file privileges, the filename, and ends with an
end line, as you can see in the following example:
begin 755 executeprog.py
KF_EF_#JFJ! …
end
binascii
The binascii module implements methods to convert data between binary and various ASCII-encoded binary representations, including binhex,uu, and base64. Note that normally, you would just use the binhex,uu, or base64 modules rather than binascii.
This module implements two exceptions: Error
(raised on errors), and Incomplete (raised on incomplete
data). The following
methods are implemented by this module:
binascii.b2a_base64(binarydata)—
Converts a string of binary data to a string of base64-encoded characters.
binascii.a2b_base64(string)—
Converts a string of base64-encoded data to binary.
binascii.b2a_uu(binarydata)—
Converts a string of binary data to a string of uuencoded characters.
binascii.a2b_uu(string)—
Converts a string of uuencoded data to binary.
binascii.b2a_hqx(binarydata)—
Converts a string of binary data to a string of binhex4-encoded characters.
binascii.a2b_hqx(string)—
Converts a string of binhex4-encoded data to binary.
binascii.rledecode_hqx(binarydata)—
Decompresses the binary data using the RLE (Run-Length Encoding) method. If the binary data is incomplete, an Incomplete exception is
raised.
binascii.rleecode_hqx(binarydata)—
Compresses the binary data according to the RLE method.
binascii.crc_hqx(binarydata, crc)—
Returns the checksum of a given binhex4-binary data. The argument crc indicates the checksum's starting value.
Note that starting with Python 2.0, there are two more functions to include in the list provided by the binascii module. They are called b2a_hex and a2b_hex. They
are used to convert between binary data and its
hexadecimal representation.