Skip to content

fix: update logger configuration, fix group member AttributeError, and refactor argument validation using decorator#20

Closed
akaBoyLovesToCode wants to merge 3 commits intoembyplus:mainfrom
akaBoyLovesToCode:main
Closed

fix: update logger configuration, fix group member AttributeError, and refactor argument validation using decorator#20
akaBoyLovesToCode wants to merge 3 commits intoembyplus:mainfrom
akaBoyLovesToCode:main

Conversation

@akaBoyLovesToCode
Copy link
Contributor

@akaBoyLovesToCode akaBoyLovesToCode commented Feb 12, 2025

This PR encompasses three distinct changes:

  1. Logger Configuration Update:

    • Updated logger configuration to output logs to both the terminal and a log file.
    • Added a StreamHandler to output logs to the terminal for easier debugging in IDEs.
    • Retained a FileHandler to write logs to default.log for persistence.
    • Logging level is now set based on config.log_level.
  2. Group Member AttributeError Fix:

    • Fixed an AttributeError caused by attempting to call 'get' on a User object when accessing the group member's username.
    • Updated the code to properly check if group member data exists before accessing the 'username' attribute.
  3. Argument Validation Refactor:

    • Refactored the _ensure_args method into an ensure_args decorator.
    • The decorator validates that the minimum number of arguments is provided before the function executes.
    • This change improves code readability and reusability by centralizing argument validation logic.

These changes collectively enhance the debugging experience, improve error handling, and contribute to a cleaner, more maintainable codebase.

好的,这是翻译成中文的 pull request 总结:

Sourcery 总结

更新了日志配置,使其可以根据 config.log_level 同时输出到终端和日志文件。修复了访问组成员用户名时出现的 AttributeError,并将参数验证重构为一个装饰器。

Bug 修复:

  • 修复了访问组成员用户名时出现的 AttributeError。

增强功能:

  • 将参数验证逻辑重构为一个装饰器。
Original summary in English

Summary by Sourcery

Update logger configuration to output to both terminal and a log file based on config.log_level, fix AttributeError when accessing group member username, and refactor argument validation into a decorator.

Bug Fixes:

  • Fixed an AttributeError when accessing a group member's username.

Enhancements:

  • Refactored the argument validation logic into a decorator.

- Resolved AttributeError caused by attempting to call 'get' on a 'User' object
- Updated code to properly check if group member data exists before accessing 'username'
- Changed logger to output to both terminal and log file
- Added StreamHandler to output logs to terminal for easier debugging in IDEs
- Retained FileHandler to write logs to default.log for persistence
- Set logging level based on config.log_level
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 12, 2025

好的,这是将审查指南翻译成中文的版本:

Sourcery 审查者指南

这个 Pull Request 包含了三个重要的更改:它更新了 logger 的配置,添加了一个 stream handler 并改进了日志输出;修复了在访问群组成员的用户名时出现的 AttributeError,通过调整群组成员 handler 中的数据访问方式;并且重构了参数验证,用一个可复用的装饰器替换了内联的 _ensure_args 检查。这些更改改进了调试、错误处理和代码可维护性。

更新的参数验证装饰器类图

classDiagram
    class BotCommands {
        +_parse_args(message: Message) : list[string]
        +_reply_html(message: Message, content: string) : Awaitable
        +_send_error(message: Message, error: Exception, prefix: string) : Awaitable
        +ensure_args(min_len: int, usage: string) : Decorator
        +create_user(message: Message, args: list[string]) : Awaitable
        +use_code(message: Message, args: list[string]) : Awaitable
        +register_until(message: Message, args: list[string]) : Awaitable
        +register_amount(message: Message, args: list[string]) : Awaitable
    }

    %% Note: The ensure_args method is now a static decorator that validates arguments before executing the command method.
    note for BotCommands "装饰器重构:ensure_args 验证参数数量并注入解析后的参数"
Loading

文件级别变更

变更 详情 文件
Logger 配置更新
  • 在 app.py 中添加了一个 StreamHandler,用于将日志输出到终端,以便更轻松地进行调试。
  • 配置了一个 StreamHandler 和一个 FileHandler,并具有适当的格式化器和日志级别。
  • 更新了 logger 的设置,以使用 config.log_level 并改进了时间戳格式。
app.py
群组成员 AttributeError 修复
  • 修改了 commands.py 中群组成员数据的处理方式,以在访问 'username' 之前检查属性是否存在。
  • 将 user_service.py 中 telegram_name 的检索从字典的 get 方法更改为属性访问,以避免 AttributeError。
bot/commands.py
services/user_service.py
参数验证重构
  • _ensure_args 方法重构为 commands.py 中的 'ensure_args' 装饰器。
  • 更新了多个命令方法(例如,create_user, use_code, register_until, register_amount)以使用新的参数验证装饰器。
  • 通过集中参数长度检查逻辑,提高了代码的可读性和可重用性。
bot/commands.py

提示和命令

与 Sourcery 互动

  • 触发新的审查: 在 Pull Request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 通过回复审查评论,让 Sourcery 从审查评论中创建一个 issue。您也可以回复审查评论并加上 @sourcery-ai issue 来从中创建一个 issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在 Pull Request 上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在 Pull Request 上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成审查者指南: 在 Pull Request 上评论 @sourcery-ai guide 以随时(重新)生成审查者指南。
  • 解决所有 Sourcery 评论: 在 Pull Request 上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经处理了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在 Pull Request 上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想重新开始新的审查,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!
  • 为 issue 生成行动计划: 在 issue 上评论 @sourcery-ai plan 以生成行动计划。

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的 Pull Request 摘要、审查者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request makes three significant changes: it updates the logger configuration to add a stream handler and improve logging output; fixes an AttributeError when accessing a group member's username by adjusting data access in the group member handler; and refactors argument validation by replacing the inline _ensure_args check with a reusable decorator. These changes improve debugging, error handling, and code maintainability.

Updated Class Diagram for Argument Validation Decorator

classDiagram
    class BotCommands {
        +_parse_args(message: Message) : list[string]
        +_reply_html(message: Message, content: string) : Awaitable
        +_send_error(message: Message, error: Exception, prefix: string) : Awaitable
        +ensure_args(min_len: int, usage: string) : Decorator
        +create_user(message: Message, args: list[string]) : Awaitable
        +use_code(message: Message, args: list[string]) : Awaitable
        +register_until(message: Message, args: list[string]) : Awaitable
        +register_amount(message: Message, args: list[string]) : Awaitable
    }

    %% Note: The ensure_args method is now a static decorator that validates arguments before executing the command method.
    note for BotCommands "Decorator refactor: ensure_args validates argument count and injects parsed args"
Loading

File-Level Changes

Change Details Files
Logger Configuration Update
  • Added a StreamHandler in app.py to output logs to the terminal for easier debugging.
  • Configured both a StreamHandler and a FileHandler with appropriate formatters and log levels.
  • Updated logger setup to use config.log_level and improved timestamp formatting.
app.py
Group Member AttributeError Fix
  • Modified the handling of group member data in commands.py to check for attribute existence before accessing 'username'.
  • Changed retrieval of telegram_name from a dictionary get method to attribute access in user_service.py to avoid AttributeError.
bot/commands.py
services/user_service.py
Argument Validation Refactor
  • Refactored the _ensure_args method into an 'ensure_args' decorator in commands.py.
  • Updated multiple command methods (e.g., create_user, use_code, register_until, register_amount) to use the new argument validation decorator.
  • Improved code readability and reusability by centralizing argument length checking logic.
bot/commands.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嘿 @Qubbby - 我已经查看了你的更改 - 这里有一些反馈:

总体评论

  • 考虑向装饰器添加默认实现,以避免重复参数解析逻辑。
  • 组员属性错误修复看起来不错,但考虑添加更多日志记录或错误处理,以帮助调试将来出现的类似问题。
以下是我在审查期间查看的内容
  • 🟢 一般问题:一切看起来都很好
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery 对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们✨
请帮助我更有用!请点击每个评论上的👍或👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @Qubbby - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a default implementation to the decorator to avoid repeating the argument parsing logic.
  • The group member attribute error fix looks good, but consider adding more logging or error handling to help debug similar issues in the future.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Frankoul
Copy link
Contributor

所有的 code format可以放在一个commit,方便review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants