-
-
Notifications
You must be signed in to change notification settings - Fork 42
feat(encode_decode): add encoder and decoder for multiple coding systems #949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Reviewer's GuideThis PR introduces an EncodeDecode cog adding two hybrid commands, encode and decode, that support multiple base coding systems (Base16, Base32, Base64, Base85) with proper error handling and usage documentation. Class diagram for the new EncodeDecode cogclassDiagram
class EncodeDecode {
- bot: Tux
+ __init__(bot: Tux)
+ encode(ctx, cs, text)
+ decode(ctx, cs, text)
}
EncodeDecode --|> commands.Cog
class Tux
class commands.Cog
Flow diagram for encode/decode command logicflowchart TD
A[User invokes encode/decode command] --> B{Is coding system valid?}
B -- No --> C[Reply: Invalid coding system]
B -- Yes --> D[Try to encode/decode text]
D -- Success --> E[Reply: Encoded/Decoded text]
D -- Error --> F[Reply: Error message]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @amadaluzia - I've reviewed your changes - here's some feedback:
- Extract the shared coding‐system validation and reply logic into a helper to DRY up the encode/decode commands.
- Catch more specific decoding/encoding exceptions (e.g. binascii.Error) rather than a broad Exception to avoid masking unexpected failures.
- Make the invalid coding system responses ephemeral to prevent cluttering public channels with error messages.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract the shared coding‐system validation and reply logic into a helper to DRY up the encode/decode commands.
- Catch more specific decoding/encoding exceptions (e.g. binascii.Error) rather than a broad Exception to avoid masking unexpected failures.
- Make the invalid coding system responses ephemeral to prevent cluttering public channels with error messages.
## Individual Comments
### Comment 1
<location> `tux/cogs/utility/encode_decode.py:59` </location>
<code_context>
+ The text you want to encode.
+ """
+
+ if cs not in CODING_SYSTEMS:
+ await ctx.reply(
+ content=invalid_cs_message,
</code_context>
<issue_to_address>
Coding system check is case-sensitive, which may confuse users.
Consider normalizing the input (e.g., cs.lower()) to accept case-insensitive values and improve usability.
</issue_to_address>
### Comment 2
<location> `tux/cogs/utility/encode_decode.py:67` </location>
<code_context>
+ return
+
+ try:
+ data = CODING_SYSTEMS[cs][0](text.encode(encoding="utf-8")).decode(encoding="utf-8")
+ await ctx.reply(
+ content=data,
</code_context>
<issue_to_address>
No output length limit may cause issues with large inputs.
Consider checking the output length and truncating or warning if it exceeds Discord's message limit.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
data = CODING_SYSTEMS[cs][0](text.encode(encoding="utf-8")).decode(encoding="utf-8")
await ctx.reply(
content=data,
allowed_mentions=allowed_mentions,
)
=======
data = CODING_SYSTEMS[cs][0](text.encode(encoding="utf-8")).decode(encoding="utf-8")
DISCORD_MESSAGE_LIMIT = 2000
if len(data) > DISCORD_MESSAGE_LIMIT:
truncated_data = data[:DISCORD_MESSAGE_LIMIT - 50] + "\n...[truncated, output exceeded Discord's 2000 character limit]"
await ctx.reply(
content=truncated_data,
allowed_mentions=allowed_mentions,
)
else:
await ctx.reply(
content=data,
allowed_mentions=allowed_mentions,
)
>>>>>>> REPLACE
</suggested_fix>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
@sourcery-ai I've refactored the code to make it less DRY on purpose. I believe this is more readable, as DRY would only be applied if it is repeated 3 or more times. |
f460dbe to
3da6bfd
Compare
Codecov ReportAttention: Patch coverage is
✅ All tests successful. No failed tests found.
Additional details and impacted files@@ Coverage Diff @@
## main #949 +/- ##
========================================
- Coverage 9.32% 9.26% -0.06%
========================================
Files 121 122 +1
Lines 10286 10349 +63
Branches 1259 1268 +9
========================================
Hits 959 959
- Misses 9226 9289 +63
Partials 101 101
*This pull request uses carry forward flags. Click here to find out more.
☔ View full report in Codecov by Sentry. |
Description
This PR adds a command named
encodeanddecode. They are both used to encode and decode messages in multiple coding systems ranging from Base16 to Base85. I have made sure to error handle, document, and checked my code with ruff and pyright.Guidelines
My code follows the style guidelines of this project (formatted with Ruff)
I have performed a self-review of my own code
I have commented my code, particularly in hard-to-understand areas
I have made corresponding changes to the documentation if needed
My changes generate no new warnings
I have tested this change
Any dependent changes have been merged and published in downstream modules
I have added all appropriate labels to this PR
I have followed all of these guidelines.
How Has This Been Tested? (if applicable)
I have ran the command using all available options, while also running commands which I know will error.
Screenshots (if applicable)
Please add screenshots to help explain your changes.
Summary by Sourcery
Add a new EncodeDecode cog with hybrid commands to encode and decode text in Base16, Base32, Base64, and Base85 systems, including input validation and error handling.
New Features:
encodecommand to convert UTF-8 text into Base16, Base32, Base64, or Base85.decodecommand to convert Base16, Base32, Base64, or Base85 encoded text back to UTF-8.CODING_SYSTEMSmapping and helper functions for supported encoding and decoding operations.Enhancements:
generate_usage.