Exemple #1
0
    def _send_request(self, url, request_body):
        """Transmits the message to the server and checks the response
        for error status."""

        request = urllib.request.Request(url, request_body.encode('utf-8'),
                                  { "Content-type": "application/x-ofx",
                                    "Accept": "*/*, application/x-ofx" })
        stream = urllib.request.urlopen(request)
        response = stream.read()
        stream.close()

        response = Response(response)
        response.check_signon_status()

        parsed_ofx = response.as_dict()

        # FIXME: This needs to account for statement closing responses.

        if "BANKMSGSRSV1" in parsed_ofx:
            bank_status = \
                parsed_ofx["BANKMSGSRSV1"]["STMTTRNRS"]["STATUS"]
            self._check_status(bank_status, "bank statement")

        elif "CREDITCARDMSGSRSV1" in parsed_ofx:
            creditcard_status = \
                parsed_ofx["CREDITCARDMSGSRSV1"]["CCSTMTTRNRS"]["STATUS"]
            self._check_status(creditcard_status, "credit card statement")

        elif "SIGNUPMSGSRSV1" in parsed_ofx:
            acctinfo_status = \
                parsed_ofx["SIGNUPMSGSRSV1"]["ACCTINFOTRNRS"]["STATUS"]
            self._check_status(acctinfo_status, "account information")

        return response
    def to_xml(self):
        ofx102 = self.to_ofx102()

        if self.debug:
            sys.stderr.write(ofx102 + "\n")
            sys.stderr.write("Parsing OFX/1.02.\n")
        response = Response(ofx102, debug=self.debug)

        if self.debug: sys.stderr.write("Making OFX/2.0.\n")

        xml = response.as_xml(original_format="OFC")

        return xml
Exemple #3
0
    def to_xml(self):
        ofx102 = self.to_ofx102()

        if self.debug:
            sys.stderr.write(ofx102 + "\n")
            sys.stderr.write("Parsing OFX/1.02.\n")
        response = Response(ofx102, debug=self.debug)

        if self.debug: sys.stderr.write("Making OFX/2.0.\n")

        xml = response.as_xml(original_format="OFC")

        return xml
Exemple #4
0
    def to_xml(self):
        ofx102 = self.to_ofx102()

        if self.debug:
            sys.stderr.write(ofx102 + "\n")
            sys.stderr.write("Parsing OFX/1.02.\n")
        response = Response(ofx102) #, debug=self.debug)

        if self.debug: sys.stderr.write("Making OFX/2.0.\n")
        if self.dayfirst:
            date_format = "DD/MM/YY"
        else:
            date_format = "MM/DD/YY"
        xml = response.as_xml(original_format="QIF", date_format=date_format)

        return xml
class ResponseTests(unittest.TestCase):
    def setUp(self):
        self.response_text = get_checking_stmt().decode('utf-8')
        self.response = Response(get_checking_stmt())

    def test_signon_success(self):
        status = self.response.check_signon_status()
        self.assertTrue(status)

    def test_account_list(self):
        statements = self.response.get_statements()
        self.assertEqual(1, len(statements))

        for stmt in statements:
            self.assertEqual("USD", stmt.get_currency())
            self.assertEqual("20100424", stmt.get_begin_date())
            self.assertEqual("20100723", stmt.get_end_date())
            self.assertEqual("1129.49", stmt.get_balance())
            self.assertEqual("20100723", stmt.get_balance_date())

            account = stmt.get_account()
            self.assertEqual("987987987", account.aba_number)
            self.assertEqual("58152460", account.acct_number)
            self.assertEqual("CHECKING", account.get_ofx_accttype())

    def test_as_xml(self):
        # First just sanity-check that ElementTree will throw an error
        # if given a non-XML document.
        try:
            response_elem = ElementTree.fromstring(self.response_text)
            self.fail("Expected parse exception but did not get one.")
        except:
            pass

        # Then see if we can get a real parse success, with no ExpatError.
        xml = self.response.as_xml()
        xml_elem = ElementTree.fromstring(xml)
        self.assertTrue(isinstance(xml_elem, ElementTree.Element))

        # Finally, for kicks, try to get a value out of it.
        org_iter = xml_elem.getiterator("ORG")
        for org in org_iter:
            self.assertEqual("FAKEOFX", org.text)
class ResponseTests(unittest.TestCase):
    def setUp(self):
        self.response_text = get_checking_stmt().decode('utf-8')
        self.response = Response(get_checking_stmt())

    def test_signon_success(self):
        status = self.response.check_signon_status()
        self.assertTrue(status)

    def test_account_list(self):
        statements = self.response.get_statements()
        self.assertEqual(1, len(statements))

        for stmt in statements:
            self.assertEqual("USD", stmt.get_currency())
            self.assertEqual("20100424", stmt.get_begin_date())
            self.assertEqual("20100723", stmt.get_end_date())
            self.assertEqual("1129.49",        stmt.get_balance())
            self.assertEqual("20100723", stmt.get_balance_date())

            account = stmt.get_account()
            self.assertEqual("987987987", account.aba_number)
            self.assertEqual("58152460", account.acct_number)
            self.assertEqual("CHECKING", account.get_ofx_accttype())

    def test_as_xml(self):
        # First just sanity-check that ElementTree will throw an error
        # if given a non-XML document.
        try:
            response_elem = ElementTree.fromstring(self.response_text)
            self.fail("Expected parse exception but did not get one.")
        except:
            pass

        # Then see if we can get a real parse success, with no ExpatError.
        xml = self.response.as_xml()
        xml_elem = ElementTree.fromstring(xml)
        self.assertTrue(isinstance(xml_elem, ElementTree.Element))

        # Finally, for kicks, try to get a value out of it.
        org_iter = xml_elem.getiterator("ORG")
        for org in org_iter:
            self.assertEqual("FAKEOFX", org.text)
Exemple #7
0
def convert(filecontent, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN",
            bankid="UNKNOWN", accttype="UNKNOWN", acctid="UNKNOWN",
            balance="UNKNOWN", curdef=None, lang="ENG", dayfirst=False,
            debug=False):

    text = os.linesep.join(s for s in filecontent.splitlines() if s)

    # This finishes a verbosity message started by the caller, where the
    # caller explains the source command-line option and this explains the
    # source format.
    if verbose:
        sys.stderr.write("Converting from %s format.\n" % filetype)

    if options.debug and (filetype in ["OFC", "QIF"] or filetype.startswith("OFX")):
        sys.stderr.write("Starting work on raw text:\n")
        sys.stderr.write(rawtext + "\n\n")

    if filetype.startswith("OFX/2"):
        if verbose: sys.stderr.write("No conversion needed; returning unmodified.\n")

        # The file is already OFX 2 -- return it unaltered, ignoring
        # any of the parameters passed to this method.
        return text

    elif filetype.startswith("OFX"):
        if verbose: sys.stderr.write("Converting to OFX/2.0...\n")

        # This will throw a ParseException if it is unable to recognize
        # the source format.
        response = Response(text, debug=debug)
        return response.as_xml(original_format=filetype)

    elif filetype == "OFC":
        if verbose: sys.stderr.write("Beginning OFC conversion...\n")
        converter = OfcConverter(text, fid=fid, org=org, curdef=curdef,
                                 lang=lang, debug=debug)

        # This will throw a ParseException if it is unable to recognize
        # the source format.
        if verbose:
            sys.stderr.write("Converting to OFX/1.02...\n\n%s\n\n" %
                             converter.to_ofx102())
            sys.stderr.write("Converting to OFX/2.0...\n")

        return converter.to_xml()

    elif filetype == "QIF":
        if verbose: sys.stderr.write("Beginning QIF conversion...\n")
        converter = QifConverter(text, fid=fid, org=org,
                                 bankid=bankid, accttype=accttype,
                                 acctid=acctid, balance=balance,
                                 curdef=curdef, lang=lang, dayfirst=dayfirst,
                                 debug=debug)

        # This will throw a ParseException if it is unable to recognize
        # the source format.
        if verbose:
            sys.stderr.write("Converting to OFX/1.02...\n\n%s\n\n" %
                             converter.to_ofx102())
            sys.stderr.write("Converting to OFX/2.0...\n")

        return converter.to_xml()

    else:
        raise TypeError("Unable to convert source format '%s'." % filetype)
 def setUp(self):
     self.response_text = get_checking_stmt().decode('utf-8')
     self.response = Response(get_checking_stmt())
 def test_statement_as_xml(self):
     response = Response(self.checking)
     self.assertEqual('<?xml version="1.0"', response.as_xml()[:19])
Exemple #10
0
def convert(filecontent,
            filetype,
            verbose=False,
            fid="UNKNOWN",
            org="UNKNOWN",
            bankid="UNKNOWN",
            accttype="UNKNOWN",
            acctid="UNKNOWN",
            balance="UNKNOWN",
            curdef=None,
            lang="ENG",
            dayfirst=False,
            debug=False):

    text = os.linesep.join(s for s in filecontent.splitlines() if s)

    # This finishes a verbosity message started by the caller, where the
    # caller explains the source command-line option and this explains the
    # source format.
    if verbose:
        sys.stderr.write("Converting from %s format.\n" % filetype)

    if options.debug and (filetype in ["OFC", "QIF"]
                          or filetype.startswith("OFX")):
        sys.stderr.write("Starting work on raw text:\n")
        sys.stderr.write(rawtext + "\n\n")

    if filetype.startswith("OFX/2"):
        if verbose:
            sys.stderr.write("No conversion needed; returning unmodified.\n")

        # The file is already OFX 2 -- return it unaltered, ignoring
        # any of the parameters passed to this method.
        return text

    elif filetype.startswith("OFX"):
        if verbose: sys.stderr.write("Converting to OFX/2.0...\n")

        # This will throw a ParseException if it is unable to recognize
        # the source format.
        response = Response(text, debug=debug)
        return response.as_xml(original_format=filetype)

    elif filetype == "OFC":
        if verbose: sys.stderr.write("Beginning OFC conversion...\n")
        converter = OfcConverter(text,
                                 fid=fid,
                                 org=org,
                                 curdef=curdef,
                                 lang=lang,
                                 debug=debug)

        # This will throw a ParseException if it is unable to recognize
        # the source format.
        if verbose:
            sys.stderr.write("Converting to OFX/1.02...\n\n%s\n\n" %
                             converter.to_ofx102())
            sys.stderr.write("Converting to OFX/2.0...\n")

        return converter.to_xml()

    elif filetype == "QIF":
        if verbose: sys.stderr.write("Beginning QIF conversion...\n")
        converter = QifConverter(text,
                                 fid=fid,
                                 org=org,
                                 bankid=bankid,
                                 accttype=accttype,
                                 acctid=acctid,
                                 balance=balance,
                                 curdef=curdef,
                                 lang=lang,
                                 dayfirst=dayfirst,
                                 debug=debug)

        # This will throw a ParseException if it is unable to recognize
        # the source format.
        if verbose:
            sys.stderr.write("Converting to OFX/1.02...\n\n%s\n\n" %
                             converter.to_ofx102())
            sys.stderr.write("Converting to OFX/2.0...\n")

        return converter.to_xml()

    else:
        raise TypeError("Unable to convert source format '%s'." % filetype)
Exemple #11
0
 def test_statement_as_xml(self):
     response = Response(self.checking)
     self.assertEqual('<?xml version="1.0"', response.as_xml()[:19])
 def setUp(self):
     self.response_text = get_checking_stmt().decode('utf-8')
     self.response = Response(get_checking_stmt())