Skip to content

Commit

Permalink
BUGFIX(download): _fetch_file gives permission denied depending where…
Browse files Browse the repository at this point in the history
… the code is executed. (#106)
  • Loading branch information
luabida authored Dec 6, 2022
1 parent 84bac0c commit 891e3ac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 0 additions & 1 deletion pysus/online_data/SINAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ def download(disease, year, return_chunks=False, data_path="/tmp/pysus"):
logger.debug(f"{fname} file not found. Proceeding to download..")
try:
_fetch_file(fname, sus_path, "DBC", return_df=False)
shutil.move(Path(fname), data_path)
logger.info(f"{fname} downloaded at {data_path}")

except Exception as e:
Expand Down
9 changes: 7 additions & 2 deletions pysus/online_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ def _fetch_file(
ftp = FTP("ftp.datasus.gov.br")
ftp.login()
ftp.cwd(path)

Path('/tmp/pysus').mkdir(exist_ok=True)

try:
ftp.retrbinary("RETR {}".format(fname), open(fname, "wb").write)
ftp.retrbinary(f"RETR {fname}", open(f'/tmp/pysus/{fname}', "wb").write)
except Exception:
raise Exception("File {} not available on {}".format(fname, path))
if return_df:
df = get_dataframe(fname, ftype)
df = get_dataframe(f'/tmp/pysus/{fname}', ftype)
return df
else:
return pd.DataFrame()
Expand All @@ -65,6 +68,8 @@ def get_dataframe(fname: str, ftype: str) -> pd.DataFrame:
:param ftype: 'DBC' or 'DBF'
:return: DataFrame
"""
fname = f'/tmp/pysus/{fname}'

if ftype == "DBC":
df = read_dbc(fname, encoding="iso-8859-1", raw=False)
elif ftype == "DBF":
Expand Down

0 comments on commit 891e3ac

Please sign in to comment.