annotate Lib/mhlib.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 8dfc252963c7
children f82b608afb56
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # MH interface -- purely object-oriented (well, almost)
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Executive summary:
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # import mhlib
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # mh = mhlib.MH() # use default mailbox directory and profile
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # mh = mhlib.MH(mailbox) # override mailbox location (default from profile)
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # mh = mhlib.MH(mailbox, profile) # override mailbox and profile
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # mh.error(format, ...) # print error message -- can be overridden
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # s = mh.getprofile(key) # profile entry (None if not set)
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # path = mh.getpath() # mailbox pathname
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # name = mh.getcontext() # name of current folder
3632
44161fb04b7e added setcontext and parsesequence; several small fixes
Guido van Rossum <guido@python.org>
parents: 2246
diff changeset
15 # mh.setcontext(name) # set name of current folder
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # list = mh.listfolders() # names of top-level folders
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # list = mh.listallfolders() # names of all folders, including subfolders
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # list = mh.listsubfolders(name) # direct subfolders of given folder
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # list = mh.listallsubfolders(name) # all subfolders of given folder
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # mh.makefolder(name) # create new folder
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # mh.deletefolder(name) # delete folder -- must have no subfolders
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # f = mh.openfolder(name) # new open folder object
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # f.error(format, ...) # same as mh.error(format, ...)
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # path = f.getfullname() # folder's full pathname
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # path = f.getsequencesfilename() # full pathname of folder's sequences file
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # path = f.getmessagefilename(n) # full pathname of message n in folder
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # list = f.listmessages() # list of messages in folder (as numbers)
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # n = f.getcurrent() # get current message
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # f.setcurrent(n) # set current message
3632
44161fb04b7e added setcontext and parsesequence; several small fixes
Guido van Rossum <guido@python.org>
parents: 2246
diff changeset
35 # list = f.parsesequence(seq) # parse msgs syntax into list of messages
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
36 # n = f.getlast() # get last message (0 if no messagse)
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
37 # f.setlast(n) # set last message (internal use only)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # dict = f.getsequences() # dictionary of sequences in folder {name: list}
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # f.putsequences(dict) # write sequences back to folder
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 #
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
42 # f.removemessages(list) # remove messages in list from folder
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
43 # f.refilemessages(list, tofolder) # move messages in list to other folder
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
44 # f.movemessage(n, tofolder, ton) # move one message to a given destination
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
45 # f.copymessage(n, tofolder, ton) # copy one message to a given destination
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
46 #
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # m = f.openmessage(n) # new open message object (costs a file descriptor)
1872
32e6c3a671d0 mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents: 1739
diff changeset
48 # m is a derived class of mimetools.Message(rfc822.Message), with:
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # s = m.getheadertext() # text of message's headers
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # s = m.getheadertext(pred) # text of message's headers, filtered by pred
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # s = m.getbodytext() # text of message's body, decoded
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # s = m.getbodytext(0) # text of message's body, not decoded
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # XXX To do, functionality:
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # - annotate messages
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # - create, send messages
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 #
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
58 # XXX To do, organization:
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # - move IntSet to separate file
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 # - move most Message functionality to module mimetools
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # Customizable defaults
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 MH_PROFILE = '~/.mh_profile'
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 PATH = '~/Mail'
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 MH_SEQUENCES = '.mh_sequences'
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 FOLDER_PROTECT = 0700
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # Imported modules
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 import os
3632
44161fb04b7e added setcontext and parsesequence; several small fixes
Guido van Rossum <guido@python.org>
parents: 2246
diff changeset
74 import sys
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 from stat import ST_NLINK
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 import regex
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 import string
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 import mimetools
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 import multifile
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
80 import shutil
5331
6232acba7eb2 Rewrite parsesequence() to emulate MH without invoking pick.
Guido van Rossum <guido@python.org>
parents: 5228
diff changeset
81 from bisect import bisect
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 # Exported constants
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 Error = 'mhlib.Error'
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 # Class representing a particular collection of folders.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 # Optional constructor arguments are the pathname for the directory
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 # containing the collection, and the MH profile to use.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 # If either is omitted or empty a default is used; the default
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 # directory is taken from the MH profile if it is specified there.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 class MH:
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
97 # Constructor
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
98 def __init__(self, path = None, profile = None):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
99 if not profile: profile = MH_PROFILE
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
100 self.profile = os.path.expanduser(profile)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
101 if not path: path = self.getprofile('Path')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
102 if not path: path = PATH
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
103 if not os.path.isabs(path) and path[0] != '~':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
104 path = os.path.join('~', path)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
105 path = os.path.expanduser(path)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
106 if not os.path.isdir(path): raise Error, 'MH() path not found'
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
107 self.path = path
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
108
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
109 # String representation
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
110 def __repr__(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
111 return 'MH(%s, %s)' % (`self.path`, `self.profile`)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
113 # Routine to print an error. May be overridden by a derived class
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
114 def error(self, msg, *args):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
115 sys.stderr.write('MH error: %s\n' % (msg % args))
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
117 # Return a profile entry, None if not found
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
118 def getprofile(self, key):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
119 return pickline(self.profile, key)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
121 # Return the path (the name of the collection's directory)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
122 def getpath(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
123 return self.path
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
125 # Return the name of the current folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
126 def getcontext(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
127 context = pickline(os.path.join(self.getpath(), 'context'),
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
128 'Current-Folder')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
129 if not context: context = 'inbox'
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
130 return context
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
132 # Set the name of the current folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
133 def setcontext(self, context):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
134 fn = os.path.join(self.getpath(), 'context')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
135 f = open(fn, "w")
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
136 f.write("Current-Folder: %s\n" % context)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
137 f.close()
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
139 # Return the names of the top-level folders
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
140 def listfolders(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
141 folders = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
142 path = self.getpath()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
143 for name in os.listdir(path):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
144 fullname = os.path.join(path, name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
145 if os.path.isdir(fullname):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
146 folders.append(name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
147 folders.sort()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
148 return folders
3632
44161fb04b7e added setcontext and parsesequence; several small fixes
Guido van Rossum <guido@python.org>
parents: 2246
diff changeset
149
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
150 # Return the names of the subfolders in a given folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
151 # (prefixed with the given folder name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
152 def listsubfolders(self, name):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
153 fullname = os.path.join(self.path, name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
154 # Get the link count so we can avoid listing folders
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
155 # that have no subfolders.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
156 st = os.stat(fullname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
157 nlinks = st[ST_NLINK]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
158 if nlinks <= 2:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
159 return []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
160 subfolders = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
161 subnames = os.listdir(fullname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
162 for subname in subnames:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
163 fullsubname = os.path.join(fullname, subname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
164 if os.path.isdir(fullsubname):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
165 name_subname = os.path.join(name, subname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
166 subfolders.append(name_subname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
167 # Stop looking for subfolders when
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
168 # we've seen them all
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
169 nlinks = nlinks - 1
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 if nlinks <= 2:
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
171 break
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
172 subfolders.sort()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
173 return subfolders
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
174
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
175 # Return the names of all folders, including subfolders, recursively
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
176 def listallfolders(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
177 return self.listallsubfolders('')
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
179 # Return the names of subfolders in a given folder, recursively
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
180 def listallsubfolders(self, name):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
181 fullname = os.path.join(self.path, name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
182 # Get the link count so we can avoid listing folders
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
183 # that have no subfolders.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
184 st = os.stat(fullname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
185 nlinks = st[ST_NLINK]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
186 if nlinks <= 2:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
187 return []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
188 subfolders = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
189 subnames = os.listdir(fullname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
190 for subname in subnames:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
191 if subname[0] == ',' or isnumeric(subname): continue
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
192 fullsubname = os.path.join(fullname, subname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
193 if os.path.isdir(fullsubname):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
194 name_subname = os.path.join(name, subname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
195 subfolders.append(name_subname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
196 if not os.path.islink(fullsubname):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
197 subsubfolders = self.listallsubfolders(
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
198 name_subname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
199 subfolders = subfolders + subsubfolders
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
200 # Stop looking for subfolders when
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
201 # we've seen them all
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
202 nlinks = nlinks - 1
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 if nlinks <= 2:
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
204 break
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
205 subfolders.sort()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
206 return subfolders
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
207
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
208 # Return a new Folder object for the named folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
209 def openfolder(self, name):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
210 return Folder(self, name)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
212 # Create a new folder. This raises os.error if the folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
213 # cannot be created
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
214 def makefolder(self, name):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
215 protect = pickline(self.profile, 'Folder-Protect')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
216 if protect and isnumeric(protect):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
217 mode = string.atoi(protect, 8)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
218 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
219 mode = FOLDER_PROTECT
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
220 os.mkdir(os.path.join(self.getpath(), name), mode)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
222 # Delete a folder. This removes files in the folder but not
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
223 # subdirectories. If deleting the folder itself fails it
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
224 # raises os.error
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
225 def deletefolder(self, name):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
226 fullname = os.path.join(self.getpath(), name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
227 for subname in os.listdir(fullname):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
228 fullsubname = os.path.join(fullname, subname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
229 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
230 os.unlink(fullsubname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
231 except os.error:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
232 self.error('%s not deleted, continuing...' %
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
233 fullsubname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
234 os.rmdir(fullname)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 # Class representing a particular folder
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
5228
59e8a02a4774 Optimized the hell out of listmessages().
Guido van Rossum <guido@python.org>
parents: 4541
diff changeset
239 numericprog = regex.compile('^[1-9][0-9]*$')
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 def isnumeric(str):
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
241 return numericprog.match(str) >= 0
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 class Folder:
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
245 # Constructor
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
246 def __init__(self, mh, name):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
247 self.mh = mh
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
248 self.name = name
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
249 if not os.path.isdir(self.getfullname()):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
250 raise Error, 'no folder %s' % name
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
251
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
252 # String representation
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
253 def __repr__(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
254 return 'Folder(%s, %s)' % (`self.mh`, `self.name`)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
256 # Error message handler
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
257 def error(self, *args):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
258 apply(self.mh.error, args)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
260 # Return the full pathname of the folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
261 def getfullname(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
262 return os.path.join(self.mh.path, self.name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
263
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
264 # Return the full pathname of the folder's sequences file
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
265 def getsequencesfilename(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
266 return os.path.join(self.getfullname(), MH_SEQUENCES)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
268 # Return the full pathname of a message in the folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
269 def getmessagefilename(self, n):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
270 return os.path.join(self.getfullname(), str(n))
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
272 # Return list of direct subfolders
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
273 def listsubfolders(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
274 return self.mh.listsubfolders(self.name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
275
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
276 # Return list of all subfolders
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
277 def listallsubfolders(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
278 return self.mh.listallsubfolders(self.name)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
279
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
280 # Return the list of messages currently present in the folder.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
281 # As a side effect, set self.last to the last message (or 0)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
282 def listmessages(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
283 messages = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
284 match = numericprog.match
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
285 append = messages.append
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
286 for name in os.listdir(self.getfullname()):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
287 if match(name) >= 0:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
288 append(name)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
289 messages = map(string.atoi, messages)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
290 messages.sort()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
291 if messages:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
292 self.last = messages[-1]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
293 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
294 self.last = 0
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
295 return messages
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
297 # Return the set of sequences for the folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
298 def getsequences(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
299 sequences = {}
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
300 fullname = self.getsequencesfilename()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
301 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
302 f = open(fullname, 'r')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
303 except IOError:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
304 return sequences
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
305 while 1:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
306 line = f.readline()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
307 if not line: break
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
308 fields = string.splitfields(line, ':')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
309 if len(fields) <> 2:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
310 self.error('bad sequence in %s: %s' %
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
311 (fullname, string.strip(line)))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
312 key = string.strip(fields[0])
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
313 value = IntSet(string.strip(fields[1]), ' ').tolist()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
314 sequences[key] = value
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
315 return sequences
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
316
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
317 # Write the set of sequences back to the folder
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
318 def putsequences(self, sequences):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
319 fullname = self.getsequencesfilename()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
320 f = None
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
321 for key in sequences.keys():
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
322 s = IntSet('', ' ')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
323 s.fromlist(sequences[key])
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
324 if not f: f = open(fullname, 'w')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
325 f.write('%s: %s\n' % (key, s.tostring()))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
326 if not f:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
327 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
328 os.unlink(fullname)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
329 except os.error:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
330 pass
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
331 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
332 f.close()
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
334 # Return the current message. Raise KeyError when there is none
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
335 def getcurrent(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
336 seqs = self.getsequences()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
337 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
338 return max(seqs['cur'])
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
339 except (ValueError, KeyError):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
340 raise Error, "no cur message"
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
341
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
342 # Set the current message
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
343 def setcurrent(self, n):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
344 updateline(self.getsequencesfilename(), 'cur', str(n), 0)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
346 # Parse an MH sequence specification into a message list.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
347 # Attempt to mimic mh-sequence(5) as close as possible.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
348 # Also attempt to mimic observed behavior regarding which
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
349 # conditions cause which error messages
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
350 def parsesequence(self, seq):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
351 # XXX Still not complete (see mh-format(5)).
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
352 # Missing are:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
353 # - 'prev', 'next' as count
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
354 # - Sequence-Negation option
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
355 all = self.listmessages()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
356 # Observed behavior: test for empty folder is done first
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
357 if not all:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
358 raise Error, "no messages in %s" % self.name
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
359 # Common case first: all is frequently the default
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
360 if seq == 'all':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
361 return all
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
362 # Test for X:Y before X-Y because 'seq:-n' matches both
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
363 i = string.find(seq, ':')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
364 if i >= 0:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
365 head, dir, tail = seq[:i], '', seq[i+1:]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
366 if tail[:1] in '-+':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
367 dir, tail = tail[:1], tail[1:]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
368 if not isnumeric(tail):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
369 raise Error, "bad message list %s" % seq
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
370 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
371 count = string.atoi(tail)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
372 except (ValueError, OverflowError):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
373 # Can't use sys.maxint because of i+count below
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
374 count = len(all)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
375 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
376 anchor = self._parseindex(head, all)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
377 except Error, msg:
5331
6232acba7eb2 Rewrite parsesequence() to emulate MH without invoking pick.
Guido van Rossum <guido@python.org>
parents: 5228
diff changeset
378 seqs = self.getsequences()
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
379 if not seqs.has_key(head):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
380 if not msg:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
381 msg = "bad message list %s" % seq
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
382 raise Error, msg, sys.exc_traceback
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
383 msgs = seqs[head]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
384 if not msgs:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
385 raise Error, "sequence %s empty" % head
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
386 if dir == '-':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
387 return msgs[-count:]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
388 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
389 return msgs[:count]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
390 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
391 if not dir:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
392 if head in ('prev', 'last'):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
393 dir = '-'
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
394 if dir == '-':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
395 i = bisect(all, anchor)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
396 return all[max(0, i-count):i]
5331
6232acba7eb2 Rewrite parsesequence() to emulate MH without invoking pick.
Guido van Rossum <guido@python.org>
parents: 5228
diff changeset
397 else:
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
398 i = bisect(all, anchor-1)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
399 return all[i:i+count]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
400 # Test for X-Y next
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
401 i = string.find(seq, '-')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
402 if i >= 0:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
403 begin = self._parseindex(seq[:i], all)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
404 end = self._parseindex(seq[i+1:], all)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
405 i = bisect(all, begin-1)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
406 j = bisect(all, end)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
407 r = all[i:j]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
408 if not r:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
409 raise Error, "bad message list %s" % seq
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
410 return r
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
411 # Neither X:Y nor X-Y; must be a number or a (pseudo-)sequence
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
412 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
413 n = self._parseindex(seq, all)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
414 except Error, msg:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
415 seqs = self.getsequences()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
416 if not seqs.has_key(seq):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
417 if not msg:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
418 msg = "bad message list %s" % seq
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
419 raise Error, msg
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
420 return seqs[seq]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
421 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
422 if n not in all:
5331
6232acba7eb2 Rewrite parsesequence() to emulate MH without invoking pick.
Guido van Rossum <guido@python.org>
parents: 5228
diff changeset
423 if isnumeric(seq):
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
424 raise Error, "message %d doesn't exist" % n
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
425 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
426 raise Error, "no %s message" % seq
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
427 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
428 return [n]
3632
44161fb04b7e added setcontext and parsesequence; several small fixes
Guido van Rossum <guido@python.org>
parents: 2246
diff changeset
429
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
430 # Internal: parse a message number (or cur, first, etc.)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
431 def _parseindex(self, seq, all):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
432 if isnumeric(seq):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
433 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
434 return string.atoi(seq)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
435 except (OverflowError, ValueError):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
436 return sys.maxint
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
437 if seq in ('cur', '.'):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
438 return self.getcurrent()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
439 if seq == 'first':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
440 return all[0]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
441 if seq == 'last':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
442 return all[-1]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
443 if seq == 'next':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
444 n = self.getcurrent()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
445 i = bisect(all, n)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
446 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
447 return all[i]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
448 except IndexError:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
449 raise Error, "no next message"
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
450 if seq == 'prev':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
451 n = self.getcurrent()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
452 i = bisect(all, n-1)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
453 if i == 0:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
454 raise Error, "no prev message"
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
455 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
456 return all[i-1]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
457 except IndexError:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
458 raise Error, "no prev message"
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
459 raise Error, None
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
460
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
461 # Open a message -- returns a Message object
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
462 def openmessage(self, n):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
463 return Message(self, n)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
464
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
465 # Remove one or more messages -- may raise os.error
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
466 def removemessages(self, list):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
467 errors = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
468 deleted = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
469 for n in list:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
470 path = self.getmessagefilename(n)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
471 commapath = self.getmessagefilename(',' + str(n))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
472 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
473 os.unlink(commapath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
474 except os.error:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
475 pass
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
476 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
477 os.rename(path, commapath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
478 except os.error, msg:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
479 errors.append(msg)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
480 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
481 deleted.append(n)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
482 if deleted:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
483 self.removefromallsequences(deleted)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
484 if errors:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
485 if len(errors) == 1:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
486 raise os.error, errors[0]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
487 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
488 raise os.error, ('multiple errors:', errors)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
489
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
490 # Refile one or more messages -- may raise os.error.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
491 # 'tofolder' is an open folder object
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
492 def refilemessages(self, list, tofolder, keepsequences=0):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
493 errors = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
494 refiled = {}
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
495 for n in list:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
496 ton = tofolder.getlast() + 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
497 path = self.getmessagefilename(n)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
498 topath = tofolder.getmessagefilename(ton)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
499 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
500 os.rename(path, topath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
501 except os.error:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
502 # Try copying
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
503 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
504 shutil.copy2(path, topath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
505 os.unlink(path)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
506 except (IOError, os.error), msg:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
507 errors.append(msg)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
508 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
509 os.unlink(topath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
510 except os.error:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
511 pass
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
512 continue
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
513 tofolder.setlast(ton)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
514 refiled[n] = ton
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
515 if refiled:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
516 if keepsequences:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
517 tofolder._copysequences(self, refiled.items())
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
518 self.removefromallsequences(refiled.keys())
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
519 if errors:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
520 if len(errors) == 1:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
521 raise os.error, errors[0]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
522 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
523 raise os.error, ('multiple errors:', errors)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
524
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
525 # Helper for refilemessages() to copy sequences
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
526 def _copysequences(self, fromfolder, refileditems):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
527 fromsequences = fromfolder.getsequences()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
528 tosequences = self.getsequences()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
529 changed = 0
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
530 for name, seq in fromsequences.items():
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
531 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
532 toseq = tosequences[name]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
533 new = 0
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
534 except:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
535 toseq = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
536 new = 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
537 for fromn, ton in refileditems:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
538 if fromn in seq:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
539 toseq.append(ton)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
540 changed = 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
541 if new and toseq:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
542 tosequences[name] = toseq
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
543 if changed:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
544 self.putsequences(tosequences)
3755
6598a518668f Added option to refilemessages() to keep sequences
Guido van Rossum <guido@python.org>
parents: 3632
diff changeset
545
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
546 # Move one message over a specific destination message,
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
547 # which may or may not already exist.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
548 def movemessage(self, n, tofolder, ton):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
549 path = self.getmessagefilename(n)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
550 # Open it to check that it exists
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
551 f = open(path)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
552 f.close()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
553 del f
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
554 topath = tofolder.getmessagefilename(ton)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
555 backuptopath = tofolder.getmessagefilename(',%d' % ton)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
556 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
557 os.rename(topath, backuptopath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
558 except os.error:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
559 pass
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
560 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
561 os.rename(path, topath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
562 except os.error:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
563 # Try copying
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
564 ok = 0
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
565 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
566 tofolder.setlast(None)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
567 shutil.copy2(path, topath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
568 ok = 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
569 finally:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
570 if not ok:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
571 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
572 os.unlink(topath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
573 except os.error:
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
574 pass
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
575 os.unlink(path)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
576 self.removefromallsequences([n])
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
577
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
578 # Copy one message over a specific destination message,
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
579 # which may or may not already exist.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
580 def copymessage(self, n, tofolder, ton):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
581 path = self.getmessagefilename(n)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
582 # Open it to check that it exists
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
583 f = open(path)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
584 f.close()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
585 del f
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
586 topath = tofolder.getmessagefilename(ton)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
587 backuptopath = tofolder.getmessagefilename(',%d' % ton)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
588 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
589 os.rename(topath, backuptopath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
590 except os.error:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
591 pass
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
592 ok = 0
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
593 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
594 tofolder.setlast(None)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
595 shutil.copy2(path, topath)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
596 ok = 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
597 finally:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
598 if not ok:
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
599 try:
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
600 os.unlink(topath)
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
601 except os.error:
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
602 pass
2050
19363f8570a2 * Lib/mhlib.py: added movemessage(), copymessage(), added copy
Guido van Rossum <guido@python.org>
parents: 1872
diff changeset
603
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
604 # Remove one or more messages from all sequeuces (including last)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
605 # -- but not from 'cur'!!!
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
606 def removefromallsequences(self, list):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
607 if hasattr(self, 'last') and self.last in list:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
608 del self.last
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
609 sequences = self.getsequences()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
610 changed = 0
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
611 for name, seq in sequences.items():
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
612 if name == 'cur':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
613 continue
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
614 for n in list:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
615 if n in seq:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
616 seq.remove(n)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
617 changed = 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
618 if not seq:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
619 del sequences[name]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
620 if changed:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
621 self.putsequences(sequences)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
622
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
623 # Return the last message number
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
624 def getlast(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
625 if not hasattr(self, 'last'):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
626 messages = self.listmessages()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
627 return self.last
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
628
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
629 # Set the last message number
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
630 def setlast(self, last):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
631 if last is None:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
632 if hasattr(self, 'last'):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
633 del self.last
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
634 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
635 self.last = last
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
636
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 class Message(mimetools.Message):
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
638
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
639 # Constructor
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
640 def __init__(self, f, n, fp = None):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
641 self.folder = f
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
642 self.number = n
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
643 if not fp:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
644 path = f.getmessagefilename(n)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
645 fp = open(path, 'r')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
646 mimetools.Message.__init__(self, fp)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
647
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
648 # String representation
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
649 def __repr__(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
650 return 'Message(%s, %s)' % (repr(self.folder), self.number)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
651
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
652 # Return the message's header text as a string. If an
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
653 # argument is specified, it is used as a filter predicate to
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
654 # decide which headers to return (its argument is the header
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
655 # name converted to lower case).
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
656 def getheadertext(self, pred = None):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
657 if not pred:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
658 return string.joinfields(self.headers, '')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
659 headers = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
660 hit = 0
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
661 for line in self.headers:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
662 if line[0] not in string.whitespace:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
663 i = string.find(line, ':')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
664 if i > 0:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
665 hit = pred(string.lower(line[:i]))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
666 if hit: headers.append(line)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
667 return string.joinfields(headers, '')
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
668
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
669 # Return the message's body text as string. This undoes a
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
670 # Content-Transfer-Encoding, but does not interpret other MIME
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
671 # features (e.g. multipart messages). To suppress to
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
672 # decoding, pass a 0 as argument
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
673 def getbodytext(self, decode = 1):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
674 self.fp.seek(self.startofbody)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
675 encoding = self.getencoding()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
676 if not decode or encoding in ('7bit', '8bit', 'binary'):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
677 return self.fp.read()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
678 from StringIO import StringIO
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
679 output = StringIO()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
680 mimetools.decode(self.fp, output, encoding)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
681 return output.getvalue()
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
682
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
683 # Only for multipart messages: return the message's body as a
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
684 # list of SubMessage objects. Each submessage object behaves
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
685 # (almost) as a Message object.
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
686 def getbodyparts(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
687 if self.getmaintype() != 'multipart':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
688 raise Error, 'Content-Type is not multipart/*'
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
689 bdry = self.getparam('boundary')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
690 if not bdry:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
691 raise Error, 'multipart/* without boundary param'
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
692 self.fp.seek(self.startofbody)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
693 mf = multifile.MultiFile(self.fp)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
694 mf.push(bdry)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
695 parts = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
696 while mf.next():
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
697 n = str(self.number) + '.' + `1 + len(parts)`
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
698 part = SubMessage(self.folder, n, mf)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
699 parts.append(part)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
700 mf.pop()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
701 return parts
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
702
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
703 # Return body, either a string or a list of messages
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
704 def getbody(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
705 if self.getmaintype() == 'multipart':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
706 return self.getbodyparts()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
707 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
708 return self.getbodytext()
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
709
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
710
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 class SubMessage(Message):
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
712
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
713 # Constructor
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
714 def __init__(self, f, n, fp):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
715 Message.__init__(self, f, n, fp)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
716 if self.getmaintype() == 'multipart':
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
717 self.body = Message.getbodyparts(self)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
718 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
719 self.body = Message.getbodytext(self)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
720 # XXX If this is big, should remember file pointers
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
721
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
722 # String representation
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
723 def __repr__(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
724 f, n, fp = self.folder, self.number, self.fp
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
725 return 'SubMessage(%s, %s, %s)' % (f, n, fp)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
726
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
727 def getbodytext(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
728 if type(self.body) == type(''):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
729 return self.body
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
730
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
731 def getbodyparts(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
732 if type(self.body) == type([]):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
733 return self.body
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
734
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
735 def getbody(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
736 return self.body
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
737
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
738
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 # Class implementing sets of integers.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
741 # This is an efficient representation for sets consisting of several
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
742 # continuous ranges, e.g. 1-100,200-400,402-1000 is represented
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 # internally as a list of three pairs: [(1,100), (200,400),
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 # (402,1000)]. The internal representation is always kept normalized.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
745 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 # The constructor has up to three arguments:
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 # - the string used to initialize the set (default ''),
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 # - the separator between ranges (default ',')
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 # - the separator between begin and end of a range (default '-')
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 # The separators may be regular expressions and should be different.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 # The tostring() function yields a string that can be passed to another
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 # IntSet constructor; __repr__() is a valid IntSet constructor itself.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 # XXX The default begin/end separator means that negative numbers are
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 # not supported very well.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 #
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 # XXX There are currently no operations to remove set elements.
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
759
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 class IntSet:
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
761
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
762 def __init__(self, data = None, sep = ',', rng = '-'):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
763 self.pairs = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
764 self.sep = sep
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
765 self.rng = rng
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
766 if data: self.fromstring(data)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
767
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
768 def reset(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
769 self.pairs = []
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
770
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
771 def __cmp__(self, other):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
772 return cmp(self.pairs, other.pairs)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
773
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
774 def __hash__(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
775 return hash(self.pairs)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
776
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
777 def __repr__(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
778 return 'IntSet(%s, %s, %s)' % (`self.tostring()`,
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
779 `self.sep`, `self.rng`)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
780
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
781 def normalize(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
782 self.pairs.sort()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
783 i = 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
784 while i < len(self.pairs):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
785 alo, ahi = self.pairs[i-1]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
786 blo, bhi = self.pairs[i]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
787 if ahi >= blo-1:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
788 self.pairs[i-1:i+1] = [(alo, max(ahi, bhi))]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
789 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
790 i = i+1
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
791
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
792 def tostring(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
793 s = ''
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
794 for lo, hi in self.pairs:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
795 if lo == hi: t = `lo`
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
796 else: t = `lo` + self.rng + `hi`
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
797 if s: s = s + (self.sep + t)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
798 else: s = t
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
799 return s
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
800
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
801 def tolist(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
802 l = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
803 for lo, hi in self.pairs:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
804 m = range(lo, hi+1)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
805 l = l + m
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
806 return l
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
807
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
808 def fromlist(self, list):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
809 for i in list:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
810 self.append(i)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
811
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
812 def clone(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
813 new = IntSet()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
814 new.pairs = self.pairs[:]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
815 return new
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
816
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
817 def min(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
818 return self.pairs[0][0]
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
819
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
820 def max(self):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
821 return self.pairs[-1][-1]
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
822
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
823 def contains(self, x):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
824 for lo, hi in self.pairs:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
825 if lo <= x <= hi: return 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
826 return 0
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
827
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
828 def append(self, x):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
829 for i in range(len(self.pairs)):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
830 lo, hi = self.pairs[i]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
831 if x < lo: # Need to insert before
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
832 if x+1 == lo:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
833 self.pairs[i] = (x, hi)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
834 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
835 self.pairs.insert(i, (x, x))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
836 if i > 0 and x-1 == self.pairs[i-1][1]:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
837 # Merge with previous
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
838 self.pairs[i-1:i+1] = [
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
839 (self.pairs[i-1][0],
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
840 self.pairs[i][1])
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
841 ]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
842 return
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
843 if x <= hi: # Already in set
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
844 return
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
845 i = len(self.pairs) - 1
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
846 if i >= 0:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
847 lo, hi = self.pairs[i]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
848 if x-1 == hi:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
849 self.pairs[i] = lo, x
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
850 return
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
851 self.pairs.append((x, x))
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
852
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
853 def addpair(self, xlo, xhi):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
854 if xlo > xhi: return
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
855 self.pairs.append((xlo, xhi))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
856 self.normalize()
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
857
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
858 def fromstring(self, data):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
859 import string, regsub
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
860 new = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
861 for part in regsub.split(data, self.sep):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
862 list = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
863 for subp in regsub.split(part, self.rng):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
864 s = string.strip(subp)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
865 list.append(string.atoi(s))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
866 if len(list) == 1:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
867 new.append((list[0], list[0]))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
868 elif len(list) == 2 and list[0] <= list[1]:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
869 new.append((list[0], list[1]))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
870 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
871 raise ValueError, 'bad data passed to IntSet'
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
872 self.pairs = self.pairs + new
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
873 self.normalize()
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
874
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
875
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 # Subroutines to read/write entries in .mh_profile and .mh_sequences
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
877
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 def pickline(file, key, casefold = 1):
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
879 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
880 f = open(file, 'r')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
881 except IOError:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
882 return None
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
883 pat = key + ':'
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
884 if casefold:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
885 prog = regex.compile(pat, regex.casefold)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
886 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
887 prog = regex.compile(pat)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
888 while 1:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
889 line = f.readline()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
890 if not line: break
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
891 if prog.match(line) >= 0:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
892 text = line[len(key)+1:]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
893 while 1:
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 line = f.readline()
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
895 if not line or line[0] not in string.whitespace:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
896 break
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
897 text = text + line
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
898 return string.strip(text)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
899 return None
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
900
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
901 def updateline(file, key, value, casefold = 1):
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
902 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
903 f = open(file, 'r')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
904 lines = f.readlines()
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
905 f.close()
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
906 except IOError:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
907 lines = []
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
908 pat = key + ':\(.*\)\n'
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
909 if casefold:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
910 prog = regex.compile(pat, regex.casefold)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
911 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
912 prog = regex.compile(pat)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
913 if value is None:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
914 newline = None
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
915 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
916 newline = '%s: %s\n' % (key, value)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
917 for i in range(len(lines)):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
918 line = lines[i]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
919 if prog.match(line) == len(line):
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
920 if newline is None:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
921 del lines[i]
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
922 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
923 lines[i] = newline
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
924 break
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
925 else:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
926 if newline is not None:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
927 lines.append(newline)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
928 tempfile = file + "~"
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
929 f = open(tempfile, 'w')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
930 for line in lines:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
931 f.write(line)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
932 f.close()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
933 os.rename(tempfile, file)
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
934
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
935
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
936 # Test program
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
937
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
938 def test():
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
939 global mh, f
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
940 os.system('rm -rf $HOME/Mail/@test')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
941 mh = MH()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
942 def do(s): print s; print eval(s)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
943 do('mh.listfolders()')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
944 do('mh.listallfolders()')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
945 testfolders = ['@test', '@test/test1', '@test/test2',
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
946 '@test/test1/test11', '@test/test1/test12',
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
947 '@test/test1/test11/test111']
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
948 for t in testfolders: do('mh.makefolder(%s)' % `t`)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
949 do('mh.listsubfolders(\'@test\')')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
950 do('mh.listallsubfolders(\'@test\')')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
951 f = mh.openfolder('@test')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
952 do('f.listsubfolders()')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
953 do('f.listallsubfolders()')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
954 do('f.getsequences()')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
955 seqs = f.getsequences()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
956 seqs['foo'] = IntSet('1-10 12-20', ' ').tolist()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
957 print seqs
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
958 f.putsequences(seqs)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
959 do('f.getsequences()')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
960 testfolders.reverse()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
961 for t in testfolders: do('mh.deletefolder(%s)' % `t`)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
962 do('mh.getcontext()')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
963 context = mh.getcontext()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
964 f = mh.openfolder(context)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
965 do('f.getcurrent()')
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
966 for seq in ['first', 'last', 'cur', '.', 'prev', 'next',
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
967 'first:3', 'last:3', 'cur:3', 'cur:-3',
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
968 'prev:3', 'next:3',
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
969 '1:3', '1:-3', '100:3', '100:-3', '10000:3', '10000:-3',
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
970 'all']:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
971 try:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
972 do('f.parsesequence(%s)' % `seq`)
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
973 except Error, msg:
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
974 print "Error:", msg
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
975 stuff = os.popen("pick %s 2>/dev/null" % `seq`).read()
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
976 list = map(string.atoi, string.split(stuff))
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
977 print list, "<-- pick"
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
978 do('f.listmessages()')
1681
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
979
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
980
dc8e1d72d78d mhlib.py: new interface to MH folders and messages
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 if __name__ == '__main__':
5332
8dfc252963c7 Reindented with 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5331
diff changeset
982 test()