Skip to content

Refactor Website Blocker: OOP design, improved reliability, and better user interaction#564

Open
AhmadAL-Quraan wants to merge 19 commits into
wasmerio:mainfrom
AhmadAL-Quraan:improve-web_blocker-structure
Open

Refactor Website Blocker: OOP design, improved reliability, and better user interaction#564
AhmadAL-Quraan wants to merge 19 commits into
wasmerio:mainfrom
AhmadAL-Quraan:improve-web_blocker-structure

Conversation

@AhmadAL-Quraan

@AhmadAL-Quraan AhmadAL-Quraan commented Apr 9, 2026

Copy link
Copy Markdown

PR Title

Refactor Website Blocker: OOP design, improved reliability, and better user interaction

Summary

Closes #562

This PR improves the website blocker implementation by refactoring the code into a modular, object-oriented design and enhancing reliability, usability, and maintainability.

Description

This change builds on the existing implementation by improving the internal structure and addressing several limitations in the current script.

The previous implementation relied on a procedural approach with limited error handling and responsiveness. This PR restructures the code into a class-based design, improves user interaction, and ensures safer handling of system modifications.

The changes are as follows:

  • Refactored the script into a WebsiteBlocker class to improve structure and maintainability
  • Added cross-platform hosts file handling inside the class
  • Implemented interactive user input for selecting predefined or custom websites
  • Improved error handling for permission-related issues
  • Fixed a design issue where the hosts file was not restored if the script was interrupted
  • Added a cleanup mechanism using signal handling (Ctrl+C)
  • Improved responsiveness by replacing long sleep intervals with shorter checks
  • Enhanced code readability with type hints and better organization
  • Ensured code quality and consistency by passing flake8 and mypy checks
  • Updated README with usage instructions and project details

Checks

in the repository

  • Made no changes that degrade the functioning of the repository
  • Gave each commit a meaningful title

in the PR

  • Followed the format of the pull_request_template
  • Made the Pull Request focused and well-scoped
  • Tested the changes locally

Thank You,

Ahmad AL-Quraan

@AhmadAL-Quraan AhmadAL-Quraan left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggestions + mistakes

# ----------------------------
# Blocking logic
# ----------------------------
def block_websites(self) -> None:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

block_websites checks if entry not in content, which guards against duplicate lines within a single file, but it appends without ever sorting or namespacing the block, so the hosts file accumulates whatever ordering happens to occur from prior runs with different site lists. If someone runs this with ["facebook.com"] today and ["twitter.com"] tomorrow, both lists are still partially present and there's no anchor (no # BEGIN/END blocker markers) to cleanly identify and strip "everything this script ever wrote."

# ----------------------------
# Unblocking logic
# ----------------------------
def unblock_websites(self) -> None:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

unblock_websites only removes lines containing strings from the current self.websites set, so any site blocked in a previous session with a different website list is now orphaned in /etc/hosts forever, with no command to list or clean it up. That's a real bug, not a style nit: it leaves the system in a state the user can't easily recover from without manually editing the hosts file.

# ----------------------------
# Time logic
# ----------------------------
def is_working_hours(self) -> bool:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggestion: working-hours logic doesn't account for weekends, timezones, or DST, and there's no day-of-week parameter despite this being pitched as a "work hours" blocker. That's a product gap I'd want flagged even if it's out of scope for this PR — at minimum it deserves a TODO and a docstring caveat.

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.

Web Blocker only works for mac and linux distributions

2 participants