Skip to content

Commit 6e5b43a

Browse files
committed
Update source code modules so documentation can build
1 parent 12d8c96 commit 6e5b43a

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

pydis_core/exts/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Reusable Discord cogs."""
2-
from .source import SourceCode
2+
from pydis_core.exts import source
33

4-
__all__ = [SourceCode]
4+
__all__ = [source]
55

66
__all__ = [module.__name__ for module in __all__]

pydis_core/exts/source.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Pre-built cog to display source code links for commands and cogs."""
12
import enum
23
import inspect
34
from pathlib import Path
@@ -11,22 +12,22 @@
1112
from pydis_core import BotBase as Bot
1213

1314

14-
class TagIdentifierStub(NamedTuple):
15+
class _TagIdentifierStub(NamedTuple):
1516
"""A minmally functioning stub representing a tag identifier."""
1617

1718
group: str | None
1819
name: str
1920

2021
@classmethod
21-
def from_string(cls, string: str) -> "TagIdentifierStub":
22+
def from_string(cls, string: str) -> "_TagIdentifierStub":
2223
"""Create a TagIdentifierStub from a string."""
2324
split_string = string.split(" ", maxsplit=2)
2425
if len(split_string) == 1:
2526
return cls(None, split_string[0])
2627
return cls(split_string[0], split_string[1])
2728

2829

29-
class SourceType(enum.StrEnum):
30+
class _SourceType(enum.StrEnum):
3031
"""The types of source objects recognized by the source command."""
3132

3233
help_command = enum.auto()
@@ -71,46 +72,46 @@ async def source_command(
7172
await ctx.send(embed=embed)
7273

7374
@staticmethod
74-
async def _get_source_object(ctx: commands.Context, argument: str) -> tuple[object, SourceType]:
75+
async def _get_source_object(ctx: commands.Context, argument: str) -> tuple[object, _SourceType]:
7576
"""Convert argument into the source object and source type."""
7677
if argument.lower() == "help":
77-
return ctx.bot.help_command, SourceType.help_command
78+
return ctx.bot.help_command, _SourceType.help_command
7879

7980
cog = ctx.bot.get_cog(argument)
8081
if cog:
81-
return cog, SourceType.cog
82+
return cog, _SourceType.cog
8283

8384
cmd = ctx.bot.get_command(argument)
8485
if cmd:
85-
return cmd, SourceType.command
86+
return cmd, _SourceType.command
8687

8788
tags_cog = ctx.bot.get_cog("Tags")
8889
show_tag = True
8990

9091
if not tags_cog:
9192
show_tag = False
9293
else:
93-
identifier = TagIdentifierStub.from_string(argument.lower())
94+
identifier = _TagIdentifierStub.from_string(argument.lower())
9495
if identifier in tags_cog.tags:
95-
return identifier, SourceType.tag
96+
return identifier, _SourceType.tag
9697

9798
escaped_arg = escape_markdown(argument)
9899

99100
raise commands.BadArgument(
100101
f"Unable to convert '{escaped_arg}' to valid command{', tag,' if show_tag else ''} or Cog."
101102
)
102103

103-
def _get_source_link(self, source_item: object, source_type: SourceType) -> tuple[str, str, int | None]:
104+
def _get_source_link(self, source_item: object, source_type: _SourceType) -> tuple[str, str, int | None]:
104105
"""
105106
Build GitHub link of source item, return this link, file location and first line number.
106107
107108
Raise BadArgument if `source_item` is a dynamically-created object (e.g. via internal eval).
108109
"""
109-
if source_type == SourceType.command:
110+
if source_type == _SourceType.command:
110111
source_item = inspect.unwrap(source_item.callback)
111112
src = source_item.__code__
112113
filename = src.co_filename
113-
elif source_type == SourceType.tag:
114+
elif source_type == _SourceType.tag:
114115
tags_cog = self.bot.get_cog("Tags")
115116
filename = tags_cog.tags[source_item].file_path
116117
else:
@@ -120,7 +121,7 @@ def _get_source_link(self, source_item: object, source_type: SourceType) -> tupl
120121
except TypeError:
121122
raise commands.BadArgument("Cannot get source for a dynamically-created object.")
122123

123-
if source_type != SourceType.tag:
124+
if source_type != _SourceType.tag:
124125
try:
125126
lines, first_line_no = inspect.getsourcelines(src)
126127
except OSError:
@@ -141,17 +142,17 @@ def _get_source_link(self, source_item: object, source_type: SourceType) -> tupl
141142

142143
return url, file_location, first_line_no or None
143144

144-
async def _build_embed(self, source_object: object, source_type: SourceType) -> Embed | None:
145+
async def _build_embed(self, source_object: object, source_type: _SourceType) -> Embed | None:
145146
"""Build embed based on source object."""
146147
url, location, first_line = self._get_source_link(source_object, source_type)
147148

148-
if source_type == SourceType.help_command:
149+
if source_type == _SourceType.help_command:
149150
title = "Help Command"
150151
description = source_object.__doc__.splitlines()[1]
151-
elif source_type == SourceType.command:
152+
elif source_type == _SourceType.command:
152153
description = source_object.short_doc
153154
title = f"Command: {source_object.qualified_name}"
154-
elif source_type == SourceType.tag:
155+
elif source_type == _SourceType.tag:
155156
title = f"Tag: {source_object}"
156157
description = ""
157158
else:

0 commit comments

Comments
 (0)