-
Notifications
You must be signed in to change notification settings - Fork 0
/
csm2txt.py
62 lines (57 loc) · 1.88 KB
/
csm2txt.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
#!/usr/bin/env python2
# .csm (PIMBackup Messages dump) to .txt
# v1.1 Silas S. Brown 2018-2019
# public domain - no warranty
# Where to find history:
# on GitHub at https://github.com/ssb22/wm6-utils
# and on GitLab at https://gitlab.com/ssb22/wm6-utils
# and on BitBucket https://bitbucket.org/ssb22/wm6-utils
# and at https://gitlab.developers.cam.ac.uk/ssb22/wm6-utils
# and in China: https://gitee.com/ssb22/wm6-utils
indent = 16
ignore = [
# Customise this - headers to ignore
"Msg Id",
"BodyType",
"Folder",
"Account",
"Msg Class",
"Msg Size",
"Msg Flags",
"Msg Status",
"Recipient Nbr",
"Attachment Nbr",
"Content Length",
]
import csv
from cStringIO import StringIO
class csm(csv.excel):
delimiter = ';'
escapechar = "\\"
quoting = csv.QUOTE_NONE
doublequote = False
def escape_newlines(s):
inBody = False ; o = []
for l in s.split("\n"):
numQuotes=max(0,len(l.replace('\\"','').split('"'))-1)
if numQuotes % 2: inBody = not inBody
if inBody: o.append(l+r"\\n"+" "*indent)
else: o.append(l+"\n")
return "".join(o)
csv.register_dialect("csm",csm)
headers = [] ; out = []
for r in csv.reader(StringIO(escape_newlines(open("msgs.csm").read().decode('utf-16').encode('utf-8').replace("\r\n","\n").replace("\r","\n"))),"csm"):
if headers:
row = [] ; tt = None
for k,v in zip(headers,r):
if v.startswith('"') and v.endswith('"'):
v=v[1:-1]
if k in ignore: continue
if k.endswith("Time"):
try: y,m,d,h,mm,s = v.split(",")
except: continue # no time ?
tt=v="%s-%s-%s %s:%s:%s" % (y,m,d,h,mm,s)
if v: row.append(k+":"+" "*max(1,indent-1-len(k))+v.replace(r"\n","\n"))
if row: out.append((tt,"\n".join(row)))
else: headers = r
print "\n\n".join(o for _,o in sorted(out))