1
1
"""Holds all the file and internet input and output."""
2
2
3
+
4
+ import contextlib
3
5
import json
4
6
import logging
5
7
import os
12
14
from donordrivepython .api .badge import Badge # type: ignore
13
15
from donordrivepython .api .donation import Donation
14
16
from donordrivepython .api .donor import Donor
15
- from rich import print
16
- from rich .logging import RichHandler
17
17
18
18
from eldonationtracker import file_logging
19
19
@@ -90,19 +90,17 @@ def get_donations(donations_or_donors: list, api_url: str, is_donation=True, lar
90
90
json_response = get_json (api_url , largest_first )
91
91
if not json_response :
92
92
el_io_log .error (f"[bold red]Couldn't access JSON endpoint at { api_url } .[/bold red]" )
93
- return donations_or_donors
94
93
else :
95
94
if is_donation :
96
95
donor_or_donation_list = [Donation (this_donation ) for this_donation in json_response ]
97
96
else :
98
97
donor_or_donation_list = [Donor (this_donor ) for this_donor in json_response ] # type: ignore
99
- if len ( donations_or_donors ) == 0 : # if I didn't already have donations....
98
+ if not donations_or_donors :
100
99
return donor_or_donation_list
101
- else : # add in only the new donations
102
- for a_donation in reversed (donor_or_donation_list ):
103
- if a_donation not in donations_or_donors :
104
- donations_or_donors .insert (0 , a_donation )
105
- return donations_or_donors
100
+ for a_donation in reversed (donor_or_donation_list ):
101
+ if a_donation not in donations_or_donors :
102
+ donations_or_donors .insert (0 , a_donation )
103
+ return donations_or_donors
106
104
107
105
108
106
def get_badges (api_url : str ) -> list [Badge ]:
@@ -350,7 +348,7 @@ def single_format(donor, message: bool, currency_symbol: str) -> str:
350
348
return f"{ donor .name } - { currency_symbol } { donor .amount :.2f} "
351
349
352
350
353
- def multiple_format (donors , message : bool , horizontal : bool ,
351
+ def multiple_format (donors : list [ Donor ] , message : bool , horizontal : bool ,
354
352
currency_symbol : str , how_many : int ) -> str :
355
353
"""Format string for output to text file.
356
354
@@ -399,13 +397,12 @@ def format_information_for_output(donation_list: list, currency_symbol: str, don
399
397
:param team: If true, this is creating output for a team. Otherwise, for the participant.
400
398
:returns: A dictionary with the output text formatted correctly.
401
399
"""
402
- donation_formatted_output : dict = {}
403
400
prefix = "Team_" if team else ''
404
401
middle_text = "Donation" if donation else "Donor"
405
- donation_formatted_output [ f'{ prefix } Last{ middle_text } NameAmnt' ] = single_format (donation_list [ 0 ],
406
- False , currency_symbol )
407
- donation_formatted_output [ f'{ prefix } lastN{ middle_text } NameAmts' ] = \
408
- multiple_format ( donation_list , False , False , currency_symbol , int (donors_to_display ))
402
+ donation_formatted_output : dict = { f'{ prefix } Last{ middle_text } NameAmnt' : single_format (
403
+ donation_list [ 0 ], False , currency_symbol
404
+ ), f'{ prefix } lastN{ middle_text } NameAmts' : multiple_format ( donation_list , False , False , currency_symbol ,
405
+ int (donors_to_display ))}
409
406
if donation :
410
407
donation_formatted_output [f'{ prefix } lastN{ middle_text } NameAmtsMessage' ] = \
411
408
multiple_format (donation_list , True , False , currency_symbol , int (donors_to_display ))
@@ -416,11 +413,9 @@ def format_information_for_output(donation_list: list, currency_symbol: str, don
416
413
return donation_formatted_output
417
414
418
415
419
- def output_badge_data (badge_list : list [Badge ], text_folder : str , team = False ) -> None : # pragma: no cover
416
+ def output_badge_data (badge_list : list [Badge ], text_folder : str , team = False ) -> None : # pragma: no cover
420
417
"""Write out text and HTML files for badge data."""
421
- prefix = ''
422
- if team :
423
- prefix = "team_"
418
+ prefix = "team_" if team else ''
424
419
if badge_list :
425
420
badge_text_output = {}
426
421
badge_url_output = {}
@@ -435,9 +430,9 @@ def output_badge_data(badge_list: list[Badge], text_folder: str, team=False) ->
435
430
436
431
437
432
def read_in_total_raised (text_folder : str ) -> str :
438
- """This is a temporary hack until I resolve Github issue #162"""
433
+ """This is a temporary hack until I resolve GitHub issue #162"""
439
434
try :
440
- with open (f'{ text_folder } /totalRaised.txt' , 'r' , encoding = 'utf8' ) as total_raised :
435
+ with open (f'{ text_folder } /totalRaised.txt' , encoding = 'utf8' ) as total_raised :
441
436
return total_raised .readline ()
442
437
except FileNotFoundError :
443
438
el_io_log .info ("[bold blue] totalRaised.txt doesn't exist. This is OK if this is your first run. [/bold blue]" )
@@ -454,10 +449,8 @@ def write_text_files(dictionary: dict, text_folder: str):
454
449
:param dictionary: The dictionary with items to output.
455
450
:param text_folder: The directory to write the text files.
456
451
"""
457
- try :
452
+ with contextlib . suppress ( FileExistsError ) :
458
453
os .makedirs (text_folder )
459
- except FileExistsError :
460
- pass
461
454
for filename , text in dictionary .items ():
462
455
with open (f'{ text_folder } /{ filename } .txt' , 'w' , encoding = 'utf8' ) as file :
463
456
file .write (text )
@@ -470,10 +463,8 @@ def write_html_files(data: str, filename: str, text_folder: str):
470
463
:param filename: The filename for the HTML file.
471
464
:param text_folder: The directory to write the HTML files to.
472
465
"""
473
- try :
466
+ with contextlib . suppress ( FileExistsError ) :
474
467
os .mkdir (text_folder )
475
- except FileExistsError :
476
- pass
477
- html_to_write = "<HTML><body>" + data + "</body></HTML>"
468
+ html_to_write = f"<HTML><body>{ data } </body></HTML>"
478
469
with open (f'{ text_folder } /{ filename } .html' , 'w' , encoding = 'utf8' ) as html_file :
479
470
html_file .write (html_to_write )
0 commit comments