generated from michabirklbauer/python_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
149 lines (123 loc) · 4.65 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
# APMA NEWSLETTER GENERATOR
# 2024 (c) Micha Johannes Birklbauer
# https://github.com/michabirklbauer/
# micha.birklbauer@gmail.com
# version tracking
__version = "1.0.0"
__date = "2024-05-02"
# import packages
from datetime import datetime
from defaults import APMA_CONTENT_DEFAULT
from defaults import APMA_JOURNAL_CLUB_DEFAULT
from defaults import APMA_OMICS_HUB_DEFAULT
from defaults import EUPA_NEWS_DEFAULT
from defaults import NATIONAL_EVENTS_DEFAULT
from defaults import INTERNATIONAL_EVENTS_DEFAULT
####### FUNCTIONS #######
def get_issue_date() -> str:
"""Returns the issue date in the style 'Q{Quartal}, Month Year'.
Returns
-------
issue_date : str
The issue date based on the current date.
Examples
--------
>>> from functions import get_issue_date
>>> get_issue_date()
Q2, May 2024
"""
months = {1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December"}
today = datetime.today().strftime("%Y-%m-%d")
q = int(int(today.split("-")[1]) / 4) + 1
month = months[int(today.split("-")[1])]
year = int(today.split("-")[0])
return f"Q{q}, {month} {year}"
def get_current_year() -> str:
"""Returns the current year.
Returns
-------
current_year : str
The current year based on the current date.
Examples
--------
>>> from functions import get_current_year
>>> get_current_year()
2024
"""
return datetime.today().strftime("%Y-%m-%d").split("-")[0]
def generate_html(apma_content: str = APMA_CONTENT_DEFAULT,
apma_journal_club: str = APMA_JOURNAL_CLUB_DEFAULT,
apma_omics_hub: str = APMA_OMICS_HUB_DEFAULT,
eupa_news: str = EUPA_NEWS_DEFAULT,
national_events: str = NATIONAL_EVENTS_DEFAULT,
international_events: str = INTERNATIONAL_EVENTS_DEFAULT,
save_html: bool = False) -> str:
"""Generates the html for the current newsletter based on the given input.
Parameters
----------
apma_content : str
APMA relevant announcements about organisatorial stuff.
apma_journal_club : str
APMA Journal Club announcement text.
apma_omics_hub : str
APMA Omics Tech Hub announcement text.
eupa_news : str
EUPA and EuBIC related announcements.
national_events : str
Announcements about national events.
international_events : str
Announcements about international events.
save_html : bool, default = False
Whether or not to save the generated html to file.
Returns
-------
html : str
The html for the current newsletter based on the given input.
Examples
--------
>>> from functions import generate_html
>>> html = generate_html()
"""
if apma_content is None or apma_content.strip() == "":
apma_content = "template:{APMA_CONTENT}"
if apma_journal_club is None or apma_journal_club.strip() == "":
apma_journal_club = "template:{APMA_JOURNAL_CLUB}"
if apma_omics_hub is None or apma_omics_hub.strip() == "":
apma_omics_hub = "template:{APMA_OMICS_HUB}"
if eupa_news is None or eupa_news.strip() == "":
eupa_news = "template:{EUPA_NEWS}"
if national_events is None or national_events.strip() == "":
national_events = "template:{NATIONAL_EVENTS}"
if international_events is None or international_events.strip() == "":
international_events = "template:{INTERNATIONAL_EVENTS}"
with open("template.html", "r", encoding = "utf-8") as f:
html = f.read()
f.close()
issue_date = get_issue_date()
current_year = get_current_year()
html = html.replace("template:{ISSUE_DATE}", issue_date)
html = html.replace("template:{APMA_CONTENT}", apma_content)
html = html.replace("template:{APMA_JOURNAL_CLUB}", apma_journal_club)
html = html.replace("template:{APMA_OMICS_HUB}", apma_omics_hub)
html = html.replace("template:{EUPA_NEWS}", eupa_news)
html = html.replace("template:{NATIONAL_EVENTS}", national_events)
html = html.replace("template:{INTERNATIONAL_EVENTS}", international_events)
html = html.replace("template:{CURRENT_YEAR}", current_year)
if save_html:
filename = "apma_newsletter_" + issue_date.replace(" ", "-").replace(",", "") + ".html"
with open(filename, "w", encoding = "utf-8") as f:
f.write(html)
f.close()
return html