Skip to content

Commit b803d20

Browse files
committed
Added docstring for changelog.py
1 parent 8a11a8c commit b803d20

File tree

3 files changed

+98
-12
lines changed

3 files changed

+98
-12
lines changed

bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from emoji import UNICODE_EMOJI
4141
from motor.motor_asyncio import AsyncIOMotorClient
4242

43-
from core.changelog import ChangeLog
43+
from core.changelog import Changelog
4444
from core.clients import ModmailApiClient, SelfHostedClient
4545
from core.config import ConfigManager
4646
from core.models import Bot
@@ -657,7 +657,7 @@ async def autoupdate_loop(self):
657657
embed.set_footer(text=f"Updating Modmail v{self.version} "
658658
f"-> v{metadata['latest_version']}")
659659

660-
changelog = await ChangeLog.from_repo(self)
660+
changelog = await Changelog.from_url(self)
661661
latest = changelog.latest_version
662662
embed.description = latest.description
663663
for name, value in latest.fields.items():

cogs/utility.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from discord.ext import commands
1414

1515
from core import checks
16-
from core.changelog import ChangeLog
16+
from core.changelog import Changelog
1717
from core.decorators import github_access_token_required, trigger_typing
1818
from core.models import Bot
1919
from core.paginator import PaginatorSession
@@ -135,7 +135,7 @@ async def help(self, ctx, *, command: str = None):
135135
@trigger_typing
136136
async def changelog(self, ctx):
137137
"""Show a paginated changelog of the bot."""
138-
changelog = await ChangeLog.from_repo(self.bot)
138+
changelog = await Changelog.from_url(self.bot)
139139
paginator = PaginatorSession(ctx, *changelog.embeds)
140140
await paginator.run()
141141

@@ -253,7 +253,7 @@ async def update(self, ctx):
253253
embed.set_author(name=user['username'] + ' - Updating bot',
254254
icon_url=user['avatar_url'],
255255
url=user['url'])
256-
changelog = await ChangeLog.from_repo(self.bot)
256+
changelog = await Changelog.from_url(self.bot)
257257
latest = changelog.latest_version
258258
embed.description = latest.description
259259
for name, value in latest.fields.items():

core/changelog.py

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88

99

1010
class Version:
11+
"""
12+
This class represents a single version of Modmail.
13+
14+
Parameters
15+
----------
16+
bot : Bot
17+
The Modmail bot.
18+
version : str
19+
The version string (ie. "v2.12.0").
20+
lines : str
21+
The lines of changelog messages for this version.
22+
23+
Attributes
24+
----------
25+
bot : Bot
26+
The Modmail bot.
27+
version : str
28+
The version string (ie. "v2.12.0").
29+
lines : List[str]
30+
A list of lines of changelog messages for this version.
31+
fields : defaultdict[str, str]
32+
A dict of fields separated by "Fixed", "Changed", etc sections.
33+
description : str
34+
General description of the version.
35+
"""
36+
1137
def __init__(self, bot: Bot, version: str, lines: str):
1238
self.bot = bot
1339
self.version = version
@@ -20,6 +46,13 @@ def __repr__(self) -> str:
2046
return f'Version({self.version}, description="{self.description}")'
2147

2248
def parse(self) -> None:
49+
"""
50+
Parse the lines and split them into `description` and `fields`.
51+
.
52+
Returns
53+
-------
54+
None
55+
"""
2356
curr_action = None
2457

2558
for line in self.lines:
@@ -32,6 +65,9 @@ def parse(self) -> None:
3265

3366
@property
3467
def embed(self) -> Embed:
68+
"""
69+
Embed: the formatted `Embed` of this `Version`.
70+
"""
3571
embed = Embed(color=Color.green(), description=self.description)
3672
embed.set_author(
3773
name=f'{self.version} - Changelog',
@@ -46,31 +82,81 @@ def embed(self) -> Embed:
4682
return embed
4783

4884

49-
class ChangeLog:
50-
changelog_url = ('https://raw.githubusercontent.com/'
85+
class Changelog:
86+
"""
87+
This class represents the complete changelog of Modmail.
88+
89+
Parameters
90+
----------
91+
bot : Bot
92+
The Modmail bot.
93+
text : str
94+
The complete changelog text.
95+
96+
Attributes
97+
----------
98+
bot : Bot
99+
The Modmail bot.
100+
text : str
101+
The complete changelog text.
102+
versions : List[Version]
103+
A list of `Version`'s within the changelog.
104+
105+
Class Attributes
106+
----------------
107+
CHANGELOG_URL : str
108+
The URL to Modmail changelog.
109+
VERSION_REGEX : re.Pattern
110+
The regex used to parse the versions.
111+
"""
112+
113+
CHANGELOG_URL = ('https://raw.githubusercontent.com/'
51114
'kyb3r/modmail/master/CHANGELOG.md')
52-
regex = re.compile(r'# (v\d+\.\d+\.\d+)([\S\s]*?(?=# v|$))')
115+
VERSION_REGEX = re.compile(r'# (v\d+\.\d+\.\d+)([\S\s]*?(?=# v|$))')
53116

54117
def __init__(self, bot: Bot, text: str):
55118
self.bot = bot
56119
self.text = text
57-
self.versions = [Version(bot, *m) for m in self.regex.findall(text)]
120+
self.versions = [Version(bot, *m)
121+
for m in self.VERSION_REGEX.findall(text)]
58122

59123
@property
60124
def latest_version(self) -> Version:
125+
"""
126+
Version: The latest `Version` of the `Changelog`.
127+
"""
61128
return self.versions[0]
62129

63130
@property
64131
def embeds(self) -> List[Embed]:
132+
"""
133+
List[Embed]: A list of `Embed`'s for each of the `Version`.
134+
"""
65135
return [v.embed for v in self.versions]
66136

67137
@classmethod
68-
async def from_repo(cls, bot, url: str = '') -> 'ChangeLog':
69-
url = url or cls.changelog_url
138+
async def from_url(cls, bot: Bot, url: str = '') -> 'Changelog':
139+
"""
140+
Create a `Changelog` from a URL.
141+
142+
Parameters
143+
----------
144+
bot : Bot
145+
The Modmail bot.
146+
url : str, optional
147+
Defaults to `CHANGELOG_URL`.
148+
The URL to the changelog.
149+
150+
Returns
151+
-------
152+
Changelog
153+
The newly created `Changelog` parsed from the `url`.
154+
"""
155+
url = url or cls.CHANGELOG_URL
70156
resp = await bot.session.get(url)
71157
return cls(bot, await resp.text())
72158

73159

74160
if __name__ == '__main__':
75161
with open('../CHANGELOG.md') as f:
76-
print(ChangeLog(..., f.read()).latest_version)
162+
print(Changelog(..., f.read()).latest_version)

0 commit comments

Comments
 (0)