Skip to content

refactor: move type constants to new media classes #331

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

Merged
merged 2 commits into from
Aug 21, 2024
Merged

Conversation

CyanVoxel
Copy link
Member

This refactor is meant to provide an improved interface for determining media categories from file extensions. Instead of relying on searching though lists of extension constants, this refactor provides a new MediaCategory class, which in turn is composed of a MediaType enum, a set of extensions defined by one or more internal sets, and additional data such as IANA registration (more on that later).

The MediaCategory class is then used by the MediaCategories class, which contains every pre-built category used by the program along with the get_types method, which is the new intended way to check for media category membership. Instead of checking if extension .foo is inside a BAR_TYPES list constant, foo is passed to the get_types method which returns any MediaType matches from the extension sets inside of MediaCategory objects added inside the ALL_CATEGORIES list.

While I originally intended to rely heavily on MIME types rather than curated extension lists, it became apparent to me that this wouldn't be feasible for the use cases of TagStudio. There are cases where multiple media categories are desired, such as .csv in both PLAINTEXT and SPREADSHEET categories. In addition, MIME type categories on their own are too limited in scope for some of categorization I'd like to achieve, such as with Office files. With that being said I still wanted the ability to leverage MIME types where possible. The get_types method has an optional flag that when true will leverage the mimetypes module and attempt to guess the media category of an extension if a match was not found in one of the built-in sets. The IANA registration flag within the MediaCategory class is then used to prevent the get_types method from doing unnecessary membership checks inside otherwise unregistered media categories.

TL;DR: New MediaType enums and get_types method to replace the extension list constants with a more flexible system.

@CyanVoxel CyanVoxel added Type: Refactor Code that needs to be restructured or cleaned up Priority: Medium An issue that shouldn't be be saved for last Status: Review Needed A review of this is needed labels Jul 24, 2024
@CyanVoxel CyanVoxel added this to the Alpha 9.4 milestone Jul 24, 2024
_SPREADSHEET_SET: set[str] = {
".csv",
".numbers",
".ods" ".xls",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there's a comma missing here

@CyanVoxel CyanVoxel merged commit b107fb5 into Alpha-v9.4 Aug 21, 2024
8 checks passed
@CyanVoxel CyanVoxel deleted the media_types branch August 23, 2024 19:06
CarterPillow pushed a commit to CarterPillow/TagStudio that referenced this pull request Sep 7, 2024
* refactor: move type constants to new media classes

* fix: add missing comma + sort extensions
CyanVoxel added a commit that referenced this pull request Sep 22, 2024
Ports the following thumbnail and related PRs from the `Alpha-v9.4` branch to `main` (v9.5+):
- (#273) Blender thumbnail support
- (#307) Add font thumbnail preview support
- (#331) refactor: move type constants to new media classes
- (#390) feat(ui): expanded thumbnail and preview features
- (#370) ui: "open in explorer" action follows os name
- (#373) feat(ui): preview support for source engine files
- (#274) Refactor video_player.py (Fix #270)
- (#430) feat(ui): show file creation/modified dates + restyle path label
- (#471) fix(ui): use default audio icon if ffmpeg is absent
- (#472) fix(ui): use birthtime for creation time on mac & win

Co-Authored-By: Ethnogeny <111099761+050011-code@users.noreply.github.com>
Co-Authored-By: Theasacraft <91694323+Thesacraft@users.noreply.github.com>
Co-Authored-By: SupKittyMeow <77246128+supkittymeow@users.noreply.github.com>
Co-Authored-By: EJ Stinson <93455158+favroitegamers@users.noreply.github.com>
Co-Authored-By: Sean Krueger <71362472+seakrueger@users.noreply.github.com>
CyanVoxel added a commit that referenced this pull request Oct 7, 2024
* feat: port v9.4 thumbnail + related feats to v9.5

Ports the following thumbnail and related PRs from the `Alpha-v9.4` branch to `main` (v9.5+):
- (#273) Blender thumbnail support
- (#307) Add font thumbnail preview support
- (#331) refactor: move type constants to new media classes
- (#390) feat(ui): expanded thumbnail and preview features
- (#370) ui: "open in explorer" action follows os name
- (#373) feat(ui): preview support for source engine files
- (#274) Refactor video_player.py (Fix #270)
- (#430) feat(ui): show file creation/modified dates + restyle path label
- (#471) fix(ui): use default audio icon if ffmpeg is absent
- (#472) fix(ui): use birthtime for creation time on mac & win

Co-Authored-By: Ethnogeny <111099761+050011-code@users.noreply.github.com>
Co-Authored-By: Theasacraft <91694323+Thesacraft@users.noreply.github.com>
Co-Authored-By: SupKittyMeow <77246128+supkittymeow@users.noreply.github.com>
Co-Authored-By: EJ Stinson <93455158+favroitegamers@users.noreply.github.com>
Co-Authored-By: Sean Krueger <71362472+seakrueger@users.noreply.github.com>

* remove vscode exceptions from `.gitignore`

* delete .vscode directory

* style: format for `ruff check`

* fix(tests): update `test_update_widgets_not_selected` test

* remove Send2Trash dependency

* refactor: use dataclass for MediaCateogry

* refactor: use enums for UI colors

* docs: add file docstring for silent_Popen

* refactor: replace logger with structlog

* use early return inside `ResourceManager.get()`

* add `is_ext_in_category()` method to `MediaCategory`

Add method to check if an extension is a member of a given MediaCategory.

* style: fix docstring style, missing type hints, rename `afm`

* fix: use structlog vars in logging

* refactor: move platform-dependent strings to PlatformStrings

* refactor: move `parents[2]` path to variable

* fix: undo logger regressions

---------

Co-authored-by: Ethnogeny <111099761+050011-code@users.noreply.github.com>
Co-authored-by: Theasacraft <91694323+Thesacraft@users.noreply.github.com>
Co-authored-by: SupKittyMeow <77246128+supkittymeow@users.noreply.github.com>
Co-authored-by: EJ Stinson <93455158+favroitegamers@users.noreply.github.com>
Co-authored-by: Sean Krueger <71362472+seakrueger@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: Medium An issue that shouldn't be be saved for last Status: Review Needed A review of this is needed Type: Refactor Code that needs to be restructured or cleaned up
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants