Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Armin Wieser <armin.wieser@gmail.com>
- Loading branch information
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Time-stamp: <2011-10-28 15:13:31 aw> | ||
|
||
from memacs.sms import SmsMemacs | ||
|
||
PROG_VERSION_NUMBER = u"0.1" | ||
PROG_VERSION_DATE = u"2011-12-27" | ||
PROG_SHORT_DESCRIPTION = u"Memacs for sms" | ||
PROG_TAG = u"sms" | ||
PROG_DESCRIPTION = u""" | ||
This Memacs module will parse output of sms xml backup files | ||
sample xml file: | ||
... TODO | ||
Then an Org-mode file is generated. | ||
""" | ||
COPYRIGHT_YEAR = "2011-2012" | ||
COPYRIGHT_AUTHORS = """Karl Voit <tools@Karl-Voit.at>, | ||
Armin Wieser <armin.wieser@gmail.com>""" | ||
|
||
if __name__ == "__main__": | ||
memacs = SmsMemacs( | ||
prog_version=PROG_VERSION_NUMBER, | ||
prog_version_date=PROG_VERSION_DATE, | ||
prog_description=PROG_DESCRIPTION, | ||
prog_short_description=PROG_SHORT_DESCRIPTION, | ||
prog_tag=PROG_TAG, | ||
copyright_year=COPYRIGHT_YEAR, | ||
copyright_authors=COPYRIGHT_AUTHORS | ||
) | ||
memacs.handle_main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) |