]> code.delx.au - offlineimap/blob - offlineimap/ui/Machine.py
Remove python-tk suggestion
[offlineimap] / offlineimap / ui / Machine.py
1 # Copyright (C) 2007 John Goerzen
2 # <jgoerzen@complete.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18 import offlineimap.version
19 import urllib, sys, re, time, traceback, threading, thread
20 from UIBase import UIBase
21 from threading import *
22
23 protocol = '6.0.0'
24
25 class MachineUI(UIBase):
26 def __init__(s, config, verbose = 0):
27 UIBase.__init__(s, config, verbose)
28 s.safechars=" ;,./-_=+()[]"
29 s.iswaiting = 0
30 s.outputlock = Lock()
31 s._printData('__init__', protocol)
32
33 def isusable(s):
34 return True
35
36 def _printData(s, command, data, dolock = True):
37 s._printDataOut('msg', command, data, dolock)
38
39 def _printWarn(s, command, data, dolock = True):
40 s._printDataOut('warn', command, data, dolock)
41
42 def _printDataOut(s, datatype, command, data, dolock = True):
43 if dolock:
44 s.outputlock.acquire()
45 try:
46 print "%s:%s:%s:%s" % \
47 (datatype,
48 urllib.quote(command, s.safechars),
49 urllib.quote(currentThread().getName(), s.safechars),
50 urllib.quote(data, s.safechars))
51 sys.stdout.flush()
52 finally:
53 if dolock:
54 s.outputlock.release()
55
56 def _display(s, msg):
57 s._printData('_display', msg)
58
59 def warn(s, msg, minor):
60 s._printData('warn', '%s\n%d' % (msg, int(minor)))
61
62 def registerthread(s, account):
63 UIBase.registerthread(s, account)
64 s._printData('registerthread', account)
65
66 def unregisterthread(s, thread):
67 UIBase.unregisterthread(s, thread)
68 s._printData('unregisterthread', thread.getName())
69
70 def debugging(s, debugtype):
71 s._printData('debugging', debugtype)
72
73 def acct(s, accountname):
74 s._printData('acct', accountname)
75
76 def acctdone(s, accountname):
77 s._printData('acctdone', accountname)
78
79 def validityproblem(s, folder):
80 s._printData('validityproblem', "%s\n%s\n%s\n%s" % \
81 (folder.getname(), folder.getrepository().getname(),
82 folder.getsaveduidvalidity(), folder.getuidvalidity()))
83
84 def connecting(s, hostname, port):
85 s._printData('connecting', "%s\n%s" % (hostname, str(port)))
86
87 def syncfolders(s, srcrepos, destrepos):
88 s._printData('syncfolders', "%s\n%s" % (s.getnicename(srcrepos),
89 s.getnicename(destrepos)))
90
91 def syncingfolder(s, srcrepos, srcfolder, destrepos, destfolder):
92 s._printData('syncingfolder', "%s\n%s\n%s\n%s\n" % \
93 (s.getnicename(srcrepos), srcfolder.getname(),
94 s.getnicename(destrepos), destfolder.getname()))
95
96 def loadmessagelist(s, repos, folder):
97 s._printData('loadmessagelist', "%s\n%s" % (s.getnicename(repos),
98 folder.getvisiblename()))
99
100 def messagelistloaded(s, repos, folder, count):
101 s._printData('messagelistloaded', "%s\n%s\n%d" % \
102 (s.getnicename(repos), folder.getname(), count))
103
104 def syncingmessages(s, sr, sf, dr, df):
105 s._printData('syncingmessages', "%s\n%s\n%s\n%s\n" % \
106 (s.getnicename(sr), sf.getname(), s.getnicename(dr),
107 df.getname()))
108
109 def copyingmessage(s, uid, src, destlist):
110 ds = s.folderlist(destlist)
111 s._printData('copyingmessage', "%d\n%s\n%s\n%s" % \
112 (uid, s.getnicename(src), src.getname(), ds))
113
114 def folderlist(s, list):
115 return ("\f".join(["%s\t%s" % (s.getnicename(x), x.getname()) for x in list]))
116
117 def deletingmessage(s, uid, destlist):
118 s.deletingmessages(s, [uid], destlist)
119
120 def uidlist(s, list):
121 return ("\f".join([str(u) for u in list]))
122
123 def deletingmessages(s, uidlist, destlist):
124 ds = s.folderlist(destlist)
125 s._printData('deletingmessages', "%s\n%s" % (s.uidlist(uidlist), ds))
126
127 def addingflags(s, uidlist, flags, destlist):
128 ds = s.folderlist(destlist)
129 s._printData("addingflags", "%s\n%s\n%s" % (s.uidlist(uidlist),
130 "\f".join(flags),
131 ds))
132
133 def deletingflags(s, uidlist, flags, destlist):
134 ds = s.folderlist(destlist)
135 s._printData('deletingflags', "%s\n%s\n%s" % (s.uidlist(uidlist),
136 "\f".join(flags),
137 ds))
138
139 def threadException(s, thread):
140 print s.getThreadExceptionString(thread)
141 s._printData('threadException', "%s\n%s" % \
142 (thread.getName(), s.getThreadExceptionString(thread)))
143 s.delThreadDebugLog(thread)
144 s.terminate(100)
145
146 def terminate(s, exitstatus = 0, errortitle = '', errormsg = ''):
147 s._printData('terminate', "%d\n%s\n%s" % (exitstatus, errortitle, errormsg))
148 sys.exit(exitstatus)
149
150 def mainException(s):
151 s._printData('mainException', s.getMainExceptionString())
152
153 def threadExited(s, thread):
154 s._printData('threadExited', thread.getName())
155 UIBase.threadExited(s, thread)
156
157 def sleeping(s, sleepsecs, remainingsecs):
158 s._printData('sleeping', "%d\n%d" % (sleepsecs, remainingsecs))
159 if sleepsecs > 0:
160 time.sleep(sleepsecs)
161 return 0
162
163
164 def getpass(s, accountname, config, errmsg = None):
165 s.outputlock.acquire()
166 try:
167 if errmsg:
168 s._printData('getpasserror', "%s\n%s" % (accountname, errmsg),
169 False)
170 s._printData('getpass', accountname, False)
171 return (sys.stdin.readline()[:-1])
172 finally:
173 s.outputlock.release()
174
175 def init_banner(s):
176 s._printData('initbanner', offlineimap.version.banner)
177