/
sms.py
executable file
·115 lines (92 loc) · 3.2 KB
/
sms.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time-stamp: <2011-10-28 15:13:31 aw>
import sys
import os
import logging
import xml.sax
import time
from xml.sax._exceptions import SAXParseException
from lib.orgformat import OrgFormat
from lib.memacs import Memacs
from lib.reader import CommonReader
class SmsSaxHandler(xml.sax.handler.ContentHandler):
"""
Sax handler for following xml's:
<?xml version="1.0"?>
<log>
<logentry
revision="13">
<author>bob</author>
<date>2011-11-05T18:18:22.936127Z</date>
<msg>Bugfix.</msg>
</logentry>
</log>
"""
def __init__(self, writer):
"""
Ctor
@param writer: orgwriter
"""
self._writer = writer
def startElement(self, name, attrs):
"""
at every <tag> remember the tagname
* sets the revision when in tag "logentry"
"""
logging.debug("Handler @startElement name=%s,attrs=%s", name, attrs)
if name == "sms":
sms_subject = attrs['subject']
sms_date = int(attrs['date']) / 1000 # unix epoch
sms_body = attrs['body']
sms_address = attrs['address']
sms_type_incoming = int(attrs['type']) == 1
if sms_type_incoming == True:
output = "SMS from "
else:
output = "SMS to "
output += sms_address + ": "
if sms_subject != "null":
# in case of MMS we have a subject
output += sms_subject
notes = sms_body
else:
output += sms_body
notes = ""
timestamp = OrgFormat.datetime(time.gmtime(sms_date))
#properties = OrgProperties()
self._writer.write_org_subitem(output=output,
timestamp=timestamp,
note=notes)
class SmsMemacs(Memacs):
def _parser_add_arguments(self):
"""
overwritten method of class Memacs
add additional arguments
"""
Memacs._parser_add_arguments(self)
self._parser.add_argument(
"-f", "--file", dest="smsxmlfile",
action="store", required=True,
help="path to sms xml backup file")
def _parser_parse_args(self):
"""
overwritten method of class Memacs
all additional arguments are parsed in here
"""
Memacs._parser_parse_args(self)
if not (os.path.exists(self._args.smsxmlfile) or \
os.access(self._args.smsxmlfile, os.R_OK)):
self._parser.error("input file not found or not readable")
def _main(self):
"""
get's automatically called from Memacs class
read the lines from svn xml file, parse and write them to org file
"""
data = CommonReader.get_data_from_file(self._args.smsxmlfile)
try:
xml.sax.parseString(data.encode('utf-8'),
SmsSaxHandler(self._writer))
except SAXParseException:
logging.error("No correct XML given")
sys.exit(1)