Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions rbiparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import glob
import string

from rbiparser.exceptions import RBIParserError, DownloadError, ParseError, StorageError

try:
from urlparse import urlparse
except ImportError:
Expand Down Expand Up @@ -73,14 +75,14 @@ def get_sheet_urls(url):
"""Scrapes the RBI page and gets the list of .xlsx sheets."""
r = requests.get(url)
if r.status_code != 200:
raise Exception("Invalid response from", url)
raise DownloadError(f"Invalid response from {url}")

# Extract the urls.
s = soup(r.content, "lxml")
links = s.findAll("a", href=re.compile("\.xlsx$"))

if len(links) < 1:
raise Exception("Couldn't find any .xlsx urls")
raise ParseError("Couldn't find any .xlsx urls")

return [l["href"] for l in links]

Expand All @@ -90,7 +92,7 @@ def convert_xlsx_to_csv(src, target, headers):
try:
sheet = xlrd.open_workbook(src).sheet_by_index(0)
except Exception as e:
raise Exception("Can't open sheet.", str(e))
raise ParseError(f"Can't open sheet: {e}")

with open(target, "wb") as cf:
writer = csv.writer(cf, quoting=csv.QUOTE_ALL)
Expand All @@ -108,11 +110,11 @@ def convert_xlsx_to_csv(src, target, headers):
first = True

if len(vals) != len(headers):
raise Exception("Headers don't match.")
raise ParseError("Headers don't match")

writer.writerow(vals)
except Exception as e:
raise Exception("Can't convert sheet.", str(e))
raise ParseError(f"Can't convert sheet: {e}")


def url_to_file(url):
Expand All @@ -135,7 +137,7 @@ def save_etags(etags, fname):
with open(fname, "w") as f:
f.write(json.dumps(etags, indent=4))
except Exception as e:
raise Exception("Could not write to " + fname + ": " + str(e))
raise StorageError(f"Could not write to {fname}: {e}")


def get_url_headers(url):
Expand All @@ -144,7 +146,7 @@ def get_url_headers(url):
r = requests.head(url)
return r.headers
except Exception as e:
raise Exception("Can't reach", url, ": ", str(e))
raise DownloadError(f"Can't reach {url}: {e}")


def download(url, target):
Expand All @@ -153,15 +155,15 @@ def download(url, target):
r = requests.get(url, stream=True)
r.raw.decode_content = True
except Exception as e:
raise Exception("Can't download", url, ": ", str(e))
raise DownloadError(f"Can't download {url}: {e}")

try:
with open(target, "wb") as f:
for chunk in r.iter_content(chunk_size=10000):
if chunk:
f.write(chunk)
except Exception as e:
raise Exception("Can't write ", target, ": ", str(e))
raise StorageError(f"Can't write to {target}: {e}")

return {
"etag": r.headers.get("etag", ""),
Expand Down
11 changes: 9 additions & 2 deletions rbiparser/console.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import click
import rbiparser as rbi
from rbiparser.exceptions import RBIParserError

SOURCE_URL = "https://www.rbi.org.in/scripts/bs_viewcontent.aspx?Id=2009"

Expand All @@ -20,7 +21,10 @@ def cli():
help="Etags file")
def download(source, dest, etag):
"""Download all listed bank documents from RBI as .xlsx format."""
rbi.download_all(source, dest, etag)
try:
rbi.download_all(source, dest, etag)
except RBIParserError as e:
raise click.ClickException(str(e))


@cli.command()
Expand All @@ -30,7 +34,10 @@ def download(source, dest, etag):
help="Target directory for CSV files.")
def convert(source, dest):
"""Convert all xls documents to CSV"""
rbi.convert_all(source, dest, rbi.HEADERS)
try:
rbi.convert_all(source, dest, rbi.HEADERS)
except RBIParserError as e:
raise click.ClickException(str(e))


@cli.command()
Expand Down
29 changes: 29 additions & 0 deletions rbiparser/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class RBIParserError(Exception):
"""Base exception for all rbiparser errors.
Catch this to handle any error from the library."""
pass


class DownloadError(RBIParserError):
"""Raised when downloading files from the RBI website fails."""
pass


class ParseError(RBIParserError):
"""Raised when parsing or converting an Excel/CSV file fails."""
pass


class ValidationError(RBIParserError):
"""Raised when input arguments or data fail validation."""
pass


class StorageError(RBIParserError):
"""Raised when reading or writing files to disk fails."""
pass


class CombineError(RBIParserError):
"""Raised when combining CSV files fails."""
pass