Abstracted some common data-types.
authorFredrik Tolf <fredrik@dolda2000.com>
Wed, 1 Jan 2020 20:03:47 +0000 (21:03 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Wed, 1 Jan 2020 20:03:47 +0000 (21:03 +0100)
fulbank/data.py [new file with mode: 0644]
fulbank/fsb.py

diff --git a/fulbank/data.py b/fulbank/data.py
new file mode 100644 (file)
index 0000000..36efe20
--- /dev/null
@@ -0,0 +1,48 @@
+import hashlib
+
+def _localname(type):
+    mod = type.__module__
+    if mod.startswith("fulbank."):
+        mod = mod[8:]
+    return "%s.%s" % (mod, type.__name__)
+
+class account(object):
+    @property
+    def number(self): raise NotImplementedError("account.number")
+    @property
+    def name(self): raise NotImplementedError("account.name")
+    def transactions(self): raise NotImplementedError("account.transactions")
+
+    def __repr__(self):
+        return "#<%s %s: %r>" % (_localname(type(self)), self.number, self.name)
+
+class txnaccount(account):
+    @property
+    def balance(self): raise NotImplementedError("txnaccount.balance")
+    @property
+    def clearing(self): raise NotImplementedError("txnaccount.clearing")
+    @property
+    def fullnumber(self): raise NotImplementedError("txnaccount.fullnumber")
+
+class cardaccount(account):
+    pass
+
+class transaction(object):
+    @property
+    def value(self): raise NotImplementedError("transaction.value")
+    @property
+    def message(self): raise NotImplementedError("transaction.message")
+    @property
+    def date(self): raise NotImplementedError("transaction.date")
+
+    @property
+    def hash(self):
+        dig = hashlib.sha256()
+        dig.update(str(self.date.toordinal()).encode("ascii") + b"\0")
+        dig.update(self.message.encode("utf-8") + b"\0")
+        dig.update(str(self.value.amount).encode("ascii") + b"\0")
+        dig.update(self.value.currency.symbol.encode("ascii") + b"\0")
+        return dig.hexdigest()
+
+    def __repr__(self):
+        return "#<%s %s: %r>" % (_localname(type(self)), self.value, self.message)
index 85acbb9..58a54dc 100644 (file)
@@ -1,7 +1,7 @@
-import json, http.cookiejar, binascii, time, datetime, pickle, hashlib
+import json, http.cookiejar, binascii, time, datetime, pickle
 from urllib import request, parse
 from bs4 import BeautifulSoup as soup
-from . import currency, auth
+from . import currency, auth, data
 soupify = lambda cont: soup(cont, "html.parser")
 
 apibase = "https://online.swedbank.se/TDE_DAP_Portal_REST_WEB/api/"
@@ -52,7 +52,7 @@ def getdsid():
 def base64(data):
     return binascii.b2a_base64(data).decode("ascii").strip().rstrip("=")
 
-class transaction(object):
+class transaction(data.transaction):
     def __init__(self, account, data):
         self.account = account
         self._data = data
@@ -68,19 +68,7 @@ class transaction(object):
         p = time.strptime(resolve(self._data, ("accountingDate",)), self._datefmt)
         return datetime.date(p.tm_year, p.tm_mon, p.tm_mday)
 
-    @property
-    def hash(self):
-        dig = hashlib.sha256()
-        dig.update(str(self.date.toordinal()).encode("ascii") + b"\0")
-        dig.update(self.message.encode("utf-8") + b"\0")
-        dig.update(str(self.value.amount).encode("ascii") + b"\0")
-        dig.update(self.value.currency.symbol.encode("ascii") + b"\0")
-        return dig.hexdigest()
-
-    def __repr__(self):
-        return "#<fsb.transaction %s: %r>" % (self.value, self.message)
-
-class txnaccount(object):
+class txnaccount(data.txnaccount):
     def __init__(self, sess, id, idata):
         self.sess = sess
         self.id = id
@@ -116,10 +104,7 @@ class txnaccount(object):
                 yield transaction(self, tx)
             page += 1
 
-    def __repr__(self):
-        return "#<fsb.txnaccount %s: %r>" % (self.fullnumber, self.name)
-
-class cardtransaction(object):
+class cardtransaction(data.transaction):
     def __init__(self, account, data):
         self.account = account
         self._data = data
@@ -137,19 +122,7 @@ class cardtransaction(object):
         p = time.strptime(resolve(self._data, ("date",)), self._datefmt)
         return datetime.date(p.tm_year, p.tm_mon, p.tm_mday)
 
-    @property
-    def hash(self):
-        dig = hashlib.sha256()
-        dig.update(str(self.date.toordinal()).encode("ascii") + b"\0")
-        dig.update(self.message.encode("utf-8") + b"\0")
-        dig.update(str(self.value.amount).encode("ascii") + b"\0")
-        dig.update(self.value.currency.symbol.encode("ascii") + b"\0")
-        return dig.hexdigest()
-
-    def __repr__(self):
-        return "#<fsb.cardtransaction %s: %r>" % (self.value, self.message)
-
-class cardaccount(object):
+class cardaccount(data.cardaccount):
     def __init__(self, sess, id, idata):
         self.sess = sess
         self.id = id
@@ -183,9 +156,6 @@ class cardaccount(object):
                 yield cardtransaction(self, tx)
             page += 1
 
-    def __repr__(self):
-        return "#<fsb.cardaccount %s: %r>" % (self.number, self.name)
-
 class session(object):
     def __init__(self, dsid):
         self.dsid = dsid