Skip to content

Commit

Permalink
Merge pull request #210 from djotaku/output_improvements
Browse files Browse the repository at this point in the history
Output improvements
  • Loading branch information
djotaku authored Mar 19, 2023
2 parents bdeb070 + 2e6232b commit 2e31ba6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linttest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:

steps:
- uses: actions/checkout@v1
- name: Set up Python 3.10.8
- name: Set up Python 3.11.2
uses: actions/setup-python@v1
with:
python-version: 3.10.8
python-version: 3.11.2
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/windowsbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:

steps:
- uses: actions/checkout@v1
- name: Set up Python 3.10.8
- name: Set up Python 3.11.2
uses: actions/setup-python@v1
with:
python-version: 3.10.8
python-version: 3.11.2
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/asottile/pyupgrade
rev: v3.2.2
rev: v3.3.1
hooks:
- id: pyupgrade
2 changes: 1 addition & 1 deletion eldonationtracker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

__version__ = "7.4.10"
__version__ = "7.5.0"

base_api_url: str = "https://www.extra-life.org/api"
api_version_suffix: str = "?version=1.2"
Expand Down
8 changes: 7 additions & 1 deletion eldonationtracker/api/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ def output_milestone_data(self) -> None: # pragma: no cover
for milestone in self.milestones
if milestone.is_complete
}
latest_milestone_output = "No Milestones reached yet"
# this should end up with the highest milestone that is completed being output to the file.
for milestone in self.milestones:
if milestone.is_complete:
latest_milestone_output = f"{milestone.fundraising_goal}: Achievement Unlocked: {milestone.description}"
self.write_text_files(milestone_output)
self.write_text_files({"latest_milestone": latest_milestone_output})

def output_incentive_data(self) -> None: # pragma: no cover
"""Write out the incentive data to a text file."""
Expand Down Expand Up @@ -155,7 +161,7 @@ def run(self) -> None:
self.output_activities()
# Below is protection against a situation where the API is unavailable.
# Prevents bad data being written to the participant output. Based on the assumption that it would
# absurd to have a goal of $0.
# be absurd to have a goal of $0.
if self.goal != 0:
self.output_participant_data()
if self._first_run or self.number_of_donations > number_of_donations: # type ignore
Expand Down
47 changes: 19 additions & 28 deletions eldonationtracker/utils/extralife_io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Holds all the file and internet input and output."""


import contextlib
import json
import logging
import os
Expand All @@ -12,8 +14,6 @@
from donordrivepython.api.badge import Badge # type: ignore
from donordrivepython.api.donation import Donation
from donordrivepython.api.donor import Donor
from rich import print
from rich.logging import RichHandler

from eldonationtracker import file_logging

Expand Down Expand Up @@ -90,19 +90,17 @@ def get_donations(donations_or_donors: list, api_url: str, is_donation=True, lar
json_response = get_json(api_url, largest_first)
if not json_response:
el_io_log.error(f"[bold red]Couldn't access JSON endpoint at {api_url}.[/bold red]")
return donations_or_donors
else:
if is_donation:
donor_or_donation_list = [Donation(this_donation) for this_donation in json_response]
else:
donor_or_donation_list = [Donor(this_donor) for this_donor in json_response] # type: ignore
if len(donations_or_donors) == 0: # if I didn't already have donations....
if not donations_or_donors:
return donor_or_donation_list
else: # add in only the new donations
for a_donation in reversed(donor_or_donation_list):
if a_donation not in donations_or_donors:
donations_or_donors.insert(0, a_donation)
return donations_or_donors
for a_donation in reversed(donor_or_donation_list):
if a_donation not in donations_or_donors:
donations_or_donors.insert(0, a_donation)
return donations_or_donors


def get_badges(api_url: str) -> list[Badge]:
Expand Down Expand Up @@ -350,7 +348,7 @@ def single_format(donor, message: bool, currency_symbol: str) -> str:
return f"{donor.name} - {currency_symbol}{donor.amount:.2f}"


def multiple_format(donors, message: bool, horizontal: bool,
def multiple_format(donors: list[Donor], message: bool, horizontal: bool,
currency_symbol: str, how_many: int) -> str:
"""Format string for output to text file.
Expand Down Expand Up @@ -399,13 +397,12 @@ def format_information_for_output(donation_list: list, currency_symbol: str, don
:param team: If true, this is creating output for a team. Otherwise, for the participant.
:returns: A dictionary with the output text formatted correctly.
"""
donation_formatted_output: dict = {}
prefix = "Team_" if team else ''
middle_text = "Donation" if donation else "Donor"
donation_formatted_output[f'{prefix}Last{middle_text}NameAmnt'] = single_format(donation_list[0],
False, currency_symbol)
donation_formatted_output[f'{prefix}lastN{middle_text}NameAmts'] = \
multiple_format(donation_list, False, False, currency_symbol, int(donors_to_display))
donation_formatted_output: dict = {f'{prefix}Last{middle_text}NameAmnt': single_format(
donation_list[0], False, currency_symbol
), f'{prefix}lastN{middle_text}NameAmts': multiple_format(donation_list, False, False, currency_symbol,
int(donors_to_display))}
if donation:
donation_formatted_output[f'{prefix}lastN{middle_text}NameAmtsMessage'] = \
multiple_format(donation_list, True, False, currency_symbol, int(donors_to_display))
Expand All @@ -416,11 +413,9 @@ def format_information_for_output(donation_list: list, currency_symbol: str, don
return donation_formatted_output


def output_badge_data(badge_list: list[Badge], text_folder: str, team=False) -> None: # pragma: no cover
def output_badge_data(badge_list: list[Badge], text_folder: str, team=False) -> None: # pragma: no cover
"""Write out text and HTML files for badge data."""
prefix = ''
if team:
prefix = "team_"
prefix = "team_" if team else ''
if badge_list:
badge_text_output = {}
badge_url_output = {}
Expand All @@ -435,9 +430,9 @@ def output_badge_data(badge_list: list[Badge], text_folder: str, team=False) ->


def read_in_total_raised(text_folder: str) -> str:
"""This is a temporary hack until I resolve Github issue #162"""
"""This is a temporary hack until I resolve GitHub issue #162"""
try:
with open(f'{text_folder}/totalRaised.txt', 'r', encoding='utf8') as total_raised:
with open(f'{text_folder}/totalRaised.txt', encoding='utf8') as total_raised:
return total_raised.readline()
except FileNotFoundError:
el_io_log.info("[bold blue] totalRaised.txt doesn't exist. This is OK if this is your first run. [/bold blue]")
Expand All @@ -454,10 +449,8 @@ def write_text_files(dictionary: dict, text_folder: str):
:param dictionary: The dictionary with items to output.
:param text_folder: The directory to write the text files.
"""
try:
with contextlib.suppress(FileExistsError):
os.makedirs(text_folder)
except FileExistsError:
pass
for filename, text in dictionary.items():
with open(f'{text_folder}/{filename}.txt', 'w', encoding='utf8') as file:
file.write(text)
Expand All @@ -470,10 +463,8 @@ def write_html_files(data: str, filename: str, text_folder: str):
:param filename: The filename for the HTML file.
:param text_folder: The directory to write the HTML files to.
"""
try:
with contextlib.suppress(FileExistsError):
os.mkdir(text_folder)
except FileExistsError:
pass
html_to_write = "<HTML><body>" + data + "</body></HTML>"
html_to_write = f"<HTML><body>{data}</body></HTML>"
with open(f'{text_folder}/{filename}.html', 'w', encoding='utf8') as html_file:
html_file.write(html_to_write)
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ coverage==6.4.1
dill==0.3.5.1
distlib==0.3.4
docutils==0.18.1
donordrivepython==1.4.5
donordrivepython==1.6.0
filelock==3.7.1
identify==2.5.1
idna==3.3
Expand All @@ -28,10 +28,10 @@ packaging==21.3
platformdirs==2.5.2
pluggy==1.0.0
pprintpp==0.4.0
pre-commit==2.19.0
pre-commit==3.2.0
py==1.11.0
Pygments==2.12.0
pyinstaller==5.1
pyinstaller==5.9.0
pyinstaller-hooks-contrib==2022.7
pyparsing==3.0.9
PyQt5==5.15.7
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ install_requires =
semver == 3.0.0.dev3
requests == 2.28.1
rich == 12.4.4
donordrivepython == 1.4.5
donordrivepython == 1.5.0

0 comments on commit 2e31ba6

Please sign in to comment.