# Path: memacs/sms.py
# class SmsMemacs(Memacs):
# 
#     _numberdict = False
# 
#     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")
# 
#         self._parser.add_argument(
#             "--ignore-incoming", dest="ignore_incoming",
#             action="store_true",
#             help="ignore incoming smses")
# 
#         self._parser.add_argument(
#             "--ignore-outgoing", dest="ignore_outgoing",
#             action="store_true",
#             help="ignore outgoing smses")
# 
#         self._parser.add_argument(
#             "--orgcontactsfile", dest="orgcontactsfile",
#             action="store", required=False,
#             help="path to Org-contacts file for phone number lookup. Phone numbers have to match.")
# 
# 
# 
#     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")
# 
#         if self._args.orgcontactsfile:
#             if not (os.path.exists(self._args.orgcontactsfile) or \
#                     os.access(self._args.orgcontactsfile, os.R_OK)):
#                 self._parser.error("Org-contacts file not found or not readable")
#             self._numberdict = parse_org_contact_file(self._args.orgcontactsfile)
# 
# 
#     def _main(self):
#         """
#         get's automatically called from Memacs class
#         read the lines from sms backup xml file,
#         parse and write them to org file
#         """
# 
#         ## replace HTML entities "&#" in original file to prevent XML parser from worrying:
#         temp_xml_file = tempfile.mkstemp()[1]
#         line_number = 0
#         logging.debug("tempfile [%s]", str(temp_xml_file))
#         with codecs.open(temp_xml_file, 'w', encoding='utf-8') as outputhandle:
#             for line in codecs.open(self._args.smsxmlfile, 'r', encoding='utf-8'):
#                 try:
#                     ## NOTE: this is a dirty hack to prevent te XML parser from complainaing about
#                     ##       encoding issues of UTF-8 encoded emojis. Will be reverted when parsing sms_body
#                     outputhandle.write(line.replace('&#', 'EnCoDiNgHaCk42') + '\n')
#                 except IOError as e:
#                     print("tempfile line " + str(line_number) +  " [" + str(temp_xml_file) + "]")
#                     print("I/O error({0}): {1}".format(e.errno, e.strerror))
#                 except ValueError as e:
#                     print("tempfile line " + str(line_number) +  " [" + str(temp_xml_file) + "]")
#                     print("Value error: {0}".format(e))
#                     #print "line [%s]" % str(line)
#                 except:
#                     print("tempfile line " + str(line_number) +  " [" + str(temp_xml_file) + "]")
#                     print("Unexpected error:", sys.exc_info()[0])
#                     raise
# 
#         data = CommonReader.get_data_from_file(temp_xml_file)
# 
#         try:
#             xml.sax.parseString(data.encode('utf-8'),
#                                 SmsSaxHandler(self._writer,
#                                               self._args.ignore_incoming,
#                                               self._args.ignore_outgoing,
#                                               self._numberdict))
#         except SAXParseException:
#             logging.error("No correct XML given")
#             sys.exit(1)
#         else:
#             os.remove(temp_xml_file)


import os
import re
import time
import unittest
import xml.etree.ElementTree as ET
from memacs.sms import SmsMemacs
# -*- coding: utf-8 -*-
# Time-stamp: <2012-03-09 15:36:52 armin>




class TestSmsMemacs(unittest.TestCase):

    def setUp(self):
        self._test_file = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), 'data', 'smsxml.txt'
        )
        argv = "-s -f " + self._test_file

# Path: memacs/sms.py
# class SmsMemacs(Memacs):
# 
#     _numberdict = False
# 
#     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")
# 
#         self._parser.add_argument(
#             "--ignore-incoming", dest="ignore_incoming",
#             action="store_true",
#             help="ignore incoming smses")
# 
#         self._parser.add_argument(
#             "--ignore-outgoing", dest="ignore_outgoing",
#             action="store_true",
#             help="ignore outgoing smses")
# 
#         self._parser.add_argument(
#             "--orgcontactsfile", dest="orgcontactsfile",
#             action="store", required=False,
#             help="path to Org-contacts file for phone number lookup. Phone numbers have to match.")
# 
# 
# 
#     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")
# 
#         if self._args.orgcontactsfile:
#             if not (os.path.exists(self._args.orgcontactsfile) or \
#                     os.access(self._args.orgcontactsfile, os.R_OK)):
#                 self._parser.error("Org-contacts file not found or not readable")
#             self._numberdict = parse_org_contact_file(self._args.orgcontactsfile)
# 
# 
#     def _main(self):
#         """
#         get's automatically called from Memacs class
#         read the lines from sms backup xml file,
#         parse and write them to org file
#         """
# 
#         ## replace HTML entities "&#" in original file to prevent XML parser from worrying:
#         temp_xml_file = tempfile.mkstemp()[1]
#         line_number = 0
#         logging.debug("tempfile [%s]", str(temp_xml_file))
#         with codecs.open(temp_xml_file, 'w', encoding='utf-8') as outputhandle:
#             for line in codecs.open(self._args.smsxmlfile, 'r', encoding='utf-8'):
#                 try:
#                     ## NOTE: this is a dirty hack to prevent te XML parser from complainaing about
#                     ##       encoding issues of UTF-8 encoded emojis. Will be reverted when parsing sms_body
#                     outputhandle.write(line.replace('&#', 'EnCoDiNgHaCk42') + '\n')
#                 except IOError as e:
#                     print("tempfile line " + str(line_number) +  " [" + str(temp_xml_file) + "]")
#                     print("I/O error({0}): {1}".format(e.errno, e.strerror))
#                 except ValueError as e:
#                     print("tempfile line " + str(line_number) +  " [" + str(temp_xml_file) + "]")
#                     print("Value error: {0}".format(e))
#                     #print "line [%s]" % str(line)
#                 except:
#                     print("tempfile line " + str(line_number) +  " [" + str(temp_xml_file) + "]")
#                     print("Unexpected error:", sys.exc_info()[0])
#                     raise
# 
#         data = CommonReader.get_data_from_file(temp_xml_file)
# 
#         try:
#             xml.sax.parseString(data.encode('utf-8'),
#                                 SmsSaxHandler(self._writer,
#                                               self._args.ignore_incoming,
#                                               self._args.ignore_outgoing,
#                                               self._numberdict))
#         except SAXParseException:
#             logging.error("No correct XML given")
#             sys.exit(1)
#         else:
#             os.remove(temp_xml_file)


# Path: memacs/tests/sms_test.py
import os
import re
import time
import unittest
import xml.etree.ElementTree as ET
from memacs.sms import SmsMemacs

# -*- coding: utf-8 -*-
# Time-stamp: <2012-03-09 15:36:52 armin>




class TestSmsMemacs(unittest.TestCase):

    def setUp(self):
        self._test_file = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), 'data', 'smsxml.txt'
        )
        argv = "-s -f " + self._test_file

        memacs = SmsMemacs(argv=argv.split())