PEP8 on account_banking_iban_lookup

This commit is contained in:
Sandy Carter
2014-08-25 17:11:38 -04:00
parent 54039f126c
commit c47bba08e2
2 changed files with 66 additions and 42 deletions

View File

@@ -23,10 +23,14 @@ This module provides online bank databases for conversion between BBAN and
IBAN numbers and for consulting.
'''
import re
import urllib, urllib2
import urllib
import urllib2
from BeautifulSoup import BeautifulSoup
from openerp.addons.account_banking.sepa import postalcode
from openerp.addons.account_banking_iban_lookup.urlagent import URLAgent, SoupForm
from openerp.addons.account_banking_iban_lookup.urlagent import (
URLAgent,
SoupForm,
)
from openerp.addons.account_banking.sepa.iban import IBAN
from openerp.addons.account_banking.struct import struct
@@ -38,6 +42,7 @@ __all__ = [
IBANlink_NL = 'http://www.ibannl.org/iban_check.php'
IBANlink_BE = 'http://www.ibanbic.be/'
def get_iban_bic_NL(bank_acc):
'''
Consult the Dutch online banking database to check both the account number
@@ -51,14 +56,14 @@ def get_iban_bic_NL(bank_acc):
if len(number) <= 7:
iban = IBAN.create(BBAN='INGB' + number.rjust(10, '0'),
countrycode='NL'
)
)
return struct(
iban = iban.replace(' ',''),
account = iban.BBAN[4:],
bic = 'INGBNL2A',
code = 'INGBNL',
bank = 'ING Bank N.V.',
country_id = 'NL',
iban=iban.replace(' ', ''),
account=iban.BBAN[4:],
bic='INGBNL2A',
code='INGBNL',
bank='ING Bank N.V.',
country_id='NL',
)
data = urllib.urlencode(dict(number=number, method='POST'))
@@ -66,6 +71,7 @@ def get_iban_bic_NL(bank_acc):
response = urllib2.urlopen(request)
soup = BeautifulSoup(response)
result = struct()
attr = None
for _pass, td in enumerate(soup.findAll('td')):
if _pass % 2 == 1:
result[attr] = unicode(td.find('font').contents[0])
@@ -81,6 +87,7 @@ def get_iban_bic_NL(bank_acc):
return result
return None
def get_iban_bic_BE(bank_acc):
'''
Consult the Belgian online database to check both account number and the
@@ -88,7 +95,9 @@ def get_iban_bic_BE(bank_acc):
in Belgium and will only convert Belgian local account numbers.
'''
def contents(soup, attr):
return soup.find('input', {'id': 'textbox%s' % attr}).get('value').strip()
return soup.find('input', {
'id': 'textbox%s' % attr
}).get('value').strip()
if not bank_acc.strip():
return None
@@ -121,6 +130,7 @@ def get_iban_bic_BE(bank_acc):
result.code = result.bic[:6]
return result
def BBAN_is_IBAN(bank_acc):
'''
Intelligent copy, valid for SEPA members who switched to SEPA from old
@@ -131,15 +141,16 @@ def BBAN_is_IBAN(bank_acc):
else:
iban_acc = IBAN(bank_acc)
return struct(
iban = str(iban_acc),
account = str(bank_acc),
country_id = iban_acc.countrycode,
code = iban_acc.BIC_searchkey,
iban=str(iban_acc),
account=str(bank_acc),
country_id=iban_acc.countrycode,
code=iban_acc.BIC_searchkey,
# Note: BIC can not be constructed here!
bic = False,
bank = False,
bic=False,
bank=False,
)
_account_info = {
# TODO: Add more online data banks
'BA': BBAN_is_IBAN,
@@ -153,6 +164,7 @@ _account_info = {
'SM': BBAN_is_IBAN,
}
def account_info(iso, bank_acc):
'''
Consult the online database for this country to obtain its
@@ -165,9 +177,11 @@ def account_info(iso, bank_acc):
return _account_info[iso](bank_acc)
return False
bic_re = re.compile("[^']+'([^']*)'.*")
SWIFTlink = 'http://www.swift.com/bsl/freequery.do'
def bank_info(bic):
'''
Consult the free online SWIFT service to obtain the name and address of a
@@ -177,7 +191,7 @@ def bank_info(bic):
automated usage, so user like behavior is required.
Update January 2012: Always return None, as the SWIFT page to retrieve the
information does no longer exist.
information does no longer exist.
If demand exists, maybe bite the bullet and integrate with a paid web
service such as http://www.iban-rechner.de.
lp914922 additionally suggests to make online lookup optional.
@@ -190,7 +204,7 @@ def bank_info(bic):
for trsoup in soup('tr'):
for stage, tdsoup in enumerate(trsoup('td')):
if stage == 0:
attr = tdsoup.contents[0].strip().replace(' ','_')
attr = tdsoup.contents[0].strip().replace(' ', '_')
elif stage == 2:
if tdsoup.contents:
retval[attr] = tdsoup.contents[0].strip()
@@ -203,8 +217,8 @@ def bank_info(bic):
request = agent.open(SWIFTlink)
soup = BeautifulSoup(request)
# Parse request form. As this form is intertwined with a table, use the parent
# as root to search for form elements.
# Parse request form. As this form is intertwined with a table, use the
# parent as root to search for form elements.
form = SoupForm(soup.find('form', {'id': 'frmFreeSearch1'}), parent=True)
# Fill form fields
@@ -221,7 +235,8 @@ def bank_info(bic):
if not bic_button:
return None, None
# Overwrite the location with 'any' ('XXX') to narrow the results to one or less.
# Overwrite the location with 'any' ('XXX') to narrow the results to one
# or less.
# Assume this regexp will never fail...
full_bic = bic_re.match(bic_button.get('href')).groups()[0][:8] + 'XXX'
@@ -236,13 +251,13 @@ def bank_info(bic):
soup = BeautifulSoup(response)
# Now parse the results
tables = soup.find('div', {'id':'Middle'}).findAll('table')
tables = soup.find('div', {'id': 'Middle'}).findAll('table')
if not tables:
return None, None
tablesoup = tables[2]('table')
if not tablesoup:
return None, None
codes = harvest(tablesoup[0])
if not codes:
return None, None
@@ -253,9 +268,9 @@ def bank_info(bic):
# banks world wide using the same name.
# The concatenation with the two character country code is for most
# national branches sufficient as a unique identifier.
code = full_bic[:6],
bic = full_bic,
name = codes.Institution_name,
code=full_bic[:6],
bic=full_bic,
name=codes.Institution_name,
)
address = harvest(tablesoup[1])
@@ -264,14 +279,14 @@ def bank_info(bic):
if not address.Zip_Code:
if address.Location:
iso, address.Zip_Code, address.Location = \
postalcode.split(address.Location, full_bic[4:6])
postalcode.split(address.Location, full_bic[4:6])
bankaddress = struct(
street = address.Address.title(),
city = address.Location.strip().title(),
zip = address.Zip_Code,
country = address.Country.title(),
country_id = full_bic[4:6],
street=address.Address.title(),
city=address.Location.strip().title(),
zip=address.Zip_Code,
country=address.Country.title(),
country_id=full_bic[4:6],
)
if ' ' in bankaddress.street:
bankaddress.street, bankaddress.street2 = [
@@ -281,4 +296,3 @@ def bank_info(bic):
bankaddress.street2 = ''
return bankinfo, bankaddress

View File

@@ -5,8 +5,8 @@
# All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
@@ -28,7 +28,8 @@ import urllib
__all__ = ['urlsplit', 'urljoin', 'pathbase', 'urlbase', 'SoupForm',
'URLAgent'
]
]
def urlsplit(url):
'''
@@ -43,7 +44,8 @@ def urlsplit(url):
host, path = urllib.splithost(url)
return (scheme, host, path)
def urljoin(scheme, host, path, args = None):
def urljoin(scheme, host, path, args=None):
'''
Join scheme, host and path to a full URL.
Optional: add urlencoded args.
@@ -54,15 +56,17 @@ def urljoin(scheme, host, path, args = None):
url += '?%s' % urllib.urlencode(args)
return url
def pathbase(path):
'''
Return the base for the path in order to satisfy relative paths.
Helper function.
'''
if path and '/' in path:
return path[:path.rfind('/') +1]
return path[:path.rfind('/') + 1]
return path
def urlbase(url):
'''
Return the base URL for url in order to satisfy relative paths.
@@ -71,6 +75,7 @@ def urlbase(url):
scheme, host, path = urlsplit(url)
return urljoin(scheme, host, pathbase(path))
class SoupForm(object):
'''
A SoupForm is a representation of a HTML Form in BeautifulSoup terms.
@@ -94,7 +99,7 @@ class SoupForm(object):
if parent:
self.soup = soup.parent
# Harvest input elements.
# Harvest input elements.
self._args = {}
for item in self.soup.findAll('input'):
# Make sure to initialize to '' to avoid None strings to appear
@@ -150,6 +155,7 @@ class SoupForm(object):
args.update(self._extra_args)
return args
class URLAgent(object):
'''
Assistent object to ease HTTP(S) requests.
@@ -160,8 +166,12 @@ class URLAgent(object):
super(URLAgent, self).__init__(*args, **kwargs)
self._extra_headers = {}
self.headers = {
'User-Agent': 'Mozilla/5.0 (X11; U; Linux x86_64; us; rv:1.9.0.10) Gecko/2009042708 Fedora/3.0.10-1.fc9 Firefox/3.0.10',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'User-Agent': (
'Mozilla/5.0 (X11; U; Linux x86_64; us; rv:1.9.0.10) '
'Gecko/2009042708 Fedora/3.0.10-1.fc9 Firefox/3.0.10'),
'Accept': (
'text/html,application/xhtml+xml,application/xml;'
'q=0.9,*/*;q=0.8'),
'Accept-Language': 'en-us;q=1.0',
'Accept-Charset': 'UTF-8,*',
'Cache-Control': 'max-age=0'
@@ -193,7 +203,7 @@ class URLAgent(object):
# Get and set cookies for next actions
attributes = request.info()
if attributes.has_key('set-cookie'):
if 'set-cookie' in attributes:
self.agent.addheader('Cookie', attributes['set-cookie'])
# Add referer