/usr/lib/python2.7/dist-packages/ofxclient/account.py is in python-ofxclient 1.3.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | from ofxparse import OfxParser, AccountType
import datetime
import StringIO
import time
import hashlib
class Account(object):
"""Base class for accounts at an institution
:param number: The account number
:type number: string
:param institution: The bank this belongs to
:type institution: :py:class:`ofxclient.Institution` object
:param description: optional account description
:type description: string or None
This class is almost never never instantiated on it's own. Instead,
sub-classes are instantiated.
In most cases these subclasses are either being deserialized from a
config file entry, a serialization hash, or returned by the
:py:meth:`ofxclient.Institution.accounts` method.
Example from a saved config entry::
from ofxclient.config import OfxConfig
account = OfxConfig().account('local_id() string')
Example of deserialization::
from ofxclient import BankAccount
# assume 'inst' is an Institution()
a1 = BankAccount(number='asdf',institution=inst)
data1 = a1.serialize()
a2 = Account.deserialize(data1)
Example by querying the bank directly::
from ofxclient import Institution
# assume an Institution() is configured with
# a username/password etc
accounts = institution.accounts()
.. seealso::
:py:class:`ofxclient.BankAccount`
:py:class:`ofxclient.BrokerageAccount`
:py:class:`ofxclient.CreditCardAccount`
"""
def __init__(self, number, institution, description=None):
self.institution = institution
self.number = number
self.description = description or self._default_description()
def local_id(self):
"""Locally generated unique account identifier.
:rtype: string
"""
return hashlib.sha256("%s%s" % (
self.institution.local_id(),
self.number)).hexdigest()
def number_masked(self):
"""Masked version of the account number for privacy.
:rtype: string
"""
return "***%s" % self.number[-4:]
def long_description(self):
"""Long description of the account (includes institution description).
:rtype: string
"""
return "%s: %s" % (self.institution.description, self.description)
def _default_description(self):
return self.number_masked()
def download(self, days=60):
"""Downloaded OFX response for the given time range
:param days: Number of days to look back at
:type days: integer
:rtype: :py:class:`StringIO.StringIO`
"""
days_ago = datetime.datetime.now() - datetime.timedelta(days=days)
as_of = time.strftime("%Y%m%d", days_ago.timetuple())
query = self._download_query(as_of=as_of)
response = self.institution.client().post(query)
return StringIO.StringIO(response)
def download_parsed(self, days=60):
"""Downloaded OFX response parsed by :py:meth:`OfxParser.parse`
:param days: Number of days to look back at
:type days: integer
:rtype: :py:class:`ofxparser.Ofx`
"""
return OfxParser.parse(self.download(days=days))
def statement(self, days=60):
"""Download the :py:class:`ofxparse.Statement` given the time range
:param days: Number of days to look back at
:type days: integer
:rtype: :py:class:`ofxparser.Statement`
"""
parsed = self.download_parsed(days=days)
return parsed.account.statement
def transactions(self, days=60):
"""Download a a list of :py:class:`ofxparse.Transaction` objects
:param days: Number of days to look back at
:type days: integer
:rtype: list of :py:class:`ofxparser.Transaction` objects
"""
return self.statement(days=days).transactions
def serialize(self):
"""Serialize predictably for use in configuration storage.
Output look like this::
{
'local_id': 'string',
'number': 'account num',
'description': 'descr',
'broker_id': 'may be missing - type dependent',
'routing_number': 'may be missing - type dependent,
'account_type': 'may be missing - type dependent,
'institution': {
# ... see :py:meth:`ofxclient.Institution.serialize`
}
}
:rtype: nested dictionary
"""
data = {
'local_id': self.local_id(),
'institution': self.institution.serialize(),
'number': self.number,
'description': self.description
}
if hasattr(self, 'broker_id'):
data['broker_id'] = self.broker_id
elif hasattr(self, 'routing_number'):
data['routing_number'] = self.routing_number
data['account_type'] = self.account_type
return data
@staticmethod
def deserialize(raw):
"""Instantiate :py:class:`ofxclient.Account` subclass from dictionary
:param raw: serilized Account
:param type: dict as given by :py:meth:`~ofxclient.Account.serialize`
:rtype: subclass of :py:class:`ofxclient.Account`
"""
from ofxclient.institution import Institution
institution = Institution.deserialize(raw['institution'])
del raw['institution']
del raw['local_id']
if 'broker_id' in raw:
a = BrokerageAccount(institution=institution, **raw)
elif 'routing_number' in raw:
a = BankAccount(institution=institution, **raw)
else:
a = CreditCardAccount(institution=institution, **raw)
return a
@staticmethod
def from_ofxparse(data, institution):
"""Instantiate :py:class:`ofxclient.Account` subclass from ofxparse
module
:param data: an ofxparse account
:type data: An :py:class:`ofxparse.Account` object
:param institution: The parent institution of the account
:type institution: :py:class:`ofxclient.Institution` object
"""
description = data.desc if hasattr(data, 'desc') else None
if data.type == AccountType.Bank:
return BankAccount(
institution=institution,
number=data.account_id,
routing_number=data.routing_number,
account_type=data.account_type,
description=description)
elif data.type == AccountType.CreditCard:
return CreditCardAccount(
institution=institution,
number=data.account_id,
description=description)
elif data.type == AccountType.Investment:
return BrokerageAccount(
institution=institution,
number=data.account_id,
broker_id=data.brokerid,
description=description)
raise ValueError("unknown account type: %s" % data.type)
class BrokerageAccount(Account):
""":py:class:`ofxclient.Account` subclass for brokerage/investment accounts
In addition to the parameters it's superclass requires, the following
parameters are needed.
:param broker_id: Broker ID of the account
:type broker_id: string
.. seealso::
:py:class:`ofxclient.Account`
"""
def __init__(self, broker_id, **kwargs):
super(BrokerageAccount, self).__init__(**kwargs)
self.broker_id = broker_id
def _download_query(self, as_of):
"""Formulate the specific query needed for download
Not intended to be called by developers directly.
:param as_of: Date in 'YYYYMMDD' format
:type as_of: string
"""
c = self.institution.client()
q = c.brokerage_account_query(
number=self.number, date=as_of, broker_id=self.broker_id)
return q
class BankAccount(Account):
""":py:class:`ofxclient.Account` subclass for a checking/savings account
In addition to the parameters it's superclass requires, the following
parameters are needed.
:param routing_number: Routing number or account number of the account
:type routing_number: string
:param account_type: Account type per OFX spec can be empty but not None
:type account_type: string
.. seealso::
:py:class:`ofxclient.Account`
"""
def __init__(self, routing_number, account_type, **kwargs):
super(BankAccount, self).__init__(**kwargs)
self.routing_number = routing_number
self.account_type = account_type
def _download_query(self, as_of):
"""Formulate the specific query needed for download
Not intended to be called by developers directly.
:param as_of: Date in 'YYYYMMDD' format
:type as_of: string
"""
c = self.institution.client()
q = c.bank_account_query(
number=self.number,
date=as_of,
account_type=self.account_type,
bank_id=self.routing_number)
return q
class CreditCardAccount(Account):
""":py:class:`ofxclient.Account` subclass for a credit card account
No additional parameters to the constructor are needed.
.. seealso::
:py:class:`ofxclient.Account`
"""
def __init__(self, **kwargs):
super(CreditCardAccount, self).__init__(**kwargs)
def _download_query(self, as_of):
"""Formulate the specific query needed for download
Not intended to be called by developers directly.
:param as_of: Date in 'YYYYMMDD' format
:type as_of: string
"""
c = self.institution.client()
q = c.credit_card_account_query(number=self.number, date=as_of)
return q
|