From 891e3accae778ed3afe725ebf4a52261e059f1eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=A3=20Bida=20Vacaro?= <82233055+luabida@users.noreply.github.com> Date: Tue, 6 Dec 2022 13:13:02 -0300 Subject: [PATCH] BUGFIX(download): _fetch_file gives permission denied depending where the code is executed. (#106) --- pysus/online_data/SINAN.py | 1 - pysus/online_data/__init__.py | 9 +++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pysus/online_data/SINAN.py b/pysus/online_data/SINAN.py index 5947ea82..8d51d672 100644 --- a/pysus/online_data/SINAN.py +++ b/pysus/online_data/SINAN.py @@ -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: diff --git a/pysus/online_data/__init__.py b/pysus/online_data/__init__.py index 7b61e06a..9e175f65 100644 --- a/pysus/online_data/__init__.py +++ b/pysus/online_data/__init__.py @@ -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() @@ -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":