Cosmetics
[llpp.git] / misc / gcext.py
blob110d412f1800cf2dfc1e1b4d599f0d13de4129f6
1 import argparse
2 import os
3 import xml.etree.ElementTree as ET
5 parser = argparse.ArgumentParser()
6 parser.add_argument(
7 '-i',
8 dest='input',
9 type=str,
10 metavar='SOURCE',
11 help='path to your llpp.conf; default to ~/.config/llpp.conf',
12 default='~/.config/llpp.conf')
13 parser.add_argument(
14 '-d',
15 dest='timestamp',
16 type=int,
17 help='entries saved earlier than this date will be removed')
18 parser.add_argument(
19 '-k',
20 dest='keyword',
21 type=str,
22 help='entries with path matching the keyword will be removed')
23 parser.add_argument(dest='dest',
24 type=str,
25 metavar='DEST',
26 help='path to output config file')
28 args = parser.parse_args()
29 source = args.input
30 cts = args.timestamp
31 keyword = args.keyword
32 dest = args.dest
34 tree = ET.parse(os.path.expanduser(source))
35 root = tree.getroot()
37 print('Removing the following entries in llpp.conf:')
38 i = 0
39 for doc in root.findall('doc'):
40 # skip entries with bookmarks
41 if doc.find('bookmarks'):
42 continue
43 # remove entries older than 2019.9.1
44 ts = int(doc.get('last-visit'))
45 if cts is not None and ts < cts:
46 print(doc.get('path'))
47 root.remove(doc)
48 i += 1
49 continue
50 # remove entries whose path contains the keyword
51 if keyword is not None and keyword in doc.get('path'):
52 print(doc.get('path'))
53 root.remove(doc)
54 i += 1
56 print(i, 'entries have been removed.',
57 'The new configuration file has been saved to', dest)
58 tree.write(dest)