Skip to content
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

ENH: add useful error message when can't change directories on ftp site #189

Merged
merged 1 commit into from
Feb 4, 2025

Conversation

GavinHuttley
Copy link

@GavinHuttley GavinHuttley commented Feb 4, 2025

Summary by Sourcery

Bug Fixes:

  • Handle error_perm when changing directories on the FTP site and print a more informative error message if it occurs.

Copy link

sourcery-ai bot commented Feb 4, 2025

Reviewer's Guide by Sourcery

This pull request improves error handling when the program fails to change directories on an FTP site. It adds a try-except block to catch the error and prints a user-friendly error message to the console, including potential reasons for the failure.

Sequence diagram for enhanced FTP directory listing error handling

sequenceDiagram
    participant Client
    participant ListDir
    participant FTP

    Client->>ListDir: listdir(path)
    ListDir->>FTP: configured_ftp()
    activate FTP
    FTP-->>ListDir: ftp connection
    deactivate FTP

    ListDir->>FTP: cwd(path)
    alt Success
        FTP-->>ListDir: directory changed
        ListDir->>FTP: nlst()
        FTP-->>ListDir: file list
        ListDir-->>Client: yield files
    else error_perm
        FTP-->>ListDir: permission error
        ListDir->>ListDir: print error message
        ListDir-->>Client: return None
    end

    ListDir->>FTP: close()
Loading

File-Level Changes

Change Details Files
Improved error handling when changing directories on FTP site.
  • Added a try-except block to catch the error_perm exception when changing directories.
  • Prints a user-friendly error message to the console when the directory change fails.
  • The error message suggests potential reasons for the failure, such as Ensembl naming inconsistencies.
src/ensembl_tui/_ftp_download.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.

Hey @GavinHuttley - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟡 Security: 1 issue found
  • 🟢 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.

Comment on lines +26 to +35
try:
ftp.cwd(path)
except error_perm:
msg = "\n".join(
[
"",
f"Skipping {path}",
"Could not change into directory on FTP site.",
"This can be because of Ensembl naming inconsistencies.",
"Check names are consistent between genomes and homology.",
Copy link

Choose a reason for hiding this comment

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

🚨 suggestion (security): Consider using a context manager or try-finally block to ensure FTP connection is always closed

The current implementation might leak FTP connections if errors occur. Using 'with' statement or try-finally would guarantee proper cleanup.

Suggested implementation:

from contextlib import contextmanager
from ftplib import FTP, error_perm
    """returns directory listing"""
    pattern = pattern or (lambda x: True)

    with configured_ftp_cm(host=host) as ftp:
        try:
            ftp.cwd(path)
        except error_perm:
            msg = "\n".join(
                [
                    "",
                    f"Skipping {path}",
                    "Could not change into directory on FTP site.",
                    "This can be because of Ensembl naming inconsistencies.",
                    "Check names are consistent between genomes and homology.",
                ],
            )
            eti_util.print_colour(msg, "red")
            return

        for fn in ftp.nlst():
            if pattern(fn):
                yield f"{path}/{fn}"

@contextmanager
def configured_ftp_cm(host: str):
    """Context manager for FTP connections"""
    ftp = configured_ftp(host=host)
    try:
        yield ftp
    finally:
        ftp.close()

)
eti_util.print_colour(msg, "red")
return

for fn in ftp.nlst():
Copy link

Choose a reason for hiding this comment

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

issue: Add error handling for potential FTP failures during directory listing

The nlst() call could fail if the connection is lost after changing directory. Consider wrapping it in a try-except block.

@GavinHuttley GavinHuttley merged commit 1086bf0 into cogent3:develop Feb 4, 2025
12 of 21 checks passed
@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 13146911781

Details

  • 3 of 7 (42.86%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.1%) to 87.605%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ensembl_tui/_ftp_download.py 3 7 42.86%
Totals Coverage Status
Change from base Build 13145796607: -0.1%
Covered Lines: 2608
Relevant Lines: 2977

💛 - Coveralls

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