-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutag.py
executable file
·297 lines (207 loc) · 10.1 KB
/
mutag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# mutag - A tagging tool for mails indexed by mu
# Copyright 2012 Abdó Roig-Maranges <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
from optparse import OptionParser, OptionGroup
from configparser import RawConfigParser
import mutag.archui as ui
from mutag.mutag import Mutag, MutagError
from mutag import __version__
def get_config_path(conf, name, key, default=None):
if conf.has_option('profile %s' % name, key):
return os.path.expanduser(os.path.expandvars(conf.get('profile %s' % name, key)))
else:
return default
def get_config_string(conf, name, key, default=None):
if conf.has_option('profile %s' % name, key):
return conf.get('profile %s' % name, key)
else:
return default
def get_config_int(conf, name, key, default=0):
if conf.has_option('profile %s' % name, key):
return int(conf.get('profile %s' % name, key))
else:
return default
def get_profile(conf, opts):
if opts.profile: name = opts.profile
else: name = conf.get('mutag', 'defaultprofile')
# TODO: catch nonexistent profile
prof = {}
prof['muhome'] = get_config_path(conf, name, 'muhome')
prof['maildir'] = get_config_path(conf, name, 'maildir')
prof['queuedir'] = get_config_path(conf, name, 'queuedir')
prof['trashtag'] = get_config_string(conf, name, 'trashtag')
prof['trashfolder'] = get_config_string(conf, name, 'trashfolder')
gmailfolders = get_config_string(conf, name, 'gmailfolders')
if gmailfolders:
prof['gmailfolders'] = set([f.strip() for f in gmailfolders.split(',')])
else:
prof['gmailfolders'] = set([])
prof['expiredays'] = get_config_int(conf, name, 'expiredays', 100)
prof['mtimelist'] = get_config_path(conf, name, 'mtimelist')
prof['lastmtime'] = get_config_path(conf, name, 'lastmtime')
prof['tagrules'] = get_config_path(conf, name, 'tagrules')
if opts.muhome: prof['muhome'] = os.path.expanduser(opts.muhome)
if opts.muhome: prof['maildir'] = os.path.expanduser(opts.maildir)
return prof
def eval_command(opts, args):
conf = RawConfigParser(defaults={})
conf.read([os.path.expanduser('~/.config/mutag/mutag.conf')])
ui.set_debug(opts.debug)
ui.use_color(conf.getboolean("mutag", 'color'))
# If the output is not a terminal, remove the colors
if not sys.stdout.isatty(): ui.use_color(False)
prof = get_profile(conf, opts)
mutag = Mutag(prof = prof)
# escape '\' in query so xapian understands us.
if opts.query:
opts.query = opts.query.replace('\\', '\\\\')
if opts.cmd == 'autotag':
mutag.autotag(query=opts.query, path=opts.path, modified_only=opts.modified, related=True, dryrun=opts.dryrun, silent=opts.silent)
elif opts.cmd == 'expire':
mutag.expire(dryrun=opts.dryrun, silent=opts.silent)
elif opts.cmd in set(['autotag', 'expire']) and opts.index:
mutag.index(dryrun=opts.dryrun, silent=opts.silent)
elif opts.cmd == 'count':
num = mutag.count(opts.query, modified_only=opts.modified)
print(num)
elif opts.cmd == 'dedup':
# TODO
print("dedup not implemented")
sys.exit()
elif opts.cmd == 'tag':
L = mutag.query(opts.query, path = opts.path,
modified_only=opts.modified, related=False)
mutag.change_tags(L, args, dryrun=opts.dryrun, silent=opts.silent)
if opts.index:
mutag.index(dryrun=opts.dryrun, silent=opts.silent)
elif opts.cmd == 'flag':
L = mutag.query(opts.query, path = opts.path,
modified_only=opts.modified, related=False)
mutag.change_flags(L, args, dryrun=opts.dryrun, silent=opts.silent)
if opts.index:
mutag.index(dryrun=opts.dryrun, silent=opts.silent)
elif opts.cmd == 'list':
L = mutag.query(opts.query, path = opts.path,
modified_only=opts.modified, related=False)
for msg in L:
ui.print_color(msg.tostring(fmt=opts.format))
elif opts.cmd == 'queue':
L = mutag.queue()
for msg in L:
ui.print_color(msg.tostring(fmt=opts.format, outbound=True))
elif opts.cmd == 'print':
L = mutag.query(opts.query, path = opts.path,
modified_only=opts.modified, related=False)
for msg in L:
print(msg.raw())
elif opts.cmd == 'filename':
L = mutag.query(opts.query, path = opts.path,
modified_only=opts.modified, related=False)
for msg in L:
print(msg['path'])
elif opts.cmd == 'rebuild':
mutag.rebuild(dryrun=opts.dryrun, silent=opts.silent)
elif opts.cmd == 'trash':
mutag.empty_trash(dryrun=opts.dryrun, silent=opts.silent)
# Index if asked to and not done in a specific command
if opts.index and not opts.cmd in ['autotag', 'tag', 'rebuild']:
mutag.index(dryrun=opts.dryrun, silent=opts.silent)
# Update mtime
if opts.update:
mutag.update_mtime(dryrun=opts.dryrun, silent=opts.silent)
# commit mail
if opts.commit:
mutag.commit(dryrun=opts.dryrun, silent=opts.silent)
# Main stuff
# -----------------------
usage = """usage: %prog [options] [-q <query>] <tags>
"""
parser = OptionParser(usage=usage)
# Commands
parser.add_option("-C", "--count", action="store_const", const="count", default=None, dest="cmd",
help="Count messages")
parser.add_option("-A", "--autotag", action="store_const", const="autotag", default=None, dest="cmd",
help="Tag rule to apply")
parser.add_option("-E", "--expire", action="store_const", const="expire", default=None, dest="cmd",
help="Expire old messages")
parser.add_option("-D", "--dedup", action="store_const", const="dedup", default=None, dest="cmd",
help="Remove duplicate message with same uid content on same folder")
parser.add_option("-T", "--tag", action="store_const", const="tag", default=None, dest="cmd",
help="Change tags")
parser.add_option("-G", "--flag", action="store_const", const="flag", default=None, dest="cmd",
help="Change flags")
parser.add_option("-L", "--list", action="store_const", const="list", default=None, dest="cmd",
help="List messages")
parser.add_option("-Q", "--queue", action="store_const", const="queue", default=None, dest="cmd",
help="List messages in the outbound queue")
parser.add_option("-P", "--print", action="store_const", const="print", default=None, dest="cmd",
help="Print raw messages")
parser.add_option("-F", "--filename", action="store_const", const="filename", default=None, dest="cmd",
help="Print the filenames")
parser.add_option("--rebuild", action="store_const", const="rebuild", default=None, dest="cmd",
help="rebuilds the entire database and quits")
parser.add_option("--empty-trash", action="store_const", const="trash", default=None, dest="cmd",
help="empties the trash folder")
# queries
parser.add_option("-q", "--query", action="store", type="string", default=None, dest="query",
help="mu query to which the action is restricted. Default is none")
parser.add_option("-m", "--modified", action="store_true", default=False, dest="modified",
help="Restricts to messages that are modified since last call to mutag -u")
parser.add_option("-t", "--target", action="store", type="string", default=None, dest="path",
help="Restrict to the message at the given path")
# Options
parser.add_option("-p", "--profile", action="store", type="string", default=None, dest="profile",
help="Select a configuration profile")
parser.add_option("-f", "--format", action="store", type="string", default='compact', dest="format",
help="Format to print output")
parser.add_option("-u", "--update", action="store_true", default=False, dest="update",
help="Update list of modification times for the files.")
parser.add_option("-i", "--index", action="store_true", default=False, dest="index",
help="Index new messages")
parser.add_option("-c", "--commit", action="store_true", default=False, dest="commit",
help="Commit mail if stored in a git repo")
parser.add_option("-s", "--silent", action="store_true", default=False, dest="silent",
help="Runs silently.")
parser.add_option("--dryrun", action="store_true", default=False, dest="dryrun",
help="Performs a dry run. Does not change anything on disk.")
parser.add_option("--muhome", action="store", type="string", default=None, dest="muhome",
help="Path to the mu database")
parser.add_option("--maildir", action="store", type="string", default=None, dest="maildir",
help="Path to maildir")
parser.add_option("--version", action="store_true", default=False, dest="version",
help="Print the version and exit")
parser.add_option("--debug", action="store_true", default=False, dest="debug",
help="Print debug information")
(opts, args) = parser.parse_args()
if opts.version:
print(__version__)
sys.exit(0)
try:
eval_command(opts, args)
except MutagError as err:
ui.print_error(str(err))
sys.exit(1)
except KeyboardInterrupt:
print("")
sys.exit()
except EOFError:
print("")
sys.exit()
# vim: expandtab:shiftwidth=4:tabstop=4:softtabstop=4:textwidth=80