Skip to content

Commit

Permalink
add try/except to CTA-file request.get request for company firewall (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-sch authored Sep 22, 2023
1 parent f4bd7db commit 56c131c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
39 changes: 23 additions & 16 deletions SBTi/data/sbti.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,33 @@ def __init__(
self, config: Type[PortfolioCoverageTVPConfig] = PortfolioCoverageTVPConfig
):
self.c = config
# Fetch CTA file from SBTi website
resp = requests.get(self.c.CTA_FILE_URL)
# If status code == 200 then Write CTA file to disk
if resp.status_code == 200:
with open(self.c.FILE_TARGETS, 'wb') as output:
output.write(resp.content)
print(f'Status code from fetching the CTA file: {resp.status_code}, 200 = OK')
# Read CTA file into pandas dataframe
# Suppress warning about openpyxl - check if this is still needed in the released version.
else:
print('Could not fetch the CTA file from the SBTi website')
print('Will read older file from this package version')


fallback_err_log_statement = 'Will read older file from this package version'
try:
# Fetch CTA file from SBTi website
resp = requests.get(self.c.CTA_FILE_URL)

# If status code == 200 then write CTA file to disk
if resp.ok:
with open(self.c.FILE_TARGETS, 'wb') as output:
output.write(resp.content)
print(f'Status code from fetching the CTA file: {resp.status_code}, 200 = OK')
else:
print(f'Non-200 status code when fetching the CTA file from the SBTi website: {resp.status_code}')
print(fallback_err_log_statement)

except requests.exceptions.RequestException as e:
print(f'Exception when fetching the CTA file from the SBTi website: {e}')
print(fallback_err_log_statement)

# Read CTA file into pandas dataframe
# Suppress warning about openpyxl - check if this is still needed in the released version.
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
self.targets = pd.read_excel(self.c.FILE_TARGETS)



def filter_cta_file(self, targets):
"""
Filter the CTA file to create a datafram that has on row per company
Filter the CTA file to create a dataframe that has one row per company
with the columns "Action" and "Target".
If Action = Target then only keep the rows where Target = Near-term.
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup_kwargs = {
"name": "sbti-finance-tool",
"version": "1.0.9",
"version": "1.0.10",
"description": "This package helps companies and financial institutions to assess the temperature alignment of current targets, commitments, and investment and lending portfolios, and to use this information to develop targets for official validation by the SBTi.'",
"long_description": "> Visit https://sciencebasedtargets.github.io/SBTi-finance-tool/ for the full documentation\n\n> If you have any additional questions or comments send a mail to: finance@sciencebasedtargets.org\n\n# SBTi Temperature Alignment tool\n\nThis package helps companies and financial institutions to assess the temperature alignment of current\ntargets, commitments, and investment and lending portfolios, and to use this information to develop\ntargets for official validation by the SBTi.\n\nThis tool can be used either as a standalone Python package, a REST API or as a simple webapp which provides a simple skin on the API.\nSo, the SBTi toolkit caters for three types of usage:\n\n- Users can integrate the Python package in their codebase\n- The tool can be included as a Microservice (containerised REST API) in any IT infrastructure (in the cloud or on premise)\n- As an webapp, exposing the functionality with a simple user interface.\n\nTo following diagram provides an overview of the different parts of the toolkit:\n\n +-------------------------------------------------+\n | UI : Simple user interface on top of API |\n | Install: via dockerhub |\n | docker.io/sbti/ui:latest |\n | |\n | +-----------------------------------------+ |\n | | REST API: Dockerized FastAPI/NGINX | |\n | | Source : github.com/OFBDABV/SBTi_api | |\n | | Install: via source or dockerhub | |\n | | docker.io/sbti/sbti/api:latest | |\n | | | |\n | | +---------------------------------+ | |\n | | | | | |\n | | |Core : Python Module | | |\n | | |Source : github.com/ScienceBasedTargets/ |\n | | | SBTi-finance-tool | | |\n | | |Install: via source or PyPi | | |\n | | | | | |\n | | +---------------------------------+ | |\n | +-----------------------------------------+ |\n +-------------------------------------------------+\n\nAs shown above the API is dependent on the Python Repo, in the same way the UI requires the API backend. These dependencies are scripted in the Docker files.\n\n> This repository only contains the Python module. If you'd like to use the REST API, please refer to [this repository](https://github.com/ScienceBasedTargets/SBTi-finance-tool_api) or the same repository on [Dockerhub](https://docker.io/sbti/sbti/api:latest).\n\n## Structure\n\nThe folder structure for this project is as follows:\n\n .\n ├── .github # Github specific files (Github Actions workflows)\n ├── app # FastAPI app files for the API endpoints\n ├── docs # Documentation files (Sphinx)\n ├── config # Config files for the Docker container\n ├── SBTi # The main Python package for the temperature alignment tool\n └── test # Automated unit tests for the SBTi package (Nose2 tests)\n\n## Installation\n\nThe SBTi package may be installed using PIP. If you'd like to install it locally use the following command. For testing or production please see the deployment section for further instructions\n\n```bash\npip install -e .\n```\n\nFor installing the latest stable release in PyPi run:\n\n```bash\npip install sbti\n```\n\n## Development\n\nTo set up the local dev environment with all dependencies, [install poetry](https://python-poetry.org/docs/#osx--linux--bashonwindows-install-instructions) and run\n\n```bash\npoetry install\n```\n\nThis will create a virtual environment inside the project folder under `.venv`.\n\n### Testing\n\nEach class should be unit tested. The unit tests are written using the Nose2 framework.\nThe setup.py script should have already installed Nose2, so now you may run the tests as follows:\n\n```bash\nnose2 -v\n```\n\n### Publish to PyPi\n\nThe package should be published to PyPi when any changes to main are merged.\n\nUpdate package\n\n1. bump version in `pyproject.toml` based on semantic versioning principles\n2. run `poetry build`\n3. run `poetry publish`\n4. check whether package has been successfully uploaded\n\n**Initial Setup**\n\n- Create account on [PyPi](https://pypi.org/)\n",
"author": "sbti",
Expand Down

0 comments on commit 56c131c

Please sign in to comment.