Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retrieve files from icpac's ftp server #27

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Tinkaa
Copy link
Contributor

@Tinkaa Tinkaa commented Nov 9, 2021

  • Functionality to acces ICPAC's FTP server and download a file
  • Proposal on how we could use an .env.example with .dotenv

Leaving at as a draft because

  • Would like to hear your opinion on the usage of .env
  • Question in retrieve_file_ftp if you know a better method to check for not None and make mypy happy?

Copy link
Contributor

@caldwellst caldwellst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put some comments in the code, it looks good! My main other thoughts are:

  • The functions should clearly document the environment variables expected for passing in. (and I think both functions should expect env variables, not mixing up passed in arguments vs env variables). That documentation should probably link to more extensive documentation about ICPAC and how to access/setup these environment variables.
  • We should also just have a library-wide documentation on env variables, which I think should encourage use of .env files but have checking that only ensures the variables are set, not checking how (can we ensure there's no error if a .env file doesn't exist)



def connect_icpac_ftp(
ftp_address: str,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not explicitly pass these variables to connect_icpac_ftp(). I think the docstring should clarify that there are 3 expected environment variables and all the checks for the variables should be performed within this function, rather than retrieve_file_ftp(). Having env variables used for one function and explicit variables in another makes it a confusing API I think.

... output_filepath='example.nc')
"""
load_dotenv()
ftp_address = os.getenv("ICPAC_FTP_ADDRESS")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, I would move this to connect_icpac_ftp().

"""
load_dotenv()
ftp_address = os.getenv("ICPAC_FTP_ADDRESS")
ftp_username = os.getenv("ICPAC_FTP_USERNAME")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using a list could be easier and make for more informative error messaging. Maybe something like this?

env_vars = ["ICPAC_FTP_ADDRESS", "ICPAC_FTP_USERNAME", "ICPACP_FTP_PASSWORD"]
ftp_config = [os.getenv(x) for x in env_vars]
ftp_missing = [env_vars[i] for i,e in enumerate(ftp_config) if e is None]
if not ftp_missing:
     raise RuntimeError(
          f"FTP environment variable(s) missing: {', '.join(ftp_missing)}"
     )

if None in (ftp_address, ftp_username, ftp_password):
raise RuntimeError("One of the ftp variables is not set")
# TODO: ugly, is there a better method? if not doing, mypy complains
assert ftp_address is not None, ftp_address
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are already running the check above, I think explicitly ignoring the mypy error in this case is acceptable I think? For instance, this SO post recommends using assert or a check if it's missing, which has happened, just might be that mypy doesn't detect that, so safe to ignore error I think rather than duplicating with the assert.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh so I actually stopped running mypy from running on the tests in the COD AB PR, there were some super annoying issues with pytest fixtures and I thought it's probably not needed on the tests anyway. I should have flagged it more explicitly though, sorry guys. If you think we should put it back I will try to sort ou the fixture issues!

... 'PredictedRainfallProbbability-FMA2021_Jan2021.nc',
... output_filepath='example.nc')
"""
load_dotenv()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be using find_dotenv() as well here as per this SO post? Where does load_dotenv() on its own expect to find the file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the documentation of load_dotenv(), there is no warning if the file isn't found by default, so might want to be careful here. However, by default, the code also shows that find_dotenv() is used, so hopefully shouldn't be too problematic?

@Tinkaa Tinkaa mentioned this pull request Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants