Compare commits

..

757 Commits

Author SHA1 Message Date
d815264026 Comment qt functions executed in a separate thread 2025-01-22 17:25:33 -08:00
b347473a74 Set Toast to use the Popup window hint 2025-01-22 17:24:20 -08:00
ca8f36d105 Fix toasts and modal dialogs
Toasts calculated the duration bar in python this is now a QPropertyAnimation

Series/Issue Selection windows now use signals/slots to communicate
2025-01-14 21:23:17 -08:00
13ee9e9ad8 Use signals a little better and avoid QDialog.exec 2025-01-14 17:41:00 -08:00
a9da87bff3 Improve canceling during a ratelimit 2025-01-04 15:14:32 -08:00
d011975fd0 Fix typo 2024-12-24 21:17:59 -08:00
4d767f026a Fix dark mode 2024-12-23 20:10:18 -08:00
b1c164add0 Skip GUI tests on Windows and Linux 2024-12-22 20:27:23 -08:00
e184353493 Display toast notification longer 2024-12-18 22:34:03 -08:00
94ca1fd58b Add tests 2024-12-18 21:15:51 -08:00
63c836a327 Display message when a ratelimit is hit 2024-12-16 01:03:31 -08:00
92ce2987ea Regenerate settngs 2024-12-07 15:30:44 -08:00
c282ebf845 Switch ubuntu runner to 22.04 and macos to 13 2024-12-07 14:41:22 -08:00
38932f0782 Add language to ComicTagger 2024-12-06 23:18:45 -08:00
bf0a46055a Fix parsing ' in filenames
Fixes #672
2024-12-06 23:18:45 -08:00
0fa329ca75 Add language to Credit in ComicAPI 2024-12-06 23:09:25 -08:00
577e99ae39 Print CLI tags when using the print command 2024-12-06 23:02:10 -08:00
5df9359151 Merge branch 'mizaki/write_md_merge' into develop 2024-10-19 14:00:46 -07:00
119a0881e0 Merge branch 'mizaki/fix_readtags' into develop 2024-10-19 13:59:59 -07:00
f4f732b742 Fix accidental re-ordering of pages when pages.image_index is disabled on a metadata type 2024-10-19 13:58:19 -07:00
a8f269aefa Fix export to CBZ 2024-10-19 10:37:27 -07:00
6930f0cb74 Fix switching unclean read tags 2024-10-17 21:44:09 +01:00
170476a705 Preserve hidden metadata values when reading from GUI form 2024-10-15 13:06:19 +01:00
7448e9828b Sort pages in archive order before writing CR metadata 2024-10-14 16:54:13 -07:00
6d20fe348f Update pre-commit 2024-10-11 21:07:17 -07:00
5b02358bf1 Fix all inputs being disabled when an invalid tag is loaded from settings 2024-10-11 21:04:48 -07:00
78df903de7 pre-commit 2024-09-27 15:08:00 -07:00
4cd70670cc Allow custom paramaters in comicvine url 2024-09-27 15:06:26 -07:00
dcb532d7c9 Add Image Comics to publishers.json 2024-09-27 14:45:33 -07:00
5820c36ea5 Fix CV error handling 2024-09-27 14:39:25 -07:00
c0db1e52ae Make cleanup_html produce text that is more compliant with markdown 2024-09-22 16:26:15 -07:00
e46656323c Fix clearing invalid tags 2024-09-22 16:24:28 -07:00
e96de650bf Fix label names for standard location links 2024-09-21 17:06:17 -07:00
b421a0edaa Add links to standard locations 2024-09-21 15:57:09 -07:00
a9fdafdb93 Format message better 2024-09-21 15:39:37 -07:00
a4a6d54d7e Merge branch 'cv-cache' into develop 2024-09-20 15:02:20 -07:00
9358431146 Add a notice about Metron/GCD changes on PyInstaller builds 2024-09-20 14:45:06 -07:00
a60eda1602 Typo 2024-09-20 13:57:14 -07:00
c796ad7c7a Enable debug logging for pyrate-limiter 2024-09-20 13:52:25 -07:00
63718882a5 Update pyinstaller package to not include metron or gcd by default
This makes it so that users using pyinstaller can update metron and gcd without waiting for a new ComicTagger release
2024-09-19 19:23:41 -07:00
89dfec2363 ComicVine improvements
Add more logging
Add a 10 second timeout to all requests
Log unhandled exceptions
2024-09-19 19:13:03 -07:00
39a4a37d7c Add tests 2024-09-19 19:03:30 -07:00
25e5134577 Cache more ComicVine lookups 2024-09-19 17:31:06 -07:00
a7f1d566ab Merge branch 'plugin-isolation' into develop 2024-09-19 16:26:35 -07:00
234d9e49fe Fix test 2024-09-17 15:32:01 -07:00
6ea9230382 Allow .whl files 2024-09-17 14:27:01 -07:00
1803a37591 Handle None values when doing conversions and catch indexing errors 2024-09-17 09:20:11 -07:00
c50de9bed7 Fix plugin folder 2024-09-16 16:52:51 -07:00
6a97ace933 Only support zip local plugins 2024-09-16 16:46:42 -07:00
f56d58bf45 Fix reading plugin files 2024-09-16 16:13:11 -07:00
4c9096a11b Implement the most basic local plugin isolation possible
Remove modules belonging to local plugins after loading
Remove sys.path entry after loading

This means that multiple local plugins can be installed with the same import path and should work correctly
This does not allow loading a local plugin that has the same import path as an installed plugin
2024-09-15 17:09:33 -07:00
c9c0c99a2a Increase rate limits on CV to cover the 200 requests/Hr restriction
Add twitter's alternative to HTTP code 429
2024-09-12 13:56:57 -07:00
58f71cf6d9 Remove archived tags from tests 2024-09-12 13:17:06 -07:00
befffc98b1 Catch all exceptions when parsing metadata from the CLI 2024-09-12 13:11:30 -07:00
006f3cbd1f Remove comet and cbl tags 2024-09-12 12:09:07 -07:00
582224abec Fixes for quick-tag 2024-09-12 11:51:38 -07:00
acb59f9e83 Fix saving settings 2024-08-24 12:19:11 -07:00
fab30f3f29 Add experimental quick-tag 2024-08-18 19:16:55 -07:00
2cb6caea8d Ignore update with incomplete data when complete data is already cached 2024-08-16 17:05:28 -07:00
ffdf7d71e1 Fix tests 2024-08-16 12:50:14 -07:00
db3d5d6a01 Merge branch 'jxl' into develop 2024-08-09 16:34:25 -07:00
8709ef301d Fix failing test 2024-08-03 23:11:31 -07:00
b8728c5eed Improve performance when re-tagging file based tags in zip archives 2024-08-03 14:41:04 -07:00
0ba81f9f86 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-08-03 21:27:25 +00:00
8c85a60f67 Add pillow-jxl-plugin as an optional dependency 2024-08-03 14:15:00 -07:00
d089c4bb6a Merge branch 'mizaki/modify_cb_delegate' into develop 2024-08-03 14:04:49 -07:00
8ace830d5e Remove double import 2024-07-31 22:13:15 +01:00
893728cbef Merge remote-tracking branch 'origin/pre-commit-ci-update-config' into develop 2024-07-30 18:35:06 -07:00
d4a90e8934 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-07-29 17:21:53 +00:00
a529b14459 [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/asottile/pyupgrade: v3.16.0 → v3.17.0](https://github.com/asottile/pyupgrade/compare/v3.16.0...v3.17.0)
2024-07-29 17:21:31 +00:00
3227105558 Merge branch 'pre-commit-ci-update-config' into develop 2024-07-27 19:40:22 -07:00
d62dff49b4 Fix overlay tests 2024-07-27 19:39:15 -07:00
2d4d10e31d Add comment on a python oddity 2024-07-27 19:26:09 -07:00
0048901a61 Remove unused attributes 2024-07-27 19:23:37 -07:00
a7a9d38428 Make ImageMetadata a dataclass 2024-07-27 19:23:37 -07:00
219ede2d5d Improve StrEnum
Return the actual string for __str__
Allow case insensitive conversion
2024-07-27 16:45:22 -07:00
e96cb8ad15 Add button to autodetect double pages
A page is marked as a double page if it's as least as wide as tall.

Closes: #578
Co-authored-by: Sven Hesse <drmccoy@drmccoy.de>
2024-07-27 16:39:34 -07:00
0a4aef1a1b Add back apply_archive_info_to_metadata when writing tags 2024-07-27 16:24:29 -07:00
63832606b1 Add ability to auto-detect double pages
Co-authored-by: Sven Hesse <drmccoy@drmccoy.de>
2024-07-27 16:24:29 -07:00
f10ceb3216 Fix duplicate items in credits and pages when merging metadata 2024-07-27 15:45:03 -07:00
d8adbbecdd Fix inadequate checks on page attributes 2024-07-27 15:43:38 -07:00
f043da6b62 Enable navigation with left and right arrow keys in the page browser 2024-07-27 15:42:20 -07:00
77e551e582 Add Auto-Tag back to the toolbar 2024-07-27 15:40:48 -07:00
9d389970b8 [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/pre-commit/mirrors-mypy: v1.10.0 → v1.11.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.10.0...v1.11.0)
2024-07-22 17:19:27 +00:00
a44e037311 Use custom delegate to unify combobox item style 2024-07-06 22:50:49 +01:00
cc50e373dc Fix missing / in glob 2024-06-30 20:25:18 -07:00
9350a07f50 Enable support for the plaintext keyring 2024-06-30 20:03:47 -07:00
6325a2a707 Pass ACTIONS_* variables because github can't be consistent 2024-06-30 19:36:25 -07:00
ea96c44d84 Pass github actions environment variables 2024-06-30 19:06:15 -07:00
4c8a4dcbd3 Make python 3.9 compatible 2024-06-29 21:04:32 -07:00
bd53678442 Copy oidc-exchange.py from pypa/gh-action-pypi-publish 2024-06-29 20:51:27 -07:00
c370baa6a2 re-add pyinstaller to release 2024-06-29 19:22:33 -07:00
45c604b332 Remove source tar.gz from github release 2024-06-29 19:03:36 -07:00
64db58ed3d Fix dmg creation 2024-06-29 18:54:13 -07:00
ab8f4a3702 Merge branch 'mizaki/placeholder_text' into develop 2024-06-29 18:44:14 -07:00
c28dc19df6 Improve filename parsing 2024-06-29 18:43:40 -07:00
56d8c507e2 Use a directory that isn't deleted 2024-06-29 17:15:13 -07:00
10a1554e73 Fix release again
Place binaries in dist/binary to make pypa/gh-action-pypi-publish happy
Don't run the formatter and qrc generator during release as it causes issues with setuptools_scm
2024-06-29 16:04:27 -07:00
c8017c4269 Fix release (maybe) 2024-06-23 19:22:00 -07:00
3cb4dca63f Limit PyPI publishing to linux 2024-06-23 18:44:53 -07:00
beeb6336e9 Fix default value of --skip-existing-tags 2024-06-23 15:20:51 -07:00
8cb1140614 Fix rename of read_all_tags Fixes #659
Fix --skip-existing-tags Fixes #658
2024-06-23 15:09:11 -07:00
f243e8c39e Fix publishing to PyPI 2024-06-23 15:09:11 -07:00
890750819a Fix combobox placeholder text not showing when using pip PyQt5 with pip wheels on Windows or Linux 2024-06-23 18:25:26 +01:00
20806f95a2 Remove lint from release code 2024-06-23 01:33:48 -07:00
13646a306d Sync macos dependency code 2024-06-23 01:12:26 -07:00
3082aae124 bump MacOS version 2024-06-23 00:38:17 -07:00
76a92c8431 Fix test 2024-06-23 00:04:33 -07:00
385a46fc16 Simplify regexes and use logger.warning 2024-06-22 20:41:15 -07:00
e452fa153b Fix issues from static analysis 2024-06-22 20:21:01 -07:00
3fd1c13ecb Fixes for metadata parsing and printing 2024-06-22 20:19:02 -07:00
76f23d4a02 Fix tags in GUI 2024-06-22 19:15:57 -07:00
5f1ddee7ce Update build system 2024-06-22 18:22:28 -07:00
9803c9ad09 Fix Remove HTML tables checkbox 2024-06-22 14:12:18 -07:00
42448fa250 Update settngs
Fix renamed settings attributes
Add --parse-filename back
Fix conversions in fileranamer
2024-06-21 21:01:11 -07:00
6b0dca2f51 Remove unnecessary issueidentifier methods 2024-06-21 20:07:55 -07:00
6ab3a89a35 Improvements to filerenamer and filename parsing 2024-06-21 20:07:07 -07:00
3389c72a63 Merge branch 'help-messages' into develop 2024-06-21 19:53:30 -07:00
59aae5b122 [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/asottile/pyupgrade: v3.15.2 → v3.16.0](https://github.com/asottile/pyupgrade/compare/v3.15.2...v3.16.0)
- [github.com/PyCQA/flake8: 7.0.0 → 7.1.0](https://github.com/PyCQA/flake8/compare/7.0.0...7.1.0)
2024-06-21 19:31:52 -07:00
063b04c543 Add tooltips for clearing tags and applying CBL transforms 2024-06-21 19:18:52 -07:00
77d340d04d Set buddies 2024-06-21 19:18:52 -07:00
69a9566f42 Update all references of saved 'matadata' to 'tags' 2024-06-20 16:47:10 -07:00
24002c66e7 Move action definitions into ui file 2024-06-14 15:35:01 -07:00
bf87a76fdf Make the splitter visible 2024-06-09 18:19:51 -07:00
d0312e050b Fix page handling 2024-06-09 13:40:42 -07:00
44b4857fc3 Remove unneeded checks in _enable_widget 2024-06-09 13:18:27 -07:00
6132af3bb5 Support niquests 2024-06-09 13:09:26 -07:00
c91c7edd73 Re-generate SettngsNS 2024-06-09 12:55:12 -07:00
6f9fbc73d8 Fix showing a fullscreen page on double click on MacOS 2024-06-09 12:54:31 -07:00
888720b544 Allow blurring images fixes #637
for people that don't accidentally read the entire comic when editing the metadata
2024-06-09 12:52:55 -07:00
5e6682566f Allow results to include comics in the following year fixes #638 2024-06-08 19:17:42 -07:00
6351afb36c Add an option to prefer filename metadata on the CLI fixes #630 2024-06-08 19:12:38 -07:00
898ccef5c0 Set the working directory for rar commands 2024-06-08 15:00:25 -07:00
0198eb9e2b Fix saving merge settings 2024-06-06 20:41:21 -07:00
0457e19913 Merge descriptions 2024-06-06 19:01:41 -07:00
e5925b8ebc Merge branch 'mizaki/add_table_html_gui' into develop 2024-06-03 16:17:19 -07:00
710760dc91 Show the located rar exe 2024-06-03 16:08:09 -07:00
5d7e348a0e Fix remove tags menu option Fixes #650 2024-06-03 13:06:49 -07:00
979a54e2b8 Fix lexing a dot '.' as a symbol
Fixes #652
2024-06-03 13:06:49 -07:00
afc0aa4a78 missed rename 2024-06-02 00:16:55 +01:00
a552f05b23 Add remove HTML tables back 2024-06-01 19:40:13 +01:00
7bbc3f3e2c Merge branch 'mizaki/cli_interactive_fix' into develop 2024-05-31 18:19:50 -07:00
a4941a93f0 Use combined md with -i on CLI 2024-06-01 00:45:59 +01:00
d82cd95849 Fix typo in protofolius_issue_number_scheme
Fixes #648
2024-05-26 13:22:55 -07:00
5010ca60e9 Remove reduce_widget_font_size 2024-05-21 20:32:01 -07:00
419461c905 Improve Merge descriptions in settings window 2024-05-21 20:29:19 -07:00
32b570ee5b Improve help messages
Include default values
2024-05-21 19:57:47 -07:00
9849b5f6f9 Note newline delimited fields 2024-05-21 19:57:47 -07:00
706c46f2bb Fix the prompt on save button in settings 2024-05-21 19:57:47 -07:00
d1986a5d53 Update settings window 2024-05-21 19:57:47 -07:00
e864e2db48 Re-arrange settings 2024-05-21 19:57:47 -07:00
af9c8afad7 Update search/identify help message 2024-05-21 19:57:47 -07:00
4e5d8885c6 Improve help messages 2024-05-21 19:57:47 -07:00
215a4680f4 Merge branch 'mizaki/fix_autotag_overlaystyles' into develop 2024-05-21 18:28:20 -07:00
f712952b87 Fix typing issues 2024-05-21 18:22:30 -07:00
14f2599ba1 fix auto tag window 2024-05-21 23:48:46 +01:00
2897611006 Fix defaults for arguments
Bump settngs
2024-05-19 14:17:07 -07:00
250d777159 Remove combine overlay. Alter help messages in settings window and add lists message 2024-05-11 22:25:46 +01:00
6c3b63abd9 Add option for merge lists and fix saving overlay options in settings window 2024-05-11 22:08:49 +01:00
bada694fd4 Rebase corrections 2024-05-11 16:44:44 +01:00
a40438d38c Separate list merge into a separate option (lordwelch) 2024-05-11 16:42:24 +01:00
3d443e0908 lordwelch rewrite 2024-05-11 02:04:43 +01:00
b761763c4c Rename CBL option to Metadata 2024-05-11 02:02:01 +01:00
71b79bdc91 Move some overlay test data to testing/comicdata.py 2024-05-11 02:02:01 +01:00
2faac18597 norm_fold out of loop for add_credit. Explicit overlay mode for CLI metadata. 2024-05-11 02:02:01 +01:00
e9a592df50 GUI overlay settings moved to internal namespace and CLI args added 2024-05-11 02:02:01 +01:00
94b94b76dc Change settings menu overlay descriptions 2024-05-11 02:02:01 +01:00
62240bf2f4 Add OverlayMode options for read style and data source 2024-05-11 02:02:01 +01:00
ffb4efbcd7 GUI overlay settings moved to internal namespace and CLI args added 2024-05-11 02:01:59 +01:00
b2f95faac4 Change settings menu overlay descriptions 2024-05-11 01:56:10 +01:00
93be16f7eb Remove data to test empty string->None for series and issue as an empty string will never make it to genericmetadata now 2024-05-11 01:56:10 +01:00
8b0683f67c Add OverlayMode options for read style and data source 2024-05-11 01:56:06 +01:00
851339d4e3 Merge branch 'mizaki/multi_read' into develop 2024-05-10 16:25:07 -07:00
5cf54ab511 Reverse load styles only in taggerwindow and comment reverse 2024-05-11 00:11:08 +01:00
384ac5e33a Don't save in priority order 2024-05-10 19:23:27 +01:00
7271caccc9 Size combobox dropdown with extra space for move item arrows. Add same sizeHint as QComboBox for unified height 2024-05-10 17:14:39 +01:00
0c9e846bfb Force MacOS to use CE_CheckBox 2024-05-09 20:54:12 +01:00
a2a57b6da0 Add tooltip support for items (and arrows). Simplify and measure arrow images 2024-05-09 01:58:43 +01:00
35ec334c28 Merge pull request #640 from comictagger/pre-commit-ci-update-config
[pre-commit.ci] pre-commit autoupdate
2024-05-07 17:35:25 -07:00
7383b18924 Warn on read style failure in rename window 2024-05-07 22:08:27 +01:00
e0f1f7c356 Rename ItemDelegate. Remove table checkbox 2024-05-06 20:53:00 +01:00
6b8b961ff7 Report and/or log overlay tag style read errors 2024-05-06 19:40:38 +01:00
4c6a1d3215 Use custom combobox with item delegate 2024-05-06 16:33:30 +01:00
64dbf9e981 Add -t to --type-read and duplicate read styles to modify styles on the CLI if modify if empty 2024-05-04 21:14:00 +01:00
27e3803414 Reverse read styles on load. Missed conversion to overlay_ca_read_style 2024-05-04 20:59:18 +01:00
591b6bcc44 [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/psf/black: 24.4.0 → 24.4.2](https://github.com/psf/black/compare/24.4.0...24.4.2)
- [github.com/pre-commit/mirrors-mypy: v1.9.0 → v1.10.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.9.0...v1.10.0)
2024-04-29 17:22:16 +00:00
6ac2e32612 Parse numeric characters as numbers fixes #639 2024-04-29 10:20:43 -07:00
887c383229 Fix an infinite loop issue parsing numbers outside of 0-9 fixes #639 2024-04-29 10:20:25 -07:00
64c909facb Have last overlayed style labelled as 1 (human logical) 2024-04-29 00:55:54 +01:00
23ceda33bd fix self.load_data_styles name 2024-04-29 00:55:54 +01:00
7e63070f13 Change ComicArchive type to set from list 2024-04-29 00:55:51 +01:00
247ee01d6e Copy tags will copy use overlayed result of all read styles 2024-04-29 00:53:01 +01:00
f61b91acd6 Revert and change multi-read styles if dirty 2024-04-29 00:53:01 +01:00
6951113717 Change load_cache calls from load and saves style conbined list to combined set 2024-04-29 00:53:01 +01:00
73269c7c9d Add overlay_ca_read_style method to prevent duplicated code 2024-04-29 00:52:59 +01:00
f00cd1568c Clear cache on autotag rather than reloading 2024-04-29 00:51:44 +01:00
f9d79ead9d Remove answered comment 2024-04-29 00:51:44 +01:00
c01d6aaa3a Add up and down png 2024-04-29 00:51:44 +01:00
dd8767ad81 Revert copy tag status tip 2024-04-29 00:51:44 +01:00
0bbdaa96cf Split command line `--type arg into --type-modify for modify styles and --type-read for read styles 2024-04-29 00:51:40 +01:00
96bbbe51e7 More load_data_styles to list fixes 2024-04-29 00:46:03 +01:00
16088aec72 Covert missed self.load_data_styles to list 2024-04-29 00:45:15 +01:00
199167c50b Change click event handling for QTableWidget. Needs testing on MacOS and Windows 2024-04-29 00:45:15 +01:00
9359cd877d Switch to using list for storing read styles 2024-04-29 00:45:15 +01:00
003b68b3d3 Renamewindow 2024-04-29 00:45:09 +01:00
29dc7ad830 Use multi-read styles. Table combo box style improvements. Tooltips 2024-04-29 00:40:41 +01:00
770cce5ac0 Add TableComboBox 2024-04-29 00:40:41 +01:00
235e62814f Update pre-commit 2024-04-28 13:57:53 -07:00
cd2d40a379 Merge pull request #633 from comictagger/pre-commit-ci-update-config
[pre-commit.ci] pre-commit autoupdate
2024-04-28 13:55:16 -07:00
d63123b77b Add tests for prepare_metadata 2024-04-28 13:53:41 -07:00
8b4bf8d51f Allow preserving the original filename when moving 2024-04-27 19:25:33 -07:00
d98f815ce0 Add a button to attempt to identify a scanner page 2024-04-27 18:10:49 -07:00
787f3e8ea1 Enabled bulk edits in the page list editor 2024-04-27 17:28:59 -07:00
064795fac9 Fix prepare_metadata 2024-04-27 16:43:51 -07:00
9208a80ab0 Improve typing 2024-04-27 15:45:05 -07:00
a681abb854 Consolidate preparing metadata for save 2024-04-27 15:29:34 -07:00
996397b9d5 Fix select all 2024-04-23 23:54:33 -04:00
8fb180390d [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/psf/black: 24.3.0 → 24.4.0](https://github.com/psf/black/compare/24.3.0...24.4.0)
2024-04-15 17:19:43 +00:00
c311b8e351 Use comicapi for all urllib3 items 2024-04-12 14:39:34 -07:00
af059b8775 Merge branch 'metadataOverride' into develop 2024-04-12 14:12:27 -07:00
de3a9352ea Allow reading cli metadata from a file 2024-04-12 14:10:21 -07:00
d104ae1e8e Update help message for the -m option 2024-04-11 15:46:29 -07:00
88c2980e5d [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/pre-commit/pre-commit-hooks: v4.5.0 → v4.6.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.5.0...v4.6.0)
2024-04-08 17:23:46 +00:00
8bcd51f49b Improve commandline metadata override
Change parse_metadata_from_string to yaml syntax
Add a special value to remove existing values when metadata is overlayed
2024-04-06 12:03:01 -07:00
de084ffff9 Fix string value of GenericMetadata 2024-04-06 12:02:21 -07:00
eb6c2ed72b [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/asottile/pyupgrade: v3.15.1 → v3.15.2](https://github.com/asottile/pyupgrade/compare/v3.15.1...v3.15.2)
- [github.com/PyCQA/autoflake: v2.3.0 → v2.3.1](https://github.com/PyCQA/autoflake/compare/v2.3.0...v2.3.1)
- [github.com/psf/black: 24.2.0 → 24.3.0](https://github.com/psf/black/compare/24.2.0...24.3.0)
2024-03-25 17:15:40 +00:00
c99b691041 pre-commit 2024-03-17 14:03:05 -07:00
48fd1c2897 Force plain text on TextEdits 2024-03-16 11:52:14 -07:00
37c809db2a Fix crash when no comics are found in the IssueIdentifier 2024-03-16 11:52:14 -07:00
51db3e1249 Allow ignoring errors that happen the gui 2024-03-16 11:52:14 -07:00
c99f3fa083 [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0)
2024-03-12 20:00:49 +00:00
6f3a5a8860 Set the shell to bash 2024-03-09 19:49:59 -08:00
ebd99cb144 Set PKG_CONFIG_PATH as actions/setup-python@v5 overrides it 2024-03-09 18:06:30 -08:00
b1a9b0b016 Only upgrade icu4c and pkg-config 2024-03-09 14:47:47 -08:00
0929a6678b Update icu4c paths and upgrade packages on macOS 2024-03-09 14:45:49 -08:00
69824412ce Update GH Actions 2024-03-09 14:07:11 -08:00
0d9756f8b0 Pin minimum version for comicinfoxml 2024-03-09 13:51:35 -08:00
244cd9101d Remove commented code 2024-03-09 13:46:51 -08:00
3df263858d Merge branch 'web-links' into develop 2024-03-09 13:42:29 -08:00
b45c39043b Merge branch 'comicfn2dict' into develop 2024-03-09 13:10:27 -08:00
9eae71fb62 Disable checkboxes when the complicated parser is not used 2024-03-09 13:07:49 -08:00
9a95adf47d Bump comicfn2dict 2024-03-09 13:02:02 -08:00
956c383e5f Fix py7zr 2024-03-05 15:13:03 -08:00
5155762711 Add comicfn2dict as an alternative filename parser 2024-03-03 21:47:31 -08:00
ea43eccd78 Merge branch 'ii-rework' into develop 2024-03-01 15:39:01 -08:00
ff2547e7f2 Disable buttons for add/remove weblink 2024-03-01 15:26:56 -08:00
163cf44751 Open the editor when adding a now web link 2024-02-26 19:04:33 -08:00
14ce8a759f Mark all QTextEdit's as plain text only 2024-02-26 15:57:00 -08:00
22d92e1ded Move result determination out of _cover_matching 2024-02-26 15:38:13 -08:00
3c3700838b Select item on add and set the dirty flag on change 2024-02-25 08:26:29 -08:00
05423c8270 Use a QListWidget for web_links
Fix web_link in md_attributes
2024-02-24 22:31:45 -08:00
d277eb332b Add an option to disable prompt on save Fixes #422 2024-02-24 19:56:32 -08:00
dcad32ade0 Fix settngs generation 2024-02-24 19:55:28 -08:00
dd0b637566 Bump settngs 2024-02-24 19:01:10 -08:00
bad8b85874 Fix tests 2024-02-24 18:30:41 -08:00
938f760a37 Remove IssueIdentifier.search 2024-02-23 20:50:17 -08:00
f382c2f814 Update Tests 2024-02-23 20:47:22 -08:00
4e75731024 Re-write IssueIdentifier.search as IssueIdentifier.identify 2024-02-23 20:47:04 -08:00
920a0ed1af Implement better migration of changed settings should fix #609 2024-02-23 15:45:18 -08:00
9eb50da744 Fix setting rar info in the settings window Fixes #596
Look in all drive letters for rar executable
2024-02-23 15:45:18 -08:00
2e2d886cb2 Bump settngs 2024-02-22 14:52:26 -08:00
5738433c2b Fix fileselectionlist
Remove the custom widgetitem
Set a minimum size for the columns
Use a space " " a and nbsp "\xa0" for the check column to allow sorting
2024-02-22 14:30:15 -08:00
4a33dbde46 Fix PyInstaller packaging 2024-02-22 14:30:15 -08:00
10a48634bd Update talker dependencies 2024-02-19 12:29:36 -08:00
2492d96fb3 Merge branch 'pre-commit-ci-update-config' into develop 2024-02-19 12:08:43 -08:00
87248503b4 Allow 7z again 2024-02-19 11:57:30 -08:00
7705e7ea1f [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/asottile/pyupgrade: v3.15.0 → v3.15.1](https://github.com/asottile/pyupgrade/compare/v3.15.0...v3.15.1)
- [github.com/PyCQA/autoflake: v2.2.1 → v2.3.0](https://github.com/PyCQA/autoflake/compare/v2.2.1...v2.3.0)
- [github.com/psf/black: 24.1.1 → 24.2.0](https://github.com/psf/black/compare/24.1.1...24.2.0)
2024-02-19 17:19:25 +00:00
54b0630891 Allow 7z for rar decompression on Windows 2024-02-18 21:57:51 -08:00
27e70b966f Export translator_synonyms 2024-02-18 21:39:27 -08:00
ad8b92743c Remove unused variable 2024-02-18 18:01:51 -08:00
22b44c87ca Merge branch 'mizaki/autotag_source' into develop 2024-02-18 18:00:09 -08:00
2eca743f20 Fix #602
Tests were not made correctly to catch the change in 2c3a2566cc
This has now been corrected
2024-02-18 17:31:00 -08:00
bb4be306cc Fix fileselectionlist columns 2024-02-18 17:28:55 -08:00
768ef0b6bc Fix rar exe handling 2024-02-18 01:40:49 -08:00
b2d3869488 Update filerenaming for web_links
Ensure the j specifier in MetadataFormatter converts to str before joining
Add a web_link variable to the filerenamer
2024-02-17 17:42:07 -08:00
44e9a47a8b Support multiple web_links 2024-02-17 17:42:07 -08:00
215587d9a4 Move path under progress bar 2024-02-17 18:38:51 +00:00
7430e59b64 Add attributation to auto tag window 2024-02-17 18:36:49 +00:00
09490b8ebf Merge branch 'lordwelch-local-plugins' into develop 2024-02-12 17:40:09 -08:00
1e4a3b2484 Merge branch 'mizaki-meta_multi' into develop 2024-02-12 17:29:45 -08:00
b9bf3be4b2 Add short metadata style names 2024-02-12 20:57:32 +00:00
a1e4cec94f Log file path to plugin when it fails to load and remove debug statements 2024-02-11 13:18:03 -08:00
65e857af8b Move cache reset and load outside of loop. continue if it's impossible to use metadata 2024-02-11 19:32:12 +00:00
8887d48b3e Save metadata styles with one result per archive 2024-02-11 13:57:34 +00:00
e14714e26b Fix the --list-plugins command 2024-02-10 21:25:57 -08:00
8ec16528ab Implement local plugins 2024-02-10 21:00:24 -08:00
e9e619c992 Use CheckableComboBox in ui file 2024-02-11 01:51:47 +00:00
a6b60a4317 Simplify enabled widget check and reset cache before loading, break on failed metadata writing 2024-02-11 00:53:40 +00:00
69615c6c07 Fix hash and test 2024-02-10 15:02:24 -08:00
da6b2b02f4 Implement a replaceWidget helper function 2024-02-10 14:42:47 -08:00
3dfdae4033 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-02-10 01:55:15 +00:00
23021ba632 Add support for saving multiple metadata styles in the GUI
Unwind credit color comprehension

Convert save style from a string setting to a list

Use lordwelch version of Checkable combobox

Improve readbility, fix label alignment in taggerwindow.ui, better report removal of tags and clearer number meanings.

Unwind list comprehension for easier readability
2024-02-10 01:55:15 +00:00
bc335f1686 Forbid nested comprehensions 2024-02-06 18:01:26 -08:00
999d3eb497 Merge branch 'pre-commit-ci-update-config' into develop 2024-02-06 17:08:43 -08:00
bf67c6d270 Add E701 to flake8 ignores for new black version 2024-02-02 14:36:11 -08:00
df762746ec [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-01-29 17:14:26 +00:00
6687e5c6ca [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/psf/black: 23.12.1 → 24.1.1](https://github.com/psf/black/compare/23.12.1...24.1.1)
2024-01-29 17:14:04 +00:00
2becec0fb6 Update help for --overwrite 2024-01-22 17:01:40 -08:00
fbe56f4db9 Remove unnecessary dest arguments in settings 2024-01-22 17:00:59 -08:00
085543321a cbxClearFormBeforePopulating not working 2024-01-22 16:50:15 -08:00
f8c0ca195a Add cbxDisableCR, update cbxSplitWords and cbxClearFormBeforePopulating 2024-01-22 16:49:57 -08:00
dda0cb521a Add more credit synonyms 2024-01-21 15:06:34 -08:00
bb1a83b4ba Fix the rename command 2024-01-21 14:01:11 -08:00
f34e8200dd Fix add_to_path tests 2024-01-20 10:34:40 -08:00
539aac1307 Fix clearing lists via the '-m' option Fixes #587 2024-01-14 13:38:11 -08:00
f75ee58ac0 [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/PyCQA/flake8: 6.1.0 → 7.0.0](https://github.com/PyCQA/flake8/compare/6.1.0...7.0.0)
2024-01-08 17:15:56 +00:00
d27621ccd7 Merge branch 'pre-commit-ci-update-config' into develop 2023-12-31 14:45:45 -08:00
1ca585a65c Fix #584 2023-12-31 14:33:27 -08:00
39407286b3 Fix tarfile 2023-12-25 22:59:57 -08:00
6e56872121 Fix running dmgbuild again 2023-12-25 22:50:11 -08:00
888c50d72a Fix running dmgbuild 2023-12-25 22:41:57 -08:00
231b600a0e Switch to tar.gz and dmg archives to reduce space 2023-12-25 22:16:18 -08:00
db00736f58 Fix filename parsing not respecting user settings 2023-12-25 21:57:31 -08:00
5a714e40d9 [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/psf/black: 23.12.0 → 23.12.1](https://github.com/psf/black/compare/23.12.0...23.12.1)
- [github.com/pre-commit/mirrors-mypy: v1.7.1 → v1.8.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.7.1...v1.8.0)
2023-12-25 17:15:30 +00:00
230a4b6558 Update namespace 2023-12-24 18:32:52 -08:00
f7bd6ee4f3 Add cix support 2023-12-24 18:32:52 -08:00
1ef6e40c29 Allow the avif extension 2023-12-24 18:32:52 -08:00
7d1bf8525b Merge branch 'metadata-plugin' into develop 2023-12-24 18:32:42 -08:00
59694993ff Fix loading previous existing xml 2023-12-24 18:28:38 -08:00
109d8efc0b Update pyinstaller hook 2023-12-24 18:04:35 -08:00
c8507c08a9 Ensure ComicRack and CoMet metadata preserve unknown xml tags 2023-12-23 23:50:58 -08:00
28be4d9dd7 Improve errors when loading plugins 2023-12-23 23:47:44 -08:00
ceb3b30e5c Always apply the default page list when writing metadata 2023-12-20 21:24:12 -08:00
8dccedc229 Bump metron-talker minimum version 2023-12-19 09:05:56 -08:00
c3a8221d99 Return an empty object if an archive does not have the requested style 2023-12-18 16:59:31 -08:00
ed480720aa Update AUTHORS 2023-12-18 20:38:38 +00:00
f18f961dcd [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/pre-commit/pre-commit-hooks: v4.4.0 → v4.5.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.4.0...v4.5.0)
- [github.com/asottile/setup-cfg-fmt: v2.4.0 → v2.5.0](https://github.com/asottile/setup-cfg-fmt/compare/v2.4.0...v2.5.0)
- [github.com/asottile/pyupgrade: v3.10.1 → v3.15.0](https://github.com/asottile/pyupgrade/compare/v3.10.1...v3.15.0)
- [github.com/PyCQA/isort: 5.12.0 → 5.13.2](https://github.com/PyCQA/isort/compare/5.12.0...5.13.2)
- [github.com/psf/black: 23.7.0 → 23.12.0](https://github.com/psf/black/compare/23.7.0...23.12.0)
- [github.com/pre-commit/mirrors-mypy: v1.5.1 → v1.7.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.5.1...v1.7.1)
2023-12-18 17:17:28 +00:00
df781f67e3 Fix assigning black_and_white value 2023-12-18 02:46:53 -08:00
addddaf44e List metadata styles when listing plugins 2023-12-18 02:37:40 -08:00
4660b14453 Fixup metadata handling 2023-12-18 02:37:40 -08:00
9c231d7e11 Add better page info handling
Rename set_default_page_list to apply_default_page_list and apply
 during read_metadata
Add a filename attribute to the ImageMetadata class
Mark image_index as required
Always sort the page name list, a comic application will never need the
 unsorted list of names
Assign the first result from get_cover_page_index_list to coverImage in
 CoMet tags
Allow an Archiver to be passed to the ComicArchive constructor
2023-12-18 02:37:34 -08:00
989470772f Make widget disabling more consistent 2023-12-18 01:24:30 -08:00
8b7443945b Use ids for metadata type in file selection list
Removed unnecessary FileInfo class
2023-12-17 22:01:47 -08:00
da373764e0 Let the original ComicRack metadata be disabled
Ensure metadata styles can be overridden by other plugins
2023-12-17 21:47:44 -08:00
fd868d9596 Add supports_credit_role to metadata plugins 2023-12-17 21:47:44 -08:00
ae5e246180 Add plugin support for metadata 2023-12-17 21:47:43 -08:00
04b3b6b4ab Do not normalize series_name when a literal search is requested 2023-12-17 19:14:38 -08:00
564ce24988 Bump settngs to 0.9.2 2023-12-17 18:30:01 -08:00
3b2e763d7d Merge branch 'json-output' into develop 2023-12-17 18:28:53 -08:00
50859d07c4 Set the return code to 3 if any results are not successful 2023-12-17 18:17:19 -08:00
04bf7f484e Ensure IssueIdentifier output goes to the right place 2023-12-17 18:10:18 -08:00
4c1247f49c Print the summary even if quiet mode is enabled 2023-12-17 18:03:25 -08:00
17a8513efc Disable json output in interactive mode 2023-12-17 17:56:12 -08:00
7ada13bcc3 Remove unnecessary print statements 2023-12-17 17:35:21 -08:00
5b1c92e7b8 Fix a crash when fetching images during auto-tag in the gui 2023-12-17 16:25:21 -08:00
45643cc594 Add integration tests 2023-12-17 16:24:32 -08:00
ab6b970063 Create an Action tuple for determining the current command 2023-12-17 16:16:21 -08:00
9571020217 Upgrade settngs to 0.9.1 2023-12-17 16:15:26 -08:00
bb67ab009e Ensure that all output goes through a logger before output to the user
Adds an option to output json for CLI options
2023-12-17 15:51:43 -08:00
f3b235ae14 Move pyupgrade above autoflake to reduce runs of pre-commit required 2023-12-16 17:28:41 -08:00
0de95777b4 Handle multiple options sharing a dest 2023-12-16 17:06:27 -08:00
9d36ed0dc6 Update AUTHORS 2023-12-16 17:50:55 +00:00
e0eec002fa docs(contributor): contrib-readme-action has updated readme 2023-12-16 17:50:51 +00:00
79779b7a46 Merge branch 'DrMcCoy/fix_crash_shortcut_pagetype' into develop 2023-12-16 09:49:09 -08:00
df24ad0008 Fix crash when using shortcut to set page type
QListWidget has no rowCount() method, it has count() instead.
2023-12-16 17:16:31 +01:00
651c5aed37 Add packaging dependency 2023-12-13 09:53:41 -08:00
3c83dbd038 Merge branch 'mizaki/talkers_version_check' into develop 2023-12-13 09:52:20 -08:00
fc6e0c3db3 Parse ct version only once 2023-12-12 23:47:47 +00:00
c5cfd3ebdc Add a link to the log folder from the log window 2023-12-01 19:48:16 -08:00
cead69f8e3 Merge branch 'mizaki/settings_encoder' into develop 2023-12-01 19:43:18 -08:00
4d2b9e1157 Warn on bad min ct required verion and use anyway. Use clearer log messages 2023-12-01 14:09:17 +00:00
f977e70562 Rename min ct required var. Use a minimum version only check instead of full spec 2023-12-01 01:23:46 +00:00
12dd06c558 Add CT verion check against talker requirements 2023-11-30 01:50:28 +00:00
70541cc9ee Encode pathlib.Path for the settings file. Validate types from the JSON settings file after loaded. JSON.decoder not used due to its limitation with context. 2023-11-28 23:21:04 +00:00
d37c7a680d Update dependencies 2023-11-28 15:08:26 -08:00
1ff6f1768b Use importlib.resources instead of __file__ 2023-11-25 12:32:50 -08:00
99325f40cf Merge branch 'mizaki/cleanup_html' into develop 2023-11-23 16:12:02 -08:00
65948cd9cd Merge branch 'bump-settngs' into develop 2023-11-23 16:06:01 -08:00
305eb1dec5 Enable stricter mypy configuration 2023-11-23 16:05:16 -08:00
9aad872ae6 Merge branch 'uigenerator' into develop 2023-11-23 15:19:20 -08:00
a478a35f66 Simplify setting values on Qt widgets
Add explanatory comments
2023-11-23 15:18:59 -08:00
128cab077c Replace pycountry with isocodes
isocodes is updated more often and doesn't depend on deprecated packages
2023-11-23 14:21:21 -08:00
9dc6f8914f Upgrade settings to 0.8.0 2023-11-19 23:14:40 -08:00
57873136b6 Use isinstance for type check 2023-11-14 15:18:48 -08:00
987f3fc564 cleanup_html improvements 2023-11-13 01:41:26 +00:00
10776dbb07 Fix flake8 issues 2023-11-09 18:23:57 -08:00
2d3f68167c Merge branch 'progress-dialog' into develop 2023-11-09 16:57:02 -08:00
770f64b746 Merge branch 'mizaki-talker_file_picker' into develop 2023-11-09 16:53:15 -08:00
235c12bd53 Convert types back to their declared types in talkeruigenerator 2023-11-09 16:52:41 -08:00
10b19606e0 Fix GenericMetadata __str__ 2023-11-05 21:36:29 -08:00
a7d1084a4d Remove flake8-warnings 2023-11-05 13:27:31 -08:00
21575a9fb8 Fix saving CBI when credits are empty 2023-11-05 13:27:14 -08:00
2258d70d7b Add file picker to talkers options. Requires type of pathlib.Path 2023-11-01 02:01:54 +00:00
b23c3195e3 Merge branch 'lexNumbers' into develop 2023-10-27 23:50:05 -07:00
bd9b3522d8 Improve edge cases
Lex `'` as a symbol
Lex multiple symbols as a single item
Prefer `$` at the start of a number
Simplify issue number parsing
2023-10-27 23:26:40 -07:00
78060dff61 Rework parse_series 2023-10-27 23:26:40 -07:00
4a29040c74 Add format to the filename parser result 2023-10-27 23:13:12 -07:00
496f3f0e75 fix reset after space 2023-10-23 22:05:42 -07:00
f03b2e58cf Improve lexing numbers
lex currency amounts as text
lex a '.' followed by a number as a number if there is a preceding space
2023-10-23 21:13:31 -07:00
29ddc3779a Ensure FilenameInfo is always filled out fixes #556 2023-10-23 21:08:55 -07:00
7842109ca2 Pin chardet version 2023-10-22 16:01:46 -07:00
7527dc4fd8 FIX: A hamming distance of 0 is a perfect match. Adjust to 100 for empty URLs 2023-10-12 22:34:16 +01:00
8dfd38a15c Merge branch 'rar-cwd' into develop 2023-10-12 01:31:57 -07:00
6227edb0a3 Set rar cwd to reduce errors 2023-10-12 01:30:32 -07:00
114a0bb615 Fix parsing '&' with the "complicated" filename parser 2023-10-12 01:26:31 -07:00
abfd97d915 Merge branch 'protofolius_issue_scheme' into develop 2023-10-11 17:05:27 -07:00
582b8cc57b Add more parseable filenames 2023-10-11 17:03:07 -07:00
97a24d8d52 Change dialog modality and only center dialog when it is created 2023-10-08 11:59:57 -07:00
edb087abde Handle errors when reading zip comments fixes #548 2023-10-07 11:49:57 -07:00
96c5c4aa28 Fix pyinstaller build
Fix exception when PyQt is not installed
2023-10-07 11:49:30 -07:00
4b93262d5f Merge branch 'mizaki-window_sorting' into develop 2023-10-06 20:14:35 -07:00
78a890f900 Fix parsing a month name in the series fixes #542 2023-10-06 20:06:39 -07:00
5bdbe7d181 Always update rows even if None 2023-10-05 22:14:45 +01:00
f250d2c5c3 Merge branch 'mizaki-gmd_list_set' into develop 2023-10-04 20:16:33 -07:00
b6d5fe7013 Improve rar error messages 2023-10-04 19:08:17 -07:00
80f3dd7ce4 Restore issue number sorting 2023-09-30 23:19:10 +01:00
0c63f77e53 Restore issue count and year sorting 2023-09-30 23:05:06 +01:00
5688cdea89 Merge branch 'mizaki-gentalker_password' into develop 2023-09-26 17:05:20 -07:00
2949626f6d Merge branch 'mizaki-remove_series_genres' into develop 2023-09-26 17:04:45 -07:00
319aa582e5 Remove ignoring default for setting generation combobox 2023-09-25 00:55:50 +01:00
058651cc29 Change metadata lists to sets. Changed CV talker to reflect and tidied 2023-09-24 14:33:57 +01:00
5874f3bcaf Remove genres from ComicSeries as it is no longer required with the new cache system 2023-09-22 23:15:04 +01:00
c6522865ab Use casefold 2023-09-21 16:05:13 +01:00
5684694055 Generate password box for any settings dest name that end in password 2023-09-21 01:47:08 +01:00
360a9e6308 Merge branch 'mizaki-talker_gen_combo' into develop 2023-09-17 16:39:33 -07:00
015959bd97 Merge branch 'mizaki-talker_setting_logo_blurb' into develop 2023-09-17 16:35:13 -07:00
8feade923a Don't capitalise and therefore no need to use data on the combobox 2023-09-17 20:54:20 +01:00
df3e7912b3 Add talker information in setting window 2023-09-17 18:26:06 +01:00
919561099e Finish removing the script option 2023-09-17 08:36:00 -07:00
e7cc05679f Bump metron-talker version 2023-09-17 08:09:43 -07:00
99461c54f1 Fix a crash when setting the page type with no comic selected 2023-09-15 21:03:41 -07:00
56f172e7b5 Add combo box support to talker settings generator 2023-09-15 23:46:13 +01:00
ddd98ee86d Add metron-talker as an optional dependency 2023-09-15 15:13:14 -07:00
1d25179171 Allow unsetting metadata fields on the commandline fixes #528 2023-09-14 11:30:05 -07:00
7efef0bb44 Merge branch 'mizaki-on_change_windows' into develop 2023-09-14 11:20:01 -07:00
366e9cf6e8 Move update into own function. Add title missing to trigger issue update. 2023-09-13 21:35:52 +01:00
57abe22515 Merge branch 'mizaki-fix_auto_id' into develop 2023-09-12 15:16:16 -07:00
c7a49b3643 Fix crash with series and issue window if the year is None. Closes #523 2023-09-10 13:42:17 +01:00
1125788bb7 Update series and issue rows after calling for more information. Closes #512 2023-09-10 13:31:20 +01:00
034a25a813 Fix auto-identify crash 2023-09-07 14:44:30 +01:00
f72c0c8224 Fix call to check_api 2023-09-06 04:56:30 -04:00
f6be7919d7 Implement support for protofolius's permission scheme 2023-09-06 04:50:05 -04:00
0a2340b6dc Remove the --script commandline option 2023-09-06 03:00:27 -04:00
bf2b4ab268 Rename check_api_key to check_status
Parameter is changed to a settings dict so that a Talker can retrieve any info it needs
Change issue_id type annotation to str
2023-09-06 02:59:59 -04:00
40bd3d5bb8 Fix generation and saving of talker settings fixes #515 #514 2023-09-05 14:43:17 -04:00
61d2a8b833 Fix issue padding validation fixes #513 2023-09-05 14:42:03 -04:00
b04dad8015 Stop deleting self.progialog in the series selection window 2023-09-05 14:41:07 -04:00
3ade47a7e0 Convert bytes to str when printing raw tags. Fixes #510 2023-09-05 04:05:20 -04:00
5bc44650d6 Change --only-set-cv-key to --only-save-config 2023-09-05 03:56:56 -04:00
8b1bcd93e6 Add a combobox to select a metadata source in the main window Fixes #508 2023-09-05 03:55:18 -04:00
d70a98ed29 Fix --darkmode 2023-09-05 03:55:18 -04:00
05e6eaf88e Update setting group names
Make group names presentable to users and add builtin plugins during namespace generation.
Revamp talkeruigenerator.py to use generated group and setting names and remove as much hard-coded strings as possible
Add a --list-plugins commandline option
2023-09-05 03:55:12 -04:00
90eb1c3980 Fix date display in the issue selection window 2023-09-05 03:14:55 -04:00
7a63474769 Fix cbr tests and update pre-commit 2023-09-04 19:56:18 -05:00
0f07fc3153 Use a dictionary instead of a list in the issue/series selection windows
List lookups were done by row number which became inaccurate if any sorting was done

Fixes #507
2023-09-03 15:18:56 -07:00
e832b19f2f Fix attribute names 2023-09-03 15:12:06 -07:00
9499aeae10 PyrateLimter version 2 only for now. 2023-08-30 23:23:19 +01:00
f72ebdb149 Simplify ComicCacher to store a single binary data field and ID(s)
If the ComicCacher is to be a generic cache for talkers it must assume
 very little. Current assumptions:
 - There are issues that can be queried individually by an "Issue ID" and they have a relation to a single series
 - There are series that can be queried individually by an "Series ID" and they have a relation to zero or more issues
 - There are Searches that can be queried by the search term and they have a relation to zero or more series

Each series and issue have a boolean `complete` attribute which is up to the talker to decide what it means.
Data is returned as a tuple ([series, complete] or [issue, complete]) or a list of tuples
An issue consists of an ID, an series ID and a binary data attribute which is up to the talker to determine what it means.
An series consists of in ID and a binary data attribute which is up to the talker to determine what it means.

The data attribute is binary to allow for compression and efficient storage of binary data (e.g. pickle) it is suggested to store it as json or similar text format encoded with utf-8. If the talker is using a website API it is suggested to store the raw response from the server.

All caches automatically expire 7 days after insertion.
2023-08-05 03:02:12 -07:00
ea84031b87 Add more 4-digit issue number tests 2023-08-04 21:04:21 -07:00
611c40fe0b Add test for split 2023-08-03 01:06:10 -07:00
2c3a2566cc Convert ComicIssue into GenericMetadata
I could not find a good reason for ComicIssue to exist other than that
 it had more attributes than GenericMetadata, so it has been replaced.
New attributes for GenericMetadata:
  series_id:        a string uniquely identifying the series to tag_origin
  series_aliases:   alternate series names that are not the canonical name
  title_aliases:    alternate issue titles that are not the canonical name
  alternate_images: a list of urls to alternate cover images

Updated attributes for GenericMetadata:
  genre        -> genres:        str -> list[str]
  comments     -> description:   str -> str
  story_arc    -> story_arcs:    str -> list[str]
  series_group -> series_groups: str -> list[str]
  character    -> characters:    str -> list[str]
  team         -> teams:         str -> list[str]
  location     -> locations:     str -> list[str]
  tag_origin   -> tag_origin:    str -> TagOrigin (tuple[str, str])

ComicSeries has been relocated to the ComicAPI package, currently has no
 usage within ComicAPI.
CreditMetadata has been renamed to Credit and has replaced Credit from
 ComicTalker.
fetch_series has been added to ComicTalker, this is currently only used
 in the GUI when a series is selected and does not already contain the
 needed fields, this function should always be cached.

A new split function has been added to ComicAPI, all uses of split on
 single characters have been updated to use this

cleanup_html and the corresponding setting are now only used in
 ComicTagger proper, for display we want any html directly from the
 upstream. When applying the metadata we then strip the description of
 any html.

A new conversion has been added to the MetadataFormatter:
  j: joins any lists into a string with ', '. Note this is a valid
     operation on strings as well, it will add ', ' in between every
     character.

parse_settings now assigns the given ComicTaggerPaths object to the
 result ensuring that the correct path is always used.
2023-08-02 09:00:04 -07:00
1b6307f9c2 Merge branch 'mizaki-tidy_ii' into develop 2023-07-30 16:24:13 -07:00
548ad4a816 Fix folder archiver
Implement supports_comment and is_writable
Fix function call in ComicArchive for supports_comment
Add a menu option to open a folder as an archive
2023-07-29 00:07:25 -07:00
27f71833b3 Generate settngs namespace before formatting 2023-07-28 23:29:39 -07:00
6c07fab985 Fix tests taking forever caused by f90f373d20 2023-07-28 23:25:12 -07:00
4151c0e113 Cleanup sqlite
Remove the import rename
use sqlite3.Row allows retrieving value by name
2023-07-28 23:22:35 -07:00
3119d68ea2 Remove used issue id from get_issue_cover_match_score and fix test 2023-07-18 01:14:32 +01:00
f43f51aa2f Fix #396
Use a QWebEngineView if QtWebEngine is available.
If QtWebEngine is not available replace figure tags with div's to allow
 the QTextEdit to render the rest of the html properly
2023-07-01 23:29:38 -07:00
19986b64d0 Upgrade pre-commit hooks 2023-07-01 23:12:41 -07:00
00200334fb Add filter to SeriesSelectionWindow and IssueSelectionWindow fixes #476 2023-07-01 18:57:33 -07:00
cde980b470 Add LICENSE file 2023-07-01 18:13:38 -07:00
f90f373d20 Merge branch 'mizaki-rate_limit_cv' into develop 2023-07-01 18:04:24 -07:00
c246b96845 Merge branch 'mizaki-vol_to_issue' into develop 2023-07-01 18:02:57 -07:00
053afaa75e Merge branch 'mizaki-phash' into develop 2023-07-01 18:01:26 -07:00
3848aaeda3 Merge branch 'mizaki-issue_count_sort' into develop 2023-07-01 17:56:55 -07:00
16b13a6fe0 Format year and count of issues to 4 digits and do a None check 2023-06-28 01:08:04 +01:00
3f180612d3 Return int instead of hex and revert hamming_distance etc. 2023-06-27 22:44:08 +01:00
37cc66cbae Use requests.status_codes.codes.TOO_MANY_REQUESTS 2023-06-27 17:48:38 +01:00
81b15a5877 Fixes sorting by year and issue count. Removed superfluous if for publisher. Fixes #475 2023-06-27 00:21:28 +01:00
14a4055040 Add Perceptual Hash computation to imagehasher mirroring https://github.com/JohannesBuchner/imagehash but in pure python 2023-06-26 01:54:26 +01:00
2e01672e68 Fix #485
As mentioned in the comment in comictaggerlib/main.py:186
The default value should be None not the empty string.
We also check if the given value is the default or the empty string and
 the setting is unset so the default value is not saved in the settings
 file.
The default_api_url is shown in the GUI Settings Window it is not
 currently show in the cli help.
2023-06-23 17:48:18 -07:00
4a7aae4045 Add tests for fix_url 2023-06-23 17:10:40 -07:00
2187ddece8 Move volume from ComicSeries to ComicIssue 2023-06-23 22:38:15 +01:00
fba5518d06 Create two module limiters and assign class limiter var depending. Add to welcome message limits of default CV API key. 2023-06-23 21:25:02 +01:00
31cf687e2f Reduce startup time 2023-06-22 20:11:40 -07:00
526069dabf Use _guess_type from settngs for more robust type checking 2023-06-22 18:28:43 -07:00
635cb037f1 Merge branch 'mizaki-fix_add_fields' into develop 2023-06-22 17:51:26 -07:00
861584df3a Move rate limit check from defunc API status code 107 to HTTP code 429. Set a limit of 10 request every 10 seconds except for the default API key which is 1,2 (to be finisalised). Remove wait on rate limit option. 2023-06-22 23:50:32 +01:00
a53fda9fec Update linux packages in GitHub Actions 2023-06-21 19:47:41 -07:00
af5a0e50e0 Remove wait on CV rate limit in autotag 2023-06-21 22:32:06 +01:00
7a91acb60c Add pyrate-limiter and apply CV suggested rate limit 2023-06-20 22:28:29 +01:00
3a287504ae Fix setting issue and alternate_number on GenericMetadata
IssueString.as_string always returns a string this is a problem for
  GenericMetadata. When the overlay function is used it checks
  specifically for the value None this allows the -m option to unset
  attributes however the issue attribute would get set to the empty
  string when loading ComicRack tags regardless of if there was a value
  stored in the file. Fixes #465 and #480
2023-06-15 20:26:38 -07:00
82a22d25ea Merge branch 'mizaki-auto_ident_message' into develop 2023-06-11 21:44:05 -07:00
783e10a9a1 Generate a namespace object for typing settngs 2023-06-09 16:20:00 -07:00
e8f13b1f9e fix quoting 2023-06-09 02:08:38 +01:00
4b415b376f Fix tests 2023-06-08 01:26:03 +01:00
122bdf7eb1 Change auto-identfy message to point users to the auto-tag assume 1 option 2023-06-08 01:18:46 +01:00
2afb604ab3 Fix issue_count and add maturity rating 2023-06-08 00:52:24 +01:00
a912c7392b Merge branch 'mizaki-additional_comic_fields' into develop 2023-06-03 10:37:44 -07:00
3b92993ef6 Remove country name code 2023-06-03 00:11:40 +01:00
c3892082f5 Change data to metadata 2023-06-02 00:37:58 +01:00
92e2cb42e8 Replace instances of Comic Vine to use the talker's name 2023-06-01 22:05:14 +01:00
b8065e0f10 Fix #470 re-add notes when using --clear-metadata 2023-05-30 21:36:33 -07:00
a395e5541f Remove invalid comments 2023-05-25 15:00:53 +01:00
d191750231 Remove attempted validation of language and country plus minor changes 2023-05-25 01:32:52 +01:00
e72347656b Add format (1-shot, limited series, etc.) 2023-05-23 00:27:58 +01:00
8e2411a086 Add country functions to utils and try to convert a country name to ISO country name 2023-05-23 00:02:56 +01:00
97e64fa918 Add maturity_rating, language and country to ComicIssue and pass to metadata. 2023-05-18 02:02:21 +01:00
661d758315 Merge branch 'mizaki-talker_parse_key' into develop 2023-05-16 17:33:24 -07:00
364d870fe0 Merge branch 'mizaki-hide_api_token' into develop 2023-05-16 17:30:46 -07:00
2da64fd52d Remove password class from function 2023-05-16 15:20:45 +01:00
057725c5da Create generate_password_textbox 2023-05-16 00:25:12 +01:00
5996bd3588 Add show/hide icon to key field 2023-05-15 23:46:16 +01:00
fdf407898e Bump MacOS version for GitHub Actions 2023-05-15 10:59:23 -06:00
70d544b7bd Add attrib at the end of the CLI file run 2023-05-15 16:46:31 +01:00
c583f63c8c Attribution for metadata provider on command line 2023-05-14 23:39:23 +01:00
d65a120eb5 Add issue_count 2023-05-14 00:50:37 +01:00
60f47546c2 Hide the API key field as a password and add a show/show button 2023-05-13 23:12:29 +01:00
0b77078a93 Retrieve all fields instead of by (many) names 2023-05-12 23:46:34 +01:00
2598fc546a Use new xlate_int and xlate_float 2023-05-12 22:47:36 +01:00
ddf4407b77 Merge branch 'develop' into additional_comic_fields 2023-05-12 22:41:38 +01:00
6cf259191e Add volume and count_of_volumes to ComicSeries 2023-05-12 21:48:45 +01:00
30f1db1c73 Update requirements and Linux build dependencies 2023-04-26 14:46:18 -07:00
ff15bff94c Fix pypi upload 2023-04-25 16:26:05 -07:00
83aabfd9c3 Upgrade pre-commit 2023-04-25 16:11:19 -07:00
d3ff40c249 Only update the image in CoverImageWidget if the url matches the current url
This fixes an issue causing the first issue cover to show when using the auto-identify feature
Fixes #455
2023-04-25 16:00:08 -07:00
c07e1c4168 Add additional typing 2023-04-25 16:00:06 -07:00
1dc93c351d Update settngs to typed version fixes #453 2023-04-25 16:00:04 -07:00
f94c9ef857 Update appimage step
Fix platform case
Remove icu check from appimage step as ComicTagger is not installed
Add appimagetool to allowed commands
Fix appimage paths
2023-04-25 16:00:02 -07:00
14fa70e608 Separate xlate into separate functions based on return type fixes #454 2023-04-25 15:55:27 -07:00
ec65132cf2 Mark mypy as optional 2023-04-23 02:01:41 -07:00
941bbf545f Remove extraneous if 2023-04-23 01:52:56 -07:00
afdb08fa15 Fix package.yaml 2023-04-23 01:49:42 -07:00
c4b7411261 Use tox for building 2023-04-23 01:31:44 -07:00
5b3e9c9026 Switch to rarfile for rar/cbr support 2023-04-23 00:48:13 -07:00
e70c47d12a Make PyICU optional
Update README.md
2023-04-23 00:48:11 -07:00
c1aba269a9 Revert "Make PyICU optional"
This reverts commit bf55037690.
2023-04-22 21:28:14 -07:00
bf55037690 Make PyICU optional
Fix more locale issues
Update README.md
2023-04-18 21:03:50 -07:00
e2dfcc91ce Revert get_recursive_filelist Fixes #449 2023-04-13 20:58:30 -06:00
33796aa475 Fix #447 2023-04-06 10:48:40 -07:00
4218e3558b Add url 2023-03-05 18:58:06 +00:00
271bfac834 Do not fail when talker key is missing 2023-03-03 00:07:49 +00:00
9e86b5e331 Fix tests 2023-03-02 00:23:56 +00:00
c9638ba0d9 Format manga and rating 2023-03-02 00:10:52 +00:00
428879120a Merge branch 'mizaki-talkeruigen_fix' into develop 2023-02-28 11:49:27 -08:00
f0b9bc6c77 Missed name changes from options move 2023-02-28 15:37:52 +00:00
6133b886fb String widget fix-fix 2023-02-28 15:06:59 +00:00
dacd767162 String widget fix 2023-02-28 14:59:58 +00:00
4d90417ecf Update AUTHORS 2023-02-28 06:31:07 +00:00
c3e889279b Fix EOF 2023-02-27 22:30:31 -08:00
9bf998ca9e Remove check_api_url and fix docstrings 2023-02-27 22:29:23 -08:00
5b2a06870a Fix talker settings validation 2023-02-27 22:21:56 -08:00
fca5818874 Merge branch 'mizaki-talker_settings_generator' into develop 2023-02-27 22:20:53 -08:00
eaf0ef2f1b Fix Makefile dependencies
Remove dist/appimage before copy to prevent issues with 2nd run
Add dist/appimagetool target so that the appimage tool is downloaded once
2023-02-27 22:12:12 -08:00
09fb34c5ff Merge branch 'bmfrosty-feature/add-appimage-support' into develop 2023-02-27 22:01:13 -08:00
924467cc57 Add AppImage Support 2023-02-26 22:12:50 -08:00
2611c284b8 Revert "docs(contributor): contrib-readme-action has updated readme"
This reverts commit aba59bdbfe.
2023-02-24 13:23:29 +00:00
b4a3e8c2ee Add missing tool tips to labels
Change metadata select label
Use named tuple for talker tabs
Retrun a string and bool for api check
2023-02-24 00:06:48 +00:00
118429f84c Change source term to metadata
Generate API text field in their own function
API tests return string message of result
Add help to text field lables
2023-02-23 00:42:48 +00:00
8b9332e150 Fix linux build 2023-02-21 20:00:47 -08:00
5b5a483e25 Fix api key test button generation 2023-02-21 00:58:13 +00:00
33ea8da5bc Merge branch 'develop' into talker_settings_generator
# Conflicts:
#	comictaggerlib/settingswindow.py
#	comictalker/talkers/comicvine.py
2023-02-21 00:50:06 +00:00
aba59bdbfe docs(contributor): contrib-readme-action has updated readme 2023-02-21 00:43:46 +00:00
316bd52f21 Use currentData for combo box 2023-02-21 00:42:11 +00:00
59893b1d1c Fix optoin.type ifs 2023-02-21 00:38:13 +00:00
fb83863654 Update plugin settings
Make "runtime" a persistent group, allows normalizing without losing validation
Simplify archiver setting generation
Generate options for setting a url and key for all talkers
Return validated talker settings
Require that the talker id must match the entry point name
Add api_url and api_key as default attributes on talkers
Add default handling of api_url and api_key to register_settings
Update settngs to 0.6.2 to be able to add settings to a group and
  use the display_name attribute
Error if no talkers are loaded
Update talker entry point to comictagger.talker
2023-02-20 16:02:15 -08:00
f131c650fb Merge branch 'mizaki-talker_entry_points' into develop 2023-02-20 14:27:09 -08:00
f439797b03 Use new display_name from settngs. Add source combobox getting and setting and add to sources dict of widgets. 2023-02-20 18:45:39 +00:00
bd5e23f93f Add another test case for format_internal_name 2023-02-20 00:44:51 +00:00
fefb3ce6cd Remove general tab from talker tab and use base tab from settings window. Additional clean up. 2023-02-19 23:33:22 +00:00
a24bd1c719 Generate talker general tab programatically. Move search options to search tab. 2023-02-18 17:16:56 +00:00
02fd8beda8 Use None as parent for api and url message boxes
Rename test_api_key and test_api_url to api_key_btn_connect and api_url_btn_connect
Make separate function to set form values, called in settings_to_form
Change isinstance to is
Call findChildren only once
2023-02-18 01:15:46 +00:00
628dd5e456 Fix actions failure when there are no new contributors 2023-02-17 13:43:41 -08:00
c437532622 Merge branch 'mizaki-cache_role_fix' into develop 2023-02-17 10:21:54 -08:00
0714b94ca1 Restrict contributions updates to only run on pushes to develop 2023-02-17 10:16:21 -08:00
5ecaf89d15 Update AUTHORS 2023-02-17 01:23:54 +00:00
2491999a33 Update copyright statements to ComicTagger Authors 2023-02-16 17:23:13 -08:00
9c7bf2e235 Update AUTHORS 2023-02-17 01:14:29 +00:00
0c1093d58e docs(contributor): contrib-readme-action has updated readme 2023-02-17 01:14:27 +00:00
a41c5a8af5 Automate contributions 2023-02-16 17:13:26 -08:00
b727b1288d Apply credit datatype to person data from cache 2023-02-15 17:05:14 +00:00
73738010b8 Add additional fields to ComicIssue and add a genre field to ComicSeries to allow for filtering of search results from the cache. 2023-02-15 16:48:07 +00:00
2fde11a704 Test for menu generator format_internal_name 2023-02-14 01:47:32 +00:00
6a6a3320cb Move talker settings menu generator to a separate file 2023-02-14 01:32:56 +00:00
83a8d5d5e1 Generate settings tabs for each talker 2023-02-11 01:18:56 +00:00
4b3b9d8691 Entry points for talkers 2023-02-10 21:16:35 +00:00
3422a1093d Merge branch 'mizaki-showcontrols' into develop 2023-02-10 00:31:24 -08:00
4eb9e008ce Update pre-commit 2023-02-10 00:25:20 -08:00
5e86605a46 Fix docstring typos 2023-02-10 00:25:18 -08:00
8146b0c90e Merge branch 'talker-cleanup' into develop 2023-02-10 00:24:48 -08:00
983937cdea Mark internal functions in ComicVineTalker 2023-02-10 00:23:02 -08:00
e5b15abf91 clean up talker 2023-02-10 00:23:00 -08:00
4a5d02119e Merge branch 'settings-consistency' into develop 2023-02-10 00:22:44 -08:00
4b6c9fd066 Fix comicarchive_test.py 2023-02-10 00:14:58 -08:00
79a6cef794 Hide invisible controls to prevent bottom margin on source logos. 2023-02-10 00:43:05 +00:00
43cb68b38b Fix 'Default Preferences' button in the settings window 2023-02-04 11:34:49 -08:00
ad68726e1d Use consistent naming for settings
config: always values
setting: always the definition/description not the value
2023-02-04 11:33:21 -08:00
ba4b779145 Remove legacy settings 2023-02-03 20:14:31 -08:00
d987a811e3 Consolidate plugin code 2023-02-03 20:13:58 -08:00
ee426e6473 Merge branch 'mizaki-talker_settings' into develop 2023-02-03 18:14:26 -08:00
9aa42c1ca7 Add series match threshold back into search_for_series as it is no longer available via the talkers own settings. 2023-02-03 21:38:17 +00:00
d12325b7f8 Simplify parse_settings. Prefix talker_ to group name. Add back setting CV key via commandline. Other small changes as requested. 2023-02-02 00:53:13 +00:00
ce5205902a After merge isort 2023-02-01 23:53:02 +00:00
94aabcdd40 Merge branch 'develop' into talker_settings
# Conflicts:
#	comictaggerlib/ctoptions/__init__.py
#	comictaggerlib/main.py
#	comictalker/talkers/comicvine.py
2023-02-01 23:38:13 +00:00
839a918330 typed talkers var 2023-02-01 23:22:04 +00:00
053295e028 Merge branch 'mizaki-source_logo_url' into develop 2023-02-01 08:03:16 -08:00
c6e3266f60 More verbose attrib string 2023-02-01 15:39:24 +00:00
7c4e5b775b Merge branch 'plugableArchivers' into develop 2023-01-31 19:44:07 -08:00
bc02a9a2a2 Use a persistent setting group for archiver settings 2023-01-31 19:41:19 -08:00
2c5d419ee9 Remove legacy rar settings 2023-01-31 00:32:19 -08:00
46899255c8 Generate settings for an archivers executable 2023-01-30 21:36:47 -08:00
6a650514fa Rename new settings talker methods. Move parse_settings for talkers to earlier and only pass talkers own settings. 2023-01-30 01:59:23 +00:00
0f10e6e848 Create simple dict of talkers with objects. Moved thresh setting back to talkers (general) as it is called outside of talker. 2023-01-26 00:52:02 +00:00
0d69ba3c49 Rename talkers_general to talkers. Moved plugin option register to own file. Due to chicken and egg, first get talker classes then create objects. 2023-01-25 19:10:58 +00:00
d0e3b487eb Mark label for external links. attrib str to be complete. 2023-01-22 17:16:33 +00:00
c80627575a Add docstrings to Archiver 2023-01-21 15:24:27 -08:00
92eb79df71 Fix console_scripts entry point 2023-01-21 00:27:39 -08:00
ad48ad757c Fix plugin order 2023-01-20 19:32:32 -08:00
2de241cdd5 Fix typing 2023-01-20 19:32:06 -08:00
5d66815765 Add attrib string for source. Add logo and URL to issues window. 2023-01-20 00:29:02 +00:00
100e0f2101 Load plugins in init. 2023-01-15 17:38:50 +00:00
55e3b7c7e0 Use name for URL display. Window sizes. 2023-01-13 21:27:40 +00:00
f6698f7f0a Call load_archive_plugins in ComicArchive __init__ 2023-01-12 17:00:11 -08:00
50614d52fc Update PyInstaller hook 2023-01-12 15:47:34 -08:00
712986ee69 Turn comicapi.archivers.* into plugins 2023-01-12 14:45:49 -08:00
2f7e3921ef Separate archivers into their own packages 2023-01-12 14:45:17 -08:00
80f42fdc3f Move log header to execute immediately after the log is configured 2023-01-12 14:43:12 -08:00
725b2c66d3 Use imageWidget for source logo and URL. 2023-01-12 16:58:50 +00:00
5394b9f667 Fix tests. Probably not the correct way to do this? 2023-01-12 15:10:39 +00:00
fad103a7ad Use setting option for talker selection 2023-01-07 00:29:12 +00:00
87cd106b28 Add source logo and URL to series window 2023-01-04 23:51:39 +00:00
2d8c47edca Use new settings system for plugin 2023-01-02 01:04:15 +00:00
0ac5b59a1e Merge branch 'mizaki-rename_namespace_fix' into develop 2022-12-31 20:49:45 -08:00
7c735b3555 Fix rename namespace 2023-01-01 02:07:42 +00:00
9d8cf41cd3 Fix try block parsing credits in ComicCacher 2022-12-31 12:36:32 -08:00
ee3a06db46 Merge branch 'crop-border' into develop 2022-12-31 12:35:29 -08:00
7df2e3fdc0 Automatically crop black borders from covers 2022-12-31 11:52:23 -08:00
20e7de5b5f Fix reference to the user cache directory 2022-12-31 02:26:44 -08:00
f83f72fa12 Improve issue number handling regarding the '#' 2022-12-31 02:15:17 -08:00
fb4786159d Handle issue numbers with more than 3 digits 2022-12-30 21:50:10 -08:00
734b83cade Switch comictalker TypedDicts to dataclasses 2022-12-23 01:58:10 -08:00
746c98ad1c Add temp to .gitignore 2022-12-23 00:09:46 -08:00
9f00af4bba Change issue id and series id to strings 2022-12-23 00:09:19 -08:00
92fa4a874b Improve typing in ComicVineTalker 2022-12-22 10:47:37 -08:00
a33b00d77e Update ComicTalker documentation 2022-12-22 10:47:35 -08:00
a7f6349aa4 Merge branch 'volume-to-series' into develop 2022-12-22 10:45:58 -08:00
d4b4544b2f Replace most instances of volume in ComicVineTalker with series
All remaining uses of the word volume are used directly by the api and
are documented that it refers to the series
2022-12-22 10:30:48 -08:00
521d5634f3 Fix tests 2022-12-22 10:16:32 -08:00
1d9840913a Change all references of volume to series 2022-12-22 10:16:05 -08:00
53a0b23230 Collapse formatting 2022-12-15 20:21:53 -08:00
9004ee1a6b Merge branch 'settings' into develop 2022-12-15 20:17:50 -08:00
440479da8c Update to settngs 0.3.0
Use the namespace instead of a dictionary
Cleanup setting names
2022-12-15 20:10:35 -08:00
e5c3692bb9 Fail if an error occurs when loading settings 2022-12-15 18:58:53 -08:00
103379e548 Split settings out into a separate package 2022-12-14 23:16:54 -08:00
eca421e0f2 Split out settings functions 2022-12-13 08:50:38 -08:00
18566a0592 Fix setting cmdline arguments 2022-12-13 08:50:08 -08:00
48c6372cf4 Fix --no-overwrite 2022-12-10 18:35:41 -08:00
f3917c6e4d Add comments to tests 2022-12-10 18:05:27 -08:00
9bb5225301 Restrict pillow version to <10 until PyQt6 is supported 2022-12-06 17:06:13 -08:00
e9cef87154 Move test cases to the testing package
Add comments to tests
2022-12-06 17:00:21 -08:00
da01dde2b9 Fix color space on CMYK images 2022-12-06 08:38:24 -08:00
53445759f7 Add tests 2022-12-06 00:22:51 -08:00
9aff3ae38e Generalize settings
Add comments and docstrings
Create parent directories when saving
Add merging to normalize_options
Change get_option to return if the value is the default value
2022-12-06 00:22:49 -08:00
0302511f5f Settings tests 2022-12-06 00:22:48 -08:00
028949f216 Make logs use the .log extension 2022-12-06 00:22:46 -08:00
af0d7b878b Set logging level on comictalker 2022-12-06 00:22:44 -08:00
460a5bc4f4 Cleanup 2022-12-06 00:20:29 -08:00
3f6f8540c4 Fix wait_and_retry_on_rate_limit 2022-12-06 00:20:27 -08:00
17d865b72f Refactor cli.py into a class 2022-12-06 00:20:26 -08:00
da21dc110d Update help 2022-12-06 00:20:24 -08:00
3870cd0f53 Update help for --config 2022-12-06 00:20:23 -08:00
ed1df400d8 Add replacement settings 2022-12-06 00:20:21 -08:00
82d737407f Simplify --only-set-cv-key 2022-12-06 00:20:20 -08:00
d0719e7201 Fix log dir 2022-12-06 00:20:18 -08:00
19112ac79b Update Settings 2022-12-06 00:20:01 -08:00
a64d753d77 Fix package selection 2022-12-01 19:54:55 -08:00
970752435c Merge branch 'mizaki-fixii_keys' into develop 2022-11-29 15:15:42 -08:00
b1436ee76e Merge branch 'resize-volume-columns' into develop 2022-11-29 14:28:32 -08:00
8eba44cce4 Increase default size of VolumeSelectionWindow 2022-11-29 14:28:08 -08:00
5fc5a14bd9 Wider catch of series and issue_number being empty 2022-11-29 16:59:05 +00:00
10f36e9868 Allow searching without a comic archive selected 2022-11-28 21:44:01 -08:00
aab7e37bb2 Use contentsRect().width() instead of width 2022-11-28 20:55:50 -08:00
2860093b6f Set the minimum row height to the default on VolumeSelectionWindow 2022-11-28 20:54:24 -08:00
ad7b270650 Automatically resize the row height on the VolumeSelectionWindow 2022-11-28 15:34:15 -08:00
70dcb9768a Better resize columns in the VolumeSelectionWindow 2022-11-28 15:28:47 -08:00
873d976662 keys may be None if there is no comic archive. IssueString.as_string will convert None to empty string so use None comparison before. 2022-11-28 00:56:19 +00:00
fc4eb4f002 Cleanup
Move most options passed in to ComicVineTalker to ComicTalker
Give ComicCacher and ComicTalker a version argument to remove all
  references to comictaggerlib
Update default arguments to reflect what is required to use these classes
2022-11-25 19:22:01 -08:00
129e19ac9d Remove cast from taggerwindow.py 2022-11-25 19:22:00 -08:00
0dede72692 Re-add --only-set-cv-key feature 2022-11-25 19:21:58 -08:00
83ac9f91b5 Make errors loading the ComicVineTalker object explicit 2022-11-25 19:21:57 -08:00
858bc303d8 Stop setting the notes field in map_comic_issue_to_metadata 2022-11-25 19:21:55 -08:00
005d7b72f4 Fix tests 2022-11-25 19:21:54 -08:00
91b863fcb1 Merge branch 'mizaki-infosources' into dev 2022-11-25 19:21:25 -08:00
e5f6a7d1d6 Add warning about settings 2022-11-25 17:09:22 -08:00
e7f937ecd2 Enable version checking 2022-11-25 17:08:26 -08:00
d75f39fe93 Remove logos dir 2022-11-24 23:58:24 +00:00
12d9befc25 Remove unneeded code from fetch_issue_data. 2022-11-24 23:56:12 +00:00
3e8ee864b7 Remove setting options and logo_url. 2022-11-24 23:35:35 +00:00
134c4a60e9 Add some docstrings. 2022-11-24 23:26:48 +00:00
3f9e5457f6 Fix make clean 2022-11-24 09:41:51 -08:00
cc2ef8593c Update pre-commit 2022-11-24 01:25:24 -08:00
c5a5fc8bdb Fix issue with combine_notes 2022-11-24 01:24:15 -08:00
1cbed64299 Fix an issue with normalizing the platform in filerenamer.py 2022-11-23 12:36:19 -08:00
c608ff80a1 Improve typing 2022-11-22 17:11:56 -08:00
52cc692b58 Remove some TODOs. 2022-11-23 00:22:48 +00:00
31894a66ec Remove repair_urls function, taken care of in format results functions. 2022-11-19 21:59:10 +00:00
aa11a47164 HTML table patch 2022-11-18 23:22:39 +00:00
093d20a52b Remove all the cool settings changes. 2022-11-18 23:18:41 +00:00
38c3014222 Use strip().splitlines() in cacher to prevent [''] return. Some clean up. 2022-11-17 15:55:38 +00:00
df87f81698 Remove volume only functions used for testing. 2022-11-13 23:25:08 +00:00
cf12e891b0 Fix CV API test. Fix sending last source details in settings for API test and website link. 2022-11-12 23:13:53 +00:00
76fb565d4e Merge branch 'mizaki-iiemptyurl' into develop 2022-11-11 17:09:45 -08:00
06ffd9f6be Add logo/text button to source tab that links to webpage. 2022-11-12 01:09:17 +00:00
dfef425af3 Better handle missing talkers and default to comic vine. 2022-11-10 17:03:39 +00:00
880b1be401 Return zero score if there is no image url. Fixes #392 2022-11-10 16:15:27 +00:00
04ad588a58 Use source name in tag notes. 2022-11-08 16:33:46 +00:00
6b4abcf061 Update current talker object with new settings. 2022-11-08 16:32:37 +00:00
629b28f258 Small fixes after merge. 2022-11-07 02:03:36 +00:00
c34902449f Merge branch 'develop' into infosources
# Conflicts:
#	comictaggerlib/cli.py
#	comictaggerlib/comicvinetalker.py
#	comictaggerlib/taggerwindow.py
#	tests/comicvinetalker_test.py
#	tests/conftest.py
2022-11-07 01:50:47 +00:00
63e6174cf2 Not all fields are required in ComicVolume and ComicIssue but cacher would fail if any optional field were missing. 2022-11-07 01:38:19 +00:00
9da14e0f95 Fix source switching. Use start year if cover date is missing. 2022-11-07 01:19:03 +00:00
c469fdb25e Make 7zip support optional 2022-11-06 08:27:45 -08:00
67be086638 Move map comic data to utils along with remove html. Alter tests. 2022-11-05 16:49:59 +00:00
a724fd8430 Compensate for a split empty string returning ['']. I don't see a way around this? 2022-11-05 01:21:51 +00:00
685ce014b6 Fix tests for comicvinetalker 2022-11-04 16:27:30 -07:00
62bf1d3808 Update macOS packaging 2022-11-04 16:16:19 -07:00
d55d75cd79 Append notes instead of overwriting them
Add issue_id to GenericMetadata
2022-11-04 15:39:40 -07:00
19e5f10a7b Revert "Revert passing only issue id to fetch_comic_data. Instead send issue id, volume id and issue number. This is because MU will not have the issue number from the API call. Now, if it has been parsed from the file name it will be available for use by the MU talker."
This reverts commit e5e9617052.
2022-11-04 16:16:07 +00:00
e5e9617052 Revert passing only issue id to fetch_comic_data. Instead send issue id, volume id and issue number. This is because MU will not have the issue number from the API call. Now, if it has been parsed from the file name it will be available for use by the MU talker. 2022-11-04 00:52:22 +00:00
b4f6820f56 remove_fetch_alternate_cover_urls.patch 2022-11-03 23:32:35 +00:00
b07aa03c5f Use xlate for all int conversion in CV talker and compare cache issues to expected number. 2022-11-03 22:35:46 +00:00
2f54b1b36b A few minor logging tweaks. 2022-11-03 15:39:13 +00:00
70293a0819 Require PyInstaller >= 5.6.2 2022-11-01 13:51:10 -07:00
8592fdee74 Revert "Install PyInstaller from git until >5.6.1 is available"
This reverts commit 79137a12f8.
2022-11-01 13:49:52 -07:00
075faaea5a Removed TODO's checked and/or fixed. 2022-11-01 16:13:46 +00:00
870dc5e9b6 Move issue_id to first position of fetch_comic_data as most used. 2022-10-30 17:52:55 +00:00
86402af8b1 Merge branch 'develop' into infosources
# Conflicts:
#	comictaggerlib/comicvinetalker.py
2022-10-30 11:39:01 +00:00
d7976cf9d2 Hack tests. 2022-10-30 11:16:03 +00:00
b67765d9aa Merge to develop. 2022-10-30 11:07:53 +00:00
8cac2c255f Merge branch 'develop' into infosources
# Conflicts:
#	comictaggerlib/comicvinetalker.py
#	comictaggerlib/coverimagewidget.py
#	comictaggerlib/main.py
#	comictaggerlib/pagebrowser.py
#	comictaggerlib/pagelisteditor.py
#	comictaggerlib/settings.py
#	comictaggerlib/settingswindow.py
2022-10-30 01:31:58 +01:00
4f42fef4fc Return issue id from series search and use issue id for API. 2022-10-30 00:15:05 +01:00
26851475ea Clean up loading cover images. Probably more to do. 2022-10-29 16:41:34 +01:00
a06d88efc0 Fix up full issue cache types. 2022-10-29 01:33:42 +01:00
dcf853515c Tidy CV logger errors. 2022-10-28 22:32:33 +01:00
bf06b94284 Enable cache for full issue information. 2022-10-28 22:15:14 +01:00
561dc28044 Don't proxy talker (really this time). Remove talker custom logging. Move static_options and settings_options to root of class object. Temp hack to keep talker menu genration working until settings revamp. 2022-10-27 23:36:57 +01:00
4514ae80d0 Switch to API data for alt images, remove unneeded functions and removed async as new approach needed. See comments about fetch_partial_volume_data 2022-10-26 00:29:30 +01:00
cab69a32be Remove proxying from ComicTalker. Add some checks for talkers. 2022-10-25 00:37:18 +01:00
c5ad75370f Work around having to scrape alt covers from CV. Use cache to get issue page url for scrape. 2022-10-24 16:30:58 +01:00
d23258f359 Change ComicVolume, ComicIssue to image_url and image_thumb_url. Add/change search/volume DB layout to remove duplication of data. Fixup some test. 2022-10-23 22:40:15 +01:00
c9cd58fecb Remove fetch_issue_cover_urls and async_fetch_issue_cover_urls. Reduce API calls by using data already available with coverimagewidget. 2022-10-22 01:43:56 +01:00
fb1616aaa1 Remove CV parse date. Strings names. 2022-10-20 00:32:40 +01:00
4be12d857d Reuse CV test data in comic_issue_result data. Cover possible empty volume data in get_volume_issues_info. 2022-10-19 23:30:11 +01:00
e1ab72ec2a Rename super_url to image_url in comiccacher. Merge fetch_issue_data_by_issue_id into fetch_comic_data. Fill comic volume info in comiccacher:get_volume_issues_info 2022-10-19 19:33:51 +01:00
8a8dea8aa4 Fix autotagstartwindow.ui missed from merge. 2022-10-15 23:36:52 +01:00
43464724bd Convert all start_year to int. 2022-10-15 23:20:50 +01:00
34163fe9d7 Update the comicvine_api fixture in conftest.py to actually return the comicvinetalker. 2022-10-15 02:02:10 +01:00
9aa29f1445 Merge fetch_issue_data and fetch_volume_data to fetch_comic_data. 2022-10-14 01:10:46 +01:00
3ea44b7ca7 Remove fetch_issue_page_url from comictalker etc. 2022-10-12 23:08:47 +01:00
c1c8f4eb6e black 2022-10-12 00:11:57 +01:00
a14c24a78a Fix for issueidentifier_test 2022-10-11 16:52:41 +01:00
18d861a2be More test fixes that may need to be looked at further. 2022-10-09 23:43:52 +01:00
ac15a4dd72 More test fixes. 2022-10-06 01:14:03 +01:00
6a98afb89c After second merge. 2022-10-06 00:34:32 +01:00
21873d3830 Merge branch 'develop' into infosources
# Conflicts:
#	comictaggerlib/autotagstartwindow.py
#	comictaggerlib/cli.py
#	comictalker/talkers/comicvine.py
2022-10-05 01:58:46 +01:00
d37e4607ee After merge. Testing files still to update. 2022-10-04 23:50:55 +01:00
00e95178cd Initial support for multiple comic information sources 2022-10-04 01:08:14 +01:00
198 changed files with 43646 additions and 10099 deletions

View File

@ -1,6 +0,0 @@
[flake8]
max-line-length = 120
extend-ignore = E203, E501, A003
extend-exclude = venv, scripts, build, dist, comictaggerlib/ctversion.py
per-file-ignores =
comictaggerlib/cli.py: T20

View File

@ -1,8 +1,7 @@
name: CI
env:
PIP: pip
PYTHON: python
LC_COLLATE: en_US.UTF-8
on:
pull_request:
push:
@ -23,24 +22,18 @@ jobs:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: syphar/restore-virtualenv@v1.2
id: cache-virtualenv
- uses: syphar/restore-pip-download-cache@v1
if: steps.cache-virtualenv.outputs.cache-hit != 'true'
- name: Install build dependencies
run: |
python -m pip install --upgrade --upgrade-strategy eager -r requirements_dev.txt
python -m pip install flake8
- uses: reviewdog/action-setup@v1
with:
@ -54,65 +47,50 @@ jobs:
strategy:
matrix:
python-version: [3.9]
os: [ubuntu-latest, macos-10.15, windows-latest]
os: [ubuntu-22.04, macos-13, windows-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: syphar/restore-virtualenv@v1.2
id: cache-virtualenv
- uses: syphar/restore-pip-download-cache@v1
if: steps.cache-virtualenv.outputs.cache-hit != 'true'
- name: Install build dependencies
- name: Install tox
run: |
pip install --force-reinstall git+https://github.com/pyinstaller/pyinstaller
python -m pip install --upgrade --upgrade-strategy eager -r requirements_dev.txt
python -m pip install --upgrade --upgrade-strategy eager tox
- name: Install Windows build dependencies
run: |
choco install -y zip
if: runner.os == 'Windows'
- name: Install macos dependencies
run: |
brew install icu4c pkg-config
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig";
export PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin:$PATH"
python -m pip install --no-binary=pyicu pyicu
brew upgrade icu4c pkg-config || brew install icu4c pkg-config
if: runner.os == 'macOS'
- name: Install linux dependencies
run: |
sudo apt-get install pkg-config libicu-dev
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig";
export PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin:$PATH"
python -m pip install --no-binary=pyicu pyicu
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install pkg-config libicu-dev libqt5gui5 libfuse2 desktop-file-utils
if: runner.os == 'Linux'
- name: Build and install PyPi packages
run: |
make clean pydist
python -m pip install "dist/$(python setup.py --fullname)-py3-none-any.whl[all]"
- name: build
run: |
make dist
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig:/opt/homebrew/opt/icu4c/lib/pkgconfig${PKG_CONFIG_PATH+:$PKG_CONFIG_PATH}";
export PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin${PATH+:$PATH}"
python -m tox r -m build
shell: bash
- name: Archive production artifacts
uses: actions/upload-artifact@v2
if: runner.os != 'Linux' # linux binary currently has a segfault when running on latest fedora
uses: actions/upload-artifact@v4
with:
name: "${{ format('ComicTagger-{0}', runner.os) }}"
path: |
dist/*.zip
dist/*.whl
dist/binary/*.zip
dist/binary/*.tar.gz
dist/binary/*.dmg
dist/binary/*.AppImage
- name: PyTest
run: |
python -m pytest
python -m tox r

43
.github/workflows/contributions.yaml vendored Normal file
View File

@ -0,0 +1,43 @@
name: Contributions
on:
push:
branches:
- 'develop'
tags-ignore:
- '**'
jobs:
contrib-readme-job:
permissions:
contents: write
runs-on: ubuntu-latest
env:
CI_COMMIT_AUTHOR: github-actions[bot]
CI_COMMIT_EMAIL: <41898282+github-actions[bot]@users.noreply.github.com>
CI_COMMIT_MESSAGE: Update AUTHORS
name: A job to automate contrib in readme
steps:
- name: Contribute List
uses: akhilmhdh/contributors-readme-action@v2.3.6
with:
use_username: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Update AUTHORS
run: |
git config --global log.mailmap true
git log --reverse '--format=%aN <%aE>' | cat -n | sort -uk2 | sort -n | cut -f2- >AUTHORS
- name: Commit and push AUTHORS
run: |
if ! git diff --exit-code; then
git pull
git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}"
git config --global user.email "${{ env.CI_COMMIT_EMAIL }}"
git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}"
git push
fi

View File

@ -1,8 +1,7 @@
name: Package
env:
PIP: pip
PYTHON: python
LC_COLLATE: en_US.UTF-8
on:
push:
tags:
@ -10,70 +9,46 @@ on:
jobs:
package:
permissions:
# IMPORTANT: this permission is mandatory for trusted publishing
id-token: write
contents: write
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [3.9]
os: [ubuntu-latest, macos-10.15, windows-latest]
os: [ubuntu-22.04, macos-13, windows-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: syphar/restore-virtualenv@v1.2
id: cache-virtualenv
- uses: syphar/restore-pip-download-cache@v1
if: steps.cache-virtualenv.outputs.cache-hit != 'true'
- name: Install build dependencies
- name: Install tox
run: |
pip install --force-reinstall git+https://github.com/pyinstaller/pyinstaller
python -m pip install --upgrade --upgrade-strategy eager -r requirements_dev.txt
python -m pip install --upgrade --upgrade-strategy eager tox
- name: Install Windows build dependencies
run: |
choco install -y zip
if: runner.os == 'Windows'
- name: Install macos dependencies
run: |
brew install icu4c pkg-config
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig";
export PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin:$PATH"
python -m pip install --no-binary=pyicu pyicu
brew upgrade icu4c pkg-config || brew install icu4c pkg-config
if: runner.os == 'macOS'
- name: Install linux dependencies
run: |
sudo apt-get install pkg-config libicu-dev
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig";
export PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin:$PATH"
python -m pip install --no-binary=pyicu pyicu
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install pkg-config libicu-dev libqt5gui5 libfuse2 desktop-file-utils
if: runner.os == 'Linux'
- name: Build, Install and Test PyPi packages
run: |
make clean pydist
python -m pip install "dist/$(python setup.py --fullname)-py3-none-any.whl[all]"
python -m flake8
python -m pytest
- name: "Publish distribution 📦 to PyPI"
if: startsWith(github.ref, 'refs/tags/') && runner.os == 'Linux'
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
packages_dir: dist
- name: Build PyInstaller package
run: |
make dist
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig:/opt/homebrew/opt/icu4c/lib/pkgconfig${PKG_CONFIG_PATH+:$PKG_CONFIG_PATH}";
export PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin${PATH+:$PATH}"
python -m tox r
python -m tox r -m release
shell: bash
- name: Get release name
if: startsWith(github.ref, 'refs/tags/')
@ -83,12 +58,16 @@ jobs:
echo "release_name=$(git tag -l --format "%(refname:strip=2): %(contents:lines=1)" ${{ github.ref_name }})" >> $GITHUB_ENV
- name: Release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
name: "${{ env.release_name }}"
prerelease: "${{ contains(github.ref, '-') }}" # alpha-releases should be 1.3.0-alpha.x full releases should be 1.3.0
draft: false
# upload the single application zip file for each OS and include the wheel built on linux
files: |
dist/!(*Linux).zip
dist/binary/*.zip
dist/binary/*.tar.gz
dist/binary/*.dmg
dist/binary/*.AppImage
dist/*${{ fromJSON('["never", ""]')[runner.os == 'Linux'] }}.whl

3
.gitignore vendored
View File

@ -155,3 +155,6 @@ dmypy.json
# Cython debug symbols
cython_debug/
# for testing
temp/

9
.mailmap Normal file
View File

@ -0,0 +1,9 @@
Andrew W. Buchanan <buchanan@difference.com>
Davide Romanini <d.romanini@cineca.it> <davide.romanini@gmail.com>
Davide Romanini <d.romanini@cineca.it> <user159033@92-63-141-211.rdns.melbourne.co.uk>
Michael Fitzurka <MichaelFitzurka@users.noreply.github.com> <MichaelFitzurka@github.com>
Timmy Welch <timmy@narnian.us>
beville <beville@users.noreply.github.com> <(no author)@6c5673fe-1810-88d6-992b-cd32ca31540c>
beville <beville@users.noreply.github.com> <beville@6c5673fe-1810-88d6-992b-cd32ca31540c>
beville <beville@users.noreply.github.com> <beville@gmail.com@6c5673fe-1810-88d6-992b-cd32ca31540c>
beville <beville@users.noreply.github.com> <beville@users.noreply.github.com>

View File

@ -1,7 +1,7 @@
exclude: ^scripts
exclude: ^(scripts|comictaggerlib/graphics/resources.py)
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
@ -10,37 +10,37 @@ repos:
- id: name-tests-test
- id: requirements-txt-fixer
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.2.0
rev: v2.7.0
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
args: [--af,--add-import, 'from __future__ import annotations']
- repo: https://github.com/asottile/pyupgrade
rev: v3.1.0
rev: v3.18.0
hooks:
- id: pyupgrade
args: [--py39-plus]
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/PyCQA/autoflake
rev: v1.7.7
rev: v2.3.1
hooks:
- id: autoflake
args: [-i]
args: [-i, --remove-all-unused-imports, --ignore-init-module-imports]
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
args: [--af,--add-import, 'from __future__ import annotations']
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
rev: 7.1.1
hooks:
- id: flake8
additional_dependencies: [flake8-encodings, flake8-warnings, flake8-builtins, flake8-length, flake8-print]
additional_dependencies: [flake8-encodings, flake8-builtins, flake8-print, flake8-no-nested-comprehensions]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
rev: v1.11.2
hooks:
- id: mypy
additional_dependencies: [types-setuptools, types-requests]
additional_dependencies: [types-setuptools, types-requests, settngs>=0.10.4]
ci:
skip: [mypy]

View File

@ -1,59 +0,0 @@
language: python
# Only build tags
if: type = pull_request OR tag IS present
branches:
only:
- develop
- /^\d+\.\d+\.\d+.*$/
env:
global:
- PYTHON=python3
- PIP=pip3
- SETUPTOOLS_SCM_PRETEND_VERSION=$TRAVIS_TAG
- MAKE=make
matrix:
include:
- os: linux
python: 3.8
- name: "Python: 3.7"
os: osx
language: shell
python: 3.7
env: PYTHON=python3 PIP="python3 -m pip"
cache:
- directories:
- $HOME/Library/Caches/pip
- os: windows
language: bash
env: PATH=/C/Python37:/C/Python37/Scripts:$PATH MAKE=mingw32-make PIP=pip PYTHON=python
before_install:
- if [ "$TRAVIS_OS_NAME" = "windows" ]; then choco install -y python --version 3.7.9; choco install -y mingw zip; fi
install:
- $PIP install -r requirements_dev.txt
- $PIP install -r requirements-GUI.txt
- $PIP install -r requirements-CBR.txt
script:
- if [ "$TRAVIS_OS_NAME" != "linux" ]; then $MAKE dist ; fi
deploy:
- name: "$TRAVIS_TAG"
body: Released ComicTagger $TRAVIS_TAG
provider: releases
skip_cleanup: true
api_key:
secure: RgohcOJOfLhXXT12bMWaLwOqhe+ClSCYXjYuUJuWK4/E1fdd1xu1ebdQU+MI/R8cZ0Efz3sr2n3NkO/Aa8gN68xEfuF7RVRMm64P9oPrfZgGdsD6H43rU/6kN8bgaDRmCYpLTfXaJ+/gq0x1QDkhWJuceF2BYEGGvL0BvS/TUsLyjVxs8ujTplLyguXHNEv4/7Yz7SBNZZmUHjBuq/y+l8ds3ra9rSgAVAN1tMXoFKJPv+SNNkpTo5WUNMPzBnN041F1rzqHwYDLog2V7Krp9JkXzheRFdAr51/tJBYzEd8AtYVdYvaIvoO6A4PiTZ7MpsmcZZPAWqLQU00UTm/PhT/LVR+7+f8lOBG07RgNNHB+edjDRz3TAuqyuZl9wURWTZKTPuO49TkZMz7Wm0DRNZHvBm1IXLeSG7Tll2YL1+WpZNZg+Dhro2J1QD3vxDXafhMdTCB4z0q5aKpG93IT0p6oXOO0oEGOPZYbA2c5R3SXWSyqd1E1gdhbVjIZr59h++TEf1zz07tvWHqPuAF/Ly/j+dIcY2wj0EzRWaSASWgUpTnMljAkHtWhqDw4GXGDRkRUWRJl1d0/JyVqCeIdRzDQNl8/q7BcO3F1zqr1PgnYdz0lfwWxL1/ekw2vHOJE/GOdkyvX0aJrnaOV338mjJbfGHYv4ESc9ow1kdtIbiU=
file_glob: true
file: dist/*.zip
draft: true
on:
tags: true
condition: $TRAVIS_OS_NAME != "linux"
- provider: pypi
user: __token__
password:
secure: h+y5WkE8igf864dnsbGPFvOBkyPkuBYtnDRt+EgxHd71EZnV2YP7ns2Cx12su/SVVDdZCBlmHVtkhl6Jmqy+0rTkSYx+3mlBOqyl8Cj5+BlP/dP7Bdmhs2uLZk2YYL1avbC0A6eoNJFtCkjurnB/jCGE433rvMECWJ5x2HsQTKchCmDAEdAZbRBJrzLFsrIC+6NXW1IJZjd+OojbhLSyVar2Jr32foh6huTcBu/x278V1+zIC/Rwy3W67+3c4aZxYrI47FoYFza0jjFfr3EoSkKYUSByMTIvhWaqB2gIsF0T160jgDd8Lcgej+86ACEuG0v01VE7xoougqlOaJ94eAmapeM7oQXzekSwSAxcK3JQSfgWk/AvPhp07T4pQ8vCZmky6yqvVp1EzfKarTeub1rOnv+qo1znKLrBtOoq6t8pOAeczDdIDs51XT/hxaijpMRCM8vHxN4Kqnc4DY+3KcF7UFyH1ifQJHQe71tLBsM/GnAcJM5/3ykFVGvRJ716p4aa6IoGsdNk6bqlysNh7nURDl+bfm+CDXRkO2jkFwUFNqPHW7JwY6ZFx+b5SM3TzC3obJhfMS7OC37fo2geISOTR0xVie6NvpN6TjNAxFTfDxWJI7yH3Al2w43B3uYDd97WeiN+B+HVWtdaER87IVSRbRqFrRub+V+xrozT0y0=
skip_existing: true
skip_cleanup: true
on:
tags: true
condition: $TRAVIS_OS_NAME = "linux"

18
AUTHORS Normal file
View File

@ -0,0 +1,18 @@
beville <beville@users.noreply.github.com>
Davide Romanini <d.romanini@cineca.it>
fcanc <f.canc@icloud.com>
Alban Seurat <alkpone@alkpone.com>
tlc <tlc@users.noreply.github.com>
Marek Pawlak <francuz14@gmail.com>
Timmy Welch <timmy@narnian.us>
J.P. Cranford <philipcranford4@gmail.com>
thFrgttn <39759781+thFrgttn@users.noreply.github.com>
Andrew W. Buchanan <buchanan@difference.com>
Michael Fitzurka <MichaelFitzurka@users.noreply.github.com>
Richard Haussmann <richard.haussmann@gmail.com>
Mizaki <jinxybob@hotmail.com>
Xavier Jouvenot <x.jouvenot@gmail.com>
github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Ben Longman <deck@steamdeck.lan>
Sven Hesse <drmccoy@drmccoy.de>
pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

View File

@ -41,7 +41,7 @@ Please open a [GitHub Pull Request](https://github.com/comictagger/comictagger/p
Currently only python 3.9 is supported however 3.10 will probably work if you try it
Those on linux should install `Pillow` from the system package manager if possible and if the GUI and/or the CBR/RAR comicbooks are going to be used `pyqt5` and `unrar-cffi` should be installed from the system package manager
Those on linux should install `Pillow` from the system package manager if possible and if the GUI `pyqt5` should be installed from the system package manager
Those on macOS will need to ensure that you are using python3 in x86 mode either by installing an x86 only version of python or using the universal installer and using `python3-intel64` instead of `python3`
@ -50,10 +50,10 @@ Those on macOS will need to ensure that you are using python3 in x86 mode either
git clone https://github.com/comictagger/comictagger.git
```
2. It is preferred to use a virtual env for running from source, adding the `--system-site-packages` allows packages already installed via the system package manager to be used:
2. It is preferred to use a virtual env for running from source:
```
python3 -m venv --system-site-packages venv
python3 -m venv venv
```
3. Activate the virtual env:
@ -65,73 +65,34 @@ or if on windows PowerShell
. venv/bin/activate.ps1
```
4. install dependencies:
4. Install tox:
```bash
pip install -r requirements_dev.txt -r requirements.txt
# if installing optional dependencies
pip install -r requirements-GUI.txt -r requirements-CBR.txt
pip install tox
```
5. install ComicTagger
5. If you are on an M1 Mac you will need to export two environment variables for tests to pass.
```
pip install .
export tox_python=python3.9-intel64
export tox_env=m1env
```
6. (optionally) run pytest to ensure that their are no failures (xfailed means expected failure)
6. install ComicTagger
```
$ pytest
============================= test session starts ==============================
platform darwin -- Python 3.9.12, pytest-7.1.1, pluggy-1.0.0
rootdir: /Users/timmy/build/source/comictagger
collected 61 items
tests/test_FilenameParser.py ..x......x.xxx.xx....xxxxxx.xx.x..xxxxxxx [ 67%]
tests/test_comicarchive.py x... [ 73%]
tests/test_rename.py ..xxx.xx..XXX.XX [100%]
================== 27 passed, 29 xfailed, 5 xpassed in 2.68s ===================
tox run -e venv
```
7. Make your changes
8. run code tools and correct any issues
8. Build to ensure that your changes work: this will produce a binary build in the dist folder
```bash
black .
isort .
flake8 .
pytest
tox run -m build
```
black: formats all of the code consistently so there are no surprises<br>
The build runs these formatters and linters automatically
setup-cfg-fmt: Formats the setup.cfg file
autoflake: Removes unused imports
isort: sorts imports so that you can always find where an import is located<br>
black: formats all of the code consistently so there are no surprises<br>
flake8: checks for code quality and style (warns for unused imports and similar issues)<br>
mypy: checks the types of variables and functions to catch errors
pytest: runs tests for ComicTagger functionality
if on mac or linux most of this can be accomplished by running
```
make install
# or make PYTHON=python3-intel64 install
. venv/bin/activate
make CI
```
There is also `make check` which will run all of the code tools in a read-only capacity
```
$ make check
venv/bin/black --check .
All done! ✨ 🍰 ✨
52 files would be left unchanged.
venv/bin/isort --check .
Skipped 6 files
venv/bin/flake8 .
venv/bin/pytest
============================= test session starts ==============================
platform darwin -- Python 3.9.12, pytest-7.1.1, pluggy-1.0.0
rootdir: /Users/timmy/build/source/comictagger
collected 61 items
tests/test_FilenameParser.py ..x......x.xxx.xx....xxxxxx.xx.x..xxxxxxx [ 67%]
tests/test_comicarchive.py x... [ 73%]
tests/test_rename.py ..xxx.xx..XXX.XX [100%]
================== 27 passed, 29 xfailed, 5 xpassed in 2.68s ===================
```

202
LICENSE Normal file
View File

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -1,7 +0,0 @@
include README.md
include release_notes.txt
include requirements.txt
recursive-include scripts *.py *.txt
recursive-include desktop-integration *
include windows/app.ico
include mac/app.icns

View File

@ -1,64 +0,0 @@
PIP ?= pip3
PYTHON ?= python3
VERSION_STR := $(shell $(PYTHON) setup.py --version)
SITE_PACKAGES := $(shell $(PYTHON) -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
PACKAGE_PATH = $(SITE_PACKAGES)/comictagger.egg-link
VENV := $(shell echo $${VIRTUAL_ENV-venv})
PY3 := $(shell command -v $(PYTHON) 2> /dev/null)
PYTHON_VENV := $(VENV)/bin/python
INSTALL_STAMP := $(VENV)/.install.stamp
ifeq ($(OS),Windows_NT)
PYTHON_VENV := $(VENV)/Scripts/python.exe
OS_VERSION=win-$(PROCESSOR_ARCHITECTURE)
APP_NAME=comictagger.exe
FINAL_NAME=ComicTagger-$(VERSION_STR)-$(OS_VERSION).exe
else ifeq ($(shell uname -s),Darwin)
OS_VERSION=osx-$(shell defaults read loginwindow SystemVersionStampAsString)-$(shell uname -m)
APP_NAME=ComicTagger.app
FINAL_NAME=ComicTagger-$(VERSION_STR)-$(OS_VERSION).app
else
APP_NAME=comictagger
FINAL_NAME=ComicTagger-$(VERSION_STR)-$(shell uname -s)
endif
.PHONY: all clean pydist dist CI check
all: clean dist
$(PYTHON_VENV):
@if [ -z $(PY3) ]; then echo "Python 3 could not be found."; exit 2; fi
$(PY3) -m venv $(VENV)
clean:
find . -maxdepth 4 -type d -name "__pycache__"
rm -rf $(PACKAGE_PATH) $(INSTALL_STAMP) build dist MANIFEST comictaggerlib/ctversion.py
$(MAKE) -C mac clean
CI: install
$(PYTHON_VENV) -m black .
$(PYTHON_VENV) -m isort .
$(PYTHON_VENV) -m flake8 .
$(PYTHON_VENV) -m pytest
check: install
$(PYTHON_VENV) -m black --check .
$(PYTHON_VENV) -m isort --check .
$(PYTHON_VENV) -m flake8 .
$(PYTHON_VENV) -m pytest
pydist:
$(PYTHON_VENV) -m build
install: $(INSTALL_STAMP)
$(INSTALL_STAMP): $(PYTHON_VENV) requirements.txt requirements_dev.txt
$(PYTHON_VENV) -m pip install -r requirements_dev.txt
$(PYTHON_VENV) -m pip install -e .
touch $(INSTALL_STAMP)
dist:
pyinstaller -y comictagger.spec
cd dist && zip -m -r $(FINAL_NAME).zip $(APP_NAME)

130
README.md
View File

@ -35,7 +35,7 @@ For details, screen-shots, and more, visit [the Wiki](https://github.com/comicta
### Binaries
Windows and macOS binaries are provided in the [Releases Page](https://github.com/comictagger/comictagger/releases).
Windows, Linux and MacOS binaries are provided in the [Releases Page](https://github.com/comictagger/comictagger/releases).
Just unzip the archive in any folder and run, no additional installation steps are required.
@ -47,7 +47,14 @@ A pip package is provided, you can install it with:
$ pip3 install comictagger[GUI]
```
There are two optional dependencies GUI and CBR. You can install the optional dependencies by specifying one or more of `GUI`,`CBR` or `all` in braces e.g. `comictagger[CBR,GUI]`
There are optional dependencies. You can install the optional dependencies by specifying one or more of them in braces e.g. `comictagger[CBR,GUI]`
Optional dependencies:
1. `ICU`: Ensures that comic pages are supported correctly. This should always be installed. *Currently only exists in the latest alpha release *
1. `CBR`: Provides support for CBR/RAR files.
1. `GUI`: Installs the GUI.
1. `7Z`: Provides support for CB7/7Z files.
1. `all`: Installs all of the above optional dependencies.
### Chocolatey installation (Windows only)
@ -59,5 +66,120 @@ choco install comictagger
1. Ensure you have python 3.9 installed
2. Clone this repository `git clone https://github.com/comictagger/comictagger.git`
3. `pip3 install -r requirements_dev.txt`
7. `pip3 install .` or `pip3 install .[GUI]`
7. `pip3 install .[ICU]` or `pip3 install .[GUI,ICU]`
## Contributors
<!-- readme: beville,davide-romanini,collaborators,contributors -start -->
<table>
<tr>
<td align="center">
<a href="https://github.com/beville">
<img src="https://avatars.githubusercontent.com/u/7294848?v=4" width="100;" alt="beville"/>
<br />
<sub><b>beville</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/davide-romanini">
<img src="https://avatars.githubusercontent.com/u/731199?v=4" width="100;" alt="davide-romanini"/>
<br />
<sub><b>davide-romanini</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/fcanc">
<img src="https://avatars.githubusercontent.com/u/4999486?v=4" width="100;" alt="fcanc"/>
<br />
<sub><b>fcanc</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/lordwelch">
<img src="https://avatars.githubusercontent.com/u/7547075?v=4" width="100;" alt="lordwelch"/>
<br />
<sub><b>lordwelch</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/mizaki">
<img src="https://avatars.githubusercontent.com/u/1141189?v=4" width="100;" alt="mizaki"/>
<br />
<sub><b>mizaki</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MichaelFitzurka">
<img src="https://avatars.githubusercontent.com/u/27830765?v=4" width="100;" alt="MichaelFitzurka"/>
<br />
<sub><b>MichaelFitzurka</b></sub>
</a>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/abuchanan920">
<img src="https://avatars.githubusercontent.com/u/368793?v=4" width="100;" alt="abuchanan920"/>
<br />
<sub><b>abuchanan920</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/AlbanSeurat">
<img src="https://avatars.githubusercontent.com/u/500180?v=4" width="100;" alt="AlbanSeurat"/>
<br />
<sub><b>AlbanSeurat</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/rhaussmann">
<img src="https://avatars.githubusercontent.com/u/7084007?v=4" width="100;" alt="rhaussmann"/>
<br />
<sub><b>rhaussmann</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/jpcranford">
<img src="https://avatars.githubusercontent.com/u/21347202?v=4" width="100;" alt="jpcranford"/>
<br />
<sub><b>jpcranford</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/PawlakMarek">
<img src="https://avatars.githubusercontent.com/u/26022173?v=4" width="100;" alt="PawlakMarek"/>
<br />
<sub><b>PawlakMarek</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/DrMcCoy">
<img src="https://avatars.githubusercontent.com/u/156130?v=4" width="100;" alt="DrMcCoy"/>
<br />
<sub><b>DrMcCoy</b></sub>
</a>
</td></tr>
<tr>
<td align="center">
<a href="https://github.com/Xav83">
<img src="https://avatars.githubusercontent.com/u/6787157?v=4" width="100;" alt="Xav83"/>
<br />
<sub><b>Xav83</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/thFrgttn">
<img src="https://avatars.githubusercontent.com/u/39759781?v=4" width="100;" alt="thFrgttn"/>
<br />
<sub><b>thFrgttn</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/tlc">
<img src="https://avatars.githubusercontent.com/u/19436?v=4" width="100;" alt="tlc"/>
<br />
<sub><b>tlc</b></sub>
</a>
</td></tr>
</table>
<!-- readme: beville,davide-romanini,collaborators,contributors -end -->

View File

@ -3,7 +3,7 @@ Encoding=UTF-8
Name=ComicTagger
GenericName=Comic Metadata Editor
Comment=A cross-platform GUI/CLI app for writing metadata to comic archives
Exec=%%CTSCRIPT%% %F
Exec=comictagger %F
Icon=/usr/local/share/comictagger/app.png
Terminal=false
Type=Application

View File

@ -9,7 +9,7 @@ block_cipher = None
a = Analysis(
["comictagger.py"],
["../comictaggerlib/__main__.py"],
pathex=[],
binaries=[],
datas=[],
@ -93,15 +93,6 @@ if platform.system() not in ["Windows"]:
"CFBundleShortVersionString": ctversion.version,
"CFBundleVersion": ctversion.version,
"CFBundleDocumentTypes": [
{
"CFBundleTypeRole": "Viewer",
"LSItemContentTypes": [
"com.rarlab.rar-archive",
],
"CFBundleTypeName": "RAR Archive",
"CFBundleTypeRole": "Editor",
"LSHandlerRank": "Default",
},
{
"CFBundleTypeRole": "Editor",
"LSHandlerRank": "Default",
@ -118,7 +109,6 @@ if platform.system() not in ["Windows"]:
"NSPersistentStoreTypeKey": "Binary",
"CFBundleTypeIconSystemGenerated": True,
"CFBundleTypeName": "ZIP Comic Archive",
# 'CFBundleTypeIconFile': 'cbz',
"LSItemContentTypes": [
"public.zip-comic-archive",
"com.simplecomic.cbz-archive",
@ -129,6 +119,7 @@ if platform.system() not in ["Windows"]:
"com.milke.cbz-archive",
"com.bitcartel.comicbooklover.cbz",
"public.archive.cbz",
"public.zip-archive",
],
"CFBundleTypeRole": "Editor",
"LSHandlerRank": "Default",
@ -141,8 +132,8 @@ if platform.system() not in ["Windows"]:
"NSPersistentStoreTypeKey": "Binary",
"CFBundleTypeIconSystemGenerated": True,
"CFBundleTypeName": "7-Zip Comic Archive",
# 'CFBundleTypeIconFile': 'cb7',
"LSItemContentTypes": [
"org.7-zip.7-zip-archive",
"com.simplecomic.cb7-archive",
"public.cb7-archive",
"com.macitbetter.cb7-archive",
@ -160,8 +151,8 @@ if platform.system() not in ["Windows"]:
"NSPersistentStoreTypeKey": "Binary",
"CFBundleTypeIconSystemGenerated": True,
"CFBundleTypeName": "RAR Comic Archive",
# 'CFBundleTypeIconFile': 'cbr',
"LSItemContentTypes": [
"com.rarlab.rar-archive",
"com.rarlab.rar-comic-archive",
"com.simplecomic.cbr-archive",
"com.macitbetter.cbr-archive",
@ -195,16 +186,11 @@ if platform.system() not in ["Windows"]:
},
},
{
# 'UTTypeIcons': {
# 'UTTypeIconText': 'cbr',
# 'UTTypeIconBackgroundName': comic-fill
# }
"UTTypeConformsTo": [
"public.data",
"public.archive",
"com.rarlab.rar-archive",
],
# 'UTTypeIconFile': 'cbr',
"UTTypeIdentifier": "com.rarlab.rar-comic-archive",
"UTTypeDescription": "RAR Comic Archive",
"UTTypeTagSpecification": {
@ -218,16 +204,11 @@ if platform.system() not in ["Windows"]:
},
},
{
# 'UTTypeIcons': {
# 'UTTypeIconText': 'cbz',
# 'UTTypeIconBackgroundName': 'comic-fill',
# }
"UTTypeConformsTo": [
"public.data",
"public.archive",
"public.zip-archive",
],
# 'UTTypeIconFile': cbz,
"UTTypeIdentifier": "public.zip-comic-archive",
"UTTypeDescription": "ZIP Comic Archive",
"UTTypeTagSpecification": {
@ -237,16 +218,11 @@ if platform.system() not in ["Windows"]:
},
},
{
# 'UTTypeIcons': {
# 'UTTypeIconText': 'cb7',
# 'UTTypeIconBackgroundName': comic-fill
# }
"UTTypeConformsTo": [
"public.data",
"public.archive",
"org.7-zip.7-zip-archive",
],
# 'UTTypeIconFile': cb7
"UTTypeIdentifier": "org.7-zip.7-zip-comic-archive",
"UTTypeDescription": "7-Zip Comic Archive",
"UTTypeTagSpecification": {
@ -261,5 +237,5 @@ if platform.system() not in ["Windows"]:
},
],
},
bundle_identifier=None,
bundle_identifier="com.comictagger",
)

19
build-tools/dmgbuild.conf Normal file
View File

@ -0,0 +1,19 @@
import pathlib
app = "ComicTagger"
app_name = f"{app}.app"
path = f"dist/{app_name}"
# dmgbuild settings
format = 'ULMO'
files = (str(path),)
symlinks = {'Applications': '/Applications'}
icon = pathlib.Path().cwd() / 'build-tools' / 'mac' / 'volume.icns'
icon_locations = {
app_name: (100, 100),
'Applications': (300, 100)
}

View File

@ -0,0 +1,24 @@
from __future__ import annotations
import pathlib
import settngs
import comictaggerlib.main
def generate() -> str:
app = comictaggerlib.main.App()
app.load_plugins(app.initial_arg_parser.parse_known_args()[0])
app.register_settings(True)
imports, types = settngs.generate_dict(app.manager.definitions)
imports2, types2 = settngs.generate_ns(app.manager.definitions)
i = imports.splitlines()
i.extend(set(imports2.splitlines()) - set(i))
return "\n\n".join(("\n".join(i), types2, types))
if __name__ == "__main__":
src = generate()
pathlib.Path("./comictaggerlib/ctsettings/settngs_namespace.py").write_text(src)
print(src, end="")

View File

@ -0,0 +1,35 @@
from __future__ import annotations
import argparse
import os
import pathlib
try:
import niquests as requests
except ImportError:
import requests
parser = argparse.ArgumentParser()
parser.add_argument("APPIMAGETOOL", default="build/appimagetool-x86_64.AppImage", type=pathlib.Path, nargs="?")
opts = parser.parse_args()
opts.APPIMAGETOOL = opts.APPIMAGETOOL.absolute()
def urlretrieve(url: str, dest: pathlib.Path) -> None:
resp = requests.get(url)
if resp.status_code == 200:
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_bytes(resp.content)
if opts.APPIMAGETOOL.exists():
raise SystemExit(0)
urlretrieve(
"https://github.com/AppImage/appimagetool/releases/latest/download/appimagetool-x86_64.AppImage", opts.APPIMAGETOOL
)
os.chmod(opts.APPIMAGETOOL, 0o0700)
if not opts.APPIMAGETOOL.exists():
raise SystemExit(1)

View File

@ -0,0 +1,267 @@
from __future__ import annotations
import argparse
import base64
import json
import os
import sys
from http import HTTPStatus
from pathlib import Path
from typing import NoReturn
from urllib.parse import urlparse
import keyring
import requests
from id import IdentityError, detect_credential
_GITHUB_STEP_SUMMARY = Path(os.getenv("GITHUB_STEP_SUMMARY", "fail.txt"))
# The top-level error message that gets rendered.
# This message wraps one of the other templates/messages defined below.
_ERROR_SUMMARY_MESSAGE = """
Trusted publishing exchange failure:
{message}
You're seeing this because the action wasn't given the inputs needed to
perform password-based or token-based authentication. If you intended to
perform one of those authentication methods instead of trusted
publishing, then you should double-check your secret configuration and variable
names.
Read more about trusted publishers at https://docs.pypi.org/trusted-publishers/
Read more about how this action uses trusted publishers at
https://github.com/marketplace/actions/pypi-publish#trusted-publishing
"""
# Rendered if OIDC identity token retrieval fails for any reason.
_TOKEN_RETRIEVAL_FAILED_MESSAGE = """
OpenID Connect token retrieval failed: {identity_error}
This generally indicates a workflow configuration error, such as insufficient
permissions. Make sure that your workflow has `id-token: write` configured
at the job level, e.g.:
```yaml
permissions:
id-token: write
```
Learn more at https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#adding-permissions-settings.
""" # noqa: S105; not a password
# Specialization of the token retrieval failure case, when we know that
# the failure cause is use within a third-party PR.
_TOKEN_RETRIEVAL_FAILED_FORK_PR_MESSAGE = """
OpenID Connect token retrieval failed: {identity_error}
The workflow context indicates that this action was called from a
pull request on a fork. GitHub doesn't give these workflows OIDC permissions,
even if `id-token: write` is explicitly configured.
To fix this, change your publishing workflow to use an event that
forks of your repository cannot trigger (such as tag or release
creation, or a manually triggered workflow dispatch).
""" # noqa: S105; not a password
# Rendered if the package index refuses the given OIDC token.
_SERVER_REFUSED_TOKEN_EXCHANGE_MESSAGE = """
Token request failed: the server refused the request for the following reasons:
{reasons}
This generally indicates a trusted publisher configuration error, but could
also indicate an internal error on GitHub or PyPI's part.
{rendered_claims}
""" # noqa: S105; not a password
_RENDERED_CLAIMS = """
The claims rendered below are **for debugging purposes only**. You should **not**
use them to configure a trusted publisher unless they already match your expectations.
If a claim is not present in the claim set, then it is rendered as `MISSING`.
* `sub`: `{sub}`
* `repository`: `{repository}`
* `repository_owner`: `{repository_owner}`
* `repository_owner_id`: `{repository_owner_id}`
* `job_workflow_ref`: `{job_workflow_ref}`
* `ref`: `{ref}`
See https://docs.pypi.org/trusted-publishers/troubleshooting/ for more help.
"""
# Rendered if the package index's token response isn't valid JSON.
_SERVER_TOKEN_RESPONSE_MALFORMED_JSON = """
Token request failed: the index produced an unexpected
{status_code} response.
This strongly suggests a server configuration or downtime issue; wait
a few minutes and try again.
You can monitor PyPI's status here: https://status.python.org/
""" # noqa: S105; not a password
# Rendered if the package index's token response isn't a valid API token payload.
_SERVER_TOKEN_RESPONSE_MALFORMED_MESSAGE = """
Token response error: the index gave us an invalid response.
This strongly suggests a server configuration or downtime issue; wait
a few minutes and try again.
""" # noqa: S105; not a password
def die(msg: str) -> NoReturn:
with _GITHUB_STEP_SUMMARY.open("a", encoding="utf-8") as io:
print(_ERROR_SUMMARY_MESSAGE.format(message=msg), file=io)
# HACK: GitHub Actions' annotations don't work across multiple lines naively;
# translating `\n` into `%0A` (i.e., HTML percent-encoding) is known to work.
# See: https://github.com/actions/toolkit/issues/193
msg = msg.replace("\n", "%0A")
print(f"::error::Trusted publishing exchange failure: {msg}", file=sys.stderr)
sys.exit(1)
def debug(msg: str) -> None:
print(f"::debug::{msg.title()}", file=sys.stderr)
def assert_successful_audience_call(resp: requests.Response, domain: str) -> None:
if resp.ok:
return
if resp.status_code == HTTPStatus.FORBIDDEN:
# This index supports OIDC, but forbids the client from using
# it (either because it's disabled, ratelimited, etc.)
die(
f"audience retrieval failed: repository at {domain} has trusted publishing disabled",
)
elif resp.status_code == HTTPStatus.NOT_FOUND:
# This index does not support OIDC.
die(
f"audience retrieval failed: repository at {domain} does not indicate trusted publishing support",
)
else:
status = HTTPStatus(resp.status_code)
# Unknown: the index may or may not support OIDC, but didn't respond with
# something we expect. This can happen if the index is broken, in maintenance mode,
# misconfigured, etc.
die(
f"audience retrieval failed: repository at {domain} responded with unexpected {resp.status_code}: {status.phrase}",
)
def render_claims(token: str) -> str:
_, payload, _ = token.split(".", 2)
# urlsafe_b64decode needs padding; JWT payloads don't contain any.
payload += "=" * (4 - (len(payload) % 4))
claims = json.loads(base64.urlsafe_b64decode(payload))
def _get(name: str) -> str:
return claims.get(name, "MISSING")
return _RENDERED_CLAIMS.format(
sub=_get("sub"),
repository=_get("repository"),
repository_owner=_get("repository_owner"),
repository_owner_id=_get("repository_owner_id"),
job_workflow_ref=_get("job_workflow_ref"),
ref=_get("ref"),
)
def event_is_third_party_pr() -> bool:
# Non-`pull_request` events cannot be from third-party PRs.
if os.getenv("GITHUB_EVENT_NAME") != "pull_request":
return False
event_path = os.getenv("GITHUB_EVENT_PATH")
if not event_path:
# No GITHUB_EVENT_PATH indicates a weird GitHub or runner bug.
debug("unexpected: no GITHUB_EVENT_PATH to check")
return False
try:
event = json.loads(Path(event_path).read_bytes())
except json.JSONDecodeError:
debug("unexpected: GITHUB_EVENT_PATH does not contain valid JSON")
return False
try:
return event["pull_request"]["head"]["repo"]["fork"]
except KeyError:
return False
parser = argparse.ArgumentParser()
parser.add_argument("repository_url", default="https://upload.pypi.org/legacy/", type=urlparse, nargs="?")
opts = parser.parse_args()
repository_domain = opts.repository_url.netloc
token_exchange_url = f"https://{repository_domain}/_/oidc/mint-token"
# Indices are expected to support `https://{domain}/_/oidc/audience`,
# which tells OIDC exchange clients which audience to use.
audience_url = f"https://{repository_domain}/_/oidc/audience"
audience_resp = requests.get(audience_url, timeout=5)
assert_successful_audience_call(audience_resp, repository_domain)
oidc_audience = audience_resp.json()["audience"]
debug(f"selected trusted publishing exchange endpoint: {token_exchange_url}")
try:
oidc_token = detect_credential(audience=oidc_audience)
except IdentityError as identity_error:
cause_msg_tmpl = (
_TOKEN_RETRIEVAL_FAILED_FORK_PR_MESSAGE if event_is_third_party_pr() else _TOKEN_RETRIEVAL_FAILED_MESSAGE
)
for_cause_msg = cause_msg_tmpl.format(identity_error=identity_error)
die(for_cause_msg)
if not oidc_token:
die("Unabled to detect credentials. Is this runnnig in CI?")
# Now we can do the actual token exchange.
mint_token_resp = requests.post(
token_exchange_url,
json={"token": oidc_token},
timeout=5,
)
try:
mint_token_payload = mint_token_resp.json()
except requests.JSONDecodeError:
# Token exchange failure normally produces a JSON error response, but
# we might have hit a server error instead.
die(
_SERVER_TOKEN_RESPONSE_MALFORMED_JSON.format(
status_code=mint_token_resp.status_code,
),
)
# On failure, the JSON response includes the list of errors that
# occurred during minting.
if not mint_token_resp.ok:
reasons = "\n".join(f'* `{error["code"]}`: {error["description"]}' for error in mint_token_payload["errors"])
rendered_claims = render_claims(oidc_token)
die(
_SERVER_REFUSED_TOKEN_EXCHANGE_MESSAGE.format(
reasons=reasons,
rendered_claims=rendered_claims,
),
)
pypi_token = mint_token_payload.get("token")
if pypi_token is None:
die(_SERVER_TOKEN_RESPONSE_MALFORMED_MESSAGE)
# Mask the newly minted PyPI token, so that we don't accidentally leak it in logs.
print(f"::add-mask::{pypi_token}", file=sys.stderr)
keyring.set_password(opts.repository_url.geturl(), "__token__", pypi_token)

View File

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

View File

@ -0,0 +1,90 @@
from __future__ import annotations
import os
import pathlib
import platform
import sys
import tarfile
import zipfile
from comictaggerlib.ctversion import __version__
def addToZip(zf: zipfile.ZipFile, path: str, zippath: str) -> None:
if os.path.isfile(path):
zf.write(path, zippath)
elif os.path.isdir(path):
if zippath:
zf.write(path, zippath)
for nm in sorted(os.listdir(path)):
addToZip(zf, os.path.join(path, nm), os.path.join(zippath, nm))
def Zip(zip_file: pathlib.Path, path: pathlib.Path) -> None:
zip_file.unlink(missing_ok=True)
with zipfile.ZipFile(f"{zip_file}.zip", "w", compression=zipfile.ZIP_DEFLATED, compresslevel=8) as zf:
zippath = os.path.basename(path)
if not zippath:
zippath = os.path.basename(os.path.dirname(path))
if zippath in ("", os.curdir, os.pardir):
zippath = ""
addToZip(zf, str(path), zippath)
def addToTar(tf: tarfile.TarFile, path: str, zippath: str) -> None:
if os.path.isfile(path):
tf.add(path, zippath)
elif os.path.isdir(path):
if zippath:
tf.add(path, zippath, recursive=False)
for nm in sorted(os.listdir(path)):
addToTar(tf, os.path.join(path, nm), os.path.join(zippath, nm))
def Tar(tar_file: pathlib.Path, path: pathlib.Path) -> None:
tar_file.unlink(missing_ok=True)
with tarfile.open(f"{tar_file}.tar.gz", "w:gz") as tf:
zippath = os.path.basename(path)
if not zippath:
zippath = os.path.basename(os.path.dirname(path))
if zippath in ("", os.curdir, os.pardir):
zippath = ""
addToTar(tf, str(path), zippath)
if __name__ == "__main__":
app = "ComicTagger"
exe = app.casefold()
if platform.system() == "Windows":
os_version = f"win-{platform.machine()}"
app_name = f"{exe}.exe"
final_name = f"{app}-{__version__}-{os_version}.exe"
elif platform.system() == "Darwin":
ver = platform.mac_ver()
os_version = f"osx-{ver[0]}-{ver[2]}"
app_name = f"{app}.app"
final_name = f"{app}-{__version__}-{os_version}"
else:
app_name = exe
final_name = f"ComicTagger-{__version__}-{platform.system()}"
path = pathlib.Path(f"dist/{app_name}")
binary_path = pathlib.Path("dist/binary")
binary_path.mkdir(parents=True, exist_ok=True)
archive_destination = binary_path / final_name
if platform.system() == "Darwin":
from dmgbuild.__main__ import main as dmg_main
sys.argv = [
"zip_artifacts",
"-s",
str(pathlib.Path(__file__).parent / "dmgbuild.conf"),
f"{app} {__version__}",
f"{archive_destination}.dmg",
]
dmg_main()
elif platform.system() == "Windows":
Zip(archive_destination, path)
else:
Tar(archive_destination, path)

View File

@ -1,6 +1,10 @@
from __future__ import annotations
from PyInstaller.utils.hooks import collect_data_files
from PyInstaller.utils.hooks import collect_data_files, collect_entry_point
datas = []
datas, hiddenimports = collect_entry_point("comicapi.archiver")
mdatas, mhiddenimports = collect_entry_point("comicapi.tags")
hiddenimports += mhiddenimports
datas += mdatas
datas += collect_data_files("comicapi.data")

468
comicapi/_url.py Normal file
View File

@ -0,0 +1,468 @@
# mypy: disable-error-code="no-redef"
from __future__ import annotations
try:
from urllib3.exceptions import HTTPError, LocationParseError, LocationValueError
from urllib3.util import Url, parse_url
except ImportError:
import re
import typing
class HTTPError(Exception):
"""Base exception used by this module."""
class LocationValueError(ValueError, HTTPError):
"""Raised when there is something wrong with a given URL input."""
class LocationParseError(LocationValueError):
"""Raised when get_host or similar fails to parse the URL input."""
def __init__(self, location: str) -> None:
message = f"Failed to parse: {location}"
super().__init__(message)
self.location = location
def to_str(x: str | bytes, encoding: str | None = None, errors: str | None = None) -> str:
if isinstance(x, str):
return x
elif not isinstance(x, bytes):
raise TypeError(f"not expecting type {type(x).__name__}")
if encoding or errors:
return x.decode(encoding or "utf-8", errors=errors or "strict")
return x.decode()
# We only want to normalize urls with an HTTP(S) scheme.
# urllib3 infers URLs without a scheme (None) to be http.
_NORMALIZABLE_SCHEMES = ("http", "https", None)
# Almost all of these patterns were derived from the
# 'rfc3986' module: https://github.com/python-hyper/rfc3986
_PERCENT_RE = re.compile(r"%[a-fA-F0-9]{2}")
_SCHEME_RE = re.compile(r"^(?:[a-zA-Z][a-zA-Z0-9+-]*:|/)")
_URI_RE = re.compile(
r"^(?:([a-zA-Z][a-zA-Z0-9+.-]*):)?" r"(?://([^\\/?#]*))?" r"([^?#]*)" r"(?:\?([^#]*))?" r"(?:#(.*))?$",
re.UNICODE | re.DOTALL,
)
_IPV4_PAT = r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}"
_HEX_PAT = "[0-9A-Fa-f]{1,4}"
_LS32_PAT = "(?:{hex}:{hex}|{ipv4})".format(hex=_HEX_PAT, ipv4=_IPV4_PAT)
_subs = {"hex": _HEX_PAT, "ls32": _LS32_PAT}
_variations = [
# 6( h16 ":" ) ls32
"(?:%(hex)s:){6}%(ls32)s",
# "::" 5( h16 ":" ) ls32
"::(?:%(hex)s:){5}%(ls32)s",
# [ h16 ] "::" 4( h16 ":" ) ls32
"(?:%(hex)s)?::(?:%(hex)s:){4}%(ls32)s",
# [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
"(?:(?:%(hex)s:)?%(hex)s)?::(?:%(hex)s:){3}%(ls32)s",
# [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
"(?:(?:%(hex)s:){0,2}%(hex)s)?::(?:%(hex)s:){2}%(ls32)s",
# [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
"(?:(?:%(hex)s:){0,3}%(hex)s)?::%(hex)s:%(ls32)s",
# [ *4( h16 ":" ) h16 ] "::" ls32
"(?:(?:%(hex)s:){0,4}%(hex)s)?::%(ls32)s",
# [ *5( h16 ":" ) h16 ] "::" h16
"(?:(?:%(hex)s:){0,5}%(hex)s)?::%(hex)s",
# [ *6( h16 ":" ) h16 ] "::"
"(?:(?:%(hex)s:){0,6}%(hex)s)?::",
]
_UNRESERVED_PAT = r"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._\-~"
_IPV6_PAT = "(?:" + "|".join([x % _subs for x in _variations]) + ")"
_ZONE_ID_PAT = "(?:%25|%)(?:[" + _UNRESERVED_PAT + "]|%[a-fA-F0-9]{2})+"
_IPV6_ADDRZ_PAT = r"\[" + _IPV6_PAT + r"(?:" + _ZONE_ID_PAT + r")?\]"
_REG_NAME_PAT = r"(?:[^\[\]%:/?#]|%[a-fA-F0-9]{2})*"
_TARGET_RE = re.compile(r"^(/[^?#]*)(?:\?([^#]*))?(?:#.*)?$")
_IPV4_RE = re.compile("^" + _IPV4_PAT + "$")
_IPV6_RE = re.compile("^" + _IPV6_PAT + "$")
_IPV6_ADDRZ_RE = re.compile("^" + _IPV6_ADDRZ_PAT + "$")
_BRACELESS_IPV6_ADDRZ_RE = re.compile("^" + _IPV6_ADDRZ_PAT[2:-2] + "$")
_ZONE_ID_RE = re.compile("(" + _ZONE_ID_PAT + r")\]$")
_HOST_PORT_PAT = ("^(%s|%s|%s)(?::0*?(|0|[1-9][0-9]{0,4}))?$") % (
_REG_NAME_PAT,
_IPV4_PAT,
_IPV6_ADDRZ_PAT,
)
_HOST_PORT_RE = re.compile(_HOST_PORT_PAT, re.UNICODE | re.DOTALL)
_UNRESERVED_CHARS = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-~")
_SUB_DELIM_CHARS = set("!$&'()*+,;=")
_USERINFO_CHARS = _UNRESERVED_CHARS | _SUB_DELIM_CHARS | {":"}
_PATH_CHARS = _USERINFO_CHARS | {"@", "/"}
_QUERY_CHARS = _FRAGMENT_CHARS = _PATH_CHARS | {"?"}
class Url(
typing.NamedTuple(
"Url",
[
("scheme", typing.Optional[str]),
("auth", typing.Optional[str]),
("host", typing.Optional[str]),
("port", typing.Optional[int]),
("path", typing.Optional[str]),
("query", typing.Optional[str]),
("fragment", typing.Optional[str]),
],
)
):
"""
Data structure for representing an HTTP URL. Used as a return value for
:func:`parse_url`. Both the scheme and host are normalized as they are
both case-insensitive according to RFC 3986.
"""
def __new__( # type: ignore[no-untyped-def]
cls,
scheme: str | None = None,
auth: str | None = None,
host: str | None = None,
port: int | None = None,
path: str | None = None,
query: str | None = None,
fragment: str | None = None,
):
if path and not path.startswith("/"):
path = "/" + path
if scheme is not None:
scheme = scheme.lower()
return super().__new__(cls, scheme, auth, host, port, path, query, fragment)
@property
def hostname(self) -> str | None:
"""For backwards-compatibility with urlparse. We're nice like that."""
return self.host
@property
def request_uri(self) -> str:
"""Absolute path including the query string."""
uri = self.path or "/"
if self.query is not None:
uri += "?" + self.query
return uri
@property
def authority(self) -> str | None:
"""
Authority component as defined in RFC 3986 3.2.
This includes userinfo (auth), host and port.
i.e.
userinfo@host:port
"""
userinfo = self.auth
netloc = self.netloc
if netloc is None or userinfo is None:
return netloc
else:
return f"{userinfo}@{netloc}"
@property
def netloc(self) -> str | None:
"""
Network location including host and port.
If you need the equivalent of urllib.parse's ``netloc``,
use the ``authority`` property instead.
"""
if self.host is None:
return None
if self.port:
return f"{self.host}:{self.port}"
return self.host
@property
def url(self) -> str:
"""
Convert self into a url
This function should more or less round-trip with :func:`.parse_url`. The
returned url may not be exactly the same as the url inputted to
:func:`.parse_url`, but it should be equivalent by the RFC (e.g., urls
with a blank port will have : removed).
Example:
.. code-block:: python
import urllib3
U = urllib3.util.parse_url("https://google.com/mail/")
print(U.url)
# "https://google.com/mail/"
print( urllib3.util.Url("https", "username:password",
"host.com", 80, "/path", "query", "fragment"
).url
)
# "https://username:password@host.com:80/path?query#fragment"
"""
scheme, auth, host, port, path, query, fragment = self
url = ""
# We use "is not None" we want things to happen with empty strings (or 0 port)
if scheme is not None:
url += scheme + "://"
if auth is not None:
url += auth + "@"
if host is not None:
url += host
if port is not None:
url += ":" + str(port)
if path is not None:
url += path
if query is not None:
url += "?" + query
if fragment is not None:
url += "#" + fragment
return url
def __str__(self) -> str:
return self.url
@typing.overload
def _encode_invalid_chars(component: str, allowed_chars: typing.Container[str]) -> str: # Abstract
...
@typing.overload
def _encode_invalid_chars(component: None, allowed_chars: typing.Container[str]) -> None: # Abstract
...
def _encode_invalid_chars(component: str | None, allowed_chars: typing.Container[str]) -> str | None:
"""Percent-encodes a URI component without reapplying
onto an already percent-encoded component.
"""
if component is None:
return component
component = to_str(component)
# Normalize existing percent-encoded bytes.
# Try to see if the component we're encoding is already percent-encoded
# so we can skip all '%' characters but still encode all others.
component, percent_encodings = _PERCENT_RE.subn(lambda match: match.group(0).upper(), component)
uri_bytes = component.encode("utf-8", "surrogatepass")
is_percent_encoded = percent_encodings == uri_bytes.count(b"%")
encoded_component = bytearray()
for i in range(0, len(uri_bytes)):
# Will return a single character bytestring
byte = uri_bytes[i : i + 1]
byte_ord = ord(byte)
if (is_percent_encoded and byte == b"%") or (byte_ord < 128 and byte.decode() in allowed_chars):
encoded_component += byte
continue
encoded_component.extend(b"%" + (hex(byte_ord)[2:].encode().zfill(2).upper()))
return encoded_component.decode()
def _remove_path_dot_segments(path: str) -> str:
# See http://tools.ietf.org/html/rfc3986#section-5.2.4 for pseudo-code
segments = path.split("/") # Turn the path into a list of segments
output = [] # Initialize the variable to use to store output
for segment in segments:
# '.' is the current directory, so ignore it, it is superfluous
if segment == ".":
continue
# Anything other than '..', should be appended to the output
if segment != "..":
output.append(segment)
# In this case segment == '..', if we can, we should pop the last
# element
elif output:
output.pop()
# If the path starts with '/' and the output is empty or the first string
# is non-empty
if path.startswith("/") and (not output or output[0]):
output.insert(0, "")
# If the path starts with '/.' or '/..' ensure we add one more empty
# string to add a trailing '/'
if path.endswith(("/.", "/..")):
output.append("")
return "/".join(output)
@typing.overload
def _normalize_host(host: None, scheme: str | None) -> None: ...
@typing.overload
def _normalize_host(host: str, scheme: str | None) -> str: ...
def _normalize_host(host: str | None, scheme: str | None) -> str | None:
if host:
if scheme in _NORMALIZABLE_SCHEMES:
is_ipv6 = _IPV6_ADDRZ_RE.match(host)
if is_ipv6:
# IPv6 hosts of the form 'a::b%zone' are encoded in a URL as
# such per RFC 6874: 'a::b%25zone'. Unquote the ZoneID
# separator as necessary to return a valid RFC 4007 scoped IP.
match = _ZONE_ID_RE.search(host)
if match:
start, end = match.span(1)
zone_id = host[start:end]
if zone_id.startswith("%25") and zone_id != "%25":
zone_id = zone_id[3:]
else:
zone_id = zone_id[1:]
zone_id = _encode_invalid_chars(zone_id, _UNRESERVED_CHARS)
return f"{host[:start].lower()}%{zone_id}{host[end:]}"
else:
return host.lower()
elif not _IPV4_RE.match(host):
return to_str(
b".".join([_idna_encode(label) for label in host.split(".")]),
"ascii",
)
return host
def _idna_encode(name: str) -> bytes:
if not name.isascii():
try:
import idna
except ImportError:
raise LocationParseError("Unable to parse URL without the 'idna' module") from None
try:
return idna.encode(name.lower(), strict=True, std3_rules=True)
except idna.IDNAError:
raise LocationParseError(f"Name '{name}' is not a valid IDNA label") from None
return name.lower().encode("ascii")
def _encode_target(target: str) -> str:
"""Percent-encodes a request target so that there are no invalid characters
Pre-condition for this function is that 'target' must start with '/'.
If that is the case then _TARGET_RE will always produce a match.
"""
match = _TARGET_RE.match(target)
if not match: # Defensive:
raise LocationParseError(f"{target!r} is not a valid request URI")
path, query = match.groups()
encoded_target = _encode_invalid_chars(path, _PATH_CHARS)
if query is not None:
query = _encode_invalid_chars(query, _QUERY_CHARS)
encoded_target += "?" + query
return encoded_target
def parse_url(url: str) -> Url:
"""
Given a url, return a parsed :class:`.Url` namedtuple. Best-effort is
performed to parse incomplete urls. Fields not provided will be None.
This parser is RFC 3986 and RFC 6874 compliant.
The parser logic and helper functions are based heavily on
work done in the ``rfc3986`` module.
:param str url: URL to parse into a :class:`.Url` namedtuple.
Partly backwards-compatible with :mod:`urllib.parse`.
Example:
.. code-block:: python
import urllib3
print( urllib3.util.parse_url('http://google.com/mail/'))
# Url(scheme='http', host='google.com', port=None, path='/mail/', ...)
print( urllib3.util.parse_url('google.com:80'))
# Url(scheme=None, host='google.com', port=80, path=None, ...)
print( urllib3.util.parse_url('/foo?bar'))
# Url(scheme=None, host=None, port=None, path='/foo', query='bar', ...)
"""
if not url:
# Empty
return Url()
source_url = url
if not _SCHEME_RE.search(url):
url = "//" + url
scheme: str | None
authority: str | None
auth: str | None
host: str | None
port: str | None
port_int: int | None
path: str | None
query: str | None
fragment: str | None
try:
scheme, authority, path, query, fragment = _URI_RE.match(url).groups() # type: ignore[union-attr]
normalize_uri = scheme is None or scheme.lower() in _NORMALIZABLE_SCHEMES
if scheme:
scheme = scheme.lower()
if authority:
auth, _, host_port = authority.rpartition("@")
auth = auth or None
host, port = _HOST_PORT_RE.match(host_port).groups() # type: ignore[union-attr]
if auth and normalize_uri:
auth = _encode_invalid_chars(auth, _USERINFO_CHARS)
if port == "":
port = None
else:
auth, host, port = None, None, None
if port is not None:
port_int = int(port)
if not (0 <= port_int <= 65535):
raise LocationParseError(url)
else:
port_int = None
host = _normalize_host(host, scheme)
if normalize_uri and path:
path = _remove_path_dot_segments(path)
path = _encode_invalid_chars(path, _PATH_CHARS)
if normalize_uri and query:
query = _encode_invalid_chars(query, _QUERY_CHARS)
if normalize_uri and fragment:
fragment = _encode_invalid_chars(fragment, _FRAGMENT_CHARS)
except (ValueError, AttributeError) as e:
raise LocationParseError(source_url) from e
# For the sake of backwards compatibility we put empty
# string values for path if there are any defined values
# beyond the path in the URL.
# TODO: Remove this when we break backwards compatibility.
if not path:
if query is not None or fragment is not None:
path = ""
else:
path = None
return Url(
scheme=scheme,
auth=auth,
host=host,
port=port_int,
path=path,
query=query,
fragment=fragment,
)
__all__ = ("Url", "parse_url", "HTTPError", "LocationParseError", "LocationValueError")

View File

@ -0,0 +1,13 @@
from __future__ import annotations
from comicapi.archivers.archiver import Archiver
from comicapi.archivers.folder import FolderArchiver
from comicapi.archivers.zip import ZipArchiver
class UnknownArchiver(Archiver):
def name(self) -> str:
return "Unknown"
__all__ = ["Archiver", "UnknownArchiver", "FolderArchiver", "ZipArchiver"]

View File

@ -0,0 +1,137 @@
from __future__ import annotations
import pathlib
from typing import Protocol, runtime_checkable
@runtime_checkable
class Archiver(Protocol):
"""Archiver Protocol"""
"""The path to the archive"""
path: pathlib.Path
"""
The name of the executable used for this archiver. This should be the base name of the executable.
For example if 'rar.exe' is needed this should be "rar".
If an executable is not used this should be the empty string.
"""
exe: str = ""
"""
Whether or not this archiver is enabled.
If external imports are required and are not available this should be false. See rar.py and sevenzip.py.
"""
enabled: bool = True
def __init__(self) -> None:
self.path = pathlib.Path()
def get_comment(self) -> str:
"""
Returns the comment from the current archive as a string.
Should always return a string. If comments are not supported in the archive the empty string should be returned.
"""
return ""
def set_comment(self, comment: str) -> bool:
"""
Returns True if the comment was successfully set on the current archive.
Should always return a boolean. If comments are not supported in the archive False should be returned.
"""
return False
def supports_comment(self) -> bool:
"""
Returns True if the current archive supports comments.
Should always return a boolean. If comments are not supported in the archive False should be returned.
"""
return False
def read_file(self, archive_file: str) -> bytes:
"""
Reads the named file from the current archive.
archive_file should always come from the output of get_filename_list.
Should always return a bytes object. Exceptions should be of the type OSError.
"""
raise NotImplementedError
def remove_file(self, archive_file: str) -> bool:
"""
Removes the named file from the current archive.
archive_file should always come from the output of get_filename_list.
Should always return a boolean. Failures should return False.
Rebuilding the archive without the named file is a standard way to remove a file.
"""
return False
def write_file(self, archive_file: str, data: bytes) -> bool:
"""
Writes the named file to the current archive.
Should always return a boolean. Failures should return False.
"""
return False
def get_filename_list(self) -> list[str]:
"""
Returns a list of filenames in the current archive.
Should always return a list of string. Failures should return an empty list.
"""
return []
def supports_files(self) -> bool:
"""
Returns True if the current archive supports arbitrary non-picture files.
Should always return a boolean.
If arbitrary non-picture files are not supported in the archive False should be returned.
"""
return False
def copy_from_archive(self, other_archive: Archiver) -> bool:
"""
Copies the contents of another achive to the current archive.
Should always return a boolean. Failures should return False.
"""
return False
def is_writable(self) -> bool:
"""
Retuns True if the current archive is writeable
Should always return a boolean. Failures should return False.
"""
return False
def extension(self) -> str:
"""
Returns the extension that this archiver should use eg ".cbz".
Should always return a string. Failures should return the empty string.
"""
return ""
def name(self) -> str:
"""
Returns the name of this archiver for display purposes eg "CBZ".
Should always return a string. Failures should return the empty string.
"""
return ""
@classmethod
def is_valid(cls, path: pathlib.Path) -> bool:
"""
Returns True if the given path can be opened by this archiver.
Should always return a boolean. Failures should return False.
"""
return False
@classmethod
def open(cls, path: pathlib.Path) -> Archiver:
"""
Opens the given archive.
Should always return a an Archver.
Should never cause an exception no file operations should take place in this method,
is_valid will always be called before open.
"""
archiver = cls()
archiver.path = path
return archiver

View File

@ -0,0 +1,104 @@
from __future__ import annotations
import logging
import os
import pathlib
from comicapi.archivers import Archiver
logger = logging.getLogger(__name__)
class FolderArchiver(Archiver):
"""Folder implementation"""
def __init__(self) -> None:
super().__init__()
self.comment_file_name = "ComicTaggerFolderComment.txt"
def get_comment(self) -> str:
try:
return (self.path / self.comment_file_name).read_text()
except OSError:
return ""
def set_comment(self, comment: str) -> bool:
if (self.path / self.comment_file_name).exists() or comment:
return self.write_file(self.comment_file_name, comment.encode("utf-8"))
return True
def supports_comment(self) -> bool:
return True
def read_file(self, archive_file: str) -> bytes:
try:
data = (self.path / archive_file).read_bytes()
except OSError as e:
logger.error("Error reading folder archive [%s]: %s :: %s", e, self.path, archive_file)
raise
return data
def remove_file(self, archive_file: str) -> bool:
try:
(self.path / archive_file).unlink(missing_ok=True)
except OSError as e:
logger.error("Error removing file for folder archive [%s]: %s :: %s", e, self.path, archive_file)
return False
else:
return True
def write_file(self, archive_file: str, data: bytes) -> bool:
try:
file_path = self.path / archive_file
file_path.parent.mkdir(exist_ok=True, parents=True)
with open(self.path / archive_file, mode="wb") as f:
f.write(data)
except OSError as e:
logger.error("Error writing folder archive [%s]: %s :: %s", e, self.path, archive_file)
return False
else:
return True
def get_filename_list(self) -> list[str]:
filenames = []
try:
for root, _dirs, files in os.walk(self.path):
for f in files:
filenames.append(os.path.relpath(os.path.join(root, f), self.path).replace(os.path.sep, "/"))
return filenames
except OSError as e:
logger.error("Error listing files in folder archive [%s]: %s", e, self.path)
return []
def supports_files(self) -> bool:
return True
def copy_from_archive(self, other_archive: Archiver) -> bool:
"""Replace the current zip with one copied from another archive"""
try:
for filename in other_archive.get_filename_list():
data = other_archive.read_file(filename)
if data is not None:
self.write_file(filename, data)
# preserve the old comment
comment = other_archive.get_comment()
if comment is not None:
if not self.set_comment(comment):
return False
except Exception:
logger.exception("Error while copying archive from %s to %s", other_archive.path, self.path)
return False
else:
return True
def is_writable(self) -> bool:
return True
def name(self) -> str:
return "Folder"
@classmethod
def is_valid(cls, path: pathlib.Path) -> bool:
return path.is_dir()

324
comicapi/archivers/rar.py Normal file
View File

@ -0,0 +1,324 @@
from __future__ import annotations
import logging
import os
import pathlib
import platform
import shutil
import subprocess
import tempfile
import time
from comicapi.archivers import Archiver
try:
import rarfile
rar_support = True
except ImportError:
rar_support = False
logger = logging.getLogger(__name__)
if not rar_support:
logger.error("rar unavailable")
class RarArchiver(Archiver):
"""RAR implementation"""
enabled = rar_support
exe = "rar"
def __init__(self) -> None:
super().__init__()
# windows only, keeps the cmd.exe from popping up
if platform.system() == "Windows":
self.startupinfo = subprocess.STARTUPINFO() # type: ignore
self.startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # type: ignore
else:
self.startupinfo = None
def get_comment(self) -> str:
rarc = self.get_rar_obj()
return (rarc.comment if rarc else "") or ""
def set_comment(self, comment: str) -> bool:
if rar_support and self.exe:
try:
# write comment to temp file
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_file = pathlib.Path(tmp_dir) / "rar_comment.txt"
tmp_file.write_text(comment, encoding="utf-8")
working_dir = os.path.dirname(os.path.abspath(self.path))
# use external program to write comment to Rar archive
proc_args = [
self.exe,
"c",
f"-w{working_dir}",
"-c-",
f"-z{tmp_file}",
str(self.path),
]
result = subprocess.run(
proc_args,
startupinfo=self.startupinfo,
stdin=subprocess.DEVNULL,
capture_output=True,
encoding="utf-8",
cwd=tmp_dir,
)
if result.returncode != 0:
logger.error(
"Error writing comment to rar archive [exitcode: %d]: %s :: %s",
result.returncode,
self.path,
result.stderr,
)
return False
if platform.system() == "Darwin":
time.sleep(1)
except OSError as e:
logger.exception("Error writing comment to rar archive [%s]: %s", e, self.path)
return False
else:
return True
else:
return False
def supports_comment(self) -> bool:
return True
def read_file(self, archive_file: str) -> bytes:
rarc = self.get_rar_obj()
if rarc is None:
return b""
tries = 0
while tries < 7:
try:
tries = tries + 1
data: bytes = rarc.open(archive_file).read()
entries = [(rarc.getinfo(archive_file), data)]
if entries[0][0].file_size != len(entries[0][1]):
logger.info(
"Error reading rar archive [file is not expected size: %d vs %d] %s :: %s :: tries #%d",
entries[0][0].file_size,
len(entries[0][1]),
self.path,
archive_file,
tries,
)
continue
except OSError as e:
logger.error("Error reading rar archive [%s]: %s :: %s :: tries #%d", e, self.path, archive_file, tries)
time.sleep(1)
except Exception as e:
logger.error(
"Unexpected exception reading rar archive [%s]: %s :: %s :: tries #%d",
e,
self.path,
archive_file,
tries,
)
break
else:
# Success. Entries is a list of of tuples: ( rarinfo, filedata)
if len(entries) == 1:
return entries[0][1]
raise OSError
raise OSError
def remove_file(self, archive_file: str) -> bool:
if self.exe:
working_dir = os.path.dirname(os.path.abspath(self.path))
# use external program to remove file from Rar archive
result = subprocess.run(
[self.exe, "d", f"-w{working_dir}", "-c-", self.path, archive_file],
startupinfo=self.startupinfo,
stdin=subprocess.DEVNULL,
capture_output=True,
encoding="utf-8",
cwd=self.path.absolute().parent,
)
if platform.system() == "Darwin":
time.sleep(1)
if result.returncode != 0:
logger.error(
"Error removing file from rar archive [exitcode: %d]: %s :: %s",
result.returncode,
self.path,
archive_file,
)
return False
return True
else:
return False
def write_file(self, archive_file: str, data: bytes) -> bool:
if self.exe:
archive_path = pathlib.PurePosixPath(archive_file)
archive_name = archive_path.name
archive_parent = str(archive_path.parent).lstrip("./")
working_dir = os.path.dirname(os.path.abspath(self.path))
# use external program to write file to Rar archive
result = subprocess.run(
[
self.exe,
"a",
f"-w{working_dir}",
f"-si{archive_name}",
f"-ap{archive_parent}",
"-c-",
"-ep",
self.path,
],
input=data,
startupinfo=self.startupinfo,
capture_output=True,
cwd=self.path.absolute().parent,
)
if platform.system() == "Darwin":
time.sleep(1)
if result.returncode != 0:
logger.error(
"Error writing rar archive [exitcode: %d]: %s :: %s :: %s",
result.returncode,
self.path,
archive_file,
result.stderr,
)
return False
else:
return True
else:
return False
def get_filename_list(self) -> list[str]:
rarc = self.get_rar_obj()
tries = 0
if rar_support and rarc:
while tries < 7:
try:
tries = tries + 1
namelist = []
for item in rarc.infolist():
if item.file_size != 0:
namelist.append(item.filename)
except OSError as e:
logger.error("Error listing files in rar archive [%s]: %s :: attempt #%d", e, self.path, tries)
time.sleep(1)
else:
return namelist
return []
def supports_files(self) -> bool:
return True
def copy_from_archive(self, other_archive: Archiver) -> bool:
"""Replace the current archive with one copied from another archive"""
try:
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = pathlib.Path(tmp_dir)
rar_cwd = tmp_path / "rar"
rar_cwd.mkdir(exist_ok=True)
rar_path = (tmp_path / self.path.name).with_suffix(".rar")
working_dir = os.path.dirname(os.path.abspath(self.path))
for filename in other_archive.get_filename_list():
(rar_cwd / filename).parent.mkdir(exist_ok=True, parents=True)
data = other_archive.read_file(filename)
if data is not None:
with open(rar_cwd / filename, mode="w+b") as tmp_file:
tmp_file.write(data)
result = subprocess.run(
[self.exe, "a", f"-w{working_dir}", "-r", "-c-", str(rar_path.absolute()), "."],
cwd=rar_cwd.absolute(),
startupinfo=self.startupinfo,
stdin=subprocess.DEVNULL,
capture_output=True,
encoding="utf-8",
)
if result.returncode != 0:
logger.error(
"Error while copying to rar archive [exitcode: %d]: %s: %s",
result.returncode,
self.path,
result.stderr,
)
return False
self.path.unlink(missing_ok=True)
shutil.move(rar_path, self.path)
except Exception as e:
logger.exception("Error while copying to rar archive [%s]: from %s to %s", e, other_archive.path, self.path)
return False
else:
return True
def is_writable(self) -> bool:
try:
if bool(self.exe and (os.path.exists(self.exe) or shutil.which(self.exe))):
return (
subprocess.run(
(self.exe,),
startupinfo=self.startupinfo,
capture_output=True,
cwd=self.path.absolute().parent,
)
.stdout.strip()
.startswith(b"RAR")
)
except OSError:
...
return False
def extension(self) -> str:
return ".cbr"
def name(self) -> str:
return "RAR"
@classmethod
def is_valid(cls, path: pathlib.Path) -> bool:
if rar_support:
# Try using exe
orig = rarfile.UNRAR_TOOL
rarfile.UNRAR_TOOL = cls.exe
try:
return rarfile.is_rarfile(str(path)) and rarfile.tool_setup(sevenzip=False, sevenzip2=False, force=True)
except rarfile.RarCannotExec:
rarfile.UNRAR_TOOL = orig
# Fallback to standard
try:
return rarfile.is_rarfile(str(path)) and rarfile.tool_setup(force=True)
except rarfile.RarCannotExec as e:
logger.info(e)
return False
def get_rar_obj(self) -> rarfile.RarFile | None:
if rar_support:
try:
rarc = rarfile.RarFile(str(self.path))
except (OSError, rarfile.RarFileError) as e:
logger.error("Unable to get rar object [%s]: %s", e, self.path)
else:
return rarc
return None

View File

@ -0,0 +1,134 @@
from __future__ import annotations
import logging
import os
import pathlib
import shutil
import tempfile
from comicapi.archivers import Archiver
try:
import py7zr
z7_support = True
except ImportError:
z7_support = False
logger = logging.getLogger(__name__)
class SevenZipArchiver(Archiver):
"""7Z implementation"""
enabled = z7_support
def __init__(self) -> None:
super().__init__()
# @todo: Implement Comment?
def get_comment(self) -> str:
return ""
def set_comment(self, comment: str) -> bool:
return False
def read_file(self, archive_file: str) -> bytes:
data = b""
try:
with py7zr.SevenZipFile(self.path, "r") as zf:
data = zf.read([archive_file])[archive_file].read()
except (py7zr.Bad7zFile, OSError) as e:
logger.error("Error reading 7zip archive [%s]: %s :: %s", e, self.path, archive_file)
raise
return data
def remove_file(self, archive_file: str) -> bool:
return self.rebuild([archive_file])
def write_file(self, archive_file: str, data: bytes) -> bool:
# At the moment, no other option but to rebuild the whole
# archive w/o the indicated file. Very sucky, but maybe
# another solution can be found
files = self.get_filename_list()
if archive_file in files:
if not self.rebuild([archive_file]):
return False
try:
# now just add the archive file as a new one
with py7zr.SevenZipFile(self.path, "a") as zf:
zf.writestr(data, archive_file)
return True
except (py7zr.Bad7zFile, OSError) as e:
logger.error("Error writing 7zip archive [%s]: %s :: %s", e, self.path, archive_file)
return False
def get_filename_list(self) -> list[str]:
try:
with py7zr.SevenZipFile(self.path, "r") as zf:
namelist: list[str] = [file.filename for file in zf.list() if not file.is_directory]
return namelist
except (py7zr.Bad7zFile, OSError) as e:
logger.error("Error listing files in 7zip archive [%s]: %s", e, self.path)
return []
def supports_files(self) -> bool:
return True
def rebuild(self, exclude_list: list[str]) -> bool:
"""Zip helper func
This recompresses the zip archive, without the files in the exclude_list
"""
try:
# py7zr treats all archives as if they used solid compression
# so we need to get the filename list first to read all the files at once
with py7zr.SevenZipFile(self.path, mode="r") as zin:
targets = [f for f in zin.getnames() if f not in exclude_list]
with tempfile.NamedTemporaryFile(dir=os.path.dirname(self.path), delete=False) as tmp_file:
with py7zr.SevenZipFile(tmp_file.file, mode="w") as zout:
with py7zr.SevenZipFile(self.path, mode="r") as zin:
for filename, buffer in zin.read(targets).items():
zout.writef(buffer, filename)
self.path.unlink(missing_ok=True)
tmp_file.close() # Required on windows
shutil.move(tmp_file.name, self.path)
except (py7zr.Bad7zFile, OSError) as e:
logger.error("Error rebuilding 7zip file [%s]: %s", e, self.path)
return False
return True
def copy_from_archive(self, other_archive: Archiver) -> bool:
"""Replace the current zip with one copied from another archive"""
try:
with py7zr.SevenZipFile(self.path, "w") as zout:
for filename in other_archive.get_filename_list():
data = other_archive.read_file(
filename
) # This will be very inefficient if other_archive is a 7z file
if data is not None:
zout.writestr(data, filename)
except Exception as e:
logger.error("Error while copying to 7zip archive [%s]: from %s to %s", e, other_archive.path, self.path)
return False
else:
return True
def is_writable(self) -> bool:
return True
def extension(self) -> str:
return ".cb7"
def name(self) -> str:
return "Seven Zip"
@classmethod
def is_valid(cls, path: pathlib.Path) -> bool:
return py7zr.is_7zfile(path)

248
comicapi/archivers/zip.py Normal file
View File

@ -0,0 +1,248 @@
from __future__ import annotations
import logging
import os
import pathlib
import shutil
import tempfile
import zipfile
from typing import cast
import chardet
from comicapi.archivers import Archiver
logger = logging.getLogger(__name__)
class ZipArchiver(Archiver):
"""ZIP implementation"""
def __init__(self) -> None:
super().__init__()
def supports_comment(self) -> bool:
return True
def get_comment(self) -> str:
with zipfile.ZipFile(self.path, "r") as zf:
encoding = chardet.detect(zf.comment, True)
if encoding["confidence"] > 60:
try:
comment = zf.comment.decode(encoding["encoding"])
except UnicodeDecodeError:
comment = zf.comment.decode("utf-8", errors="replace")
else:
comment = zf.comment.decode("utf-8", errors="replace")
return comment
def set_comment(self, comment: str) -> bool:
with zipfile.ZipFile(self.path, mode="a") as zf:
zf.comment = bytes(comment, "utf-8")
return True
def read_file(self, archive_file: str) -> bytes:
with zipfile.ZipFile(self.path, mode="r") as zf:
try:
data = zf.read(archive_file)
except (zipfile.BadZipfile, OSError) as e:
logger.exception("Error reading zip archive [%s]: %s :: %s", e, self.path, archive_file)
raise
return data
def remove_file(self, archive_file: str) -> bool:
return self.rebuild([archive_file])
def write_file(self, archive_file: str, data: bytes) -> bool:
# At the moment, no other option but to rebuild the whole
# zip archive w/o the indicated file. Very sucky, but maybe
# another solution can be found
files = self.get_filename_list()
try:
# now just add the archive file as a new one
with zipfile.ZipFile(self.path, mode="a", allowZip64=True, compression=zipfile.ZIP_DEFLATED) as zf:
_patch_zipfile(zf)
if archive_file in files:
zf.remove(archive_file) # type: ignore
zf.writestr(archive_file, data)
return True
except (zipfile.BadZipfile, OSError) as e:
logger.error("Error writing zip archive [%s]: %s :: %s", e, self.path, archive_file)
return False
def get_filename_list(self) -> list[str]:
try:
with zipfile.ZipFile(self.path, mode="r") as zf:
namelist = [file.filename for file in zf.infolist() if not file.is_dir()]
return namelist
except (zipfile.BadZipfile, OSError) as e:
logger.error("Error listing files in zip archive [%s]: %s", e, self.path)
return []
def supports_files(self) -> bool:
return True
def rebuild(self, exclude_list: list[str]) -> bool:
"""Zip helper func
This recompresses the zip archive, without the files in the exclude_list
"""
try:
with zipfile.ZipFile(
tempfile.NamedTemporaryFile(dir=os.path.dirname(self.path), delete=False), "w", allowZip64=True
) as zout:
with zipfile.ZipFile(self.path, mode="r") as zin:
for item in zin.infolist():
buffer = zin.read(item.filename)
if item.filename not in exclude_list:
zout.writestr(item, buffer)
# preserve the old comment
zout.comment = zin.comment
# replace with the new file
self.path.unlink(missing_ok=True)
zout.close() # Required on windows
shutil.move(cast(str, zout.filename), self.path)
except (zipfile.BadZipfile, OSError) as e:
logger.error("Error rebuilding zip file [%s]: %s", e, self.path)
return False
return True
def copy_from_archive(self, other_archive: Archiver) -> bool:
"""Replace the current zip with one copied from another archive"""
try:
with zipfile.ZipFile(self.path, mode="w", allowZip64=True) as zout:
for filename in other_archive.get_filename_list():
data = other_archive.read_file(filename)
if data is not None:
zout.writestr(filename, data)
# preserve the old comment
comment = other_archive.get_comment()
if comment is not None:
if not self.set_comment(comment):
return False
except Exception as e:
logger.error("Error while copying to zip archive [%s]: from %s to %s", e, other_archive.path, self.path)
return False
else:
return True
def is_writable(self) -> bool:
return True
def extension(self) -> str:
return ".cbz"
def name(self) -> str:
return "ZIP"
@classmethod
def is_valid(cls, path: pathlib.Path) -> bool:
if not zipfile.is_zipfile(path): # only checks central directory ot the end of the archive
return False
try:
# test all the files in the zip. adds about 0.1 to execution time per zip
with zipfile.ZipFile(path) as zf:
for zipinfo in zf.filelist:
zf.open(zipinfo).close()
return True
except Exception:
return False
def _patch_zipfile(zf): # type: ignore
zf.remove = _zip_remove.__get__(zf, zipfile.ZipFile)
zf._remove_members = _zip_remove_members.__get__(zf, zipfile.ZipFile)
def _zip_remove(self, zinfo_or_arcname): # type: ignore
"""Remove a member from the archive."""
if self.mode not in ("w", "x", "a"):
raise ValueError("remove() requires mode 'w', 'x', or 'a'")
if not self.fp:
raise ValueError("Attempt to write to ZIP archive that was already closed")
if self._writing:
raise ValueError("Can't write to ZIP archive while an open writing handle exists")
# Make sure we have an existing info object
if isinstance(zinfo_or_arcname, zipfile.ZipInfo):
zinfo = zinfo_or_arcname
# make sure zinfo exists
if zinfo not in self.filelist:
raise KeyError("There is no item %r in the archive" % zinfo_or_arcname)
else:
# get the info object
zinfo = self.getinfo(zinfo_or_arcname)
return self._remove_members({zinfo})
def _zip_remove_members(self, members, *, remove_physical=True, chunk_size=2**20): # type: ignore
"""Remove members in a zip file.
All members (as zinfo) should exist in the zip; otherwise the zip file
will erroneously end in an inconsistent state.
"""
fp = self.fp
entry_offset = 0
member_seen = False
# get a sorted filelist by header offset, in case the dir order
# doesn't match the actual entry order
filelist = sorted(self.filelist, key=lambda x: x.header_offset)
for i in range(len(filelist)):
info = filelist[i]
is_member = info in members
if not (member_seen or is_member):
continue
# get the total size of the entry
try:
offset = filelist[i + 1].header_offset
except IndexError:
offset = self.start_dir
entry_size = offset - info.header_offset
if is_member:
member_seen = True
entry_offset += entry_size
# update caches
self.filelist.remove(info)
try:
del self.NameToInfo[info.filename]
except KeyError:
pass
continue
# update the header and move entry data to the new position
if remove_physical:
old_header_offset = info.header_offset
info.header_offset -= entry_offset
read_size = 0
while read_size < entry_size:
fp.seek(old_header_offset + read_size)
data = fp.read(min(entry_size - read_size, chunk_size))
fp.seek(info.header_offset + read_size)
fp.write(data)
fp.flush()
read_size += len(data)
# Avoid missing entry if entries have a duplicated name.
# Reverse the order as NameToInfo normally stores the last added one.
for info in reversed(self.filelist):
self.NameToInfo.setdefault(info.filename, info)
# update state
if remove_physical:
self.start_dir -= entry_offset
self._didModify = True
# seek to the start of the central dir
fp.seek(self.start_dir)

View File

@ -1,215 +0,0 @@
"""A class to encapsulate CoMet data"""
#
# Copyright 2012-2014 Anthony Beville
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import logging
import xml.etree.ElementTree as ET
from typing import Any
from comicapi import utils
from comicapi.genericmetadata import GenericMetadata
logger = logging.getLogger(__name__)
class CoMet:
writer_synonyms = ["writer", "plotter", "scripter"]
penciller_synonyms = ["artist", "penciller", "penciler", "breakdowns"]
inker_synonyms = ["inker", "artist", "finishes"]
colorist_synonyms = ["colorist", "colourist", "colorer", "colourer"]
letterer_synonyms = ["letterer"]
cover_synonyms = ["cover", "covers", "coverartist", "cover artist"]
editor_synonyms = ["editor"]
def metadata_from_string(self, string: str) -> GenericMetadata:
tree = ET.ElementTree(ET.fromstring(string))
return self.convert_xml_to_metadata(tree)
def string_from_metadata(self, metadata: GenericMetadata) -> str:
tree = self.convert_metadata_to_xml(metadata)
return str(ET.tostring(tree.getroot(), encoding="utf-8", xml_declaration=True).decode("utf-8"))
def convert_metadata_to_xml(self, metadata: GenericMetadata) -> ET.ElementTree:
# shorthand for the metadata
md = metadata
# build a tree structure
root = ET.Element("comet")
root.attrib["xmlns:comet"] = "http://www.denvog.com/comet/"
root.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
root.attrib["xsi:schemaLocation"] = "http://www.denvog.com http://www.denvog.com/comet/comet.xsd"
# helper func
def assign(comet_entry: str, md_entry: Any) -> None:
if md_entry is not None:
ET.SubElement(root, comet_entry).text = str(md_entry)
# title is manditory
if md.title is None:
md.title = ""
assign("title", md.title)
assign("series", md.series)
assign("issue", md.issue) # must be int??
assign("volume", md.volume)
assign("description", md.comments)
assign("publisher", md.publisher)
assign("pages", md.page_count)
assign("format", md.format)
assign("language", md.language)
assign("rating", md.maturity_rating)
assign("price", md.price)
assign("isVersionOf", md.is_version_of)
assign("rights", md.rights)
assign("identifier", md.identifier)
assign("lastMark", md.last_mark)
assign("genre", md.genre) # TODO repeatable
if md.characters is not None:
char_list = [c.strip() for c in md.characters.split(",")]
for c in char_list:
assign("character", c)
if md.manga is not None and md.manga == "YesAndRightToLeft":
assign("readingDirection", "rtl")
if md.year is not None:
date_str = f"{md.year:04}"
if md.month is not None:
date_str += f"-{md.month:02}"
assign("date", date_str)
assign("coverImage", md.cover_image)
# loop thru credits, and build a list for each role that CoMet supports
for credit in metadata.credits:
if credit["role"].casefold() in set(self.writer_synonyms):
ET.SubElement(root, "writer").text = str(credit["person"])
if credit["role"].casefold() in set(self.penciller_synonyms):
ET.SubElement(root, "penciller").text = str(credit["person"])
if credit["role"].casefold() in set(self.inker_synonyms):
ET.SubElement(root, "inker").text = str(credit["person"])
if credit["role"].casefold() in set(self.colorist_synonyms):
ET.SubElement(root, "colorist").text = str(credit["person"])
if credit["role"].casefold() in set(self.letterer_synonyms):
ET.SubElement(root, "letterer").text = str(credit["person"])
if credit["role"].casefold() in set(self.cover_synonyms):
ET.SubElement(root, "coverDesigner").text = str(credit["person"])
if credit["role"].casefold() in set(self.editor_synonyms):
ET.SubElement(root, "editor").text = str(credit["person"])
ET.indent(root)
# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)
return tree
def convert_xml_to_metadata(self, tree: ET.ElementTree) -> GenericMetadata:
root = tree.getroot()
if root.tag != "comet":
raise Exception("Not a CoMet file")
metadata = GenericMetadata()
md = metadata
# Helper function
def get(tag: str) -> Any:
node = root.find(tag)
if node is not None:
return node.text
return None
md.series = utils.xlate(get("series"))
md.title = utils.xlate(get("title"))
md.issue = utils.xlate(get("issue"))
md.volume = utils.xlate(get("volume"), True)
md.comments = utils.xlate(get("description"))
md.publisher = utils.xlate(get("publisher"))
md.language = utils.xlate(get("language"))
md.format = utils.xlate(get("format"))
md.page_count = utils.xlate(get("pages"), True)
md.maturity_rating = utils.xlate(get("rating"))
md.price = utils.xlate(get("price"), is_float=True)
md.is_version_of = utils.xlate(get("isVersionOf"))
md.rights = utils.xlate(get("rights"))
md.identifier = utils.xlate(get("identifier"))
md.last_mark = utils.xlate(get("lastMark"))
md.genre = utils.xlate(get("genre")) # TODO - repeatable field
_, md.month, md.year = utils.parse_date_str(utils.xlate(get("date")))
md.cover_image = utils.xlate(get("coverImage"))
reading_direction = utils.xlate(get("readingDirection"))
if reading_direction is not None and reading_direction == "rtl":
md.manga = "YesAndRightToLeft"
# loop for character tags
char_list = []
for n in root:
if n.tag == "character":
char_list.append((n.text or "").strip())
md.characters = ", ".join(char_list)
# Now extract the credit info
for n in root:
if any(
[
n.tag == "writer",
n.tag == "penciller",
n.tag == "inker",
n.tag == "colorist",
n.tag == "letterer",
n.tag == "editor",
]
):
metadata.add_credit((n.text or "").strip(), n.tag.title())
if n.tag == "coverDesigner":
metadata.add_credit((n.text or "").strip(), "Cover")
metadata.is_empty = False
return metadata
# verify that the string actually contains CoMet data in XML format
def validate_string(self, string: str) -> bool:
try:
tree = ET.ElementTree(ET.fromstring(string))
root = tree.getroot()
if root.tag != "comet":
return False
except ET.ParseError:
return False
return True
def write_to_external_file(self, filename: str, metadata: GenericMetadata) -> None:
tree = self.convert_metadata_to_xml(metadata)
tree.write(filename, encoding="utf-8")
def read_from_external_file(self, filename: str) -> GenericMetadata:
tree = ET.parse(filename)
return self.convert_xml_to_metadata(tree)

File diff suppressed because it is too large Load Diff

View File

@ -1,175 +0,0 @@
"""A class to encapsulate the ComicBookInfo data"""
# Copyright 2012-2014 Anthony Beville
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import json
import logging
from collections import defaultdict
from datetime import datetime
from typing import Any, Literal, TypedDict
from comicapi import utils
from comicapi.genericmetadata import GenericMetadata
logger = logging.getLogger(__name__)
CBILiteralType = Literal[
"series",
"title",
"issue",
"publisher",
"publicationMonth",
"publicationYear",
"numberOfIssues",
"comments",
"genre",
"volume",
"numberOfVolumes",
"language",
"country",
"rating",
"credits",
"tags",
]
class Credits(TypedDict):
person: str
role: str
primary: bool
class ComicBookInfoJson(TypedDict, total=False):
series: str
title: str
publisher: str
publicationMonth: int
publicationYear: int
issue: int
numberOfIssues: int
volume: int
numberOfVolumes: int
rating: int
genre: str
language: str
country: str
credits: list[Credits]
tags: list[str]
comments: str
CBIContainer = TypedDict("CBIContainer", {"appID": str, "lastModified": str, "ComicBookInfo/1.0": ComicBookInfoJson})
class ComicBookInfo:
def metadata_from_string(self, string: str) -> GenericMetadata:
cbi_container = json.loads(string)
metadata = GenericMetadata()
cbi = defaultdict(lambda: None, cbi_container["ComicBookInfo/1.0"])
metadata.series = utils.xlate(cbi["series"])
metadata.title = utils.xlate(cbi["title"])
metadata.issue = utils.xlate(cbi["issue"])
metadata.publisher = utils.xlate(cbi["publisher"])
metadata.month = utils.xlate(cbi["publicationMonth"], True)
metadata.year = utils.xlate(cbi["publicationYear"], True)
metadata.issue_count = utils.xlate(cbi["numberOfIssues"], True)
metadata.comments = utils.xlate(cbi["comments"])
metadata.genre = utils.xlate(cbi["genre"])
metadata.volume = utils.xlate(cbi["volume"], True)
metadata.volume_count = utils.xlate(cbi["numberOfVolumes"], True)
metadata.language = utils.xlate(cbi["language"])
metadata.country = utils.xlate(cbi["country"])
metadata.critical_rating = utils.xlate(cbi["rating"], True)
metadata.credits = [
Credits(
person=x["person"] if "person" in x else "",
role=x["role"] if "role" in x else "",
primary=x["primary"] if "primary" in x else False,
)
for x in cbi["credits"]
]
metadata.tags = set(cbi["tags"]) if cbi["tags"] is not None else set()
# make sure credits and tags are at least empty lists and not None
if metadata.credits is None:
metadata.credits = []
# need the language string to be ISO
if metadata.language:
metadata.language = utils.get_language_iso(metadata.language)
metadata.is_empty = False
return metadata
def string_from_metadata(self, metadata: GenericMetadata) -> str:
cbi_container = self.create_json_dictionary(metadata)
return json.dumps(cbi_container)
def validate_string(self, string: bytes | str) -> bool:
"""Verify that the string actually contains CBI data in JSON format"""
try:
cbi_container = json.loads(string)
except json.JSONDecodeError:
return False
return "ComicBookInfo/1.0" in cbi_container
def create_json_dictionary(self, metadata: GenericMetadata) -> CBIContainer:
"""Create the dictionary that we will convert to JSON text"""
cbi_container = CBIContainer(
{
"appID": "ComicTagger/1.0.0",
"lastModified": str(datetime.now()),
"ComicBookInfo/1.0": {},
}
) # TODO: ctversion.version,
# helper func
def assign(cbi_entry: CBILiteralType, md_entry: Any) -> None:
if md_entry is not None or isinstance(md_entry, str) and md_entry != "":
cbi_container["ComicBookInfo/1.0"][cbi_entry] = md_entry
assign("series", utils.xlate(metadata.series))
assign("title", utils.xlate(metadata.title))
assign("issue", utils.xlate(metadata.issue))
assign("publisher", utils.xlate(metadata.publisher))
assign("publicationMonth", utils.xlate(metadata.month, True))
assign("publicationYear", utils.xlate(metadata.year, True))
assign("numberOfIssues", utils.xlate(metadata.issue_count, True))
assign("comments", utils.xlate(metadata.comments))
assign("genre", utils.xlate(metadata.genre))
assign("volume", utils.xlate(metadata.volume, True))
assign("numberOfVolumes", utils.xlate(metadata.volume_count, True))
assign("language", utils.xlate(utils.get_language_from_iso(metadata.language)))
assign("country", utils.xlate(metadata.country))
assign("rating", utils.xlate(metadata.critical_rating, True))
assign("credits", metadata.credits)
assign("tags", list(metadata.tags))
return cbi_container
def write_to_external_file(self, filename: str, metadata: GenericMetadata) -> None:
cbi_container = self.create_json_dictionary(metadata)
with open(filename, "w", encoding="utf-8") as f:
f.write(json.dumps(cbi_container, indent=4))

View File

@ -1,276 +0,0 @@
"""A class to encapsulate ComicRack's ComicInfo.xml data"""
# Copyright 2012-2014 Anthony Beville
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import logging
import xml.etree.ElementTree as ET
from collections import OrderedDict
from typing import Any, cast
from xml.etree.ElementTree import ElementTree
from comicapi import utils
from comicapi.genericmetadata import GenericMetadata, ImageMetadata
from comicapi.issuestring import IssueString
logger = logging.getLogger(__name__)
class ComicInfoXml:
writer_synonyms = ["writer", "plotter", "scripter"]
penciller_synonyms = ["artist", "penciller", "penciler", "breakdowns"]
inker_synonyms = ["inker", "artist", "finishes"]
colorist_synonyms = ["colorist", "colourist", "colorer", "colourer"]
letterer_synonyms = ["letterer"]
cover_synonyms = ["cover", "covers", "coverartist", "cover artist"]
editor_synonyms = ["editor"]
def get_parseable_credits(self) -> list[str]:
parsable_credits = []
parsable_credits.extend(self.writer_synonyms)
parsable_credits.extend(self.penciller_synonyms)
parsable_credits.extend(self.inker_synonyms)
parsable_credits.extend(self.colorist_synonyms)
parsable_credits.extend(self.letterer_synonyms)
parsable_credits.extend(self.cover_synonyms)
parsable_credits.extend(self.editor_synonyms)
return parsable_credits
def metadata_from_string(self, string: bytes) -> GenericMetadata:
tree = ET.ElementTree(ET.fromstring(string))
return self.convert_xml_to_metadata(tree)
def string_from_metadata(self, metadata: GenericMetadata, xml: bytes = b"") -> str:
tree = self.convert_metadata_to_xml(metadata, xml)
tree_str = ET.tostring(tree.getroot(), encoding="utf-8", xml_declaration=True).decode("utf-8")
return str(tree_str)
def convert_metadata_to_xml(self, metadata: GenericMetadata, xml: bytes = b"") -> ElementTree:
# shorthand for the metadata
md = metadata
if xml:
root = ET.ElementTree(ET.fromstring(xml)).getroot()
else:
# build a tree structure
root = ET.Element("ComicInfo")
root.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
root.attrib["xmlns:xsd"] = "http://www.w3.org/2001/XMLSchema"
# helper func
def assign(cix_entry: str, md_entry: Any) -> None:
if md_entry is not None and md_entry:
et_entry = root.find(cix_entry)
if et_entry is not None:
et_entry.text = str(md_entry)
else:
ET.SubElement(root, cix_entry).text = str(md_entry)
else:
et_entry = root.find(cix_entry)
if et_entry is not None:
root.remove(et_entry)
assign("Title", md.title)
assign("Series", md.series)
assign("Number", md.issue)
assign("Count", md.issue_count)
assign("Volume", md.volume)
assign("AlternateSeries", md.alternate_series)
assign("AlternateNumber", md.alternate_number)
assign("StoryArc", md.story_arc)
assign("SeriesGroup", md.series_group)
assign("AlternateCount", md.alternate_count)
assign("Summary", md.comments)
assign("Notes", md.notes)
assign("Year", md.year)
assign("Month", md.month)
assign("Day", md.day)
# need to specially process the credits, since they are structured
# differently than CIX
credit_writer_list = []
credit_penciller_list = []
credit_inker_list = []
credit_colorist_list = []
credit_letterer_list = []
credit_cover_list = []
credit_editor_list = []
# first, loop thru credits, and build a list for each role that CIX
# supports
for credit in metadata.credits:
if credit["role"].casefold() in set(self.writer_synonyms):
credit_writer_list.append(credit["person"].replace(",", ""))
if credit["role"].casefold() in set(self.penciller_synonyms):
credit_penciller_list.append(credit["person"].replace(",", ""))
if credit["role"].casefold() in set(self.inker_synonyms):
credit_inker_list.append(credit["person"].replace(",", ""))
if credit["role"].casefold() in set(self.colorist_synonyms):
credit_colorist_list.append(credit["person"].replace(",", ""))
if credit["role"].casefold() in set(self.letterer_synonyms):
credit_letterer_list.append(credit["person"].replace(",", ""))
if credit["role"].casefold() in set(self.cover_synonyms):
credit_cover_list.append(credit["person"].replace(",", ""))
if credit["role"].casefold() in set(self.editor_synonyms):
credit_editor_list.append(credit["person"].replace(",", ""))
# second, convert each list to string, and add to XML struct
assign("Writer", ", ".join(credit_writer_list))
assign("Penciller", ", ".join(credit_penciller_list))
assign("Inker", ", ".join(credit_inker_list))
assign("Colorist", ", ".join(credit_colorist_list))
assign("Letterer", ", ".join(credit_letterer_list))
assign("CoverArtist", ", ".join(credit_cover_list))
assign("Editor", ", ".join(credit_editor_list))
assign("Publisher", md.publisher)
assign("Imprint", md.imprint)
assign("Genre", md.genre)
assign("Web", md.web_link)
assign("PageCount", md.page_count)
assign("LanguageISO", md.language)
assign("Format", md.format)
assign("AgeRating", md.maturity_rating)
assign("CommunityRating", md.critical_rating)
assign("BlackAndWhite", "Yes" if md.black_and_white else None)
assign("Manga", md.manga)
assign("Characters", md.characters)
assign("Teams", md.teams)
assign("Locations", md.locations)
assign("ScanInformation", md.scan_info)
# loop and add the page entries under pages node
pages_node = root.find("Pages")
if pages_node is not None:
pages_node.clear()
else:
pages_node = ET.SubElement(root, "Pages")
for page_dict in md.pages:
page_node = ET.SubElement(pages_node, "Page")
page_node.attrib = OrderedDict(sorted((k, str(v)) for k, v in page_dict.items()))
ET.indent(root)
# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)
return tree
def convert_xml_to_metadata(self, tree: ElementTree) -> GenericMetadata:
root = tree.getroot()
if root.tag != "ComicInfo":
raise Exception("Not a ComicInfo file")
def get(name: str) -> str | None:
tag = root.find(name)
if tag is None:
return None
return tag.text
md = GenericMetadata()
md.series = utils.xlate(get("Series"))
md.title = utils.xlate(get("Title"))
md.issue = IssueString(utils.xlate(get("Number"))).as_string()
md.issue_count = utils.xlate(get("Count"), True)
md.volume = utils.xlate(get("Volume"), True)
md.alternate_series = utils.xlate(get("AlternateSeries"))
md.alternate_number = IssueString(utils.xlate(get("AlternateNumber"))).as_string()
md.alternate_count = utils.xlate(get("AlternateCount"), True)
md.comments = utils.xlate(get("Summary"))
md.notes = utils.xlate(get("Notes"))
md.year = utils.xlate(get("Year"), True)
md.month = utils.xlate(get("Month"), True)
md.day = utils.xlate(get("Day"), True)
md.publisher = utils.xlate(get("Publisher"))
md.imprint = utils.xlate(get("Imprint"))
md.genre = utils.xlate(get("Genre"))
md.web_link = utils.xlate(get("Web"))
md.language = utils.xlate(get("LanguageISO"))
md.format = utils.xlate(get("Format"))
md.manga = utils.xlate(get("Manga"))
md.characters = utils.xlate(get("Characters"))
md.teams = utils.xlate(get("Teams"))
md.locations = utils.xlate(get("Locations"))
md.page_count = utils.xlate(get("PageCount"), True)
md.scan_info = utils.xlate(get("ScanInformation"))
md.story_arc = utils.xlate(get("StoryArc"))
md.series_group = utils.xlate(get("SeriesGroup"))
md.maturity_rating = utils.xlate(get("AgeRating"))
md.critical_rating = utils.xlate(get("CommunityRating"), is_float=True)
tmp = utils.xlate(get("BlackAndWhite"))
if tmp is not None and tmp.casefold() in ["yes", "true", "1"]:
md.black_and_white = True
# Now extract the credit info
for n in root:
if any(
[
n.tag == "Writer",
n.tag == "Penciller",
n.tag == "Inker",
n.tag == "Colorist",
n.tag == "Letterer",
n.tag == "Editor",
]
):
if n.text is not None:
for name in n.text.split(","):
md.add_credit(name.strip(), n.tag)
if n.tag == "CoverArtist":
if n.text is not None:
for name in n.text.split(","):
md.add_credit(name.strip(), "Cover")
# parse page data now
pages_node = root.find("Pages")
if pages_node is not None:
for page in pages_node:
p: dict[str, Any] = page.attrib
if "Image" in p:
p["Image"] = int(p["Image"])
if "DoublePage" in p:
p["DoublePage"] = True if p["DoublePage"].casefold() in ("yes", "true", "1") else False
md.pages.append(cast(ImageMetadata, p))
md.is_empty = False
return md
def write_to_external_file(self, filename: str, metadata: GenericMetadata, xml: bytes = b"") -> None:
tree = self.convert_metadata_to_xml(metadata, xml)
tree.write(filename, encoding="utf-8", xml_declaration=True)
def read_from_external_file(self, filename: str) -> GenericMetadata:
tree = ET.parse(filename)
return self.convert_xml_to_metadata(tree)

View File

@ -1,5 +1,5 @@
from __future__ import annotations
import pathlib
import importlib.resources
data_path = pathlib.Path(__file__).parent
data_path = importlib.resources.files(__package__)

View File

@ -126,5 +126,18 @@
"radio comics": "Mighty Comics Group",
"red circle Comics": "Dark Circle Comics",
"red circle": "Dark Circle Comics"
},
"Image Comics": {
"Image": "",
"avalon studios": "Avalon Studios",
"desperado publishing": "Desperado Publishing",
"extreme studios": "Extreme Studios",
"gorilla comics": "Gorilla Comics",
"highbrow entertainment": "Highbrow Entertainment",
"shadowline": "Shadowline",
"skybound entertainment": "Skybound Entertainment",
"todd mcfarlane productions": "Todd McFarlane Productions",
"top cow productions": "Top Cow Productions"
}
}

View File

@ -6,7 +6,8 @@ import calendar
import os
import unicodedata
from enum import Enum, auto
from typing import Any, Callable
from itertools import chain
from typing import Any, Callable, Protocol
class ItemType(Enum):
@ -30,10 +31,10 @@ class ItemType(Enum):
InfoSpecifier = auto() # Specifies type of info e.g. v1 for 'volume': 1
ArchiveType = auto()
Honorific = auto()
Publisher = auto()
Keywords = auto()
FCBD = auto()
ComicType = auto()
Publisher = auto()
C2C = auto()
@ -60,7 +61,6 @@ key = {
"tar": ItemType.ArchiveType,
"7z": ItemType.ArchiveType,
"annual": ItemType.ComicType,
"book": ItemType.ComicType,
"volume": ItemType.InfoSpecifier,
"vol.": ItemType.InfoSpecifier,
"vol": ItemType.InfoSpecifier,
@ -82,15 +82,21 @@ class Item:
self.typ: ItemType = typ
self.pos: int = pos
self.val: str = val
self.no_space = False
def __repr__(self) -> str:
return f"{self.val}: index: {self.pos}: {self.typ}"
class LexerFunc(Protocol):
def __call__(self, __origin: Lexer) -> LexerFunc | None: ...
class Lexer:
def __init__(self, string: str) -> None:
def __init__(self, string: str, allow_issue_start_with_letter: bool = False) -> None:
self.input: str = string # The string being scanned
self.state: Callable[[Lexer], Callable | None] | None = None # The next lexing function to enter
# The next lexing function to enter
self.state: LexerFunc | None = None
self.pos: int = -1 # Current position in the input
self.start: int = 0 # Start position of this item
self.lastPos: int = 0 # Position of most recent item returned by nextItem
@ -98,6 +104,7 @@ class Lexer:
self.brace_depth: int = 0 # Nesting depth of { }
self.sbrace_depth: int = 0 # Nesting depth of [ ]
self.items: list[Item] = []
self.allow_issue_start_with_letter = allow_issue_start_with_letter
# Next returns the next rune in the input.
def get(self) -> str:
@ -128,38 +135,38 @@ class Lexer:
self.start = self.pos
# Accept consumes the next rune if it's from the valid se:
def accept(self, valid: str) -> bool:
if self.get() in valid:
return True
def accept(self, valid: str | Callable[[str], bool]) -> bool:
if isinstance(valid, str):
if self.get() in valid:
return True
else:
if valid(self.get()):
return True
self.backup()
return False
# AcceptRun consumes a run of runes from the valid set.
def accept_run(self, valid: str) -> None:
while self.get() in valid:
continue
def accept_run(self, valid: str | Callable[[str], bool]) -> bool:
initial = self.pos
if isinstance(valid, str):
while self.get() in valid:
continue
else:
while valid(self.get()):
continue
self.backup()
return initial != self.pos
def scan_number(self) -> bool:
digits = "0123456789"
digits = "0123456789.,"
self.accept_run(digits)
if self.accept("."):
if self.accept(digits):
self.accept_run(digits)
else:
self.backup()
if self.accept("s"):
if not self.accept("t"):
self.backup()
elif self.accept("nr"):
if not self.accept("d"):
self.backup()
elif self.accept("t"):
if not self.accept("h"):
self.backup()
if not self.accept_run(lambda x: x.isnumeric() or x in digits):
return False
if self.input[self.pos] == ".":
self.backup()
self.accept_run(str.isalpha)
return True
@ -172,20 +179,22 @@ class Lexer:
# Errorf returns an error token and terminates the scan by passing
# Back a nil pointer that will be the next state, terminating self.nextItem.
def errorf(lex: Lexer, message: str) -> Callable[[Lexer], Callable | None] | None:
def errorf(lex: Lexer, message: str) -> Any:
lex.items.append(Item(ItemType.Error, lex.start, message))
return None
# Scans the elements inside action delimiters.
def lex_filename(lex: Lexer) -> Callable[[Lexer], Callable | None] | None:
def lex_filename(lex: Lexer) -> LexerFunc | None:
r = lex.get()
if r == eof:
if lex.paren_depth != 0:
return errorf(lex, "unclosed left paren")
errorf(lex, "unclosed left paren")
return None
if lex.brace_depth != 0:
return errorf(lex, "unclosed left paren")
errorf(lex, "unclosed left paren")
return None
lex.emit(ItemType.EOF)
return None
elif is_space(r):
@ -196,23 +205,27 @@ def lex_filename(lex: Lexer) -> Callable[[Lexer], Callable | None] | None:
return lex_space
elif r == ".":
r = lex.peek()
if r < "0" or "9" < r:
lex.emit(ItemType.Dot)
return lex_filename
lex.backup()
return lex_number
if r.isnumeric() and lex.pos > 0 and is_space(lex.input[lex.pos - 1]):
return lex_number
lex.emit(ItemType.Dot)
return lex_filename
elif r == "'":
r = lex.peek()
if r in "0123456789":
if r.isdigit():
return lex_number
lex.emit(ItemType.Text) # TODO: Change to Text
elif "0" <= r <= "9":
if is_symbol(r):
lex.accept_run(is_symbol)
lex.emit(ItemType.Symbol)
else:
return lex_text
elif r.isnumeric():
lex.backup()
return lex_number
elif r == "#":
if "0" <= lex.peek() <= "9":
return lex_number
if lex.allow_issue_start_with_letter and is_alpha_numeric(lex.peek()):
return lex_issue_number
elif lex.peek().isnumeric() or lex.peek() in "-+.":
return lex_issue_number
lex.emit(ItemType.Symbol)
elif is_operator(r):
if r == "-" and lex.peek() == "-":
@ -230,7 +243,8 @@ def lex_filename(lex: Lexer) -> Callable[[Lexer], Callable | None] | None:
lex.emit(ItemType.RightParen)
lex.paren_depth -= 1
if lex.paren_depth < 0:
return errorf(lex, "unexpected right paren " + r)
errorf(lex, "unexpected right paren " + r)
return None
elif r == "{":
lex.emit(ItemType.LeftBrace)
@ -239,7 +253,8 @@ def lex_filename(lex: Lexer) -> Callable[[Lexer], Callable | None] | None:
lex.emit(ItemType.RightBrace)
lex.brace_depth -= 1
if lex.brace_depth < 0:
return errorf(lex, "unexpected right brace " + r)
errorf(lex, "unexpected right brace " + r)
return None
elif r == "[":
lex.emit(ItemType.LeftSBrace)
@ -248,16 +263,33 @@ def lex_filename(lex: Lexer) -> Callable[[Lexer], Callable | None] | None:
lex.emit(ItemType.RightSBrace)
lex.sbrace_depth -= 1
if lex.sbrace_depth < 0:
return errorf(lex, "unexpected right brace " + r)
errorf(lex, "unexpected right brace " + r)
return None
elif is_symbol(r):
if unicodedata.category(r) == "Sc":
return lex_currency
lex.accept_run(is_symbol)
lex.emit(ItemType.Symbol)
else:
return errorf(lex, "unrecognized character in action: " + r)
errorf(lex, "unrecognized character in action: " + repr(r))
return None
return lex_filename
def lex_operator(lex: Lexer) -> Callable:
def lex_currency(lex: Lexer) -> LexerFunc:
orig = lex.pos
lex.accept_run(is_space)
if lex.peek().isnumeric():
return lex_number
else:
lex.pos = orig
# We don't have a number with this currency symbol. Don't treat it special
lex.emit(ItemType.Symbol)
return lex_filename
def lex_operator(lex: Lexer) -> LexerFunc:
lex.accept_run("-|:;")
lex.emit(ItemType.Operator)
return lex_filename
@ -265,36 +297,31 @@ def lex_operator(lex: Lexer) -> Callable:
# LexSpace scans a run of space characters.
# One space has already been seen.
def lex_space(lex: Lexer) -> Callable:
while is_space(lex.peek()):
lex.get()
def lex_space(lex: Lexer) -> LexerFunc:
lex.accept_run(is_space)
lex.emit(ItemType.Space)
return lex_filename
# Lex_text scans an alphanumeric.
def lex_text(lex: Lexer) -> Callable:
def lex_text(lex: Lexer) -> LexerFunc:
while True:
r = lex.get()
if is_alpha_numeric(r):
if is_alpha_numeric(r) or r in "'":
if r.isnumeric(): # E.g. v1
word = lex.input[lex.start : lex.pos]
if word.casefold() in key and key[word.casefold()] == ItemType.InfoSpecifier:
if key.get(word.casefold(), None) == ItemType.InfoSpecifier:
lex.backup()
lex.emit(key[word.casefold()])
return lex_filename
else:
if r == "'" and lex.peek() == "s":
lex.get()
else:
lex.backup()
word = lex.input[lex.start : lex.pos + 1]
if word.casefold() == "vol" and lex.peek() == ".":
lex.get()
lex.backup()
word = lex.input[lex.start : lex.pos + 1]
if word.casefold() in key:
if key[word.casefold()] in (ItemType.Honorific, ItemType.InfoSpecifier):
lex.accept(".")
lex.emit(key[word.casefold()])
elif cal(word):
lex.emit(ItemType.Calendar)
@ -305,26 +332,66 @@ def lex_text(lex: Lexer) -> Callable:
return lex_filename
def cal(value: str) -> set[Any]:
month_abbr = [i for i, x in enumerate(calendar.month_abbr) if x == value.title()]
month_name = [i for i, x in enumerate(calendar.month_name) if x == value.title()]
day_abbr = [i for i, x in enumerate(calendar.day_abbr) if x == value.title()]
day_name = [i for i, x in enumerate(calendar.day_name) if x == value.title()]
return set(month_abbr + month_name + day_abbr + day_name)
def cal(value: str) -> bool:
return value.title() in set(chain(calendar.month_abbr, calendar.month_name, calendar.day_abbr, calendar.day_name))
def lex_number(lex: Lexer) -> Callable[[Lexer], Callable | None] | None:
def lex_number(lex: Lexer) -> LexerFunc | None:
if not lex.scan_number():
return errorf(lex, "bad number syntax: " + lex.input[lex.start : lex.pos])
# Complex number logic removed. Messes with math operations without space
if lex.input[lex.start] == "#":
lex.emit(ItemType.IssueNumber)
elif not lex.input[lex.pos].isdigit():
elif not lex.input[lex.pos].isnumeric():
# Assume that 80th is just text and not a number
lex.emit(ItemType.Text)
else:
lex.emit(ItemType.Number)
# Used to check for a '$'
endNumber = lex.pos
# Consume any spaces
lex.accept_run(is_space)
# This number starts with a '$' emit it as Text instead of a Number
if "Sc" == unicodedata.category(lex.input[lex.start]):
lex.pos = endNumber
lex.emit(ItemType.Text)
# This number ends in a '$' if there is a number on the other side we assume it belongs to the following number
elif "Sc" == unicodedata.category(lex.get()):
# Store the end of the number '$'. We still need to check to see if there is a number coming up
endCurrency = lex.pos
# Consume any spaces
lex.accept_run(is_space)
# This is a number
if lex.peek().isnumeric():
# We go back to the original number before the '$' and emit a number
lex.pos = endNumber
lex.emit(ItemType.Number)
else:
# There was no following number, reset to the '$' and emit a number
lex.pos = endCurrency
lex.emit(ItemType.Text)
else:
# We go back to the original number there is no '$'
lex.pos = endNumber
lex.emit(ItemType.Number)
return lex_filename
def lex_issue_number(lex: Lexer) -> Callable[[Lexer], Callable | None] | None: # type: ignore[type-arg]
# Only called when lex.input[lex.start] == "#"
original_start = lex.pos
lex.accept_run(str.isalpha)
if lex.peek().isnumeric():
return lex_number
else:
lex.pos = original_start
lex.emit(ItemType.Symbol)
return lex_filename
@ -343,10 +410,10 @@ def is_operator(character: str) -> bool:
def is_symbol(character: str) -> bool:
return unicodedata.category(character)[0] in "PS"
return unicodedata.category(character)[0] in "PS" and character != "."
def Lex(filename: str) -> Lexer:
lex = Lexer(string=os.path.basename(filename))
def Lex(filename: str, allow_issue_start_with_letter: bool = False) -> Lexer:
lex = Lexer(os.path.basename(filename), allow_issue_start_with_letter)
lex.run()
return lex

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,8 @@ tagging schemes and databases, such as ComicVine or GCD. This makes conversion
possible, however lossy it might be
"""
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -23,15 +24,29 @@ from __future__ import annotations
import copy
import dataclasses
import logging
from typing import Any, TypedDict
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, Union, overload
from comicapi import utils
from typing_extensions import NamedTuple
from comicapi import merge, utils
from comicapi._url import Url, parse_url
from comicapi.utils import norm_fold
# needed for runtime type guessing
if TYPE_CHECKING:
Union
logger = logging.getLogger(__name__)
class PageType:
REMOVE = object()
Credit = merge.Credit
class PageType(merge.StrEnum):
"""
These page info classes are exactly the same as the CIX scheme, since
it's unique
@ -50,85 +65,147 @@ class PageType:
Deleted = "Deleted"
class ImageMetadata(TypedDict, total=False):
Type: str
Bookmark: str
DoublePage: bool
Image: int
ImageSize: str
ImageHeight: str
ImageWidth: str
@dataclasses.dataclass
class PageMetadata:
filename: str
type: str
bookmark: str
display_index: int
archive_index: int
# These are optional because getting this info requires reading in each page
double_page: bool | None = None
byte_size: int | None = None
height: int | None = None
width: int | None = None
def set_type(self, value: str) -> None:
values = {x.casefold(): x for x in PageType}
self.type = values.get(value.casefold(), value)
def is_double_page(self) -> bool:
w = self.width or 0
h = self.height or 0
return self.double_page or (w >= h and w > 0 and h > 0)
def __lt__(self, other: Any) -> bool:
if not isinstance(other, PageMetadata):
return False
return self.archive_index < other.archive_index
def __eq__(self, other: Any) -> bool:
if not isinstance(other, PageMetadata):
return False
return self.archive_index == other.archive_index
def _get_clean_metadata(self, *attributes: str) -> PageMetadata:
return PageMetadata(
filename=self.filename if "filename" in attributes else "",
type=self.type if "type" in attributes else "",
bookmark=self.bookmark if "bookmark" in attributes else "",
display_index=self.display_index if "display_index" in attributes else 0,
archive_index=self.archive_index if "archive_index" in attributes else 0,
double_page=self.double_page if "double_page" in attributes else None,
byte_size=self.byte_size if "byte_size" in attributes else None,
height=self.height if "height" in attributes else None,
width=self.width if "width" in attributes else None,
)
class CreditMetadata(TypedDict):
person: str
role: str
primary: bool
@dataclasses.dataclass
class ComicSeries:
id: str
name: str
aliases: set[str]
count_of_issues: int | None
count_of_volumes: int | None
description: str
image_url: str
publisher: str
start_year: int | None
format: str | None
def copy(self) -> ComicSeries:
return copy.deepcopy(self)
class MetadataOrigin(NamedTuple):
id: str
name: str
def __str__(self) -> str:
return self.name
@dataclasses.dataclass
class GenericMetadata:
writer_synonyms = ["writer", "plotter", "scripter"]
penciller_synonyms = ["artist", "penciller", "penciler", "breakdowns"]
inker_synonyms = ["inker", "artist", "finishes"]
colorist_synonyms = ["colorist", "colourist", "colorer", "colourer"]
letterer_synonyms = ["letterer"]
cover_synonyms = ["cover", "covers", "coverartist", "cover artist"]
editor_synonyms = ["editor"]
writer_synonyms = ("writer", "plotter", "scripter", "script")
penciller_synonyms = ("artist", "penciller", "penciler", "breakdowns", "pencils", "painting")
inker_synonyms = ("inker", "artist", "finishes", "inks", "painting")
colorist_synonyms = ("colorist", "colourist", "colorer", "colourer", "colors", "painting")
letterer_synonyms = ("letterer", "letters")
cover_synonyms = ("cover", "covers", "coverartist", "cover artist")
editor_synonyms = ("editor", "edits", "editing")
translator_synonyms = ("translator", "translation")
is_empty: bool = True
tag_origin: str | None = None
data_origin: MetadataOrigin | None = None
issue_id: str | None = None
series_id: str | None = None
series: str | None = None
series_aliases: set[str] = dataclasses.field(default_factory=set)
issue: str | None = None
title: str | None = None
publisher: str | None = None
month: int | None = None
year: int | None = None
day: int | None = None
issue_count: int | None = None
title: str | None = None
title_aliases: set[str] = dataclasses.field(default_factory=set)
volume: int | None = None
genre: str | None = None
language: str | None = None # 2 letter iso code
comments: str | None = None # use same way as Summary in CIX
volume_count: int | None = None
critical_rating: float | None = None # rating in CBL; CommunityRating in CIX
country: str | None = None
genres: set[str] = dataclasses.field(default_factory=set)
description: str | None = None # use same way as Summary in CIX
notes: str | None = None
alternate_series: str | None = None
alternate_number: str | None = None
alternate_count: int | None = None
story_arcs: list[str] = dataclasses.field(default_factory=list)
series_groups: list[str] = dataclasses.field(default_factory=list)
publisher: str | None = None
imprint: str | None = None
notes: str | None = None
web_link: str | None = None
day: int | None = None
month: int | None = None
year: int | None = None
language: str | None = None # 2 letter iso code
country: str | None = None
web_links: list[Url] = dataclasses.field(default_factory=list)
format: str | None = None
manga: str | None = None
black_and_white: bool | None = None
page_count: int | None = None
maturity_rating: str | None = None
story_arc: str | None = None
series_group: str | None = None
critical_rating: float | None = None # rating in CBL; CommunityRating in CIX
scan_info: str | None = None
characters: str | None = None
teams: str | None = None
locations: str | None = None
credits: list[CreditMetadata] = dataclasses.field(default_factory=list)
tags: set[str] = dataclasses.field(default_factory=set)
pages: list[ImageMetadata] = dataclasses.field(default_factory=list)
pages: list[PageMetadata] = dataclasses.field(default_factory=list)
page_count: int | None = None
characters: set[str] = dataclasses.field(default_factory=set)
teams: set[str] = dataclasses.field(default_factory=set)
locations: set[str] = dataclasses.field(default_factory=set)
credits: list[Credit] = dataclasses.field(default_factory=list)
# Some CoMet-only items
price: str | None = None
price: float | None = None
is_version_of: str | None = None
rights: str | None = None
identifier: str | None = None
last_mark: str | None = None
cover_image: str | None = None
def __post_init__(self):
# urls to cover image, not generally part of the metadata
_cover_image: str | None = None
_alternate_images: list[str] = dataclasses.field(default_factory=list)
def __post_init__(self) -> None:
for key, value in self.__dict__.items():
if value and key != "is_empty":
self.is_empty = False
@ -142,125 +219,208 @@ class GenericMetadata:
tmp.__dict__.update(kwargs)
return tmp
def overlay(self, new_md: GenericMetadata) -> None:
"""Overlay a metadata object on this one
def _get_clean_metadata(self, *attributes: str) -> GenericMetadata:
new_md = GenericMetadata()
list_handled = []
for attr in sorted(attributes):
if "." in attr:
lst, _, name = attr.partition(".")
if lst in list_handled:
continue
old_value = getattr(self, lst)
new_value = getattr(new_md, lst)
if old_value:
if hasattr(old_value[0], "_get_clean_metadata"):
list_attributes = [x.removeprefix(lst + ".") for x in attributes if x.startswith(lst)]
for x in old_value:
new_value.append(x._get_clean_metadata(*list_attributes))
list_handled.append(lst)
continue
if not new_value:
for x in old_value:
new_value.append(x.__class__())
for i, x in enumerate(old_value):
if isinstance(x, dict):
if name in x:
new_value[i][name] = x[name]
else:
setattr(new_value[i], name, getattr(x, name))
That is, when the new object has non-None values, over-write them
to this one.
"""
else:
old_value = getattr(self, attr)
if isinstance(old_value, list):
continue
setattr(new_md, attr, old_value)
def assign(cur: str, new: Any) -> None:
if new is not None:
if isinstance(new, str) and len(new) == 0:
setattr(self, cur, None)
else:
setattr(self, cur, new)
new_md.__post_init__()
return new_md
def overlay(
self, new_md: GenericMetadata, mode: merge.Mode = merge.Mode.OVERLAY, merge_lists: bool = False
) -> None:
"""Overlay a new metadata object on this one"""
attribute_merge = merge.attribute[mode]
list_merge = merge.lists[mode]
def assign(old: Any, new: Any, attribute_merge: Any = attribute_merge) -> Any:
if new is REMOVE:
return None
return attribute_merge(old, new)
def assign_list(old: list[Any] | set[Any], new: list[Any] | set[Any], list_merge: Any = list_merge) -> Any:
if new is REMOVE:
old.clear()
return old
if merge_lists:
return list_merge(old, new)
else:
return assign(old, new)
if not new_md.is_empty:
self.is_empty = False
assign("series", new_md.series)
assign("issue", new_md.issue)
assign("issue_count", new_md.issue_count)
assign("title", new_md.title)
assign("publisher", new_md.publisher)
assign("day", new_md.day)
assign("month", new_md.month)
assign("year", new_md.year)
assign("volume", new_md.volume)
assign("volume_count", new_md.volume_count)
assign("genre", new_md.genre)
assign("language", new_md.language)
assign("country", new_md.country)
assign("critical_rating", new_md.critical_rating)
assign("alternate_series", new_md.alternate_series)
assign("alternate_number", new_md.alternate_number)
assign("alternate_count", new_md.alternate_count)
assign("imprint", new_md.imprint)
assign("web_link", new_md.web_link)
assign("format", new_md.format)
assign("manga", new_md.manga)
assign("black_and_white", new_md.black_and_white)
assign("maturity_rating", new_md.maturity_rating)
assign("story_arc", new_md.story_arc)
assign("series_group", new_md.series_group)
assign("scan_info", new_md.scan_info)
assign("characters", new_md.characters)
assign("teams", new_md.teams)
assign("locations", new_md.locations)
assign("comments", new_md.comments)
assign("notes", new_md.notes)
self.data_origin = assign(self.data_origin, new_md.data_origin) # TODO use and purpose now?
self.issue_id = assign(self.issue_id, new_md.issue_id)
self.series_id = assign(self.series_id, new_md.series_id)
assign("price", new_md.price)
assign("is_version_of", new_md.is_version_of)
assign("rights", new_md.rights)
assign("identifier", new_md.identifier)
assign("last_mark", new_md.last_mark)
self.series = assign(self.series, new_md.series)
self.overlay_credits(new_md.credits)
# TODO
self.series_aliases = assign_list(self.series_aliases, new_md.series_aliases)
self.issue = assign(self.issue, new_md.issue)
self.issue_count = assign(self.issue_count, new_md.issue_count)
self.title = assign(self.title, new_md.title)
self.title_aliases = assign_list(self.title_aliases, new_md.title_aliases)
self.volume = assign(self.volume, new_md.volume)
self.volume_count = assign(self.volume_count, new_md.volume_count)
self.genres = assign_list(self.genres, new_md.genres)
self.description = assign(self.description, new_md.description)
self.notes = assign(self.notes, new_md.notes)
# not sure if the tags and pages should broken down, or treated
# as whole lists....
self.alternate_series = assign(self.alternate_series, new_md.alternate_series)
self.alternate_number = assign(self.alternate_number, new_md.alternate_number)
self.alternate_count = assign(self.alternate_count, new_md.alternate_count)
self.story_arcs = assign_list(self.story_arcs, new_md.story_arcs)
self.series_groups = assign_list(self.series_groups, new_md.series_groups)
# For now, go the easy route, where any overlay
# value wipes out the whole list
if len(new_md.tags) > 0:
assign("tags", new_md.tags)
self.publisher = assign(self.publisher, new_md.publisher)
self.imprint = assign(self.imprint, new_md.imprint)
self.day = assign(self.day, new_md.day)
self.month = assign(self.month, new_md.month)
self.year = assign(self.year, new_md.year)
self.language = assign(self.language, new_md.language)
self.country = assign(self.country, new_md.country)
self.web_links = assign_list(self.web_links, new_md.web_links)
self.format = assign(self.format, new_md.format)
self.manga = assign(self.manga, new_md.manga)
self.black_and_white = assign(self.black_and_white, new_md.black_and_white)
self.maturity_rating = assign(self.maturity_rating, new_md.maturity_rating)
self.critical_rating = assign(self.critical_rating, new_md.critical_rating)
self.scan_info = assign(self.scan_info, new_md.scan_info)
if len(new_md.pages) > 0:
assign("pages", new_md.pages)
self.tags = assign_list(self.tags, new_md.tags)
def overlay_credits(self, new_credits: list[CreditMetadata]) -> None:
for c in new_credits:
primary = bool("primary" in c and c["primary"])
self.characters = assign_list(self.characters, new_md.characters)
self.teams = assign_list(self.teams, new_md.teams)
self.locations = assign_list(self.locations, new_md.locations)
# Remove credit role if person is blank
if c["person"] == "":
for r in reversed(self.credits):
if r["role"].casefold() == c["role"].casefold():
self.credits.remove(r)
# otherwise, add it!
else:
self.add_credit(c["person"], c["role"], primary)
# credits are added through add_credit so that some standard checks are observed
# which means that we needs self.credits to be empty
tmp_credits = self.credits
self.credits = []
for c in assign_list(tmp_credits, new_md.credits):
self.add_credit(c)
def set_default_page_list(self, count: int) -> None:
# generate a default page list, with the first page marked as the cover
for i in range(count):
page_dict = ImageMetadata(Image=i)
if i == 0:
page_dict["Type"] = PageType.FrontCover
self.pages.append(page_dict)
self.price = assign(self.price, new_md.price)
self.is_version_of = assign(self.is_version_of, new_md.is_version_of)
self.rights = assign(self.rights, new_md.rights)
self.identifier = assign(self.identifier, new_md.identifier)
self.last_mark = assign(self.last_mark, new_md.last_mark)
self._cover_image = assign(self._cover_image, new_md._cover_image)
self._alternate_images = assign_list(self._alternate_images, new_md._alternate_images)
# pages doesn't get merged, if we did merge we would end up with duplicate pages
self.pages = assign(self.pages, new_md.pages)
self.page_count = assign(self.page_count, new_md.page_count)
def apply_default_page_list(self, page_list: Sequence[str]) -> None:
"""apply a default page list, with the first page marked as the cover"""
# Create a dictionary in the weird case that the metadata doesn't match the archive
pages = {p.archive_index: p for p in self.pages}
cover_set = False
# It might be a good idea to validate that each page in `pages` is found in page_list
for i, filename in enumerate(page_list):
page = pages.get(i, PageMetadata(archive_index=i, display_index=i, filename="", type="", bookmark=""))
page.filename = filename
pages[i] = page
# Check if we know what the cover is
cover_set = page.type == PageType.FrontCover or cover_set
self.pages = sorted(pages.values())
self.page_count = len(self.pages)
if self.page_count != len(page_list):
logger.warning("Wrong count of pages: expected %d got %d", len(self.pages), len(page_list))
# Set the cover to the first image acording to hte display index if we don't know what the cover is
if not cover_set:
first_page = self.get_archive_page_index(0)
self.pages[first_page].type = PageType.FrontCover
def get_archive_page_index(self, pagenum: int) -> int:
# convert the displayed page number to the page index of the file in the archive
"""convert the displayed page number to the page index of the file in the archive"""
if pagenum < len(self.pages):
return int(self.pages[pagenum]["Image"])
return int(sorted(self.pages, key=lambda p: p.display_index)[pagenum].archive_index)
return 0
def get_cover_page_index_list(self) -> list[int]:
# return a list of archive page indices of cover pages
if not self.pages:
return [0]
coverlist = []
for p in self.pages:
if "Type" in p and p["Type"] == PageType.FrontCover:
coverlist.append(int(p["Image"]))
if p.type == PageType.FrontCover:
coverlist.append(p.archive_index)
if len(coverlist) == 0:
coverlist.append(0)
coverlist.append(self.get_archive_page_index(0))
return coverlist
def add_credit(self, person: str, role: str, primary: bool = False) -> None:
@overload
def add_credit(self, person: Credit) -> None: ...
credit = CreditMetadata(person=person, role=role, primary=primary)
@overload
def add_credit(self, person: str, role: str, primary: bool = False, language: str = "") -> None: ...
def add_credit(
self, person: str | Credit, role: str | None = None, primary: bool = False, language: str = ""
) -> None:
credit: Credit
if isinstance(person, Credit):
credit = person
else:
assert role is not None
credit = Credit(person=person, role=role, primary=primary, language=language)
if credit.role is None:
raise TypeError("GenericMetadata.add_credit takes either a Credit object or a person name and role")
if credit.person == "":
return
person = norm_fold(credit.person)
role = norm_fold(credit.role)
# look to see if it's not already there...
found = False
for c in self.credits:
if c["person"].casefold() == person.casefold() and c["role"].casefold() == role.casefold():
if norm_fold(c.person) == person and norm_fold(c.role) == role:
# no need to add it. just adjust the "primary" flag as needed
c["primary"] = primary
c.primary = c.primary or primary
found = True
break
@ -270,12 +430,10 @@ class GenericMetadata:
def get_primary_credit(self, role: str) -> str:
primary = ""
for credit in self.credits:
if "role" not in credit or "person" not in credit:
continue
if (primary == "" and credit["role"].casefold() == role.casefold()) or (
credit["role"].casefold() == role.casefold() and "primary" in credit and credit["primary"]
if (primary == "" and credit.role.casefold() == role.casefold()) or (
credit.role.casefold() == role.casefold() and credit.primary
):
primary = credit["person"]
primary = credit.person
return primary
def __str__(self) -> str:
@ -284,59 +442,62 @@ class GenericMetadata:
return "No metadata"
def add_string(tag: str, val: Any) -> None:
if val is not None and str(val) != "":
if isinstance(val, (Sequence, set)):
if val:
vals.append((tag, val))
elif val is not None:
vals.append((tag, val))
def add_attr_string(tag: str) -> None:
add_string(tag, getattr(self, tag))
add_string("data_origin", self.data_origin)
add_string("series", self.series)
add_string("series_aliases", ",".join(self.series_aliases))
add_string("issue", self.issue)
add_string("issue_count", self.issue_count)
add_string("title", self.title)
add_string("title_aliases", ",".join(self.title_aliases))
add_string("publisher", self.publisher)
add_string("year", self.year)
add_string("month", self.month)
add_string("day", self.day)
add_string("volume", self.volume)
add_string("volume_count", self.volume_count)
add_string("genres", ", ".join(self.genres))
add_string("language", self.language)
add_string("country", self.country)
add_string("critical_rating", self.critical_rating)
add_string("alternate_series", self.alternate_series)
add_string("alternate_number", self.alternate_number)
add_string("alternate_count", self.alternate_count)
add_string("imprint", self.imprint)
add_string("web_links", [str(x) for x in self.web_links])
add_string("format", self.format)
add_string("manga", self.manga)
add_attr_string("series")
add_attr_string("issue")
add_attr_string("issue_count")
add_attr_string("title")
add_attr_string("publisher")
add_attr_string("year")
add_attr_string("month")
add_attr_string("day")
add_attr_string("volume")
add_attr_string("volume_count")
add_attr_string("genre")
add_attr_string("language")
add_attr_string("country")
add_attr_string("critical_rating")
add_attr_string("alternate_series")
add_attr_string("alternate_number")
add_attr_string("alternate_count")
add_attr_string("imprint")
add_attr_string("web_link")
add_attr_string("format")
add_attr_string("manga")
add_attr_string("price")
add_attr_string("is_version_of")
add_attr_string("rights")
add_attr_string("identifier")
add_attr_string("last_mark")
add_string("price", self.price)
add_string("is_version_of", self.is_version_of)
add_string("rights", self.rights)
add_string("identifier", self.identifier)
add_string("last_mark", self.last_mark)
if self.black_and_white:
add_attr_string("black_and_white")
add_attr_string("maturity_rating")
add_attr_string("story_arc")
add_attr_string("series_group")
add_attr_string("scan_info")
add_attr_string("characters")
add_attr_string("teams")
add_attr_string("locations")
add_attr_string("comments")
add_attr_string("notes")
add_string("black_and_white", self.black_and_white)
add_string("maturity_rating", self.maturity_rating)
add_string("story_arcs", self.story_arcs)
add_string("series_groups", self.series_groups)
add_string("scan_info", self.scan_info)
add_string("characters", ", ".join(self.characters))
add_string("teams", ", ".join(self.teams))
add_string("locations", ", ".join(self.locations))
add_string("description", self.description)
add_string("notes", self.notes)
add_string("tags", ", ".join(self.tags))
for c in self.credits:
primary = ""
if "primary" in c and c["primary"]:
if c.primary:
primary = " [P]"
add_string("credit", c["role"] + ": " + c["person"] + primary)
add_string("credit", f"{c}{primary}")
# find the longest field name
flen = 0
@ -373,9 +534,11 @@ class GenericMetadata:
md_test: GenericMetadata = GenericMetadata(
is_empty=False,
tag_origin=None,
data_origin=MetadataOrigin("comicvine", "Comic Vine"),
series="Cory Doctorow's Futuristic Tales of the Here and Now",
series_id="23437",
issue="1",
issue_id="140529",
title="Anda's Game",
publisher="IDW Publishing",
month=10,
@ -383,9 +546,9 @@ md_test: GenericMetadata = GenericMetadata(
day=1,
issue_count=6,
volume=1,
genre="Sci-Fi",
genres={"Sci-Fi"},
language="en",
comments=(
description=(
"For 12-year-old Anda, getting paid real money to kill the characters of players who were cheating"
" in her favorite online computer game was a win-win situation. Until she found out who was paying her,"
" and what those characters meant to the livelihood of children around the world."
@ -398,58 +561,269 @@ md_test: GenericMetadata = GenericMetadata(
alternate_count=7,
imprint="craphound.com",
notes="Tagged with ComicTagger 1.3.2a5 using info from Comic Vine on 2022-04-16 15:52:26. [Issue ID 140529]",
web_link="https://comicvine.gamespot.com/cory-doctorows-futuristic-tales-of-the-here-and-no/4000-140529/",
web_links=[
parse_url("https://comicvine.gamespot.com/cory-doctorows-futuristic-tales-of-the-here-and-no/4000-140529/")
],
format="Series",
manga="No",
black_and_white=None,
page_count=24,
maturity_rating="Everyone 10+",
story_arc="Here and Now",
series_group="Futuristic Tales",
story_arcs=["Here and Now"],
series_groups=["Futuristic Tales"],
scan_info="(CC BY-NC-SA 3.0)",
characters="Anda",
teams="Fahrenheit",
locations="lonely cottage ",
characters={"Anda"},
teams={"Fahrenheit"},
locations=set(utils.split("lonely cottage ", ",")),
credits=[
CreditMetadata(primary=False, person="Dara Naraghi", role="Writer"),
CreditMetadata(primary=False, person="Esteve Polls", role="Penciller"),
CreditMetadata(primary=False, person="Esteve Polls", role="Inker"),
CreditMetadata(primary=False, person="Neil Uyetake", role="Letterer"),
CreditMetadata(primary=False, person="Sam Kieth", role="Cover"),
CreditMetadata(primary=False, person="Ted Adams", role="Editor"),
Credit(primary=False, person="Dara Naraghi", role="Writer"),
Credit(primary=False, person="Esteve Polls", role="Penciller"),
Credit(primary=False, person="Esteve Polls", role="Inker"),
Credit(primary=False, person="Neil Uyetake", role="Letterer"),
Credit(primary=False, person="Sam Kieth", role="Cover"),
Credit(primary=False, person="Ted Adams", role="Editor"),
],
tags=set(),
pages=[
ImageMetadata(Image=0, ImageHeight="1280", ImageSize="195977", ImageWidth="800", Type=PageType.FrontCover),
ImageMetadata(Image=1, ImageHeight="2039", ImageSize="611993", ImageWidth="1327"),
ImageMetadata(Image=2, ImageHeight="2039", ImageSize="783726", ImageWidth="1327"),
ImageMetadata(Image=3, ImageHeight="2039", ImageSize="679584", ImageWidth="1327"),
ImageMetadata(Image=4, ImageHeight="2039", ImageSize="788179", ImageWidth="1327"),
ImageMetadata(Image=5, ImageHeight="2039", ImageSize="864433", ImageWidth="1327"),
ImageMetadata(Image=6, ImageHeight="2039", ImageSize="765606", ImageWidth="1327"),
ImageMetadata(Image=7, ImageHeight="2039", ImageSize="876427", ImageWidth="1327"),
ImageMetadata(Image=8, ImageHeight="2039", ImageSize="852622", ImageWidth="1327"),
ImageMetadata(Image=9, ImageHeight="2039", ImageSize="800205", ImageWidth="1327"),
ImageMetadata(Image=10, ImageHeight="2039", ImageSize="746243", ImageWidth="1326"),
ImageMetadata(Image=11, ImageHeight="2039", ImageSize="718062", ImageWidth="1327"),
ImageMetadata(Image=12, ImageHeight="2039", ImageSize="532179", ImageWidth="1326"),
ImageMetadata(Image=13, ImageHeight="2039", ImageSize="686708", ImageWidth="1327"),
ImageMetadata(Image=14, ImageHeight="2039", ImageSize="641907", ImageWidth="1327"),
ImageMetadata(Image=15, ImageHeight="2039", ImageSize="805388", ImageWidth="1327"),
ImageMetadata(Image=16, ImageHeight="2039", ImageSize="668927", ImageWidth="1326"),
ImageMetadata(Image=17, ImageHeight="2039", ImageSize="710605", ImageWidth="1327"),
ImageMetadata(Image=18, ImageHeight="2039", ImageSize="761398", ImageWidth="1326"),
ImageMetadata(Image=19, ImageHeight="2039", ImageSize="743807", ImageWidth="1327"),
ImageMetadata(Image=20, ImageHeight="2039", ImageSize="552911", ImageWidth="1326"),
ImageMetadata(Image=21, ImageHeight="2039", ImageSize="556827", ImageWidth="1327"),
ImageMetadata(Image=22, ImageHeight="2039", ImageSize="675078", ImageWidth="1326"),
ImageMetadata(
Bookmark="Interview",
Image=23,
ImageHeight="2032",
ImageSize="800965",
ImageWidth="1338",
Type=PageType.Letters,
PageMetadata(
archive_index=0,
display_index=0,
height=1280,
byte_size=195977,
width=800,
type=PageType.FrontCover,
filename="!cover.jpg",
bookmark="",
),
PageMetadata(
archive_index=1,
display_index=1,
height=2039,
byte_size=611993,
width=1327,
filename="01.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=2,
display_index=2,
height=2039,
byte_size=783726,
width=1327,
filename="02.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=3,
display_index=3,
height=2039,
byte_size=679584,
width=1327,
filename="03.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=4,
display_index=4,
height=2039,
byte_size=788179,
width=1327,
filename="04.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=5,
display_index=5,
height=2039,
byte_size=864433,
width=1327,
filename="05.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=6,
display_index=6,
height=2039,
byte_size=765606,
width=1327,
filename="06.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=7,
display_index=7,
height=2039,
byte_size=876427,
width=1327,
filename="07.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=8,
display_index=8,
height=2039,
byte_size=852622,
width=1327,
filename="08.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=9,
display_index=9,
height=2039,
byte_size=800205,
width=1327,
filename="09.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=10,
display_index=10,
height=2039,
byte_size=746243,
width=1326,
filename="10.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=11,
display_index=11,
height=2039,
byte_size=718062,
width=1327,
filename="11.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=12,
display_index=12,
height=2039,
byte_size=532179,
width=1326,
filename="12.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=13,
display_index=13,
height=2039,
byte_size=686708,
width=1327,
filename="13.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=14,
display_index=14,
height=2039,
byte_size=641907,
width=1327,
filename="14.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=15,
display_index=15,
height=2039,
byte_size=805388,
width=1327,
filename="15.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=16,
display_index=16,
height=2039,
byte_size=668927,
width=1326,
filename="16.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=17,
display_index=17,
height=2039,
byte_size=710605,
width=1327,
filename="17.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=18,
display_index=18,
height=2039,
byte_size=761398,
width=1326,
filename="18.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=19,
display_index=19,
height=2039,
byte_size=743807,
width=1327,
filename="19.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=20,
display_index=20,
height=2039,
byte_size=552911,
width=1326,
filename="20.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=21,
display_index=21,
height=2039,
byte_size=556827,
width=1327,
filename="21.jpg",
bookmark="",
type="",
),
PageMetadata(
archive_index=22,
display_index=22,
height=2039,
byte_size=675078,
width=1326,
filename="22.jpg",
bookmark="",
type="",
),
PageMetadata(
bookmark="Interview",
archive_index=23,
display_index=23,
height=2032,
byte_size=800965,
width=1338,
type=PageType.Letters,
filename="23.jpg",
),
],
price=None,
@ -457,5 +831,17 @@ md_test: GenericMetadata = GenericMetadata(
rights=None,
identifier=None,
last_mark=None,
cover_image=None,
_cover_image=None,
)
__all__ = (
"Url",
"parse_url",
"PageType",
"PageMetadata",
"Credit",
"ComicSeries",
"MetadataOrigin",
"GenericMetadata",
)

View File

@ -4,7 +4,8 @@ Class for handling the odd permutations of an 'issue number' that the
comics industry throws at us.
e.g.: "12", "12.1", "0", "-1", "5AU", "100-2"
"""
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -27,12 +28,12 @@ logger = logging.getLogger(__name__)
class IssueString:
def __init__(self, text: str | None) -> None:
# break up the issue number string into 2 parts: the numeric and suffix string.
# (assumes that the numeric portion is always first)
self.num = None
self.suffix = ""
self.prefix = ""
if text is None:
return
@ -42,18 +43,25 @@ class IssueString:
if len(text) == 0:
return
for idx, r in enumerate(text):
if not r.isalpha():
break
self.prefix = text[:idx]
self.num, self.suffix = self.get_number(text[idx:])
def get_number(self, text: str) -> tuple[float | None, str]:
num, suffix = None, ""
start = 0
# skip the minus sign if it's first
if text[0] == "-":
if text[0] in ("-", "+"):
start = 1
else:
start = 0
# if it's still not numeric at start skip it
if text[start].isdigit() or text[start] == ".":
# walk through the string, look for split point (the first non-numeric)
decimal_count = 0
for idx in range(start, len(text)):
if text[idx] not in "0123456789.":
if not (text[idx].isdigit() or text[idx] in "."):
break
# special case: also split on second "."
if text[idx] == ".":
@ -72,42 +80,48 @@ class IssueString:
if idx == 1 and start == 1:
idx = 0
part1 = text[0:idx]
part2 = text[idx : len(text)]
if part1 != "":
self.num = float(part1)
self.suffix = part2
if text[0:idx]:
num = float(text[0:idx])
suffix = text[idx : len(text)]
else:
self.suffix = text
suffix = text
return num, suffix
def as_string(self, pad: int = 0) -> str:
# return the float, left side zero-padded, with suffix attached
"""return the number, left side zero-padded, with suffix attached"""
# if there is no number return the text
if self.num is None:
return self.suffix
return self.prefix + self.suffix
# negative is added back in last
negative = self.num < 0
num_f = abs(self.num)
# used for padding
num_int = int(num_f)
num_s = str(num_int)
if float(num_int) != num_f:
num_s = str(num_f)
num_s += self.suffix
if num_f.is_integer():
num_s = str(num_int)
else:
num_s = str(num_f)
# create padding
padding = ""
# we only pad the whole number part, we don't care about the decimal
length = len(str(num_int))
if length < pad:
padding = "0" * (pad - length)
# add the padding to the front
num_s = padding + num_s
# finally add the negative back in
if negative:
num_s = "-" + num_s
return num_s
# return the prefix + formatted number + suffix
return self.prefix + num_s + self.suffix
def as_float(self) -> float | None:
# return the float, with no suffix

73
comicapi/merge.py Normal file
View File

@ -0,0 +1,73 @@
from __future__ import annotations
import dataclasses
from collections import defaultdict
from collections.abc import Collection
from enum import auto
from typing import Any
from comicapi.utils import StrEnum, norm_fold
@dataclasses.dataclass
class Credit:
person: str = ""
role: str = ""
primary: bool = False
language: str = "" # Should be ISO 639 language code
def __str__(self) -> str:
lang = ""
if self.language:
lang = f" [{self.language}]"
return f"{self.role}: {self.person}{lang}"
class Mode(StrEnum):
OVERLAY = auto()
ADD_MISSING = auto()
def merge_lists(old: Collection[Any], new: Collection[Any]) -> list[Any] | set[Any]:
"""Dedupes normalised (NFKD), casefolded values using 'new' values on collisions"""
if len(new) == 0:
return old if isinstance(old, set) else list(old)
if len(old) == 0:
return new if isinstance(new, set) else list(new)
# Create dict to preserve case
new_dict = {norm_fold(str(n)): n for n in new}
old_dict = {norm_fold(str(c)): c for c in old}
old_dict.update(new_dict)
if isinstance(old, set):
return set(old_dict.values())
return list(old_dict.values())
def overlay(old: Any, new: Any) -> Any:
"""overlay - When the `new` object is not empty, replace `old` with `new`."""
if new is None or (isinstance(new, Collection) and len(new) == 0):
return old
return new
attribute = defaultdict(
lambda: overlay,
{
Mode.OVERLAY: overlay,
Mode.ADD_MISSING: lambda old, new: overlay(new, old),
},
)
lists = defaultdict(
lambda: overlay,
{
Mode.OVERLAY: merge_lists,
Mode.ADD_MISSING: lambda old, new: merge_lists(new, old),
},
)

View File

@ -0,0 +1,5 @@
from __future__ import annotations
from comicapi.tags.tag import Tag
__all__ = ["Tag"]

400
comicapi/tags/comicrack.py Normal file
View File

@ -0,0 +1,400 @@
"""A class to encapsulate ComicRack's ComicInfo.xml data"""
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import logging
import xml.etree.ElementTree as ET
from typing import Any
from comicapi import utils
from comicapi.archivers import Archiver
from comicapi.genericmetadata import GenericMetadata, PageMetadata
from comicapi.tags import Tag
logger = logging.getLogger(__name__)
class ComicRack(Tag):
enabled = True
id = "cr"
def __init__(self, version: str) -> None:
super().__init__(version)
self.file = "ComicInfo.xml"
self.supported_attributes = {
"series",
"issue",
"issue_count",
"title",
"volume",
"genres",
"description",
"notes",
"alternate_series",
"alternate_number",
"alternate_count",
"story_arcs",
"series_groups",
"publisher",
"imprint",
"day",
"month",
"year",
"language",
"web_links",
"format",
"manga",
"black_and_white",
"maturity_rating",
"critical_rating",
"scan_info",
"pages",
"pages.bookmark",
"pages.double_page",
"pages.height",
"pages.image_index",
"pages.size",
"pages.type",
"pages.width",
"page_count",
"characters",
"teams",
"locations",
"credits",
"credits.person",
"credits.role",
}
def supports_credit_role(self, role: str) -> bool:
return role.casefold() in self._get_parseable_credits()
def supports_tags(self, archive: Archiver) -> bool:
return archive.supports_files()
def has_tags(self, archive: Archiver) -> bool:
try: # read_file can cause an exception
return (
self.supports_tags(archive)
and self.file in archive.get_filename_list()
and self._validate_bytes(archive.read_file(self.file))
)
except Exception:
return False
def remove_tags(self, archive: Archiver) -> bool:
return self.has_tags(archive) and archive.remove_file(self.file)
def read_tags(self, archive: Archiver) -> GenericMetadata:
if self.has_tags(archive):
try: # read_file can cause an exception
metadata = archive.read_file(self.file) or b""
if self._validate_bytes(metadata):
return self._metadata_from_bytes(metadata)
except Exception:
...
return GenericMetadata()
def read_raw_tags(self, archive: Archiver) -> str:
try: # read_file can cause an exception
if self.has_tags(archive):
b = archive.read_file(self.file)
# ET.fromstring is used as xml can declare the encoding
return ET.tostring(ET.fromstring(b), encoding="unicode", xml_declaration=True)
except Exception:
...
return ""
def write_tags(self, metadata: GenericMetadata, archive: Archiver) -> bool:
if self.supports_tags(archive):
xml = b""
try: # read_file can cause an exception
if self.has_tags(archive):
xml = archive.read_file(self.file)
return archive.write_file(self.file, self._bytes_from_metadata(metadata, xml))
except Exception:
...
else:
logger.warning(f"Archive ({archive.name()}) does not support {self.name()} metadata")
return False
def name(self) -> str:
return "Comic Rack"
@classmethod
def _get_parseable_credits(cls) -> list[str]:
parsable_credits: list[str] = []
parsable_credits.extend(GenericMetadata.writer_synonyms)
parsable_credits.extend(GenericMetadata.penciller_synonyms)
parsable_credits.extend(GenericMetadata.inker_synonyms)
parsable_credits.extend(GenericMetadata.colorist_synonyms)
parsable_credits.extend(GenericMetadata.letterer_synonyms)
parsable_credits.extend(GenericMetadata.cover_synonyms)
parsable_credits.extend(GenericMetadata.editor_synonyms)
return parsable_credits
def _metadata_from_bytes(self, string: bytes) -> GenericMetadata:
root = ET.fromstring(string)
return self._convert_xml_to_metadata(root)
def _bytes_from_metadata(self, metadata: GenericMetadata, xml: bytes = b"") -> bytes:
root = self._convert_metadata_to_xml(metadata, xml)
return ET.tostring(root, encoding="utf-8", xml_declaration=True)
def _convert_metadata_to_xml(self, metadata: GenericMetadata, xml: bytes = b"") -> ET.Element:
# shorthand for the metadata
md = metadata
if xml:
root = ET.fromstring(xml)
else:
# build a tree structure
root = ET.Element("ComicInfo")
root.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
root.attrib["xmlns:xsd"] = "http://www.w3.org/2001/XMLSchema"
# helper func
def assign(cr_entry: str, md_entry: Any) -> None:
if md_entry:
text = str(md_entry)
if isinstance(md_entry, (list, set)):
text = ",".join(md_entry)
et_entry = root.find(cr_entry)
if et_entry is not None:
et_entry.text = text
else:
ET.SubElement(root, cr_entry).text = text
else:
et_entry = root.find(cr_entry)
if et_entry is not None:
root.remove(et_entry)
# need to specially process the credits, since they are structured
# differently than CIX
credit_writer_list = []
credit_penciller_list = []
credit_inker_list = []
credit_colorist_list = []
credit_letterer_list = []
credit_cover_list = []
credit_editor_list = []
# first, loop thru credits, and build a list for each role that CIX
# supports
for credit in metadata.credits:
if credit.role.casefold() in set(GenericMetadata.writer_synonyms):
credit_writer_list.append(credit.person.replace(",", ""))
if credit.role.casefold() in set(GenericMetadata.penciller_synonyms):
credit_penciller_list.append(credit.person.replace(",", ""))
if credit.role.casefold() in set(GenericMetadata.inker_synonyms):
credit_inker_list.append(credit.person.replace(",", ""))
if credit.role.casefold() in set(GenericMetadata.colorist_synonyms):
credit_colorist_list.append(credit.person.replace(",", ""))
if credit.role.casefold() in set(GenericMetadata.letterer_synonyms):
credit_letterer_list.append(credit.person.replace(",", ""))
if credit.role.casefold() in set(GenericMetadata.cover_synonyms):
credit_cover_list.append(credit.person.replace(",", ""))
if credit.role.casefold() in set(GenericMetadata.editor_synonyms):
credit_editor_list.append(credit.person.replace(",", ""))
assign("Series", md.series)
assign("Number", md.issue)
assign("Count", md.issue_count)
assign("Title", md.title)
assign("Volume", md.volume)
assign("Genre", md.genres)
assign("Summary", md.description)
assign("Notes", md.notes)
assign("AlternateSeries", md.alternate_series)
assign("AlternateNumber", md.alternate_number)
assign("AlternateCount", md.alternate_count)
assign("StoryArc", md.story_arcs)
assign("SeriesGroup", md.series_groups)
assign("Publisher", md.publisher)
assign("Imprint", md.imprint)
assign("Day", md.day)
assign("Month", md.month)
assign("Year", md.year)
assign("LanguageISO", md.language)
assign("Web", " ".join(u.url for u in md.web_links))
assign("Format", md.format)
assign("Manga", md.manga)
assign("BlackAndWhite", "Yes" if md.black_and_white else None)
assign("AgeRating", md.maturity_rating)
assign("CommunityRating", md.critical_rating)
assign("ScanInformation", md.scan_info)
assign("PageCount", md.page_count)
assign("Characters", md.characters)
assign("Teams", md.teams)
assign("Locations", md.locations)
assign("Writer", ", ".join(credit_writer_list))
assign("Penciller", ", ".join(credit_penciller_list))
assign("Inker", ", ".join(credit_inker_list))
assign("Colorist", ", ".join(credit_colorist_list))
assign("Letterer", ", ".join(credit_letterer_list))
assign("CoverArtist", ", ".join(credit_cover_list))
assign("Editor", ", ".join(credit_editor_list))
# loop and add the page entries under pages node
pages_node = root.find("Pages")
if pages_node is not None:
pages_node.clear()
else:
pages_node = ET.SubElement(root, "Pages")
for page in sorted(md.pages, key=lambda x: x.archive_index):
page_node = ET.SubElement(pages_node, "Page")
page_node.attrib = {"Image": str(page.display_index)}
if page.bookmark:
page_node.attrib["Bookmark"] = page.bookmark
if page.type:
page_node.attrib["Type"] = page.type
if page.double_page is not None:
page_node.attrib["DoublePage"] = str(page.double_page)
if page.height is not None:
page_node.attrib["ImageHeight"] = str(page.height)
if page.byte_size is not None:
page_node.attrib["ImageSize"] = str(page.byte_size)
if page.width is not None:
page_node.attrib["ImageWidth"] = str(page.width)
page_node.attrib = dict(sorted(page_node.attrib.items()))
ET.indent(root)
return root
def _convert_xml_to_metadata(self, root: ET.Element) -> GenericMetadata:
if root.tag != "ComicInfo":
raise Exception("Not a ComicInfo file")
def get(name: str) -> str | None:
tag = root.find(name)
if tag is None:
return None
return tag.text
md = GenericMetadata()
md.series = utils.xlate(get("Series"))
md.issue = utils.xlate(get("Number"))
md.issue_count = utils.xlate_int(get("Count"))
md.title = utils.xlate(get("Title"))
md.volume = utils.xlate_int(get("Volume"))
md.genres = set(utils.split(get("Genre"), ","))
md.description = utils.xlate(get("Summary"))
md.notes = utils.xlate(get("Notes"))
md.alternate_series = utils.xlate(get("AlternateSeries"))
md.alternate_number = utils.xlate(get("AlternateNumber"))
md.alternate_count = utils.xlate_int(get("AlternateCount"))
md.story_arcs = utils.split(get("StoryArc"), ",")
md.series_groups = utils.split(get("SeriesGroup"), ",")
md.publisher = utils.xlate(get("Publisher"))
md.imprint = utils.xlate(get("Imprint"))
md.day = utils.xlate_int(get("Day"))
md.month = utils.xlate_int(get("Month"))
md.year = utils.xlate_int(get("Year"))
md.language = utils.xlate(get("LanguageISO"))
md.web_links = utils.split_urls(utils.xlate(get("Web")))
md.format = utils.xlate(get("Format"))
md.manga = utils.xlate(get("Manga"))
md.maturity_rating = utils.xlate(get("AgeRating"))
md.critical_rating = utils.xlate_float(get("CommunityRating"))
md.scan_info = utils.xlate(get("ScanInformation"))
md.page_count = utils.xlate_int(get("PageCount"))
md.characters = set(utils.split(get("Characters"), ","))
md.teams = set(utils.split(get("Teams"), ","))
md.locations = set(utils.split(get("Locations"), ","))
tmp = utils.xlate(get("BlackAndWhite"))
if tmp is not None:
md.black_and_white = tmp.casefold() in ["yes", "true", "1"]
# Now extract the credit info
for n in root:
if any(
[
n.tag == "Writer",
n.tag == "Penciller",
n.tag == "Inker",
n.tag == "Colorist",
n.tag == "Letterer",
n.tag == "Editor",
]
):
if n.text is not None:
for name in utils.split(n.text, ","):
md.add_credit(name.strip(), n.tag)
if n.tag == "CoverArtist":
if n.text is not None:
for name in utils.split(n.text, ","):
md.add_credit(name.strip(), "Cover")
# parse page data now
pages_node = root.find("Pages")
if pages_node is not None:
for i, page in enumerate(pages_node):
p: dict[str, Any] = page.attrib
md_page = PageMetadata(
filename="", # cr doesn't record the filename it just assumes it's always ordered the same
display_index=int(p.get("Image", i)),
archive_index=i,
bookmark=p.get("Bookmark", ""),
type="",
)
md_page.set_type(p.get("Type", ""))
if isinstance(p.get("DoublePage", None), str):
md_page.double_page = p["DoublePage"].casefold() in ("yes", "true", "1")
if p.get("ImageHeight", "").isnumeric():
md_page.height = int(float(p["ImageHeight"]))
if p.get("ImageWidth", "").isnumeric():
md_page.width = int(float(p["ImageWidth"]))
if p.get("ImageSize", "").isnumeric():
md_page.byte_size = int(float(p["ImageSize"]))
md.pages.append(md_page)
md.is_empty = False
return md
def _validate_bytes(self, string: bytes) -> bool:
"""verify that the string actually contains CIX data in XML format"""
try:
root = ET.fromstring(string)
if root.tag != "ComicInfo":
return False
except ET.ParseError:
return False
return True

124
comicapi/tags/tag.py Normal file
View File

@ -0,0 +1,124 @@
from __future__ import annotations
from comicapi.archivers import Archiver
from comicapi.genericmetadata import GenericMetadata
class Tag:
enabled: bool = False
id: str = ""
def __init__(self, version: str) -> None:
self.version: str = version
self.supported_attributes = {
"data_origin",
"issue_id",
"series_id",
"series",
"series_aliases",
"issue",
"issue_count",
"title",
"title_aliases",
"volume",
"volume_count",
"genres",
"description",
"notes",
"alternate_series",
"alternate_number",
"alternate_count",
"story_arcs",
"series_groups",
"publisher",
"imprint",
"day",
"month",
"year",
"language",
"country",
"web_link",
"format",
"manga",
"black_and_white",
"maturity_rating",
"critical_rating",
"scan_info",
"tags",
"pages",
"pages.type",
"pages.bookmark",
"pages.double_page",
"pages.image_index",
"pages.size",
"pages.height",
"pages.width",
"page_count",
"characters",
"teams",
"locations",
"credits",
"credits.person",
"credits.role",
"credits.primary",
"credits.language",
"price",
"is_version_of",
"rights",
"identifier",
"last_mark",
}
def supports_credit_role(self, role: str) -> bool:
return False
def supports_tags(self, archive: Archiver) -> bool:
"""
Checks the given archive for the ability to save these tags.
Should always return a bool. Failures should return False.
Typically consists of a call to either `archive.supports_comment` or `archive.supports_file`
"""
return False
def has_tags(self, archive: Archiver) -> bool:
"""
Checks the given archive for tags.
Should always return a bool. Failures should return False.
"""
return False
def remove_tags(self, archive: Archiver) -> bool:
"""
Removes the tags from the given archive.
Should always return a bool. Failures should return False.
"""
return False
def read_tags(self, archive: Archiver) -> GenericMetadata:
"""
Returns a GenericMetadata representing the tags saved in the given archive.
Should always return a GenericMetadata. Failures should return an empty metadata object.
"""
return GenericMetadata()
def read_raw_tags(self, archive: Archiver) -> str:
"""
Returns the raw tags as a string.
If the tags are a binary format a roughly similar text format should be used.
Should always return a string. Failures should return the empty string.
"""
return ""
def write_tags(self, metadata: GenericMetadata, archive: Archiver) -> bool:
"""
Saves the given metadata to the given archive.
Should always return a bool. Failures should return False.
"""
return False
def name(self) -> str:
"""
Returns the name of these tags for display purposes eg "Comic Rack".
Should always return a string. Failures should return the empty string.
"""
return ""

View File

@ -1,5 +1,6 @@
"""Some generic utilities"""
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -14,53 +15,313 @@
# limitations under the License.
from __future__ import annotations
import glob
import json
import logging
import os
import pathlib
import platform
import sys
import unicodedata
from collections import defaultdict
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
from enum import Enum, auto
from shutil import which # noqa: F401
from typing import Any
from typing import Any, TypeVar, cast
import pycountry
import rapidfuzz.fuzz
from comicfn2dict import comicfn2dict
import comicapi.data
from comicapi import filenamelexer, filenameparser
from comicapi._url import LocationParseError as LocationParseError # noqa: F401
from comicapi._url import Url as Url
from comicapi._url import parse_url as parse_url
try:
import icu
del icu
icu_available = True
except ImportError:
icu_available = False
if sys.version_info < (3, 11):
class StrEnum(str, Enum):
"""
Enum where members are also (and must be) strings
"""
def __new__(cls, *values: Any) -> Any:
"values must already be of type `str`"
if len(values) > 3:
raise TypeError(f"too many arguments for str(): {values!r}")
if len(values) == 1:
# it must be a string
if not isinstance(values[0], str):
raise TypeError(f"{values[0]!r} is not a string")
if len(values) >= 2:
# check that encoding argument is a string
if not isinstance(values[1], str):
raise TypeError(f"encoding must be a string, not {values[1]!r}")
if len(values) == 3:
# check that errors argument is a string
if not isinstance(values[2], str):
raise TypeError("errors must be a string, not %r" % (values[2]))
value = str(*values)
member = str.__new__(cls, value)
member._value_ = value
return member
@staticmethod
def _generate_next_value_(name: str, start: int, count: int, last_values: Any) -> str:
"""
Return the lower-cased version of the member name.
"""
return name.lower()
@classmethod
def _missing_(cls, value: Any) -> str | None:
if not isinstance(value, str):
return None
if not hasattr(cls, "_lower_members"):
cls._lower_members = {x.casefold(): x for x in cls} # type: ignore[attr-defined]
return cls._lower_members.get(value.casefold(), None) # type: ignore[attr-defined]
def __str__(self) -> str:
return self.value
else:
from enum import StrEnum as s
class StrEnum(s):
@classmethod
def _missing_(cls, value: Any) -> str | None:
if not isinstance(value, str):
return None
if not hasattr(cls, "_lower_members"):
cls._lower_members = {x.casefold(): x for x in cls} # type: ignore[attr-defined]
return cls._lower_members.get(value.casefold(), None) # type: ignore[attr-defined]
logger = logging.getLogger(__name__)
class UtilsVars:
already_fixed_encoding = False
class Parser(StrEnum):
ORIGINAL = auto()
COMPLICATED = auto()
COMICFN2DICT = auto()
def parse_date_str(date_str: str) -> tuple[int | None, int | None, int | None]:
def _custom_key(tup: Any) -> Any:
import natsort
lst = []
for x in natsort.os_sort_keygen()(tup):
ret = x
if len(x) > 1 and isinstance(x[1], int) and isinstance(x[0], str) and x[0] == "":
ret = ("a", *x[1:])
lst.append(ret)
return tuple(lst)
T = TypeVar("T")
def os_sorted(lst: Iterable[T]) -> Iterable[T]:
import natsort
key = _custom_key
if icu_available or platform.system() == "Windows":
key = natsort.os_sort_keygen()
return sorted(lst, key=key)
def parse_filename(
filename: str,
parser: Parser = Parser.ORIGINAL,
remove_c2c: bool = False,
remove_fcbd: bool = False,
remove_publisher: bool = False,
split_words: bool = False,
allow_issue_start_with_letter: bool = False,
protofolius_issue_number_scheme: bool = False,
) -> filenameparser.FilenameInfo:
fni = filenameparser.FilenameInfo(
alternate="",
annual=False,
archive="",
c2c=False,
fcbd=False,
format="",
issue="",
issue_count="",
publisher="",
remainder="",
series="",
title="",
volume="",
volume_count="",
year="",
)
if not filename:
return fni
if split_words:
import wordninja
filename, ext = os.path.splitext(filename)
filename = " ".join(wordninja.split(filename)) + ext
if parser == Parser.COMPLICATED:
lex = filenamelexer.Lex(filename, allow_issue_start_with_letter)
p = filenameparser.Parse(
lex.items,
remove_c2c=remove_c2c,
remove_fcbd=remove_fcbd,
remove_publisher=remove_publisher,
protofolius_issue_number_scheme=protofolius_issue_number_scheme,
)
if p.error:
logger.info("Issue parsing filename: '%s': %s ", filename, p.error.val)
fni = p.filename_info
elif parser == Parser.COMICFN2DICT:
fn2d = comicfn2dict(filename)
fni = filenameparser.FilenameInfo(
alternate="",
annual=False,
archive=fn2d.get("ext", ""),
c2c=False,
fcbd=False,
issue=fn2d.get("issue", ""),
issue_count=fn2d.get("issue_count", ""),
publisher=fn2d.get("publisher", ""),
remainder=fn2d.get("scan_info", ""),
series=fn2d.get("series", ""),
title=fn2d.get("title", ""),
volume=fn2d.get("volume", ""),
volume_count=fn2d.get("volume_count", ""),
year=fn2d.get("year", ""),
format=fn2d.get("original_format", ""),
)
else:
fnp = filenameparser.FileNameParser()
fnp.parse_filename(filename)
fni = filenameparser.FilenameInfo(
alternate="",
annual=False,
archive="",
c2c=False,
fcbd=False,
issue=fnp.issue,
issue_count=fnp.issue_count,
publisher="",
remainder=fnp.remainder,
series=fnp.series,
title="",
volume=fnp.volume,
volume_count="",
year=fnp.year,
format="",
)
return fni
def norm_fold(string: str) -> str:
"""Normalise and casefold string"""
return unicodedata.normalize("NFKD", string).casefold()
def combine_notes(existing_notes: str | None, new_notes: str | None, split: str) -> str:
split_notes, split_str, untouched_notes = (existing_notes or "").rpartition(split)
if split_notes or split_str:
return (split_notes + (new_notes or "")).strip()
else:
return (untouched_notes + "\n" + (new_notes or "")).strip()
def parse_date_str(date_str: str | None) -> tuple[int | None, int | None, int | None]:
day = None
month = None
year = None
if date_str:
parts = date_str.split("-")
year = xlate(parts[0], True)
year = xlate_int(parts[0])
if len(parts) > 1:
month = xlate(parts[1], True)
month = xlate_int(parts[1])
if len(parts) > 2:
day = xlate(parts[2], True)
day = xlate_int(parts[2])
return day, month, year
def shorten_path(path: pathlib.Path, path2: pathlib.Path | None = None) -> tuple[pathlib.Path, pathlib.Path]:
if path2:
path2 = path2.absolute()
path = path.absolute()
shortened_path: pathlib.Path = path
relative_path = pathlib.Path(path.anchor)
if path.is_relative_to(path.home()):
relative_path = path.home()
shortened_path = path.relative_to(path.home())
if path.is_relative_to(path.cwd()):
relative_path = path.cwd()
shortened_path = path.relative_to(path.cwd())
if path2 and shortened_path.is_relative_to(path2.parent):
relative_path = path2
shortened_path = shortened_path.relative_to(path2)
return relative_path, shortened_path
def path_to_short_str(original_path: pathlib.Path, renamed_path: pathlib.Path | None = None) -> str:
rel, _original_path = shorten_path(original_path)
path_str = str(_original_path)
if rel.samefile(rel.cwd()):
path_str = f"./{_original_path}"
elif rel.samefile(rel.home()):
path_str = f"~/{_original_path}"
if renamed_path:
rel, path = shorten_path(renamed_path, original_path.parent)
rename_str = f" -> {path}"
if rel.samefile(rel.cwd()):
rename_str = f" -> ./{_original_path}"
elif rel.samefile(rel.home()):
rename_str = f" -> ~/{_original_path}"
path_str += rename_str
return path_str
def get_page_name_list(files: list[str]) -> list[str]:
# get the list file names in the archive, and sort
files = cast(list[str], os_sorted(files))
# make a sub-list of image files
page_list = []
for name in files:
if (
os.path.splitext(name)[1].casefold() in [".jpg", ".jpeg", ".png", ".gif", ".webp", ".avif"]
and os.path.basename(name)[0] != "."
):
page_list.append(name)
return page_list
def get_recursive_filelist(pathlist: list[str]) -> list[str]:
"""Get a recursive list of of all files under all path items in the list"""
filelist: list[str] = []
for p in pathlist:
if os.path.isdir(p):
filelist.extend(x for x in glob.glob(f"{p}{os.sep}/**", recursive=True) if not os.path.isdir(x))
elif str(p) not in filelist:
filelist.append(str(p))
for root, _, files in os.walk(p):
for f in files:
filelist.append(os.path.join(root, f))
elif os.path.exists(p):
filelist.append(p)
return filelist
@ -68,32 +329,76 @@ def get_recursive_filelist(pathlist: list[str]) -> list[str]:
def add_to_path(dirname: str) -> None:
if dirname:
dirname = os.path.abspath(dirname)
paths = [os.path.normpath(x) for x in os.environ["PATH"].split(os.pathsep)]
paths = [os.path.normpath(x) for x in split(os.environ["PATH"], os.pathsep)]
if dirname not in paths:
paths.insert(0, dirname)
os.environ["PATH"] = os.pathsep.join(paths)
def xlate(data: Any, is_int: bool = False, is_float: bool = False) -> Any:
def remove_from_path(dirname: str) -> None:
if dirname:
dirname = os.path.abspath(dirname)
paths = [os.path.normpath(x) for x in split(os.environ["PATH"], os.pathsep) if dirname != os.path.normpath(x)]
os.environ["PATH"] = os.pathsep.join(paths)
def xlate_int(data: Any) -> int | None:
data = xlate_float(data)
if data is None:
return None
return int(data)
def xlate_float(data: Any) -> float | None:
if isinstance(data, str):
data = data.strip()
if data is None or data == "":
return None
if is_int or is_float:
i: str | int | float
if isinstance(data, (int, float)):
i = data
else:
i = str(data).translate(defaultdict(lambda: None, zip((ord(c) for c in "1234567890."), "1234567890.")))
if i == "":
return None
try:
if is_float:
return float(i)
return int(float(i))
except ValueError:
return None
i: str | int | float
if isinstance(data, (int, float)):
i = data
else:
i = str(data).translate(defaultdict(lambda: None, zip((ord(c) for c in "1234567890."), "1234567890.")))
if i == "":
return None
try:
return float(i)
except ValueError:
return None
return str(data)
def xlate(data: Any) -> str | None:
if data is None or isinstance(data, str) and data.strip() == "":
return None
return str(data).strip()
def split(s: str | None, c: str) -> list[str]:
s = xlate(s)
if s:
return [x.strip() for x in s.strip().split(c) if x.strip()]
return []
def split_urls(s: str | None) -> list[Url]:
if s is None:
return []
# Find occurences of ' http'
if s.count("http") > 1 and s.count(" http") >= 1:
urls = []
# Split urls out
url_strings = split(s, " http")
# Return the scheme 'http' and parse the url
for i, url_string in enumerate(url_strings):
if not url_string.startswith("http"):
url_string = "http" + url_string
urls.append(parse_url(url_string))
return urls
else:
return [parse_url(s)]
def remove_articles(text: str) -> str:
@ -155,9 +460,11 @@ def sanitize_title(text: str, basic: bool = False) -> str:
def titles_match(search_title: str, record_title: str, threshold: int = 90) -> bool:
import rapidfuzz.fuzz
sanitized_search = sanitize_title(search_title)
sanitized_record = sanitize_title(record_title)
ratio: int = rapidfuzz.fuzz.ratio(sanitized_search, sanitized_record)
ratio = int(rapidfuzz.fuzz.ratio(sanitized_search, sanitized_record))
logger.debug(
"search title: %s ; record title: %s ; ratio: %d ; match threshold: %d",
search_title,
@ -178,35 +485,66 @@ def unique_file(file_name: pathlib.Path) -> pathlib.Path:
counter += 1
languages: dict[str | None, str | None] = defaultdict(lambda: None)
def parse_version(s: str) -> tuple[int, int, int]:
str_parts = s.split(".")[:3]
parts = [int(x) if x.isdigit() else 0 for x in str_parts]
parts.extend([0] * (3 - len(parts))) # Ensure exactly three elements in the resulting list
countries: dict[str | None, str | None] = defaultdict(lambda: None)
return (parts[0], parts[1], parts[2])
for c in pycountry.countries:
if "alpha_2" in c._fields:
countries[c.alpha_2] = c.name
for lng in pycountry.languages:
if "alpha_2" in lng._fields:
languages[lng.alpha_2] = lng.name
_languages: dict[str | None, str | None] = defaultdict(lambda: None)
_countries: dict[str | None, str | None] = defaultdict(lambda: None)
def countries() -> dict[str | None, str | None]:
if not _countries:
import isocodes
for alpha_2, c in isocodes.countries.by_alpha_2:
_countries[alpha_2] = c["name"]
return _countries
def languages() -> dict[str | None, str | None]:
if not _languages:
import isocodes
for alpha_2, lng in isocodes.extendend_languages._sorted_by_index(index="alpha_2"):
_languages[alpha_2] = lng["name"]
return _languages
def get_language_from_iso(iso: str | None) -> str | None:
return languages[iso]
return languages()[iso]
def get_language_iso(string: str | None) -> str | None:
if string is None:
return None
import isocodes
# Return current string if all else fails
lang = string.casefold()
try:
return getattr(pycountry.languages.lookup(string), "alpha_2", None)
except LookupError:
pass
found = None
for lng in isocodes.extendend_languages.items:
for x in ("alpha_2", "alpha_3", "bibliographic", "common_name", "name"):
if x in lng and lng[x].casefold() == lang:
found = lng
if found:
break
if found:
return found.get("alpha_2", None)
return lang
def get_country_from_iso(iso: str | None) -> str | None:
return countries()[iso]
def get_publisher(publisher: str) -> tuple[str, str]:
imprint = ""
@ -215,7 +553,7 @@ def get_publisher(publisher: str) -> tuple[str, str]:
if ok:
break
return (imprint, publisher)
return imprint, publisher
def update_publishers(new_publishers: Mapping[str, Mapping[str, str]]) -> None:
@ -226,7 +564,7 @@ def update_publishers(new_publishers: Mapping[str, Mapping[str, str]]) -> None:
publishers[publisher] = ImprintDict(publisher, new_publishers[publisher])
class ImprintDict(dict):
class ImprintDict(dict): # type: ignore
"""
ImprintDict takes a publisher and a dict or mapping of lowercased
imprint names to the proper imprint name. Retrieving a value from an
@ -234,7 +572,7 @@ class ImprintDict(dict):
if the key does not exist the key is returned as the publisher unchanged
"""
def __init__(self, publisher: str, mapping: tuple | Mapping = (), **kwargs: dict) -> None:
def __init__(self, publisher: str, mapping: tuple | Mapping = (), **kwargs: dict) -> None: # type: ignore
super().__init__(mapping, **kwargs)
self.publisher = publisher
@ -244,11 +582,11 @@ class ImprintDict(dict):
def __getitem__(self, k: str) -> tuple[str, str, bool]:
item = super().__getitem__(k.casefold())
if k.casefold() == self.publisher.casefold():
return ("", self.publisher, True)
return "", self.publisher, True
if item is None:
return ("", k, False)
return "", k, False
else:
return (item, self.publisher, True)
return item, self.publisher, True
def copy(self) -> ImprintDict:
return ImprintDict(self.publisher, super().copy())

View File

@ -1,9 +0,0 @@
#!/usr/bin/env python3
from __future__ import annotations
import localefix
from comictaggerlib.main import ctmain
if __name__ == "__main__":
localefix.configure_locale()
ctmain()

View File

@ -0,0 +1,5 @@
from __future__ import annotations
from comictaggerlib.main import main
main()

View File

@ -1,7 +1,8 @@
from __future__ import annotations
from PyInstaller.utils.hooks import collect_data_files
from PyInstaller.utils.hooks import collect_data_files, collect_entry_point, collect_submodules
datas = []
datas, hiddenimports = collect_entry_point("comictagger.talker")
hiddenimports += collect_submodules("comictaggerlib")
datas += collect_data_files("comictaggerlib.ui")
datas += collect_data_files("comictaggerlib.graphics")

View File

@ -1,6 +1,7 @@
from __future__ import annotations
import logging
import pathlib
from PyQt5 import QtCore, QtGui, QtWidgets, uic
@ -23,9 +24,12 @@ class QTextEditLogger(QtCore.QObject, logging.Handler):
class ApplicationLogWindow(QtWidgets.QDialog):
def __init__(self, log_handler: QTextEditLogger, parent: QtCore.QObject = None) -> None:
def __init__(
self, log_folder: pathlib.Path, log_handler: QTextEditLogger, parent: QtCore.QObject | None = None
) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "logwindow.ui", self)
with (ui_path / "applicationlogwindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.log_handler = log_handler
self.log_handler.qlog.connect(self.textEdit.append)
@ -36,6 +40,9 @@ class ApplicationLogWindow(QtWidgets.QDialog):
self._button = QtWidgets.QPushButton(self)
self._button.setText("Test Me")
self.log_folder = log_folder
self.lblLogLocation.setText(f'Log Location: <a href="file://{log_folder}">{log_folder}</a>')
layout = self.layout()
layout.addWidget(self._button)

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to select from automated issue matches"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -17,53 +18,50 @@ from __future__ import annotations
import logging
import os
from typing import Callable
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from comicapi.comicarchive import MetaDataStyle
from comicapi.genericmetadata import GenericMetadata
from comicapi.comicarchive import ComicArchive, tags
from comictaggerlib.coverimagewidget import CoverImageWidget
from comictaggerlib.resulttypes import IssueResult, MultipleMatch
from comictaggerlib.settings import ComicTaggerSettings
from comictaggerlib.ctsettings import ct_ns
from comictaggerlib.md import prepare_metadata
from comictaggerlib.resulttypes import IssueResult, Result
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import reduce_widget_font_size
from comictalker.comictalker import ComicTalker, TalkerError
logger = logging.getLogger(__name__)
class AutoTagMatchWindow(QtWidgets.QDialog):
volume_id = 0
def __init__(
self,
parent: QtWidgets.QWidget,
match_set_list: list[MultipleMatch],
style: int,
fetch_func: Callable[[IssueResult], GenericMetadata],
settings: ComicTaggerSettings,
match_set_list: list[Result],
read_tags: list[str],
config: ct_ns,
talker: ComicTalker,
) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "matchselectionwindow.ui", self)
with (ui_path / "matchselectionwindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.settings = settings
self.config = config
self.current_match_set: MultipleMatch = match_set_list[0]
self.current_match_set: Result = match_set_list[0]
self.altCoverWidget = CoverImageWidget(self.altCoverContainer, CoverImageWidget.AltCoverMode)
self.altCoverWidget = CoverImageWidget(
self.altCoverContainer, CoverImageWidget.AltCoverMode, config.Runtime_Options__config.user_cache_dir
)
gridlayout = QtWidgets.QGridLayout(self.altCoverContainer)
gridlayout.addWidget(self.altCoverWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
self.archiveCoverWidget = CoverImageWidget(self.archiveCoverContainer, CoverImageWidget.ArchiveMode)
self.archiveCoverWidget = CoverImageWidget(self.archiveCoverContainer, CoverImageWidget.ArchiveMode, None)
gridlayout = QtWidgets.QGridLayout(self.archiveCoverContainer)
gridlayout.addWidget(self.archiveCoverWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
reduce_widget_font_size(self.twList)
reduce_widget_font_size(self.teDescription, 1)
self.setWindowFlags(
QtCore.Qt.WindowType(
self.windowFlags()
@ -77,8 +75,8 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setText("Accept and Write Tags")
self.match_set_list = match_set_list
self._style = style
self.fetch_func = fetch_func
self._tags = read_tags
self.talker = talker
self.current_match_set_idx = 0
@ -89,7 +87,6 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
self.update_data()
def update_data(self) -> None:
self.current_match_set = self.match_set_list[self.current_match_set_idx]
if self.current_match_set_idx + 1 == len(self.match_set_list):
@ -101,7 +98,7 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
self.twList.resizeColumnsToContents()
self.twList.selectRow(0)
path = self.current_match_set.ca.path
path = self.current_match_set.original_path
self.setWindowTitle(
"Select correct match or skip ({} of {}): {}".format(
self.current_match_set_idx + 1,
@ -118,19 +115,18 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
self.twList.setSortingEnabled(False)
row = 0
for match in self.current_match_set.matches:
for row, match in enumerate(self.current_match_set.online_results):
self.twList.insertRow(row)
item_text = match["series"]
item_text = match.series
item = QtWidgets.QTableWidgetItem(item_text)
item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
item.setData(QtCore.Qt.ItemDataRole.UserRole, (match,))
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 0, item)
if match["publisher"] is not None:
item_text = str(match["publisher"])
if match.publisher is not None:
item_text = str(match.publisher)
else:
item_text = "Unknown"
item = QtWidgets.QTableWidgetItem(item_text)
@ -140,10 +136,10 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
month_str = ""
year_str = "????"
if match["month"] is not None:
month_str = f"-{int(match['month']):02d}"
if match["year"] is not None:
year_str = str(match["year"])
if match.month is not None:
month_str = f"-{int(match.month):02d}"
if match.year is not None:
year_str = str(match.year)
item_text = year_str + month_str
item = QtWidgets.QTableWidgetItem(item_text)
@ -151,7 +147,7 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 2, item)
item_text = match["issue_title"]
item_text = match.issue_title
if item_text is None:
item_text = ""
item = QtWidgets.QTableWidgetItem(item_text)
@ -159,8 +155,6 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 3, item)
row += 1
self.twList.resizeColumnsToContents()
self.twList.setSortingEnabled(True)
self.twList.sortItems(2, QtCore.Qt.SortOrder.AscendingOrder)
@ -172,20 +166,20 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
self.accept()
def current_item_changed(self, curr: QtCore.QModelIndex, prev: QtCore.QModelIndex) -> None:
if curr is None:
return None
if prev is not None and prev.row() == curr.row():
return None
self.altCoverWidget.set_issue_id(self.current_match()["issue_id"])
if self.current_match()["description"] is None:
match = self.current_match()
self.altCoverWidget.set_issue_details(match.issue_id, [match.image_url, *match.alt_image_urls])
if match.description is None:
self.teDescription.setText("")
else:
self.teDescription.setText(self.current_match()["description"])
self.teDescription.setText(match.description)
def set_cover_image(self) -> None:
ca = self.current_match_set.ca
ca = ComicArchive(self.current_match_set.original_path)
self.archiveCoverWidget.set_archive(ca)
def current_match(self) -> IssueResult:
@ -194,7 +188,6 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
return match
def accept(self) -> None:
self.save_match()
self.current_match_set_idx += 1
@ -228,33 +221,51 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
QtWidgets.QDialog.reject(self)
def save_match(self) -> None:
match = self.current_match()
ca = self.current_match_set.ca
md = ca.read_metadata(self._style)
if md.is_empty:
md = ca.metadata_from_filename(
self.settings.complicated_parser,
self.settings.remove_c2c,
self.settings.remove_fcbd,
self.settings.remove_publisher,
)
# now get the particular issue data
cv_md = self.fetch_func(match)
if cv_md is None:
ca = ComicArchive(self.current_match_set.original_path)
md, _, error = self.parent().read_selected_tags(self._tags, ca)
if error is not None:
logger.error("Failed to load tags for %s: %s", ca.path, error)
QtWidgets.QApplication.restoreOverrideCursor()
QtWidgets.QMessageBox.critical(
self, "Network Issue", "Could not connect to Comic Vine to get issue details!"
self,
"Read Failed!",
f"One or more of the read tags failed to load for {ca.path}, check log for details",
)
return
if md.is_empty:
md = ca.metadata_from_filename(
self.config.Filename_Parsing__filename_parser,
self.config.Filename_Parsing__remove_c2c,
self.config.Filename_Parsing__remove_fcbd,
self.config.Filename_Parsing__remove_publisher,
)
# now get the particular issue data
try:
self.current_match_set.md = ct_md = self.talker.fetch_comic_data(issue_id=match.issue_id)
except TalkerError as e:
QtWidgets.QApplication.restoreOverrideCursor()
QtWidgets.QMessageBox.critical(self, f"{e.source} {e.code_name} Error", f"{e}")
return
if ct_md is None or ct_md.is_empty:
QtWidgets.QMessageBox.critical(self, "Network Issue", "Could not retrieve issue details!")
return
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CursorShape.WaitCursor))
md.overlay(cv_md)
success = ca.write_metadata(md, self._style)
ca.load_cache([MetaDataStyle.CBI, MetaDataStyle.CIX])
md = prepare_metadata(md, ct_md, self.config)
for tag_id in self._tags:
success = ca.write_tags(md, tag_id)
QtWidgets.QApplication.restoreOverrideCursor()
if not success:
QtWidgets.QMessageBox.warning(
self,
"Write Error",
f"Saving {tags[tag_id].name()} the tags to the archive seemed to fail!",
)
break
QtWidgets.QApplication.restoreOverrideCursor()
if not success:
QtWidgets.QMessageBox.warning(self, "Write Error", "Saving the tags to the archive seemed to fail!")
ca.reset_cache()

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to show ID log and progress"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -16,34 +17,225 @@
from __future__ import annotations
import logging
import pathlib
import re
from PyQt5 import QtCore, QtWidgets, uic
from comicapi import utils
from comicapi.comicarchive import ComicArchive, tags
from comicapi.genericmetadata import GenericMetadata
from comictaggerlib.coverimagewidget import CoverImageWidget
from comictaggerlib.ctsettings.settngs_namespace import SettngsNS
from comictaggerlib.issueidentifier import IssueIdentifierCancelled
from comictaggerlib.md import read_selected_tags
from comictaggerlib.resulttypes import Action, OnlineMatchResults, Result, Status
from comictaggerlib.tag import identify_comic
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import reduce_widget_font_size
from comictalker.comictalker import ComicTalker, RLCallBack
logger = logging.getLogger(__name__)
class AutoTagThread(QtCore.QThread):
autoTagComplete = QtCore.pyqtSignal(OnlineMatchResults, list)
autoTagLogMsg = QtCore.pyqtSignal(str)
autoTagProgress = QtCore.pyqtSignal(object, object, object, bytes, bytes) # see progress_callback
ratelimit = QtCore.pyqtSignal(float, float)
def __init__(
self, series_override: str, ca_list: list[ComicArchive], config: SettngsNS, talker: ComicTalker
) -> None:
QtCore.QThread.__init__(self)
self.series_override = series_override
self.ca_list = ca_list
self.config = config
self.talker = talker
self.canceled = False
def log_output(self, text: str) -> None:
self.autoTagLogMsg.emit(str(text))
def progress_callback(
self, cur: int | None, total: int | None, path: pathlib.Path | None, archive_image: bytes, remote_image: bytes
) -> None:
self.autoTagProgress.emit(cur, total, path, archive_image, remote_image)
def run(self) -> None:
match_results = OnlineMatchResults()
archives_to_remove = []
for prog_idx, ca in enumerate(self.ca_list):
self.log_output("==========================================================================\n")
self.log_output(f"Auto-Tagging {prog_idx} of {len(self.ca_list)}\n")
self.log_output(f"{ca.path}\n")
try:
cover_idx = ca.read_tags(self.config.internal__read_tags[0]).get_cover_page_index_list()[0]
except Exception as e:
cover_idx = 0
logger.error("Failed to load metadata for %s: %s", ca.path, e)
image_data = ca.get_page(cover_idx)
self.progress_callback(prog_idx, len(self.ca_list), ca.path, image_data, b"")
if self.canceled:
break
if ca.is_writable():
success, match_results = self.identify_and_tag_single_archive(ca, match_results)
if self.canceled:
break
if success and self.config.internal__remove_archive_after_successful_match:
archives_to_remove.append(ca)
self.autoTagComplete.emit(match_results, archives_to_remove)
def on_rate_limit(self, full_time: float, sleep_time: float) -> None:
if self.canceled:
raise IssueIdentifierCancelled
self.log_output(
f"Rate limit reached: {full_time:.0f}s until next request. Waiting {sleep_time:.0f}s for ratelimit"
)
self.ratelimit.emit(full_time, sleep_time)
def identify_and_tag_single_archive(
self, ca: ComicArchive, match_results: OnlineMatchResults
) -> tuple[Result, OnlineMatchResults]:
ratelimit_callback = RLCallBack(
self.on_rate_limit,
60,
)
# read in tags, and parse file name if not there
md, tags_used, error = read_selected_tags(self.config.internal__read_tags, ca)
if error is not None:
QtWidgets.QMessageBox.warning(
None,
"Aborting...",
f"One or more of the read tags failed to load for {ca.path}. Aborting to prevent any possible further damage. Check log for details.",
)
logger.error("Failed to load tags from %s: %s", ca.path, error)
return (
Result(
Action.save,
original_path=ca.path,
status=Status.read_failure,
),
match_results,
)
if md.is_empty:
md = ca.metadata_from_filename(
self.config.Filename_Parsing__filename_parser,
self.config.Filename_Parsing__remove_c2c,
self.config.Filename_Parsing__remove_fcbd,
self.config.Filename_Parsing__remove_publisher,
self.config.Filename_Parsing__split_words,
self.config.Filename_Parsing__allow_issue_start_with_letter,
self.config.Filename_Parsing__protofolius_issue_number_scheme,
)
if self.config.Auto_Tag__ignore_leading_numbers_in_filename and md.series is not None:
# remove all leading numbers
md.series = re.sub(r"(^[\d.]*)(.*)", r"\2", md.series)
# use the dialog specified search string
if self.series_override:
md.series = self.series_override
if not self.config.Auto_Tag__use_year_when_identifying:
md.year = None
# If it's empty we need it to stay empty for identify_comic to report the correct error
if (md.issue is None or md.issue == "") and not md.is_empty:
if self.config.Auto_Tag__assume_issue_one:
md.issue = "1"
else:
md.issue = utils.xlate(md.volume)
def on_progress(x: int, y: int, image: bytes) -> None:
# We don't (currently) care about the progress of an individual comic here we just want the cover for the autotagprogresswindow
self.progress_callback(None, None, None, b"", image)
if self.canceled:
return (
Result(
Action.save,
original_path=ca.path,
status=Status.read_failure,
),
match_results,
)
try:
res, match_results = identify_comic(
ca,
md,
tags_used,
match_results,
self.config,
self.talker,
self.log_output,
on_rate_limit=ratelimit_callback,
on_progress=on_progress,
)
except IssueIdentifierCancelled:
return (
Result(
Action.save,
original_path=ca.path,
status=Status.fetch_data_failure,
),
match_results,
)
if self.canceled:
return res, match_results
if res.status == Status.success:
assert res.md
def write_Tags(ca: ComicArchive, md: GenericMetadata) -> bool:
for tag_id in self.config.Runtime_Options__tags_write:
# write out the new data
if not ca.write_tags(md, tag_id):
self.log_output(f"{tags[tag_id].name()} save failed! Aborting any additional tag saves.\n")
return False
return True
# Save tags
if write_Tags(ca, res.md):
match_results.good_matches.append(res)
res.tags_written = self.config.Runtime_Options__tags_write
self.log_output("Save complete!\n")
else:
res.status = Status.write_failure
match_results.write_failures.append(res)
ca.reset_cache()
ca.load_cache({*self.config.Runtime_Options__tags_read})
return res, match_results
def cancel(self) -> None:
self.canceled = True
class AutoTagProgressWindow(QtWidgets.QDialog):
def __init__(self, parent: QtWidgets.QWidget) -> None:
def __init__(self, parent: QtWidgets.QWidget, talker: ComicTalker) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "autotagprogresswindow.ui", self)
with (ui_path / "autotagprogresswindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.archiveCoverWidget = CoverImageWidget(self.archiveCoverContainer, CoverImageWidget.DataMode, False)
self.lblSourceName.setText(talker.attribution)
self.archiveCoverWidget = CoverImageWidget(self.archiveCoverContainer, CoverImageWidget.DataMode, None, False)
gridlayout = QtWidgets.QGridLayout(self.archiveCoverContainer)
gridlayout.addWidget(self.archiveCoverWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
self.testCoverWidget = CoverImageWidget(self.testCoverContainer, CoverImageWidget.DataMode, False)
self.testCoverWidget = CoverImageWidget(self.testCoverContainer, CoverImageWidget.DataMode, None, False)
gridlayout = QtWidgets.QGridLayout(self.testCoverContainer)
gridlayout.addWidget(self.testCoverWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
self.isdone = False
self.setWindowFlags(
QtCore.Qt.WindowType(
self.windowFlags()
@ -52,8 +244,6 @@ class AutoTagProgressWindow(QtWidgets.QDialog):
)
)
reduce_widget_font_size(self.textEdit)
def set_archive_image(self, img_data: bytes) -> None:
self.set_cover_image(img_data, self.archiveCoverWidget)
@ -65,6 +255,20 @@ class AutoTagProgressWindow(QtWidgets.QDialog):
QtCore.QCoreApplication.processEvents()
QtCore.QCoreApplication.processEvents()
# @QtCore.pyqtSlot(int, int, 'Optional[pathlib.Path]', bytes, bytes)
def on_progress(
self, x: int | None, y: int | None, title: pathlib.Path | None, archive_image: bytes, remote_image: bytes
) -> None:
if x is not None and y is not None:
self.progressBar: QtWidgets.QProgressBar
self.progressBar.setValue(x)
self.progressBar.setMaximum(y)
if title:
self.setWindowTitle(str(title))
if archive_image:
self.set_archive_image(archive_image)
if remote_image:
self.set_test_image(remote_image)
def reject(self) -> None:
QtWidgets.QDialog.reject(self)
self.isdone = True

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to confirm and set options for auto-tag"""
"""A PyQT4 dialog to confirm and set config for auto-tag"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -19,37 +20,37 @@ import logging
from PyQt5 import QtCore, QtWidgets, uic
from comictaggerlib.settings import ComicTaggerSettings
from comictaggerlib.ctsettings import ct_ns
from comictaggerlib.ui import ui_path
logger = logging.getLogger(__name__)
class AutoTagStartWindow(QtWidgets.QDialog):
def __init__(self, parent: QtWidgets.QWidget, settings: ComicTaggerSettings, msg: str) -> None:
def __init__(self, parent: QtWidgets.QWidget, config: ct_ns, msg: str) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "autotagstartwindow.ui", self)
with (ui_path / "autotagstartwindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.label.setText(msg)
self.setWindowFlags(
QtCore.Qt.WindowType(self.windowFlags() & ~QtCore.Qt.WindowType.WindowContextHelpButtonHint)
)
self.settings = settings
self.config = config
self.cbxSpecifySearchString.setChecked(False)
self.cbxSplitWords.setChecked(False)
self.sbNameMatchSearchThresh.setValue(self.settings.id_series_match_identify_thresh)
self.sbNameMatchSearchThresh.setValue(self.config.Issue_Identifier__series_match_identify_thresh)
self.leSearchString.setEnabled(False)
self.cbxSaveOnLowConfidence.setChecked(self.settings.save_on_low_confidence)
self.cbxDontUseYear.setChecked(self.settings.dont_use_year_when_identifying)
self.cbxAssumeIssueOne.setChecked(self.settings.assume_1_if_no_issue_num)
self.cbxIgnoreLeadingDigitsInFilename.setChecked(self.settings.ignore_leading_numbers_in_filename)
self.cbxRemoveAfterSuccess.setChecked(self.settings.remove_archive_after_successful_match)
self.cbxWaitForRateLimit.setChecked(self.settings.wait_and_retry_on_rate_limit)
self.cbxAutoImprint.setChecked(self.settings.auto_imprint)
self.cbxSaveOnLowConfidence.setChecked(self.config.Auto_Tag__save_on_low_confidence)
self.cbxDontUseYear.setChecked(not self.config.Auto_Tag__use_year_when_identifying)
self.cbxAssumeIssueOne.setChecked(self.config.Auto_Tag__assume_issue_one)
self.cbxIgnoreLeadingDigitsInFilename.setChecked(self.config.Auto_Tag__ignore_leading_numbers_in_filename)
self.cbxRemoveAfterSuccess.setChecked(self.config.internal__remove_archive_after_successful_match)
self.cbxAutoImprint.setChecked(self.config.Auto_Tag__auto_imprint)
nlmt_tip = """<html>The <b>Name Match Ratio Threshold: Auto-Identify</b> is for eliminating automatic
search matches that are too long compared to your series name search. The lower
@ -73,9 +74,8 @@ class AutoTagStartWindow(QtWidgets.QDialog):
self.assume_issue_one = False
self.ignore_leading_digits_in_filename = False
self.remove_after_success = False
self.wait_and_retry_on_rate_limit = False
self.search_string = ""
self.name_length_match_tolerance = self.settings.id_series_match_search_thresh
self.name_length_match_tolerance = self.config.Issue_Identifier__series_match_search_thresh
self.split_words = self.cbxSplitWords.isChecked()
def search_string_toggle(self) -> None:
@ -91,16 +91,14 @@ class AutoTagStartWindow(QtWidgets.QDialog):
self.ignore_leading_digits_in_filename = self.cbxIgnoreLeadingDigitsInFilename.isChecked()
self.remove_after_success = self.cbxRemoveAfterSuccess.isChecked()
self.name_length_match_tolerance = self.sbNameMatchSearchThresh.value()
self.wait_and_retry_on_rate_limit = self.cbxWaitForRateLimit.isChecked()
self.split_words = self.cbxSplitWords.isChecked()
# persist some settings
self.settings.save_on_low_confidence = self.auto_save_on_low
self.settings.dont_use_year_when_identifying = self.dont_use_year
self.settings.assume_1_if_no_issue_num = self.assume_issue_one
self.settings.ignore_leading_numbers_in_filename = self.ignore_leading_digits_in_filename
self.settings.remove_archive_after_successful_match = self.remove_after_success
self.settings.wait_and_retry_on_rate_limit = self.wait_and_retry_on_rate_limit
self.config.Auto_Tag__save_on_low_confidence = self.auto_save_on_low
self.config.Auto_Tag__use_year_when_identifying = not self.dont_use_year
self.config.Auto_Tag__assume_issue_one = self.assume_issue_one
self.config.Auto_Tag__ignore_leading_numbers_in_filename = self.ignore_leading_digits_in_filename
self.config.internal__remove_archive_after_successful_match = self.remove_after_success
if self.cbxSpecifySearchString.isChecked():
self.search_string = self.leSearchString.text()

View File

@ -1,6 +1,7 @@
"""A class to manage modifying metadata specifically for CBL/CBI"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -17,44 +18,32 @@ from __future__ import annotations
import logging
from comicapi.genericmetadata import CreditMetadata, GenericMetadata
from comictaggerlib.settings import ComicTaggerSettings
from comicapi.genericmetadata import Credit, GenericMetadata
from comictaggerlib.ctsettings import ct_ns
logger = logging.getLogger(__name__)
class CBLTransformer:
def __init__(self, metadata: GenericMetadata, settings: ComicTaggerSettings) -> None:
self.metadata = metadata
self.settings = settings
def __init__(self, metadata: GenericMetadata, config: ct_ns) -> None:
self.metadata = metadata.copy()
self.config = config
def apply(self) -> GenericMetadata:
# helper funcs
def append_to_tags_if_unique(item: str) -> None:
if item.casefold() not in (tag.casefold() for tag in self.metadata.tags):
self.metadata.tags.add(item)
def add_string_list_to_tags(str_list: str | None) -> None:
if str_list:
items = [s.strip() for s in str_list.split(",")]
for item in items:
append_to_tags_if_unique(item)
if self.settings.assume_lone_credit_is_primary:
if self.config.Metadata_Options__assume_lone_credit_is_primary:
# helper
def set_lone_primary(role_list: list[str]) -> tuple[CreditMetadata | None, int]:
lone_credit: CreditMetadata | None = None
def set_lone_primary(role_list: list[str]) -> tuple[Credit | None, int]:
lone_credit: Credit | None = None
count = 0
for c in self.metadata.credits:
if c["role"].casefold() in role_list:
if c.role.casefold() in role_list:
count += 1
lone_credit = c
if count > 1:
lone_credit = None
break
if lone_credit is not None:
lone_credit["primary"] = True
lone_credit.primary = True
return lone_credit, count
# need to loop three times, once for 'writer', 'artist', and then
@ -64,37 +53,38 @@ class CBLTransformer:
if c is None and count == 0:
c, count = set_lone_primary(["penciler", "penciller"])
if c is not None:
c["primary"] = False
self.metadata.add_credit(c["person"], "Artist", True)
c.primary = False
self.metadata.add_credit(c.person, "Artist", True)
if self.settings.copy_characters_to_tags:
add_string_list_to_tags(self.metadata.characters)
if self.config.Metadata_Options__copy_characters_to_tags:
self.metadata.tags.update(x for x in self.metadata.characters)
if self.settings.copy_teams_to_tags:
add_string_list_to_tags(self.metadata.teams)
if self.config.Metadata_Options__copy_teams_to_tags:
self.metadata.tags.update(x for x in self.metadata.teams)
if self.settings.copy_locations_to_tags:
add_string_list_to_tags(self.metadata.locations)
if self.config.Metadata_Options__copy_locations_to_tags:
self.metadata.tags.update(x for x in self.metadata.locations)
if self.settings.copy_storyarcs_to_tags:
add_string_list_to_tags(self.metadata.story_arc)
if self.config.Metadata_Options__copy_storyarcs_to_tags:
self.metadata.tags.update(x for x in self.metadata.story_arcs)
if self.settings.copy_notes_to_comments:
if self.config.Metadata_Options__copy_notes_to_comments:
if self.metadata.notes is not None:
if self.metadata.comments is None:
self.metadata.comments = ""
if self.metadata.description is None:
self.metadata.description = ""
else:
self.metadata.comments += "\n\n"
if self.metadata.notes not in self.metadata.comments:
self.metadata.comments += self.metadata.notes
self.metadata.description += "\n\n"
if self.metadata.notes not in self.metadata.description:
self.metadata.description += self.metadata.notes
if self.settings.copy_weblink_to_comments:
if self.metadata.web_link is not None:
if self.metadata.comments is None:
self.metadata.comments = ""
if self.config.Metadata_Options__copy_weblink_to_comments:
for web_link in self.metadata.web_links:
temp_desc = self.metadata.description
if temp_desc is None:
temp_desc = ""
else:
self.metadata.comments += "\n\n"
if self.metadata.web_link not in self.metadata.comments:
self.metadata.comments += self.metadata.web_link
temp_desc += "\n\n"
if web_link.url and web_link.url not in temp_desc:
self.metadata.description = temp_desc + web_link.url
return self.metadata

File diff suppressed because it is too large Load Diff

View File

@ -1,469 +0,0 @@
"""A python class to manage caching of data from Comic Vine"""
#
# Copyright 2012-2014 Anthony Beville
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import datetime
import logging
import os
import sqlite3 as lite
from typing import Any
from comictaggerlib import ctversion
from comictaggerlib.resulttypes import CVImage, CVIssuesResults, CVPublisher, CVVolumeResults, SelectDetails
from comictaggerlib.settings import ComicTaggerSettings
logger = logging.getLogger(__name__)
class ComicCacher:
def __init__(self) -> None:
self.settings_folder = ComicTaggerSettings.get_settings_folder()
self.db_file = os.path.join(self.settings_folder, "comic_cache.db")
self.version_file = os.path.join(self.settings_folder, "cache_version.txt")
# verify that cache is from same version as this one
data = ""
try:
with open(self.version_file, "rb") as f:
data = f.read().decode("utf-8")
f.close()
except Exception:
pass
if data != ctversion.version:
self.clear_cache()
if not os.path.exists(self.db_file):
self.create_cache_db()
def clear_cache(self) -> None:
try:
os.unlink(self.db_file)
except Exception:
pass
try:
os.unlink(self.version_file)
except Exception:
pass
def create_cache_db(self) -> None:
# create the version file
with open(self.version_file, "w", encoding="utf-8") as f:
f.write(ctversion.version)
# this will wipe out any existing version
open(self.db_file, "wb").close()
con = lite.connect(self.db_file)
# create tables
with con:
cur = con.cursor()
# source_name,name,id,start_year,publisher,image,description,count_of_issues
cur.execute(
"CREATE TABLE VolumeSearchCache("
+ "search_term TEXT,"
+ "id INT NOT NULL,"
+ "name TEXT,"
+ "start_year INT,"
+ "publisher TEXT,"
+ "count_of_issues INT,"
+ "image_url TEXT,"
+ "description TEXT,"
+ "timestamp DATE DEFAULT (datetime('now','localtime')),"
+ "source_name TEXT NOT NULL,"
+ "aliases TEXT)" # Newline separated
)
cur.execute(
"CREATE TABLE Volumes("
+ "id INT NOT NULL,"
+ "name TEXT,"
+ "publisher TEXT,"
+ "count_of_issues INT,"
+ "start_year INT,"
+ "timestamp DATE DEFAULT (datetime('now','localtime')), "
+ "source_name TEXT NOT NULL,"
+ "aliases TEXT," # Newline separated
+ "PRIMARY KEY (id, source_name))"
)
cur.execute(
"CREATE TABLE AltCovers("
+ "issue_id INT NOT NULL,"
+ "url_list TEXT,"
+ "timestamp DATE DEFAULT (datetime('now','localtime')), "
+ "source_name TEXT NOT NULL,"
+ "aliases TEXT," # Newline separated
+ "PRIMARY KEY (issue_id, source_name))"
)
cur.execute(
"CREATE TABLE Issues("
+ "id INT NOT NULL,"
+ "volume_id INT,"
+ "name TEXT,"
+ "issue_number TEXT,"
+ "super_url TEXT,"
+ "thumb_url TEXT,"
+ "cover_date TEXT,"
+ "site_detail_url TEXT,"
+ "description TEXT,"
+ "timestamp DATE DEFAULT (datetime('now','localtime')), "
+ "source_name TEXT NOT NULL,"
+ "aliases TEXT," # Newline separated
+ "PRIMARY KEY (id, source_name))"
)
def add_search_results(self, source_name: str, search_term: str, cv_search_results: list[CVVolumeResults]) -> None:
con = lite.connect(self.db_file)
with con:
con.text_factory = str
cur = con.cursor()
# remove all previous entries with this search term
cur.execute(
"DELETE FROM VolumeSearchCache WHERE search_term = ? AND source_name = ?",
[search_term.casefold(), source_name],
)
# now add in new results
for record in cv_search_results:
if record["publisher"] is None:
pub_name = ""
else:
pub_name = record["publisher"]["name"]
if record["image"] is None:
url = ""
else:
url = record["image"]["super_url"]
cur.execute(
"INSERT INTO VolumeSearchCache "
+ "(source_name, search_term, id, name, start_year, publisher, count_of_issues, image_url, description, aliases) "
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
source_name,
search_term.casefold(),
record["id"],
record["name"],
record["start_year"],
pub_name,
record["count_of_issues"],
url,
record["description"],
record["aliases"],
),
)
def get_search_results(self, source_name: str, search_term: str) -> list[CVVolumeResults]:
results = []
con = lite.connect(self.db_file)
with con:
con.text_factory = str
cur = con.cursor()
# purge stale search results
a_day_ago = datetime.datetime.today() - datetime.timedelta(days=1)
cur.execute("DELETE FROM VolumeSearchCache WHERE timestamp < ?", [str(a_day_ago)])
# fetch
cur.execute(
"SELECT * FROM VolumeSearchCache WHERE search_term=? AND source_name=?",
[search_term.casefold(), source_name],
)
rows = cur.fetchall()
# now process the results
for record in rows:
result = CVVolumeResults(
id=record[1],
name=record[2],
start_year=record[3],
count_of_issues=record[5],
description=record[7],
publisher=CVPublisher(name=record[4]),
image=CVImage(super_url=record[6]),
aliases=record[10],
)
results.append(result)
return results
def add_alt_covers(self, source_name: str, issue_id: int, url_list: list[str]) -> None:
con = lite.connect(self.db_file)
with con:
con.text_factory = str
cur = con.cursor()
# remove all previous entries with this search term
cur.execute("DELETE FROM AltCovers WHERE issue_id=? AND source_name=?", [issue_id, source_name])
url_list_str = ",".join(url_list)
# now add in new record
cur.execute(
"INSERT INTO AltCovers (source_name, issue_id, url_list) VALUES(?, ?, ?)",
(source_name, issue_id, url_list_str),
)
def get_alt_covers(self, source_name: str, issue_id: int) -> list[str]:
con = lite.connect(self.db_file)
with con:
cur = con.cursor()
con.text_factory = str
# purge stale issue info - probably issue data won't change
# much....
a_month_ago = datetime.datetime.today() - datetime.timedelta(days=30)
cur.execute("DELETE FROM AltCovers WHERE timestamp < ?", [str(a_month_ago)])
cur.execute("SELECT url_list FROM AltCovers WHERE issue_id=? AND source_name=?", [issue_id, source_name])
row = cur.fetchone()
if row is None:
return []
url_list_str = row[0]
if not url_list_str:
return []
url_list = str(url_list_str).split(",")
return url_list
def add_volume_info(self, source_name: str, cv_volume_record: CVVolumeResults) -> None:
con = lite.connect(self.db_file)
with con:
cur = con.cursor()
timestamp = datetime.datetime.now()
if cv_volume_record["publisher"] is None:
pub_name = ""
else:
pub_name = cv_volume_record["publisher"]["name"]
data = {
"id": cv_volume_record["id"],
"source_name": source_name,
"name": cv_volume_record["name"],
"publisher": pub_name,
"count_of_issues": cv_volume_record["count_of_issues"],
"start_year": cv_volume_record["start_year"],
"timestamp": timestamp,
"aliases": cv_volume_record["aliases"],
}
self.upsert(cur, "volumes", data)
def add_volume_issues_info(self, source_name: str, volume_id: int, cv_volume_issues: list[CVIssuesResults]) -> None:
con = lite.connect(self.db_file)
with con:
cur = con.cursor()
timestamp = datetime.datetime.now()
# add in issues
for issue in cv_volume_issues:
data = {
"id": issue["id"],
"volume_id": volume_id,
"source_name": source_name,
"name": issue["name"],
"issue_number": issue["issue_number"],
"site_detail_url": issue["site_detail_url"],
"cover_date": issue["cover_date"],
"super_url": issue["image"]["super_url"],
"thumb_url": issue["image"]["thumb_url"],
"description": issue["description"],
"timestamp": timestamp,
"aliases": issue["aliases"],
}
self.upsert(cur, "issues", data)
def get_volume_info(self, volume_id: int, source_name: str) -> CVVolumeResults | None:
result: CVVolumeResults | None = None
con = lite.connect(self.db_file)
with con:
cur = con.cursor()
con.text_factory = str
# purge stale volume info
a_week_ago = datetime.datetime.today() - datetime.timedelta(days=7)
cur.execute("DELETE FROM Volumes WHERE timestamp < ?", [str(a_week_ago)])
# fetch
cur.execute(
"SELECT source_name,id,name,publisher,count_of_issues,start_year,aliases FROM Volumes"
" WHERE id=? AND source_name=?",
[volume_id, source_name],
)
row = cur.fetchone()
if row is None:
return result
# since ID is primary key, there is only one row
result = CVVolumeResults(
id=row[1],
name=row[2],
count_of_issues=row[4],
start_year=row[5],
publisher=CVPublisher(name=row[3]),
aliases=row[6],
)
return result
def get_volume_issues_info(self, volume_id: int, source_name: str) -> list[CVIssuesResults]:
con = lite.connect(self.db_file)
with con:
cur = con.cursor()
con.text_factory = str
# purge stale issue info - probably issue data won't change
# much....
a_week_ago = datetime.datetime.today() - datetime.timedelta(days=7)
cur.execute("DELETE FROM Issues WHERE timestamp < ?", [str(a_week_ago)])
# fetch
results: list[CVIssuesResults] = []
cur.execute(
(
"SELECT source_name,id,name,issue_number,site_detail_url,cover_date,super_url,thumb_url,description,aliases"
" FROM Issues WHERE volume_id=? AND source_name=?"
),
[volume_id, source_name],
)
rows = cur.fetchall()
# now process the results
for row in rows:
record = CVIssuesResults(
id=row[1],
name=row[2],
issue_number=row[3],
site_detail_url=row[4],
cover_date=row[5],
image=CVImage(super_url=row[6], thumb_url=row[7]),
description=row[8],
aliases=row[9],
)
results.append(record)
return results
def add_issue_select_details(
self,
source_name: str,
issue_id: int,
image_url: str,
thumb_image_url: str,
cover_date: str,
site_detail_url: str,
) -> None:
con = lite.connect(self.db_file)
with con:
cur = con.cursor()
con.text_factory = str
timestamp = datetime.datetime.now()
data = {
"id": issue_id,
"source_name": source_name,
"super_url": image_url,
"thumb_url": thumb_image_url,
"cover_date": cover_date,
"site_detail_url": site_detail_url,
"timestamp": timestamp,
}
self.upsert(cur, "issues", data)
def get_issue_select_details(self, issue_id: int, source_name: str) -> SelectDetails:
con = lite.connect(self.db_file)
with con:
cur = con.cursor()
con.text_factory = str
cur.execute(
"SELECT super_url,thumb_url,cover_date,site_detail_url FROM Issues WHERE id=? AND source_name=?",
[issue_id, source_name],
)
row = cur.fetchone()
details = SelectDetails(
image_url=None,
thumb_image_url=None,
cover_date=None,
site_detail_url=None,
)
if row is not None and row[0] is not None:
details["image_url"] = row[0]
details["thumb_image_url"] = row[1]
details["cover_date"] = row[2]
details["site_detail_url"] = row[3]
return details
def upsert(self, cur: lite.Cursor, tablename: str, data: dict[str, Any]) -> None:
"""This does an insert if the given PK doesn't exist, and an
update it if does
TODO: should the cursor be created here, and not up the stack?
"""
keys = ""
vals = []
ins_slots = ""
set_slots = ""
for key in data:
if data[key] is None:
continue
if keys != "":
keys += ", "
if ins_slots != "":
ins_slots += ", "
if set_slots != "":
set_slots += ", "
keys += key
vals.append(data[key])
ins_slots += "?"
set_slots += key + " = ?"
sql_ins = f"INSERT OR REPLACE INTO {tablename} ({keys}) VALUES ({ins_slots})"
cur.execute(sql_ins, vals)

View File

@ -1,808 +0,0 @@
"""A python class to manage communication with Comic Vine's REST API"""
#
# Copyright 2012-2014 Anthony Beville
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import json
import logging
import re
import time
from datetime import datetime
from typing import Any, Callable, cast
from urllib.parse import urlencode, urljoin, urlsplit
import requests
from bs4 import BeautifulSoup
from comicapi import utils
from comicapi.genericmetadata import GenericMetadata
from comicapi.issuestring import IssueString
from comictaggerlib import ctversion, resulttypes
from comictaggerlib.comiccacher import ComicCacher
from comictaggerlib.settings import ComicTaggerSettings
logger = logging.getLogger(__name__)
try:
from PyQt5 import QtCore, QtNetwork
qt_available = True
except ImportError:
qt_available = False
logger = logging.getLogger(__name__)
class CVTypeID:
Volume = "4050"
Issue = "4000"
class ComicVineTalkerException(Exception):
Unknown = -1
Network = -2
InvalidKey = 100
RateLimit = 107
def __init__(self, code: int = -1, desc: str = "") -> None:
super().__init__()
self.desc = desc
self.code = code
def __str__(self) -> str:
if self.code in (ComicVineTalkerException.Unknown, ComicVineTalkerException.Network):
return self.desc
return f"CV error #{self.code}: [{self.desc}]. \n"
def list_fetch_complete(url_list: list[str]) -> None:
...
def url_fetch_complete(image_url: str, thumb_url: str | None) -> None:
...
class ComicVineTalker:
logo_url = "http://static.comicvine.com/bundles/comicvinesite/images/logo.png"
api_key = ""
api_base_url = ""
alt_url_list_fetch_complete = list_fetch_complete
url_fetch_complete = url_fetch_complete
@staticmethod
def get_rate_limit_message() -> str:
if ComicVineTalker.api_key == "":
return "Comic Vine rate limit exceeded. You should configure your own Comic Vine API key."
return "Comic Vine rate limit exceeded. Please wait a bit."
def __init__(self, series_match_thresh: int = 90) -> None:
# Identity name for the information source
self.source_name = "comicvine"
self.wait_for_rate_limit = False
self.series_match_thresh = series_match_thresh
# key that is registered to comictagger
default_api_key = "27431e6787042105bd3e47e169a624521f89f3a4"
default_url = "https://comicvine.gamespot.com/api"
self.issue_id: int | None = None
self.api_key = ComicVineTalker.api_key or default_api_key
tmp_url = urlsplit(ComicVineTalker.api_base_url or default_url)
# joinurl only works properly if there is a trailing slash
if tmp_url.path and tmp_url.path[-1] != "/":
tmp_url = tmp_url._replace(path=tmp_url.path + "/")
self.api_base_url = tmp_url.geturl()
self.log_func: Callable[[str], None] | None = None
if qt_available:
self.nam = QtNetwork.QNetworkAccessManager()
def set_log_func(self, log_func: Callable[[str], None]) -> None:
self.log_func = log_func
def write_log(self, text: str) -> None:
if self.log_func is None:
logger.info(text)
else:
self.log_func(text)
def parse_date_str(self, date_str: str) -> tuple[int | None, int | None, int | None]:
return utils.parse_date_str(date_str)
def test_key(self, key: str, url: str) -> bool:
if not url:
url = self.api_base_url
try:
test_url = urljoin(url, "issue/1/")
cv_response: resulttypes.CVResult = requests.get(
test_url,
headers={"user-agent": "comictagger/" + ctversion.version},
params={
"api_key": key,
"format": "json",
"field_list": "name",
},
).json()
# Bogus request, but if the key is wrong, you get error 100: "Invalid API Key"
return cv_response["status_code"] != 100
except Exception:
return False
def get_cv_content(self, url: str, params: dict[str, Any]) -> resulttypes.CVResult:
"""
Get the content from the CV server. If we're in "wait mode" and status code is a rate limit error
sleep for a bit and retry.
"""
total_time_waited = 0
limit_wait_time = 1
counter = 0
wait_times = [1, 2, 3, 4]
while True:
cv_response: resulttypes.CVResult = self.get_url_content(url, params)
if self.wait_for_rate_limit and cv_response["status_code"] == ComicVineTalkerException.RateLimit:
self.write_log(f"Rate limit encountered. Waiting for {limit_wait_time} minutes\n")
time.sleep(limit_wait_time * 60)
total_time_waited += limit_wait_time
limit_wait_time = wait_times[counter]
if counter < 3:
counter += 1
# don't wait much more than 20 minutes
if total_time_waited < 20:
continue
if cv_response["status_code"] != 1:
self.write_log(
f"Comic Vine query failed with error #{cv_response['status_code']}: [{cv_response['error']}]. \n"
)
raise ComicVineTalkerException(cv_response["status_code"], cv_response["error"])
# it's all good
break
return cv_response
def get_url_content(self, url: str, params: dict[str, Any]) -> Any:
# connect to server:
# if there is a 500 error, try a few more times before giving up
# any other error, just bail
for tries in range(3):
try:
resp = requests.get(url, params=params, headers={"user-agent": "comictagger/" + ctversion.version})
if resp.status_code == 200:
return resp.json()
if resp.status_code == 500:
self.write_log(f"Try #{tries + 1}: ")
time.sleep(1)
self.write_log(str(resp.status_code) + "\n")
else:
break
except requests.exceptions.RequestException as e:
self.write_log(f"{e}\n")
raise ComicVineTalkerException(ComicVineTalkerException.Network, "Network Error!") from e
except json.JSONDecodeError as e:
self.write_log(f"{e}\n")
raise ComicVineTalkerException(ComicVineTalkerException.Unknown, "ComicVine did not provide json")
raise ComicVineTalkerException(
ComicVineTalkerException.Unknown, f"Error on Comic Vine server: {resp.status_code}"
)
def search_for_series(
self,
series_name: str,
callback: Callable[[int, int], None] | None = None,
refresh_cache: bool = False,
literal: bool = False,
) -> list[resulttypes.CVVolumeResults]:
# Sanitize the series name for comicvine searching, comicvine search ignore symbols
search_series_name = utils.sanitize_title(series_name, literal)
logger.info("Searching: %s", search_series_name)
# Before we search online, look in our cache, since we might have done this same search recently
# For literal searches always retrieve from online
cvc = ComicCacher()
if not refresh_cache and not literal:
cached_search_results = cvc.get_search_results(self.source_name, series_name)
if len(cached_search_results) > 0:
return cached_search_results
params = {
"api_key": self.api_key,
"format": "json",
"resources": "volume",
"query": search_series_name,
"field_list": "volume,name,id,start_year,publisher,image,description,count_of_issues,aliases",
"page": 1,
"limit": 100,
}
cv_response = self.get_cv_content(urljoin(self.api_base_url, "search"), params)
search_results: list[resulttypes.CVVolumeResults] = []
# see http://api.comicvine.com/documentation/#handling_responses
current_result_count = cv_response["number_of_page_results"]
total_result_count = cv_response["number_of_total_results"]
# 8 Dec 2018 - Comic Vine changed query results again. Terms are now
# ORed together, and we get thousands of results. Good news is the
# results are sorted by relevance, so we can be smart about halting the search.
# 1. Don't fetch more than some sane amount of pages.
# 2. Halt when any result on the current page is less than or equal to a set ratio using rapidfuzz
max_results = 500 # 5 pages
total_result_count = min(total_result_count, max_results)
if callback is None:
self.write_log(
f"Found {cv_response['number_of_page_results']} of {cv_response['number_of_total_results']} results\n"
)
search_results.extend(cast(list[resulttypes.CVVolumeResults], cv_response["results"]))
page = 1
if callback is not None:
callback(current_result_count, total_result_count)
# see if we need to keep asking for more pages...
while current_result_count < total_result_count:
if not literal:
# Stop searching once any entry falls below the threshold
stop_searching = any(
not utils.titles_match(search_series_name, volume["name"], self.series_match_thresh)
for volume in cast(list[resulttypes.CVVolumeResults], cv_response["results"])
)
if stop_searching:
break
if callback is None:
self.write_log(f"getting another page of results {current_result_count} of {total_result_count}...\n")
page += 1
params["page"] = page
cv_response = self.get_cv_content(urljoin(self.api_base_url, "search"), params)
search_results.extend(cast(list[resulttypes.CVVolumeResults], cv_response["results"]))
current_result_count += cv_response["number_of_page_results"]
if callback is not None:
callback(current_result_count, total_result_count)
# Cache these search results, even if it's literal we cache the results
# The most it will cause is extra processing time
cvc.add_search_results(self.source_name, series_name, search_results)
return search_results
def fetch_volume_data(self, series_id: int) -> resulttypes.CVVolumeResults:
# before we search online, look in our cache, since we might already have this info
cvc = ComicCacher()
cached_volume_result = cvc.get_volume_info(series_id, self.source_name)
if cached_volume_result is not None:
return cached_volume_result
volume_url = urljoin(self.api_base_url, f"volume/{CVTypeID.Volume}-{series_id}")
params = {
"api_key": self.api_key,
"format": "json",
"field_list": "name,id,start_year,publisher,count_of_issues,aliases",
}
cv_response = self.get_cv_content(volume_url, params)
volume_results = cast(resulttypes.CVVolumeResults, cv_response["results"])
if volume_results:
cvc.add_volume_info(self.source_name, volume_results)
return volume_results
def fetch_issues_by_volume(self, series_id: int) -> list[resulttypes.CVIssuesResults]:
# before we search online, look in our cache, since we might already have this info
volume_data = self.fetch_volume_data(series_id)
cvc = ComicCacher()
cached_volume_issues_result = cvc.get_volume_issues_info(series_id, self.source_name)
if len(cached_volume_issues_result) >= volume_data["count_of_issues"]:
return cached_volume_issues_result
params = {
"api_key": self.api_key,
"filter": f"volume:{series_id}",
"format": "json",
"field_list": "id,volume,issue_number,name,image,cover_date,site_detail_url,description,aliases",
"offset": 0,
}
cv_response = self.get_cv_content(urljoin(self.api_base_url, "issues/"), params)
current_result_count = cv_response["number_of_page_results"]
total_result_count = cv_response["number_of_total_results"]
volume_issues_result = cast(list[resulttypes.CVIssuesResults], cv_response["results"])
page = 1
offset = 0
# see if we need to keep asking for more pages...
while current_result_count < total_result_count:
page += 1
offset += cv_response["number_of_page_results"]
params["offset"] = offset
cv_response = self.get_cv_content(urljoin(self.api_base_url, "issues/"), params)
volume_issues_result.extend(cast(list[resulttypes.CVIssuesResults], cv_response["results"]))
current_result_count += cv_response["number_of_page_results"]
self.repair_urls(volume_issues_result)
cvc.add_volume_issues_info(self.source_name, series_id, volume_issues_result)
return volume_issues_result
def fetch_issues_by_volume_issue_num_and_year(
self, volume_id_list: list[int], issue_number: str, year: str | int | None
) -> list[resulttypes.CVIssuesResults]:
volume_filter = ""
for vid in volume_id_list:
volume_filter += str(vid) + "|"
flt = f"volume:{volume_filter},issue_number:{issue_number}"
int_year = utils.xlate(year, True)
if int_year is not None:
flt += f",cover_date:{int_year}-1-1|{int_year+1}-1-1"
params: dict[str, str | int] = {
"api_key": self.api_key,
"format": "json",
"field_list": "id,volume,issue_number,name,image,cover_date,site_detail_url,description,aliases",
"filter": flt,
}
cv_response = self.get_cv_content(urljoin(self.api_base_url, "issues/"), params)
current_result_count = cv_response["number_of_page_results"]
total_result_count = cv_response["number_of_total_results"]
filtered_issues_result = cast(list[resulttypes.CVIssuesResults], cv_response["results"])
page = 1
offset = 0
# see if we need to keep asking for more pages...
while current_result_count < total_result_count:
page += 1
offset += cv_response["number_of_page_results"]
params["offset"] = offset
cv_response = self.get_cv_content(urljoin(self.api_base_url, "issues/"), params)
filtered_issues_result.extend(cast(list[resulttypes.CVIssuesResults], cv_response["results"]))
current_result_count += cv_response["number_of_page_results"]
self.repair_urls(filtered_issues_result)
cvc = ComicCacher()
for c in filtered_issues_result:
cvc.add_volume_issues_info(self.source_name, c["volume"]["id"], [c])
return filtered_issues_result
def fetch_issue_data(self, series_id: int, issue_number: str, settings: ComicTaggerSettings) -> GenericMetadata:
volume_results = self.fetch_volume_data(series_id)
issues_list_results = self.fetch_issues_by_volume(series_id)
f_record = None
for record in issues_list_results:
if not IssueString(issue_number).as_string():
issue_number = "1"
if (
IssueString(record["issue_number"]).as_string().casefold()
== IssueString(issue_number).as_string().casefold()
):
f_record = record
break
if f_record is not None:
issue_url = urljoin(self.api_base_url, f"issue/{CVTypeID.Issue}-{f_record['id']}")
params = {"api_key": self.api_key, "format": "json"}
cv_response = self.get_cv_content(issue_url, params)
issue_results = cast(resulttypes.CVIssueDetailResults, cv_response["results"])
else:
return GenericMetadata()
# Now, map the Comic Vine data to generic metadata
return self.map_cv_data_to_metadata(volume_results, issue_results, settings)
def fetch_issue_data_by_issue_id(self, issue_id: int, settings: ComicTaggerSettings) -> GenericMetadata:
issue_url = urljoin(self.api_base_url, f"issue/{CVTypeID.Issue}-{issue_id}")
params = {"api_key": self.api_key, "format": "json"}
cv_response = self.get_cv_content(issue_url, params)
issue_results = cast(resulttypes.CVIssueDetailResults, cv_response["results"])
volume_results = self.fetch_volume_data(issue_results["volume"]["id"])
# Now, map the Comic Vine data to generic metadata
md = self.map_cv_data_to_metadata(volume_results, issue_results, settings)
md.is_empty = False
return md
def map_cv_data_to_metadata(
self,
volume_results: resulttypes.CVVolumeResults,
issue_results: resulttypes.CVIssueDetailResults,
settings: ComicTaggerSettings,
) -> GenericMetadata:
# Now, map the Comic Vine data to generic metadata
metadata = GenericMetadata()
metadata.is_empty = False
metadata.series = utils.xlate(issue_results["volume"]["name"])
metadata.issue = IssueString(issue_results["issue_number"]).as_string()
metadata.title = utils.xlate(issue_results["name"])
if volume_results["publisher"] is not None:
metadata.publisher = utils.xlate(volume_results["publisher"]["name"])
metadata.day, metadata.month, metadata.year = self.parse_date_str(issue_results["cover_date"])
metadata.comments = self.cleanup_html(issue_results["description"], settings.remove_html_tables)
if settings.use_series_start_as_volume:
metadata.volume = int(volume_results["start_year"])
metadata.notes = (
f"Tagged with ComicTagger {ctversion.version} using info from Comic Vine on"
f" {datetime.now():%Y-%m-%d %H:%M:%S}. [Issue ID {issue_results['id']}]"
)
metadata.web_link = issue_results["site_detail_url"]
person_credits = issue_results["person_credits"]
for person in person_credits:
if "role" in person:
roles = person["role"].split(",")
for role in roles:
# can we determine 'primary' from CV??
metadata.add_credit(person["name"], role.title().strip(), False)
character_credits = issue_results["character_credits"]
character_list = []
for character in character_credits:
character_list.append(character["name"])
metadata.characters = ", ".join(character_list)
team_credits = issue_results["team_credits"]
team_list = []
for team in team_credits:
team_list.append(team["name"])
metadata.teams = ", ".join(team_list)
location_credits = issue_results["location_credits"]
location_list = []
for location in location_credits:
location_list.append(location["name"])
metadata.locations = ", ".join(location_list)
story_arc_credits = issue_results["story_arc_credits"]
arc_list = []
for arc in story_arc_credits:
arc_list.append(arc["name"])
if len(arc_list) > 0:
metadata.story_arc = ", ".join(arc_list)
return metadata
def cleanup_html(self, string: str, remove_html_tables: bool) -> str:
if string is None:
return ""
# find any tables
soup = BeautifulSoup(string, "html.parser")
tables = soup.findAll("table")
# remove all newlines first
string = string.replace("\n", "")
# put in our own
string = string.replace("<br>", "\n")
string = string.replace("</li>", "\n")
string = string.replace("</p>", "\n\n")
string = string.replace("<h1>", "*")
string = string.replace("</h1>", "*\n")
string = string.replace("<h2>", "*")
string = string.replace("</h2>", "*\n")
string = string.replace("<h3>", "*")
string = string.replace("</h3>", "*\n")
string = string.replace("<h4>", "*")
string = string.replace("</h4>", "*\n")
string = string.replace("<h5>", "*")
string = string.replace("</h5>", "*\n")
string = string.replace("<h6>", "*")
string = string.replace("</h6>", "*\n")
# remove the tables
p = re.compile(r"<table[^<]*?>.*?</table>")
if remove_html_tables:
string = p.sub("", string)
string = string.replace("*List of covers and their creators:*", "")
else:
string = p.sub("{}", string)
# now strip all other tags
p = re.compile(r"<[^<]*?>")
newstring = p.sub("", string)
newstring = newstring.replace("&nbsp;", " ")
newstring = newstring.replace("&amp;", "&")
newstring = newstring.strip()
if not remove_html_tables:
# now rebuild the tables into text from BSoup
try:
table_strings = []
for table in tables:
rows = []
hdrs = []
col_widths = []
for hdr in table.findAll("th"):
item = hdr.string.strip()
hdrs.append(item)
col_widths.append(len(item))
rows.append(hdrs)
for row in table.findAll("tr"):
cols = []
col = row.findAll("td")
i = 0
for c in col:
item = c.string.strip()
cols.append(item)
if len(item) > col_widths[i]:
col_widths[i] = len(item)
i += 1
if len(cols) != 0:
rows.append(cols)
# now we have the data, make it into text
fmtstr = ""
for w in col_widths:
fmtstr += f" {{:{w + 1}}}|"
width = sum(col_widths) + len(col_widths) * 2
table_text = ""
counter = 0
for row in rows:
table_text += fmtstr.format(*row) + "\n"
if counter == 0 and len(hdrs) != 0:
table_text += "-" * width + "\n"
counter += 1
table_strings.append(table_text)
newstring = newstring.format(*table_strings)
except Exception:
# we caught an error rebuilding the table.
# just bail and remove the formatting
logger.exception("table parse error")
newstring.replace("{}", "")
return newstring
def fetch_issue_date(self, issue_id: int) -> tuple[int | None, int | None]:
details = self.fetch_issue_select_details(issue_id)
_, month, year = self.parse_date_str(details["cover_date"] or "")
return month, year
def fetch_issue_cover_urls(self, issue_id: int) -> tuple[str | None, str | None]:
details = self.fetch_issue_select_details(issue_id)
return details["image_url"], details["thumb_image_url"]
def fetch_issue_page_url(self, issue_id: int) -> str | None:
details = self.fetch_issue_select_details(issue_id)
return details["site_detail_url"]
def fetch_issue_select_details(self, issue_id: int) -> resulttypes.SelectDetails:
cached_details = self.fetch_cached_issue_select_details(issue_id)
if cached_details["image_url"] is not None:
return cached_details
issue_url = urljoin(self.api_base_url, f"issue/{CVTypeID.Issue}-{issue_id}")
logger.error("%s, %s", self.api_base_url, issue_url)
params = {"api_key": self.api_key, "format": "json", "field_list": "image,cover_date,site_detail_url"}
cv_response = self.get_cv_content(issue_url, params)
results = cast(resulttypes.CVIssueDetailResults, cv_response["results"])
details = resulttypes.SelectDetails(
image_url=results["image"]["super_url"],
thumb_image_url=results["image"]["thumb_url"],
cover_date=results["cover_date"],
site_detail_url=results["site_detail_url"],
)
if (
details["image_url"] is not None
and details["thumb_image_url"] is not None
and details["cover_date"] is not None
and details["site_detail_url"] is not None
):
self.cache_issue_select_details(
issue_id,
details["image_url"],
details["thumb_image_url"],
details["cover_date"],
details["site_detail_url"],
)
return details
def fetch_cached_issue_select_details(self, issue_id: int) -> resulttypes.SelectDetails:
# before we search online, look in our cache, since we might already have this info
cvc = ComicCacher()
return cvc.get_issue_select_details(issue_id, self.source_name)
def cache_issue_select_details(
self, issue_id: int, image_url: str, thumb_url: str, cover_date: str, page_url: str
) -> None:
cvc = ComicCacher()
cvc.add_issue_select_details(self.source_name, issue_id, image_url, thumb_url, cover_date, page_url)
def fetch_alternate_cover_urls(self, issue_id: int, issue_page_url: str) -> list[str]:
url_list = self.fetch_cached_alternate_cover_urls(issue_id)
if url_list:
return url_list
# scrape the CV issue page URL to get the alternate cover URLs
content = requests.get(issue_page_url, headers={"user-agent": "comictagger/" + ctversion.version}).text
alt_cover_url_list = self.parse_out_alt_cover_urls(content)
# cache this alt cover URL list
self.cache_alternate_cover_urls(issue_id, alt_cover_url_list)
return alt_cover_url_list
def parse_out_alt_cover_urls(self, page_html: str) -> list[str]:
soup = BeautifulSoup(page_html, "html.parser")
alt_cover_url_list = []
# Using knowledge of the layout of the Comic Vine issue page here:
# look for the divs that are in the classes 'imgboxart' and 'issue-cover'
div_list = soup.find_all("div")
covers_found = 0
for d in div_list:
if "class" in d.attrs:
c = d["class"]
if "imgboxart" in c and "issue-cover" in c:
if d.img["src"].startswith("http"):
covers_found += 1
if covers_found != 1:
alt_cover_url_list.append(d.img["src"])
elif d.img["data-src"].startswith("http"):
covers_found += 1
if covers_found != 1:
alt_cover_url_list.append(d.img["data-src"])
return alt_cover_url_list
def fetch_cached_alternate_cover_urls(self, issue_id: int) -> list[str]:
# before we search online, look in our cache, since we might already have this info
cvc = ComicCacher()
url_list = cvc.get_alt_covers(self.source_name, issue_id)
return url_list
def cache_alternate_cover_urls(self, issue_id: int, url_list: list[str]) -> None:
cvc = ComicCacher()
cvc.add_alt_covers(self.source_name, issue_id, url_list)
def async_fetch_issue_cover_urls(self, issue_id: int) -> None:
self.issue_id = issue_id
details = self.fetch_cached_issue_select_details(issue_id)
if details["image_url"] is not None:
ComicVineTalker.url_fetch_complete(details["image_url"], details["thumb_image_url"])
return
issue_url = urlsplit(self.api_base_url)
issue_url = issue_url._replace(
query=urlencode(
{
"api_key": self.api_key,
"format": "json",
"field_list": "image,cover_date,site_detail_url",
}
),
path=f"issue/{CVTypeID.Issue}-{issue_id}",
)
self.nam.finished.connect(self.async_fetch_issue_cover_url_complete)
self.nam.get(QtNetwork.QNetworkRequest(QtCore.QUrl(issue_url.geturl())))
def async_fetch_issue_cover_url_complete(self, reply: QtNetwork.QNetworkReply) -> None:
# read in the response
data = reply.readAll()
try:
cv_response = cast(resulttypes.CVResult, json.loads(bytes(data)))
except Exception:
logger.exception("Comic Vine query failed to get JSON data\n%s", str(data))
return
if cv_response["status_code"] != 1:
logger.error("Comic Vine query failed with error: [%s]. ", cv_response["error"])
return
result = cast(resulttypes.CVIssuesResults, cv_response["results"])
image_url = result["image"]["super_url"]
thumb_url = result["image"]["thumb_url"]
cover_date = result["cover_date"]
page_url = result["site_detail_url"]
self.cache_issue_select_details(cast(int, self.issue_id), image_url, thumb_url, cover_date, page_url)
ComicVineTalker.url_fetch_complete(image_url, thumb_url)
def async_fetch_alternate_cover_urls(self, issue_id: int, issue_page_url: str) -> None:
# This async version requires the issue page url to be provided!
self.issue_id = issue_id
url_list = self.fetch_cached_alternate_cover_urls(issue_id)
if url_list:
ComicVineTalker.alt_url_list_fetch_complete(url_list)
return
self.nam.finished.connect(self.async_fetch_alternate_cover_urls_complete)
self.nam.get(QtNetwork.QNetworkRequest(QtCore.QUrl(str(issue_page_url))))
def async_fetch_alternate_cover_urls_complete(self, reply: QtNetwork.QNetworkReply) -> None:
# read in the response
html = str(reply.readAll())
alt_cover_url_list = self.parse_out_alt_cover_urls(html)
# cache this alt cover URL list
self.cache_alternate_cover_urls(cast(int, self.issue_id), alt_cover_url_list)
ComicVineTalker.alt_url_list_fetch_complete(alt_cover_url_list)
def repair_urls(
self,
issue_list: list[resulttypes.CVIssuesResults]
| list[resulttypes.CVVolumeResults]
| list[resulttypes.CVIssueDetailResults],
) -> None:
# make sure there are URLs for the image fields
for issue in issue_list:
if issue["image"] is None:
issue["image"] = resulttypes.CVImage(
super_url=ComicVineTalker.logo_url,
thumb_url=ComicVineTalker.logo_url,
)

View File

@ -1,10 +1,11 @@
"""A PyQt5 widget to display cover images
Display cover images from either a local archive, or from Comic Vine.
Display cover images from either a local archive, or from comic source metadata.
TODO: This should be re-factored using subclasses!
"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -20,19 +21,16 @@ TODO: This should be re-factored using subclasses!
from __future__ import annotations
import logging
from typing import Callable, cast
import pathlib
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from comicapi import utils
from comicapi.comicarchive import ComicArchive
from comictaggerlib.comicvinetalker import ComicVineTalker
from comictaggerlib.graphics import graphics_path
from comictaggerlib.imagefetcher import ImageFetcher
from comictaggerlib.imagepopup import ImagePopup
from comictaggerlib.pageloader import PageLoader
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import get_qimage_from_data, reduce_widget_font_size
from comictaggerlib.ui.qtutils import get_qimage_from_data
logger = logging.getLogger(__name__)
@ -41,7 +39,6 @@ def clickable(widget: QtWidgets.QWidget) -> QtCore.pyqtBoundSignal:
"""Allow a label to be clickable"""
class Filter(QtCore.QObject):
dblclicked = QtCore.pyqtSignal()
def eventFilter(self, obj: QtCore.QObject, event: QtCore.QEvent) -> bool:
@ -56,58 +53,45 @@ def clickable(widget: QtWidgets.QWidget) -> QtCore.pyqtBoundSignal:
return flt.dblclicked
class Signal(QtCore.QObject):
alt_url_list_fetch_complete = QtCore.pyqtSignal(list)
url_fetch_complete = QtCore.pyqtSignal(str, str)
image_fetch_complete = QtCore.pyqtSignal(QtCore.QByteArray)
def __init__(
self,
list_fetch: Callable[[list[str]], None],
url_fetch: Callable[[str, str], None],
image_fetch: Callable[[bytes], None],
) -> None:
super().__init__()
self.alt_url_list_fetch_complete.connect(list_fetch)
self.url_fetch_complete.connect(url_fetch)
self.image_fetch_complete.connect(image_fetch)
def emit_list(self, url_list: list[str]) -> None:
self.alt_url_list_fetch_complete.emit(url_list)
def emit_url(self, image_url: str, thumb_url: str | None) -> None:
self.url_fetch_complete.emit(image_url, thumb_url)
def emit_image(self, image_data: bytes | QtCore.QByteArray) -> None:
self.image_fetch_complete.emit(image_data)
class CoverImageWidget(QtWidgets.QWidget):
ArchiveMode = 0
AltCoverMode = 1
URLMode = 1
DataMode = 3
def __init__(self, parent: QtWidgets.QWidget, mode: int, expand_on_click: bool = True) -> None:
image_fetch_complete = QtCore.pyqtSignal(str, QtCore.QByteArray)
def __init__(
self,
parent: QtWidgets.QWidget,
mode: int,
cache_folder: pathlib.Path | None,
blur: bool = False,
expand_on_click: bool = True,
) -> None:
super().__init__(parent)
self.cover_fetcher = ImageFetcher()
uic.loadUi(ui_path / "coverimagewidget.ui", self)
reduce_widget_font_size(self.label)
self.sig = Signal(
self.alt_cover_url_list_fetch_complete, self.primary_url_fetch_complete, self.cover_remote_fetch_complete
)
if mode not in (self.AltCoverMode, self.URLMode) or cache_folder is None:
self.cover_fetcher = None
self.talker = None
else:
self.cover_fetcher = ImageFetcher(cache_folder)
self.talker = None
with (ui_path / "coverimagewidget.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.cache_folder = cache_folder
self.mode: int = mode
self.page_loader: PageLoader | None = None
self.showControls = True
self.blur = blur
self.scene = QtWidgets.QGraphicsScene(parent=self)
self.current_pixmap = QtGui.QPixmap()
self.comic_archive: ComicArchive | None = None
self.issue_id: int | None = None
self.issue_id: str = ""
self.issue_url: str | None = None
self.url_list: list[str] = []
if self.page_loader is not None:
self.page_loader.abandoned = True
@ -116,21 +100,24 @@ class CoverImageWidget(QtWidgets.QWidget):
self.imageCount = 1
self.imageData = b""
self.btnLeft.setIcon(QtGui.QIcon(str(graphics_path / "left.png")))
self.btnRight.setIcon(QtGui.QIcon(str(graphics_path / "right.png")))
self.btnLeft.setIcon(QtGui.QIcon(":/graphics/left.png"))
self.btnRight.setIcon(QtGui.QIcon(":/graphics/right.png"))
self.btnLeft.clicked.connect(self.decrement_image)
self.btnRight.clicked.connect(self.increment_image)
self.image_fetch_complete.connect(self.cover_remote_fetch_complete)
if expand_on_click:
clickable(self.lblImage).connect(self.show_popup)
clickable(self.graphicsView).connect(self.show_popup)
else:
self.lblImage.setToolTip("")
self.graphicsView.setToolTip("")
self.graphicsView.setScene(self.scene)
self.update_content()
def reset_widget(self) -> None:
self.comic_archive = None
self.issue_id = None
self.issue_id = ""
self.issue_url = None
self.url_list = []
if self.page_loader is not None:
self.page_loader.abandoned = True
@ -173,15 +160,13 @@ class CoverImageWidget(QtWidgets.QWidget):
self.imageCount = 1
self.update_content()
def set_issue_id(self, issue_id: int) -> None:
def set_issue_details(self, issue_id: str, url_list: list[str]) -> None:
if self.mode == CoverImageWidget.AltCoverMode:
self.reset_widget()
self.update_content()
self.issue_id = issue_id
comic_vine = ComicVineTalker()
ComicVineTalker.url_fetch_complete = self.sig.emit_url
comic_vine.async_fetch_issue_cover_urls(self.issue_id)
self.set_url_list(url_list)
def set_image_data(self, image_data: bytes) -> None:
if self.mode == CoverImageWidget.DataMode:
@ -195,31 +180,11 @@ class CoverImageWidget(QtWidgets.QWidget):
self.update_content()
def primary_url_fetch_complete(self, primary_url: str, thumb_url: str | None = None) -> None:
self.url_list.append(str(primary_url))
def set_url_list(self, url_list: list[str]) -> None:
self.url_list = url_list
self.imageIndex = 0
self.imageCount = len(self.url_list)
self.update_content()
# defer the alt cover search
QtCore.QTimer.singleShot(1, self.start_alt_cover_search)
def start_alt_cover_search(self) -> None:
if self.issue_id is not None:
# now we need to get the list of alt cover URLs
self.label.setText("Searching for alt. covers...")
# page URL should already be cached, so no need to defer
comic_vine = ComicVineTalker()
issue_page_url = comic_vine.fetch_issue_page_url(self.issue_id)
ComicVineTalker.alt_url_list_fetch_complete = self.sig.emit_list
comic_vine.async_fetch_alternate_cover_urls(utils.xlate(self.issue_id), cast(str, issue_page_url))
def alt_cover_url_list_fetch_complete(self, url_list: list[str]) -> None:
if url_list:
self.url_list.extend(url_list)
self.imageCount = len(self.url_list)
self.update_controls()
def set_page(self, pagenum: int) -> None:
@ -237,7 +202,7 @@ class CoverImageWidget(QtWidgets.QWidget):
elif self.mode in [CoverImageWidget.AltCoverMode, CoverImageWidget.URLMode]:
self.load_url()
elif self.mode == CoverImageWidget.DataMode:
self.cover_remote_fetch_complete(self.imageData)
self.cover_remote_fetch_complete("", self.imageData)
else:
self.load_page()
@ -267,13 +232,18 @@ class CoverImageWidget(QtWidgets.QWidget):
self.label.setText(f"Page {self.imageIndex + 1} (of {self.imageCount})")
def load_url(self) -> None:
assert isinstance(self.cache_folder, pathlib.Path)
self.load_default()
self.cover_fetcher = ImageFetcher()
ImageFetcher.image_fetch_complete = self.sig.emit_image
self.cover_fetcher.fetch(self.url_list[self.imageIndex])
self.cover_fetcher = ImageFetcher(self.cache_folder)
ImageFetcher.image_fetch_complete = self.image_fetch_complete.emit
data = self.cover_fetcher.fetch(self.url_list[self.imageIndex])
if data:
self.cover_remote_fetch_complete(self.url_list[self.imageIndex], data)
# called when the image is done loading from internet
def cover_remote_fetch_complete(self, image_data: bytes) -> None:
def cover_remote_fetch_complete(self, url: str, image_data: bytes) -> None:
if url and url not in self.url_list:
return
img = get_qimage_from_data(image_data)
self.current_pixmap = QtGui.QPixmap.fromImage(img)
self.set_display_pixmap()
@ -293,7 +263,7 @@ class CoverImageWidget(QtWidgets.QWidget):
self.page_loader = None
def load_default(self) -> None:
self.current_pixmap = QtGui.QPixmap(str(graphics_path / "nocover.png"))
self.current_pixmap = QtGui.QPixmap(":/graphics/nocover.png")
self.set_display_pixmap()
def resizeEvent(self, resize_event: QtGui.QResizeEvent) -> None:
@ -303,28 +273,36 @@ class CoverImageWidget(QtWidgets.QWidget):
def set_display_pixmap(self) -> None:
"""The deltas let us know what the new width and height of the label will be"""
new_h = self.frame.height()
new_w = self.frame.width()
new_h = self.frame.height()
frame_w = self.frame.width()
frame_h = self.frame.height()
new_h -= 4
new_w -= 4
new_h -= 8
new_w -= 8
new_h = max(new_h, 0)
new_w = max(new_w, 0)
# scale the pixmap to fit in the frame
scaled_pixmap = self.current_pixmap.scaled(
new_w, new_h, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.SmoothTransformation
new_w, new_h, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.SmoothTransformation
)
self.lblImage.setPixmap(scaled_pixmap)
self.scene.clear()
qpix = self.scene.addPixmap(scaled_pixmap)
assert qpix
if self.blur:
blur = QtWidgets.QGraphicsBlurEffect(parent=self)
blur.setBlurHints(QtWidgets.QGraphicsBlurEffect.BlurHint.PerformanceHint)
blur.setBlurRadius(30)
qpix.setGraphicsEffect(blur)
# move and resize the label to be centered in the fame
img_w = scaled_pixmap.width()
img_h = scaled_pixmap.height()
self.lblImage.resize(img_w, img_h)
self.lblImage.move(int((frame_w - img_w) / 2), int((frame_h - img_h) / 2))
self.scene.setSceneRect(0, 0, img_w, img_h)
self.graphicsView.resize(img_w + 2, img_h + 2)
self.graphicsView.move(int((frame_w - img_w) / 2), int((frame_h - img_h) / 2))
def show_popup(self) -> None:
ImagePopup(self, self.current_pixmap)

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to edit credits"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -16,10 +17,13 @@
from __future__ import annotations
import logging
from typing import Any
import operator
import natsort
from PyQt5 import QtWidgets, uic
from comicapi import utils
from comicapi.genericmetadata import Credit
from comictaggerlib.ui import ui_path
logger = logging.getLogger(__name__)
@ -29,10 +33,11 @@ class CreditEditorWindow(QtWidgets.QDialog):
ModeEdit = 0
ModeNew = 1
def __init__(self, parent: QtWidgets.QWidget, mode: int, role: str, name: str, primary: bool) -> None:
def __init__(self, parent: QtWidgets.QWidget, mode: int, credit: Credit) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "crediteditorwindow.ui", self)
with (ui_path / "crediteditorwindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.mode = mode
@ -43,54 +48,47 @@ class CreditEditorWindow(QtWidgets.QDialog):
# Add the entries to the role combobox
self.cbRole.addItem("")
self.cbRole.addItem("Writer")
self.cbRole.addItem("Artist")
self.cbRole.addItem("Penciller")
self.cbRole.addItem("Inker")
self.cbRole.addItem("Colorist")
self.cbRole.addItem("Letterer")
self.cbRole.addItem("Cover Artist")
self.cbRole.addItem("Editor")
self.cbRole.addItem("Other")
self.cbRole.addItem("Inker")
self.cbRole.addItem("Letterer")
self.cbRole.addItem("Penciller")
self.cbRole.addItem("Plotter")
self.cbRole.addItem("Scripter")
self.cbRole.addItem("Translator")
self.cbRole.addItem("Writer")
self.cbRole.addItem("Other")
self.leName.setText(name)
self.cbLanguage.addItem("", "")
for f in natsort.humansorted(utils.languages().items(), operator.itemgetter(1)):
self.cbLanguage.addItem(f[1], f[0])
if role is not None and role != "":
i = self.cbRole.findText(role)
self.leName.setText(credit.person)
if credit.role is not None and credit.role != "":
i = self.cbRole.findText(credit.role)
if i == -1:
self.cbRole.setEditText(role)
self.cbRole.setEditText(credit.role)
else:
self.cbRole.setCurrentIndex(i)
self.cbPrimary.setChecked(primary)
if credit.language != "":
i = self.cbLanguage.findText(credit.language)
if i == -1:
self.cbLanguage.setEditText(credit.language)
else:
self.cbLanguage.setCurrentIndex(i)
self.cbRole.currentIndexChanged.connect(self.role_changed)
self.cbRole.editTextChanged.connect(self.role_changed)
self.cbPrimary.setChecked(credit.primary)
self.update_primary_button()
def update_primary_button(self) -> None:
enabled = self.current_role_can_be_primary()
self.cbPrimary.setEnabled(enabled)
def current_role_can_be_primary(self) -> bool:
role = self.cbRole.currentText()
if role.casefold() in ("artist", "writer"):
return True
return False
def role_changed(self, s: Any) -> None:
self.update_primary_button()
def get_credits(self) -> tuple[str, str, bool]:
def get_credit(self) -> Credit:
primary = self.current_role_can_be_primary() and self.cbPrimary.isChecked()
return self.cbRole.currentText(), self.leName.text(), primary
return Credit(self.leName.text(), self.cbRole.currentText(), primary, self.cbLanguage.currentText())
def accept(self) -> None:
if self.cbRole.currentText() == "" or self.leName.text() == "":
QtWidgets.QMessageBox.warning(self, "Whoops", "You need to enter both role and name for a credit.")
if self.leName.text() == "":
QtWidgets.QMessageBox.warning(self, "Whoops", "You need to enter a name for a credit.")
else:
QtWidgets.QDialog.accept(self)

View File

@ -0,0 +1,120 @@
from __future__ import annotations
import json
import logging
import pathlib
from enum import Enum
from typing import Any
import settngs
from comictaggerlib.ctsettings.commandline import (
initial_commandline_parser,
register_commandline_settings,
validate_commandline_settings,
)
from comictaggerlib.ctsettings.file import register_file_settings, validate_file_settings
from comictaggerlib.ctsettings.plugin import group_for_plugin, register_plugin_settings, validate_plugin_settings
from comictaggerlib.ctsettings.settngs_namespace import SettngsNS as ct_ns
from comictaggerlib.ctsettings.types import ComicTaggerPaths
from comictalker import ComicTalker
logger = logging.getLogger(__name__)
talkers: dict[str, ComicTalker] = {}
__all__ = [
"initial_commandline_parser",
"register_commandline_settings",
"register_file_settings",
"register_plugin_settings",
"validate_commandline_settings",
"validate_file_settings",
"validate_plugin_settings",
"ComicTaggerPaths",
"ct_ns",
"group_for_plugin",
]
class SettingsEncoder(json.JSONEncoder):
def default(self, obj: Any) -> Any:
if isinstance(obj, pathlib.Path):
return str(obj)
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, obj)
def validate_types(config: settngs.Config[settngs.Values]) -> settngs.Config[settngs.Values]:
# Go through each setting
for group in config.definitions.values():
for setting in group.v.values():
# Get the value and if it is the default
value, default = settngs.get_option(config.values, setting)
if not default:
if setting.type is not None:
# If it is not the default and the type attribute is not None
# use it to convert the loaded string into the expected value
if (
isinstance(value, str)
or isinstance(default, Enum)
or (isinstance(setting.type, type) and issubclass(setting.type, Enum))
):
config.values[setting.group][setting.dest] = setting.type(value)
return config
def parse_config(
manager: settngs.Manager,
config_path: pathlib.Path,
args: list[str] | None = None,
) -> tuple[settngs.Config[settngs.Values], bool]:
"""
Function to parse options from a json file and passes the resulting Config object to parse_cmdline.
Args:
manager: settngs Manager object
config_path: A `pathlib.Path` object
args: Passed to argparse.ArgumentParser.parse_args
"""
file_options, success = settngs.parse_file(manager.definitions, config_path)
file_options = validate_types(file_options)
cmdline_options = settngs.parse_cmdline(
manager.definitions,
manager.description,
manager.epilog,
args,
file_options,
)
final_options = settngs.normalize_config(cmdline_options, file=True, cmdline=True)
return final_options, success
def save_file(
config: settngs.Config[settngs.T],
filename: pathlib.Path,
) -> bool:
"""
Helper function to save options from a json dictionary to a file
Args:
config: The options to save to a json dictionary
filename: A pathlib.Path object to save the json dictionary to
"""
file_options = settngs.clean_config(config, file=True)
if "Quick Tag" in file_options and "url" in file_options["Quick Tag"]:
file_options["Quick Tag"]["url"] = str(file_options["Quick Tag"]["url"])
try:
if not filename.exists():
filename.parent.mkdir(exist_ok=True, parents=True)
filename.touch()
json_str = json.dumps(file_options, cls=SettingsEncoder, indent=2)
filename.write_text(json_str + "\n", encoding="utf-8")
except Exception:
logger.exception("Failed to save config file: %s", filename)
return False
return True

View File

@ -0,0 +1,331 @@
"""CLI settings for ComicTagger"""
#
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import argparse
import logging
import os
import platform
import shlex
import subprocess
import settngs
from comicapi import utils
from comicapi.comicarchive import tags
from comictaggerlib import ctversion, quick_tag
from comictaggerlib.ctsettings.settngs_namespace import SettngsNS as ct_ns
from comictaggerlib.ctsettings.types import ComicTaggerPaths, tag
from comictaggerlib.resulttypes import Action
logger = logging.getLogger(__name__)
def initial_commandline_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(add_help=False)
# Ensure this stays up to date with register_runtime
parser.add_argument(
"--config",
help="Config directory for ComicTagger to use.\ndefault: %(default)s\n\n",
type=ComicTaggerPaths,
default=ComicTaggerPaths(),
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="Be noisy when doing what it does. Use a second time to enable debug logs.\nShort option cannot be combined with other options.",
)
parser.add_argument(
"--enable-quick-tag",
action=argparse.BooleanOptionalAction,
default=False,
help='Enable the expiremental "quick tagger"',
)
return parser
def register_runtime(parser: settngs.Manager) -> None:
parser.add_setting(
"--config",
help="Config directory for ComicTagger to use.\ndefault: %(default)s\n\n",
type=ComicTaggerPaths,
default=ComicTaggerPaths(),
file=False,
)
parser.add_setting(
"-v",
"--verbose",
action="count",
default=0,
help="Be noisy when doing what it does. Use a second time to enable debug logs.\nShort option cannot be combined with other options.",
file=False,
)
parser.add_setting(
"--enable-quick-tag",
action=argparse.BooleanOptionalAction,
default=False,
help='Enable the expiremental "quick tagger"',
file=False,
)
parser.add_setting("-q", "--quiet", action="store_true", help="Don't say much (for print mode).", file=False)
parser.add_setting(
"-j",
"--json",
action="store_true",
help="Output json on stdout. Ignored in interactive mode.\n\n",
file=False,
)
parser.add_setting(
"--raw",
action="store_true",
help="""With -p, will print out the raw tag block(s) from the file.""",
file=False,
)
parser.add_setting(
"-i",
"--interactive",
action="store_true",
help="""Interactively query the user when there are\nmultiple matches for an online search. Disabled json output\n\n""",
file=False,
)
parser.add_setting(
"--abort",
dest="abort_on_low_confidence",
action=argparse.BooleanOptionalAction,
default=True,
help="""Abort save operation when online match is of low confidence.\ndefault: %(default)s""",
file=False,
)
parser.add_setting(
"-n",
"--dryrun",
action="store_true",
help="Don't actually modify file (only relevant for -d, -s, or -r).\n\n",
file=False,
)
parser.add_setting(
"--summary",
default=True,
action=argparse.BooleanOptionalAction,
help="Show the summary after a save operation.\ndefault: %(default)s",
file=False,
)
parser.add_setting(
"-R",
"--recursive",
action="store_true",
help="Recursively include files in sub-folders.",
file=False,
)
parser.add_setting("-g", "--glob", action="store_true", help="Windows only. Enable globbing", file=False)
parser.add_setting("--darkmode", action="store_true", help="Windows only. Force a dark pallet", file=False)
parser.add_setting("--no-gui", action="store_true", help="Do not open the GUI, force the commandline", file=False)
parser.add_setting(
"--abort-on-conflict",
action="store_true",
help="""Don't export to zip if intended new filename exists\n(otherwise, creates a new unique filename).\n\n""",
file=False,
)
parser.add_setting(
"--delete-original",
action="store_true",
help="""Delete original archive after successful export to Zip.\n(only relevant for -e)\n\n""",
file=False,
)
parser.add_setting(
"-t",
"--tags-read",
metavar=f"{{{','.join(tags).upper()}}}",
default=[],
type=tag,
help="""Specify the tags to read.\nUse commas for multiple tags.\nSee --list-plugins for the available tags.\nThe tags used will be 'overlaid' in order:\ne.g. '-t cbl,cr' with no CBL tags, CR will be used if they exist and CR will overwrite any shared CBL tags.\n\n""",
file=False,
)
parser.add_setting(
"--tags-write",
metavar=f"{{{','.join(tags).upper()}}}",
default=[],
type=tag,
help="""Specify the tags to write.\nUse commas for multiple tags.\nRead tags will be used if unspecified\nSee --list-plugins for the available tags.\n\n""",
file=False,
)
parser.add_setting(
"--skip-existing-tags",
action=argparse.BooleanOptionalAction,
default=False,
help="""Skip archives that already have tags specified with -t,\notherwise merges new tags with existing tags (relevant for -s or -c).\ndefault: %(default)s""",
file=False,
)
parser.add_setting("files", nargs="*", default=[], file=False)
def register_commands(parser: settngs.Manager) -> None:
parser.add_setting("--version", action="store_true", help="Display version.", file=False)
parser.add_setting(
"-p",
"--print",
dest="command",
action="store_const",
const=Action.print,
default=Action.gui,
help="""Print out tag info from file. Specify via -t to only print specific tags.\n\n""",
file=False,
)
parser.add_setting(
"-d",
"--delete",
dest="command",
action="store_const",
const=Action.delete,
help="Deletes the tags specified via -t.",
file=False,
)
parser.add_setting(
"-c",
"--copy",
type=tag,
default=[],
metavar=f"{{{','.join(tags).upper()}}}",
help="Copy the specified source tags to\ndestination tags specified via --tags-write\n(potentially lossy operation).\n\n",
file=False,
)
parser.add_setting(
"-s",
"--save",
dest="command",
action="store_const",
const=Action.save,
help="Save out tags as specified tags (via --tags-write).\nMust specify also at least -o, -f, or -m.\n\n",
file=False,
)
parser.add_setting(
"-r",
"--rename",
dest="command",
action="store_const",
const=Action.rename,
help="Rename the file based on specified tags.",
file=False,
)
parser.add_setting(
"-e",
"--export-to-zip",
dest="command",
action="store_const",
const=Action.export,
help="Export archive to Zip format.",
file=False,
)
parser.add_setting(
"--only-save-config",
dest="command",
action="store_const",
const=Action.save_config,
help="Only save the configuration (eg, Comic Vine API key) and quit.",
file=False,
)
parser.add_setting(
"--list-plugins",
dest="command",
action="store_const",
const=Action.list_plugins,
default=Action.gui,
help="List the available plugins.\n\n",
file=False,
)
def register_commandline_settings(parser: settngs.Manager, enable_quick_tag: bool) -> None:
parser.add_group("Commands", register_commands, True)
parser.add_persistent_group("Runtime Options", register_runtime)
if enable_quick_tag:
parser.add_group("Quick Tag", quick_tag.settings)
def validate_commandline_settings(config: settngs.Config[ct_ns], parser: settngs.Manager) -> settngs.Config[ct_ns]:
if config[0].Commands__version:
parser.exit(
status=1,
message=f"ComicTagger {ctversion.version}: Copyright (c) 2012-2022 ComicTagger Team\n"
+ "Distributed under Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)\n",
)
config[0].Runtime_Options__no_gui = any(
(config[0].Commands__command != Action.gui, config[0].Runtime_Options__no_gui, config[0].Commands__copy)
)
if platform.system() == "Windows" and config[0].Runtime_Options__glob:
# no globbing on windows shell, so do it for them
import glob
globs = config[0].Runtime_Options__files
config[0].Runtime_Options__files = []
for item in globs:
config[0].Runtime_Options__files.extend(glob.glob(item))
if config[0].Runtime_Options__json and config[0].Runtime_Options__interactive:
config[0].Runtime_Options__json = False
if config[0].Runtime_Options__tags_read and not config[0].Runtime_Options__tags_write:
config[0].Runtime_Options__tags_write = config[0].Runtime_Options__tags_read
if config[0].Runtime_Options__no_gui and not config[0].Runtime_Options__files:
if config[0].Commands__command == Action.print and not config[0].Auto_Tag__metadata.is_empty:
... # allow printing the metadata provided on the commandline
elif config[0].Commands__command not in (Action.save_config, Action.list_plugins):
parser.exit(message="Command requires at least one filename!\n", status=1)
if config[0].Commands__command == Action.delete and not config[0].Runtime_Options__tags_write:
parser.exit(message="Please specify the tags to delete with --tags-write\n", status=1)
if config[0].Commands__command == Action.save and not config[0].Runtime_Options__tags_write:
parser.exit(message="Please specify the tags to save with --tags-write\n", status=1)
if config[0].Commands__copy:
config[0].Commands__command = Action.copy
if not config[0].Runtime_Options__tags_write:
parser.exit(message="Please specify the tags to copy to with --tags-write\n", status=1)
if config[0].Runtime_Options__recursive:
config[0].Runtime_Options__files = utils.get_recursive_filelist(config[0].Runtime_Options__files)
# take a crack at finding rar exe if it's not in the path
if not utils.which("rar"):
if platform.system() == "Windows":
letters = ["C"]
letters.extend({f"{d}" for d in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" if os.path.exists(f"{d}:\\")} - {"C"})
for letter in letters:
# look in some likely places for Windows machines
utils.add_to_path(rf"{letter}:\Program Files\WinRAR")
utils.add_to_path(rf"{letter}:\Program Files (x86)\WinRAR")
else:
if platform.system() == "Darwin":
result = subprocess.run(("/usr/libexec/path_helper", "-s"), capture_output=True)
for path in reversed(
shlex.split(result.stdout.decode("utf-8", errors="ignore"))[0]
.partition("=")[2]
.rstrip(";")
.split(os.pathsep)
):
utils.add_to_path(path)
utils.add_to_path("/opt/homebrew/bin")
return config

View File

@ -0,0 +1,397 @@
from __future__ import annotations
import argparse
import uuid
import settngs
from comicapi import merge, utils
from comicapi.genericmetadata import GenericMetadata
from comictaggerlib.ctsettings.settngs_namespace import SettngsNS as ct_ns
from comictaggerlib.ctsettings.types import parse_metadata_from_string
from comictaggerlib.defaults import DEFAULT_REPLACEMENTS, Replacement, Replacements
def general(parser: settngs.Manager) -> None:
# General Settings
parser.add_setting("check_for_new_version", default=False, cmdline=False)
parser.add_setting("blur", default=False, cmdline=False)
parser.add_setting(
"--prompt-on-save",
default=True,
action=argparse.BooleanOptionalAction,
help="Prompts the user to confirm saving tags when using the GUI.\ndefault: %(default)s",
)
def internal(parser: settngs.Manager) -> None:
# automatic settings
parser.add_setting("install_id", default=uuid.uuid4().hex, cmdline=False)
parser.add_setting("write_tags", default=["cr"], cmdline=False)
parser.add_setting("read_tags", default=["cr"], cmdline=False)
parser.add_setting("last_opened_folder", default="", cmdline=False)
parser.add_setting("window_width", default=0, cmdline=False)
parser.add_setting("window_height", default=0, cmdline=False)
parser.add_setting("window_x", default=0, cmdline=False)
parser.add_setting("window_y", default=0, cmdline=False)
parser.add_setting("form_width", default=-1, cmdline=False)
parser.add_setting("list_width", default=-1, cmdline=False)
parser.add_setting("sort_column", default=-1, cmdline=False)
parser.add_setting("sort_direction", default=0, cmdline=False)
parser.add_setting("remove_archive_after_successful_match", default=False, cmdline=False)
def identifier(parser: settngs.Manager) -> None:
parser.add_setting(
"--series-match-identify-thresh",
default=91,
type=int,
help="The minimum Series name similarity needed to auto-identify an issue default: %(default)s",
)
parser.add_setting(
"--series-match-search-thresh",
default=90,
type=int,
help="The minimum Series name similarity to return from a search result default: %(default)s",
)
parser.add_setting(
"-b",
"--border-crop-percent",
default=10,
type=int,
help="ComicTagger will automatically add an additional cover that has any black borders cropped.\nIf the difference in height is less than %(default)s%% the cover will not be cropped.\ndefault: %(default)s\n\n",
)
parser.add_setting(
"--sort-series-by-year",
default=True,
action=argparse.BooleanOptionalAction,
help="Sorts series by year default: %(default)s",
)
parser.add_setting(
"--exact-series-matches-first",
default=True,
action=argparse.BooleanOptionalAction,
help="Puts series that are an exact match at the top of the list default: %(default)s",
)
def dialog(parser: settngs.Manager) -> None:
parser.add_setting("show_disclaimer", default=True, cmdline=False)
parser.add_setting("dont_notify_about_this_version", default="", cmdline=False)
parser.add_setting("notify_plugin_changes", default=True, cmdline=False)
def filename(parser: settngs.Manager) -> None:
parser.add_setting(
"--filename-parser",
default=utils.Parser.ORIGINAL,
metavar=f"{{{','.join(utils.Parser)}}}",
type=utils.Parser,
choices=utils.Parser,
help="Select the filename parser.\ndefault: %(default)s",
)
parser.add_setting(
"--remove-c2c",
default=False,
action=argparse.BooleanOptionalAction,
help="Removes c2c from filenames.\nRequires --complicated-parser\ndefault: %(default)s\n\n",
)
parser.add_setting(
"--remove-fcbd",
default=False,
action=argparse.BooleanOptionalAction,
help="Removes FCBD/free comic book day from filenames.\nRequires --complicated-parser\ndefault: %(default)s\n\n",
)
parser.add_setting(
"--remove-publisher",
default=False,
action=argparse.BooleanOptionalAction,
help="Attempts to remove publisher names from filenames, currently limited to Marvel and DC.\nRequires --complicated-parser\ndefault: %(default)s\n\n",
)
parser.add_setting(
"--split-words",
action="store_true",
help="""Splits words before parsing the filename.\ne.g. 'judgedredd' to 'judge dredd'\ndefault: %(default)s\n\n""",
file=False,
)
parser.add_setting(
"--protofolius-issue-number-scheme",
default=False,
action=argparse.BooleanOptionalAction,
help="Use an issue number scheme devised by protofolius for encoding format information as a letter in front of an issue number.\nImplies --allow-issue-start-with-letter. Requires --complicated-parser\ndefault: %(default)s\n\n",
)
parser.add_setting(
"--allow-issue-start-with-letter",
default=False,
action=argparse.BooleanOptionalAction,
help="Allows an issue number to start with a single letter (e.g. '#X01').\nRequires --complicated-parser\ndefault: %(default)s\n\n",
)
def talker(parser: settngs.Manager) -> None:
parser.add_setting(
"--source",
default="comicvine",
help="Use a specified source by source ID (use --list-plugins to list all sources).\ndefault: %(default)s",
)
def md_options(parser: settngs.Manager) -> None:
# CBL Transform settings
parser.add_setting("--assume-lone-credit-is-primary", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting("--copy-characters-to-tags", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting("--copy-teams-to-tags", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting("--copy-locations-to-tags", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting("--copy-storyarcs-to-tags", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting("--copy-notes-to-comments", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting("--copy-weblink-to-comments", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting("--apply-transform-on-import", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting("--apply-transform-on-bulk-operation", default=False, action=argparse.BooleanOptionalAction)
parser.add_setting(
"--remove-html-tables",
default=False,
action=argparse.BooleanOptionalAction,
display_name="Remove HTML tables",
help="Removes html tables instead of converting them to text",
)
parser.add_setting("use_short_tag_names", default=False, action=argparse.BooleanOptionalAction, cmdline=False)
parser.add_setting(
"--cr",
default=True,
action=argparse.BooleanOptionalAction,
help="Enable ComicRack tags. Turn off to only use CIX tags.\ndefault: %(default)s",
)
parser.add_setting(
"--tag-merge",
metavar=f"{{{','.join(merge.Mode)}}}",
default=merge.Mode.OVERLAY,
choices=merge.Mode,
type=merge.Mode,
help="How to merge fields when reading enabled tags (CR, CBL, etc.) See -t, --tags-read default: %(default)s",
)
parser.add_setting(
"--metadata-merge",
metavar=f"{{{','.join(merge.Mode)}}}",
default=merge.Mode.OVERLAY,
choices=merge.Mode,
type=merge.Mode,
help="How to merge fields when downloading new metadata (CV, Metron, GCD, etc.) default: %(default)s",
)
parser.add_setting(
"--tag-merge-lists",
action=argparse.BooleanOptionalAction,
default=True,
help="Merge lists when reading enabled tags (genres, characters, etc.) default: %(default)s",
)
parser.add_setting(
"--metadata-merge-lists",
action=argparse.BooleanOptionalAction,
default=True,
help="Merge lists when downloading new metadata (genres, characters, etc.) default: %(default)s",
)
def rename(parser: settngs.Manager) -> None:
parser.add_setting(
"--template",
default="{series} #{issue} ({year})",
help="The teplate to use when renaming.\ndefault: %(default)s",
)
parser.add_setting(
"--issue-number-padding",
default=3,
type=int,
help="The minimum number of digits to use for the issue number when renaming.\ndefault: %(default)s",
)
parser.add_setting(
"--use-smart-string-cleanup",
default=True,
action=argparse.BooleanOptionalAction,
help="Attempts to intelligently cleanup whitespace when renaming.\ndefault: %(default)s",
)
parser.add_setting(
"--auto-extension",
default=True,
action=argparse.BooleanOptionalAction,
help="Automatically sets the extension based on the archive type e.g. cbr for rar, cbz for zip.\ndefault: %(default)s",
)
parser.add_setting("--dir", default="", help="The directory to move renamed files to.")
parser.add_setting(
"--move",
default=False,
action=argparse.BooleanOptionalAction,
help="Enables moving renamed files to a separate directory.\ndefault: %(default)s",
)
parser.add_setting(
"--only-move",
default=False,
action=argparse.BooleanOptionalAction,
help="Ignores the filename when moving renamed files to a separate directory.\ndefault: %(default)s",
)
parser.add_setting(
"--strict-filenames",
default=False,
action=argparse.BooleanOptionalAction,
help="Ensures that filenames are valid for all OSs.\ndefault: %(default)s",
)
parser.add_setting("replacements", default=DEFAULT_REPLACEMENTS, cmdline=False)
def autotag(parser: settngs.Manager) -> None:
parser.add_setting(
"-o",
"--online",
action="store_true",
help="""Search online and attempt to identify file\nusing existing tags and images in archive.\nMay be used in conjunction with -f and -m.\n\n""",
file=False,
)
parser.add_setting(
"--save-on-low-confidence",
default=False,
action=argparse.BooleanOptionalAction,
help="Automatically save tags on low-confidence matches.\ndefault: %(default)s",
cmdline=False,
)
parser.add_setting(
"--use-year-when-identifying",
default=True,
action=argparse.BooleanOptionalAction,
help="Use the year metadata attribute when auto-tagging a comic.\ndefault: %(default)s",
)
parser.add_setting(
"-1",
"--assume-issue-one",
action=argparse.BooleanOptionalAction,
help="Assume issue number is 1 if not found (relevant for -s).\ndefault: %(default)s\n\n",
default=False,
)
parser.add_setting(
"--ignore-leading-numbers-in-filename",
default=False,
action=argparse.BooleanOptionalAction,
help="When searching ignore leading numbers in the filename.\ndefault: %(default)s",
)
parser.add_setting(
"-f",
"--parse-filename",
action="store_true",
help="""Parse the filename to get some info,\nspecifically series name, issue number,\nvolume, and publication year.\n\n""",
file=False,
)
parser.add_setting(
"--prefer-filename",
action="store_true",
help="""Prefer metadata parsed from the filename. CLI only.\n\n""",
file=False,
)
parser.add_setting(
"--id",
dest="issue_id",
type=str,
help="""Use the issue ID when searching online.\nOverrides all other metadata.\n\n""",
file=False,
)
parser.add_setting(
"-m",
"--metadata",
default=GenericMetadata(),
type=parse_metadata_from_string,
help="""Explicitly define some metadata to be used in YAML syntax. Use @file.yaml to read from a file. e.g.:\n"series: Plastic Man, publisher: Quality Comics, year: "\n"series: 'Kickers, Inc.', issue: '1', year: 1986"\nIf you want to erase a tag leave the value blank.\nSome names that can be used: series, issue, issue_count, year,\npublisher, title\n\n""",
file=False,
)
parser.add_setting(
"--clear-tags",
default=False,
action=argparse.BooleanOptionalAction,
help="Clears all existing tags during import, default is to merge tags.\nMay be used in conjunction with -o, -f and -m.\ndefault: %(default)s\n\n",
)
parser.add_setting(
"--publisher-filter",
default=["Panini Comics", "Abril", "Planeta DeAgostini", "Editorial Televisa", "Dino Comics"],
action="extend",
nargs="+",
help="When enabled, filters the listed publishers from all search results.\nEnding a publisher with a '-' removes a publisher from this list\ndefault: %(default)s\n\n",
)
parser.add_setting(
"--use-publisher-filter",
default=False,
action=argparse.BooleanOptionalAction,
help="Enables the publisher filter.\ndefault: %(default)s",
)
parser.add_setting(
"-a",
"--auto-imprint",
default=False,
action=argparse.BooleanOptionalAction,
help="Enables the auto imprint functionality.\ne.g. if the publisher is set to 'vertigo' it\nwill be updated to 'DC Comics' and the imprint\nproperty will be set to 'Vertigo'.\ndefault: %(default)s\n\n",
)
def parse_filter(config: settngs.Config[ct_ns]) -> settngs.Config[ct_ns]:
new_filter = []
remove = []
for x in config[0].Auto_Tag__publisher_filter:
x = x.strip()
if x: # ignore empty arguments
if x[-1] == "-": # this publisher needs to be removed. We remove after all publishers have been enumerated
remove.append(x.strip("-"))
else:
if x not in new_filter:
new_filter.append(x)
for x in remove: # remove publishers
if x in new_filter:
new_filter.remove(x)
config[0].Auto_Tag__publisher_filter = new_filter
return config
def migrate_settings(config: settngs.Config[ct_ns]) -> settngs.Config[ct_ns]:
original_types = ("cbi", "cr", "comet")
write_Tags = config[0].internal__write_tags
if not isinstance(write_Tags, list):
if isinstance(write_Tags, int) and write_Tags in (0, 1, 2):
config[0].internal__write_tags = [original_types[write_Tags]]
elif isinstance(write_Tags, str):
config[0].internal__write_tags = [write_Tags]
else:
config[0].internal__write_tags = ["cr"]
read_tags = config[0].internal__read_tags
if not isinstance(read_tags, list):
if isinstance(read_tags, int) and read_tags in (0, 1, 2):
config[0].internal__read_tags = [original_types[read_tags]]
elif isinstance(read_tags, str):
config[0].internal__read_tags = [read_tags]
else:
config[0].internal__read_tags = ["cr"]
return config
def validate_file_settings(config: settngs.Config[ct_ns]) -> settngs.Config[ct_ns]:
config = parse_filter(config)
config = migrate_settings(config)
if config[0].Filename_Parsing__protofolius_issue_number_scheme:
config[0].Filename_Parsing__allow_issue_start_with_letter = True
config[0].File_Rename__replacements = Replacements(
[Replacement(x[0], x[1], x[2]) for x in config[0].File_Rename__replacements[0]],
[Replacement(x[0], x[1], x[2]) for x in config[0].File_Rename__replacements[1]],
)
return config
def register_file_settings(parser: settngs.Manager) -> None:
parser.add_group("internal", internal, False)
parser.add_group("Issue Identifier", identifier, False)
parser.add_group("Filename Parsing", filename, False)
parser.add_group("Sources", talker, False)
parser.add_group("Metadata Options", md_options, False)
parser.add_group("File Rename", rename, False)
parser.add_group("Auto-Tag", autotag, False)
parser.add_group("General", general, False)
parser.add_group("Dialog Flags", dialog, False)

View File

@ -0,0 +1,107 @@
from __future__ import annotations
import logging
import os
from typing import Any, cast
import settngs
import comicapi.comicarchive
import comicapi.utils
import comictaggerlib.ctsettings
from comicapi.comicarchive import Archiver
from comictaggerlib.ctsettings.settngs_namespace import SettngsNS as ct_ns
from comictalker.comictalker import ComicTalker
logger = logging.getLogger("comictagger")
def group_for_plugin(plugin: Archiver | ComicTalker | type[Archiver]) -> str:
if isinstance(plugin, ComicTalker):
return f"Source {plugin.id}"
if isinstance(plugin, Archiver) or plugin == Archiver:
return "Archive"
raise NotImplementedError(f"Invalid plugin received: {plugin=}")
def archiver(manager: settngs.Manager) -> None:
for archiver in comicapi.comicarchive.archivers:
if archiver.exe:
# add_setting will overwrite anything with the same name.
# So we only end up with one option even if multiple archivers use the same exe.
manager.add_setting(
f"--{settngs.sanitize_name(archiver.exe)}",
default=archiver.exe,
help="Path to the %(default)s executable",
)
def register_talker_settings(manager: settngs.Manager, talkers: dict[str, ComicTalker]) -> None:
for talker in talkers.values():
def api_options(manager: settngs.Manager) -> None:
# The default needs to be unset or None.
# This allows this setting to be unset with the empty string, allowing the default to change
manager.add_setting(
f"--{talker.id}-key",
display_name="API Key",
help=f"API Key for {talker.name} (default: {talker.default_api_key})",
)
manager.add_setting(
f"--{talker.id}-url",
display_name="URL",
help=f"URL for {talker.name} (default: {talker.default_api_url})",
)
try:
manager.add_persistent_group(group_for_plugin(talker), api_options, False)
if hasattr(talker, "register_settings"):
manager.add_persistent_group(group_for_plugin(talker), talker.register_settings, False)
except Exception:
logger.exception("Failed to register settings for %s", talker.id)
def validate_archive_settings(config: settngs.Config[ct_ns]) -> settngs.Config[ct_ns]:
cfg = settngs.normalize_config(config, file=True, cmdline=True, default=False)
for archiver in comicapi.comicarchive.archivers:
group = group_for_plugin(archiver())
exe_name = settngs.sanitize_name(archiver.exe)
if not exe_name:
continue
if exe_name in cfg[0][group] and cfg[0][group][exe_name]:
path = cfg[0][group][exe_name]
name = os.path.basename(path)
# If the path is not the basename then this is a relative or absolute path.
# Ensure it is absolute
if path != name:
path = os.path.abspath(path)
archiver.exe = path
return config
def validate_talker_settings(config: settngs.Config[ct_ns], talkers: dict[str, ComicTalker]) -> settngs.Config[ct_ns]:
# Apply talker settings from config file
cfg = cast(settngs.Config[dict[str, Any]], settngs.normalize_config(config, True, True))
for talker in list(talkers.values()):
try:
cfg[0][group_for_plugin(talker)] = talker.parse_settings(cfg[0][group_for_plugin(talker)])
except Exception as e:
# Remove talker as we failed to apply the settings
del comictaggerlib.ctsettings.talkers[talker.id]
logger.exception("Failed to initialize talker settings: %s", e)
return cast(settngs.Config[ct_ns], settngs.get_namespace(cfg, file=True, cmdline=True))
def validate_plugin_settings(config: settngs.Config[ct_ns], talkers: dict[str, ComicTalker]) -> settngs.Config[ct_ns]:
config = validate_archive_settings(config)
config = validate_talker_settings(config, talkers)
return config
def register_plugin_settings(manager: settngs.Manager, talkers: dict[str, ComicTalker]) -> None:
manager.add_persistent_group("Archive", archiver, False)
register_talker_settings(manager, talkers)

View File

@ -0,0 +1,185 @@
"""Functions related to finding and loading plugins."""
# Lifted from flake8 https://github.com/PyCQA/flake8/blob/main/src/flake8/plugins/finder.py#L127
from __future__ import annotations
import importlib.util
import logging
import pathlib
import platform
import re
import sys
from collections.abc import Generator, Iterable
from typing import Any, NamedTuple, TypeVar
if sys.version_info < (3, 10):
import importlib_metadata
else:
import importlib.metadata as importlib_metadata
logger = logging.getLogger(__name__)
NORMALIZE_PACKAGE_NAME_RE = re.compile(r"[-_.]+")
PLUGIN_GROUPS = frozenset(("comictagger.talker", "comicapi.archiver", "comicapi.tags"))
icu_available = importlib.util.find_spec("icu") is not None
def _custom_key(tup: Any) -> Any:
import natsort
lst = []
for x in natsort.os_sort_keygen()(tup):
ret = x
if len(x) > 1 and isinstance(x[1], int) and isinstance(x[0], str) and x[0] == "":
ret = ("a", *x[1:])
lst.append(ret)
return tuple(lst)
T = TypeVar("T")
def os_sorted(lst: Iterable[T]) -> Iterable[T]:
import natsort
key = _custom_key
if icu_available or platform.system() == "Windows":
key = natsort.os_sort_keygen()
return sorted(lst, key=key)
class FailedToLoadPlugin(Exception):
"""Exception raised when a plugin fails to load."""
FORMAT = 'ComicTagger failed to load local plugin "{name}" due to {exc}.'
def __init__(self, plugin_name: str, exception: Exception) -> None:
"""Initialize our FailedToLoadPlugin exception."""
self.plugin_name = plugin_name
self.original_exception = exception
super().__init__(plugin_name, exception)
def __str__(self) -> str:
"""Format our exception message."""
return self.FORMAT.format(
name=self.plugin_name,
exc=self.original_exception,
)
def normalize_pypi_name(s: str) -> str:
"""Normalize a distribution name according to PEP 503."""
return NORMALIZE_PACKAGE_NAME_RE.sub("-", s).lower()
class Plugin(NamedTuple):
"""A plugin before loading."""
package: str
version: str
entry_point: importlib_metadata.EntryPoint
path: pathlib.Path
def load(self) -> LoadedPlugin:
return LoadedPlugin(self, self.entry_point.load())
class LoadedPlugin(NamedTuple):
"""Represents a plugin after being imported."""
plugin: Plugin
obj: Any
@property
def entry_name(self) -> str:
"""Return the name given in the packaging metadata."""
return self.plugin.entry_point.name
@property
def display_name(self) -> str:
"""Return the name for use in user-facing / error messages."""
return f"{self.plugin.package}[{self.entry_name}]"
class Plugins(NamedTuple):
"""Classified plugins."""
archivers: list[LoadedPlugin]
tags: list[LoadedPlugin]
talkers: list[LoadedPlugin]
def all_plugins(self) -> Generator[LoadedPlugin]:
"""Return an iterator over all :class:`LoadedPlugin`s."""
yield from self.archivers
yield from self.tags
yield from self.talkers
def versions_str(self) -> str:
"""Return a user-displayed list of plugin versions."""
return ", ".join(sorted({f"{plugin.plugin.package}: {plugin.plugin.version}" for plugin in self.all_plugins()}))
def _find_local_plugins(plugin_path: pathlib.Path) -> Generator[Plugin]:
logger.debug("Checking for distributions in %s", plugin_path)
for dist in importlib_metadata.distributions(path=[str(plugin_path)]):
logger.debug("found distribution %s", dist.name)
eps = dist.entry_points
for group in PLUGIN_GROUPS:
for ep in eps.select(group=group):
logger.debug("found EntryPoint group %s %s=%s", group, ep.name, ep.value)
yield Plugin(plugin_path.name, dist.version, ep, plugin_path)
def find_plugins(plugin_folder: pathlib.Path) -> Plugins:
"""Discovers all plugins (but does not load them)."""
ret: list[LoadedPlugin] = []
if not plugin_folder.is_dir():
return _classify_plugins(ret)
zips = [x for x in plugin_folder.iterdir() if x.is_file() and x.suffix in (".zip", ".whl")]
for plugin_path in os_sorted(zips):
logger.debug("looking for plugins in %s", plugin_path)
try:
sys.path.append(str(plugin_path))
for plugin in _find_local_plugins(plugin_path):
logger.debug("Attempting to load %s from %s", plugin.entry_point.name, plugin.path)
ret.append(plugin.load())
except Exception as err:
logger.exception(FailedToLoadPlugin(plugin_path.name, err))
finally:
sys.path.remove(str(plugin_path))
for mod in list(sys.modules.values()):
if (
mod is not None
and hasattr(mod, "__spec__")
and mod.__spec__
and str(plugin_path) in (mod.__spec__.origin or "")
):
sys.modules.pop(mod.__name__)
return _classify_plugins(ret)
def _classify_plugins(plugins: list[LoadedPlugin]) -> Plugins:
archivers = []
tags = []
talkers = []
for p in plugins:
if p.plugin.entry_point.group == "comictagger.talker":
talkers.append(p)
elif p.plugin.entry_point.group == "comicapi.tags":
tags.append(p)
elif p.plugin.entry_point.group == "comicapi.archiver":
archivers.append(p)
else:
logger.warning(NotImplementedError(f"what plugin type? {p}"))
return Plugins(
tags=tags,
archivers=archivers,
talkers=talkers,
)

View File

@ -0,0 +1,300 @@
from __future__ import annotations
import typing
import settngs
import urllib3.util.url
import comicapi.genericmetadata
import comicapi.merge
import comicapi.utils
import comictaggerlib.ctsettings.types
import comictaggerlib.defaults
import comictaggerlib.resulttypes
class SettngsNS(settngs.TypedNS):
Commands__version: bool
Commands__command: comictaggerlib.resulttypes.Action
Commands__copy: list[str]
Runtime_Options__config: comictaggerlib.ctsettings.types.ComicTaggerPaths
Runtime_Options__verbose: int
Runtime_Options__enable_quick_tag: bool
Runtime_Options__quiet: bool
Runtime_Options__json: bool
Runtime_Options__raw: bool
Runtime_Options__interactive: bool
Runtime_Options__abort_on_low_confidence: bool
Runtime_Options__dryrun: bool
Runtime_Options__summary: bool
Runtime_Options__recursive: bool
Runtime_Options__glob: bool
Runtime_Options__darkmode: bool
Runtime_Options__no_gui: bool
Runtime_Options__abort_on_conflict: bool
Runtime_Options__delete_original: bool
Runtime_Options__tags_read: list[str]
Runtime_Options__tags_write: list[str]
Runtime_Options__skip_existing_tags: bool
Runtime_Options__files: list[str]
Quick_Tag__url: urllib3.util.url.Url
Quick_Tag__max: int
Quick_Tag__simple: bool
Quick_Tag__aggressive_filtering: bool
Quick_Tag__hash: list[comictaggerlib.quick_tag.HashType]
Quick_Tag__exact_only: bool
internal__install_id: str
internal__write_tags: list[str]
internal__read_tags: list[str]
internal__last_opened_folder: str
internal__window_width: int
internal__window_height: int
internal__window_x: int
internal__window_y: int
internal__form_width: int
internal__list_width: int
internal__sort_column: int
internal__sort_direction: int
internal__remove_archive_after_successful_match: bool
Issue_Identifier__series_match_identify_thresh: int
Issue_Identifier__series_match_search_thresh: int
Issue_Identifier__border_crop_percent: int
Issue_Identifier__sort_series_by_year: bool
Issue_Identifier__exact_series_matches_first: bool
Filename_Parsing__filename_parser: comicapi.utils.Parser
Filename_Parsing__remove_c2c: bool
Filename_Parsing__remove_fcbd: bool
Filename_Parsing__remove_publisher: bool
Filename_Parsing__split_words: bool
Filename_Parsing__protofolius_issue_number_scheme: bool
Filename_Parsing__allow_issue_start_with_letter: bool
Sources__source: str
Metadata_Options__assume_lone_credit_is_primary: bool
Metadata_Options__copy_characters_to_tags: bool
Metadata_Options__copy_teams_to_tags: bool
Metadata_Options__copy_locations_to_tags: bool
Metadata_Options__copy_storyarcs_to_tags: bool
Metadata_Options__copy_notes_to_comments: bool
Metadata_Options__copy_weblink_to_comments: bool
Metadata_Options__apply_transform_on_import: bool
Metadata_Options__apply_transform_on_bulk_operation: bool
Metadata_Options__remove_html_tables: bool
Metadata_Options__use_short_tag_names: bool
Metadata_Options__cr: bool
Metadata_Options__tag_merge: comicapi.merge.Mode
Metadata_Options__metadata_merge: comicapi.merge.Mode
Metadata_Options__tag_merge_lists: bool
Metadata_Options__metadata_merge_lists: bool
File_Rename__template: str
File_Rename__issue_number_padding: int
File_Rename__use_smart_string_cleanup: bool
File_Rename__auto_extension: bool
File_Rename__dir: str
File_Rename__move: bool
File_Rename__only_move: bool
File_Rename__strict_filenames: bool
File_Rename__replacements: comictaggerlib.defaults.Replacements
Auto_Tag__online: bool
Auto_Tag__save_on_low_confidence: bool
Auto_Tag__use_year_when_identifying: bool
Auto_Tag__assume_issue_one: bool
Auto_Tag__ignore_leading_numbers_in_filename: bool
Auto_Tag__parse_filename: bool
Auto_Tag__prefer_filename: bool
Auto_Tag__issue_id: str | None
Auto_Tag__metadata: comicapi.genericmetadata.GenericMetadata
Auto_Tag__clear_tags: bool
Auto_Tag__publisher_filter: list[str]
Auto_Tag__use_publisher_filter: bool
Auto_Tag__auto_imprint: bool
General__check_for_new_version: bool
General__blur: bool
General__prompt_on_save: bool
Dialog_Flags__show_disclaimer: bool
Dialog_Flags__dont_notify_about_this_version: str
Dialog_Flags__notify_plugin_changes: bool
Archive__rar: str
Source_comicvine__comicvine_key: str | None
Source_comicvine__comicvine_url: str | None
Source_comicvine__cv_use_series_start_as_volume: bool
Source_comicvine__comicvine_custom_parameters: str | None
class Commands(typing.TypedDict):
version: bool
command: comictaggerlib.resulttypes.Action
copy: list[str]
class Runtime_Options(typing.TypedDict):
config: comictaggerlib.ctsettings.types.ComicTaggerPaths
verbose: int
enable_quick_tag: bool
quiet: bool
json: bool
raw: bool
interactive: bool
abort_on_low_confidence: bool
dryrun: bool
summary: bool
recursive: bool
glob: bool
darkmode: bool
no_gui: bool
abort_on_conflict: bool
delete_original: bool
tags_read: list[str]
tags_write: list[str]
skip_existing_tags: bool
files: list[str]
class Quick_Tag(typing.TypedDict):
url: urllib3.util.url.Url
max: int
simple: bool
aggressive_filtering: bool
hash: list[comictaggerlib.quick_tag.HashType]
exact_only: bool
class internal(typing.TypedDict):
install_id: str
write_tags: list[str]
read_tags: list[str]
last_opened_folder: str
window_width: int
window_height: int
window_x: int
window_y: int
form_width: int
list_width: int
sort_column: int
sort_direction: int
remove_archive_after_successful_match: bool
class Issue_Identifier(typing.TypedDict):
series_match_identify_thresh: int
series_match_search_thresh: int
border_crop_percent: int
sort_series_by_year: bool
exact_series_matches_first: bool
class Filename_Parsing(typing.TypedDict):
filename_parser: comicapi.utils.Parser
remove_c2c: bool
remove_fcbd: bool
remove_publisher: bool
split_words: bool
protofolius_issue_number_scheme: bool
allow_issue_start_with_letter: bool
class Sources(typing.TypedDict):
source: str
class Metadata_Options(typing.TypedDict):
assume_lone_credit_is_primary: bool
copy_characters_to_tags: bool
copy_teams_to_tags: bool
copy_locations_to_tags: bool
copy_storyarcs_to_tags: bool
copy_notes_to_comments: bool
copy_weblink_to_comments: bool
apply_transform_on_import: bool
apply_transform_on_bulk_operation: bool
remove_html_tables: bool
use_short_tag_names: bool
cr: bool
tag_merge: comicapi.merge.Mode
metadata_merge: comicapi.merge.Mode
tag_merge_lists: bool
metadata_merge_lists: bool
class File_Rename(typing.TypedDict):
template: str
issue_number_padding: int
use_smart_string_cleanup: bool
auto_extension: bool
dir: str
move: bool
only_move: bool
strict_filenames: bool
replacements: comictaggerlib.defaults.Replacements
class Auto_Tag(typing.TypedDict):
online: bool
save_on_low_confidence: bool
use_year_when_identifying: bool
assume_issue_one: bool
ignore_leading_numbers_in_filename: bool
parse_filename: bool
prefer_filename: bool
issue_id: str | None
metadata: comicapi.genericmetadata.GenericMetadata
clear_tags: bool
publisher_filter: list[str]
use_publisher_filter: bool
auto_imprint: bool
class General(typing.TypedDict):
check_for_new_version: bool
blur: bool
prompt_on_save: bool
class Dialog_Flags(typing.TypedDict):
show_disclaimer: bool
dont_notify_about_this_version: str
notify_plugin_changes: bool
class Archive(typing.TypedDict):
rar: str
class Source_comicvine(typing.TypedDict):
comicvine_key: str | None
comicvine_url: str | None
cv_use_series_start_as_volume: bool
comicvine_custom_parameters: str | None
SettngsDict = typing.TypedDict(
"SettngsDict",
{
"Commands": Commands,
"Runtime Options": Runtime_Options,
"Quick Tag": Quick_Tag,
"internal": internal,
"Issue Identifier": Issue_Identifier,
"Filename Parsing": Filename_Parsing,
"Sources": Sources,
"Metadata Options": Metadata_Options,
"File Rename": File_Rename,
"Auto-Tag": Auto_Tag,
"General": General,
"Dialog Flags": Dialog_Flags,
"Archive": Archive,
"Source comicvine": Source_comicvine,
},
)

View File

@ -0,0 +1,245 @@
from __future__ import annotations
import argparse
import logging
import pathlib
import sys
import types
import typing
from collections.abc import Collection, Mapping
from typing import Any
import yaml
from appdirs import AppDirs
from comicapi import utils
from comicapi.comicarchive import tags
from comicapi.genericmetadata import REMOVE, GenericMetadata
logger = logging.getLogger(__name__)
if sys.version_info < (3, 10):
@typing.no_type_check
def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
if getattr(obj, "__no_type_check__", None):
return {}
# Classes require a special treatment.
if isinstance(obj, type):
hints = {}
for base in reversed(obj.__mro__):
if globalns is None:
base_globals = getattr(sys.modules.get(base.__module__, None), "__dict__", {})
else:
base_globals = globalns
ann = base.__dict__.get("__annotations__", {})
if isinstance(ann, types.GetSetDescriptorType):
ann = {}
base_locals = dict(vars(base)) if localns is None else localns
if localns is None and globalns is None:
# This is surprising, but required. Before Python 3.10,
# get_type_hints only evaluated the globalns of
# a class. To maintain backwards compatibility, we reverse
# the globalns and localns order so that eval() looks into
# *base_globals* first rather than *base_locals*.
# This only affects ForwardRefs.
base_globals, base_locals = base_locals, base_globals
for name, value in ann.items():
if value is None:
value = type(None)
if isinstance(value, str):
if "|" in value:
value = "Union[" + value.replace(" |", ",") + "]"
value = typing.ForwardRef(value, is_argument=False, is_class=True)
value = typing._eval_type(value, base_globals, base_locals)
hints[name] = value
return hints if include_extras else {k: typing._strip_annotations(t) for k, t in hints.items()}
if globalns is None:
if isinstance(obj, types.ModuleType):
globalns = obj.__dict__
else:
nsobj = obj
# Find globalns for the unwrapped object.
while hasattr(nsobj, "__wrapped__"):
nsobj = nsobj.__wrapped__
globalns = getattr(nsobj, "__globals__", {})
if localns is None:
localns = globalns
elif localns is None:
localns = globalns
hints = getattr(obj, "__annotations__", None)
if hints is None:
# Return empty annotations for something that _could_ have them.
if isinstance(obj, typing._allowed_types):
return {}
else:
raise TypeError("{!r} is not a module, class, method, " "or function.".format(obj))
hints = dict(hints)
for name, value in hints.items():
if value is None:
value = type(None)
if isinstance(value, str):
if "|" in value:
value = "Union[" + value.replace(" |", ",") + "]"
# class-level forward refs were handled above, this must be either
# a module-level annotation or a function argument annotation
value = typing.ForwardRef(
value,
is_argument=not isinstance(obj, types.ModuleType),
is_class=False,
)
hints[name] = typing._eval_type(value, globalns, localns)
return hints if include_extras else {k: typing._strip_annotations(t) for k, t in hints.items()}
else:
from typing import get_type_hints
class ComicTaggerPaths(AppDirs):
def __init__(self, config_path: pathlib.Path | str | None = None) -> None:
super().__init__("ComicTagger", None, None, False, False)
self.path: pathlib.Path | None = None
if config_path:
self.path = pathlib.Path(config_path).absolute()
@property
def user_data_dir(self) -> pathlib.Path:
if self.path:
return self.path
return pathlib.Path(super().user_data_dir)
@property
def user_config_dir(self) -> pathlib.Path:
if self.path:
return self.path
return pathlib.Path(super().user_config_dir)
@property
def user_cache_dir(self) -> pathlib.Path:
if self.path:
return self.path / "cache"
return pathlib.Path(super().user_cache_dir)
@property
def user_state_dir(self) -> pathlib.Path:
if self.path:
return self.path
return pathlib.Path(super().user_state_dir)
@property
def user_log_dir(self) -> pathlib.Path:
if self.path:
return self.path / "log"
return pathlib.Path(super().user_log_dir)
@property
def user_plugin_dir(self) -> pathlib.Path:
if self.path:
return self.path / "plugins"
return pathlib.Path(super().user_config_dir) / "plugins"
@property
def site_data_dir(self) -> pathlib.Path:
return pathlib.Path(super().site_data_dir)
@property
def site_config_dir(self) -> pathlib.Path:
return pathlib.Path(super().site_config_dir)
def __str__(self) -> str:
return f"logs: {self.user_log_dir}, config: {self.user_config_dir}, cache: {self.user_cache_dir}"
def tag(types: str) -> list[str]:
result = []
types = types.casefold()
for typ in utils.split(types, ","):
if typ not in tags:
choices = ", ".join(tags)
raise argparse.ArgumentTypeError(f"invalid choice: {typ} (choose from {choices.upper()})")
result.append(tags[typ].id)
return result
def parse_metadata_from_string(mdstr: str) -> GenericMetadata:
def get_type(key: str, tt: Any = get_type_hints(GenericMetadata)) -> Any:
t: Any = tt.get(key, None)
if t is None:
return None
if getattr(t, "__origin__", None) is typing.Union and len(t.__args__) == 2 and t.__args__[1] is type(None):
t = t.__args__[0]
elif isinstance(t, types.GenericAlias) and issubclass(t.mro()[0], Collection):
t = t.mro()[0], t.__args__[0]
if isinstance(t, tuple) and issubclass(t[1], dict):
return (t[0], dict)
if isinstance(t, type) and issubclass(t, dict):
return dict
return t
def convert_value(t: type, value: Any) -> Any:
if isinstance(value, t):
return value
try:
if isinstance(value, (Mapping)):
value = t(**value)
elif not isinstance(value, str) and isinstance(value, (Collection)):
value = t(*value)
else:
if t is utils.Url and isinstance(value, str):
value = utils.parse_url(value)
else:
value = t(value)
except (ValueError, TypeError):
raise argparse.ArgumentTypeError(f"Invalid syntax for tag '{key}': {value}")
return value
md = GenericMetadata()
try:
if not mdstr:
return md
if mdstr[0] == "@":
p = pathlib.Path(mdstr[1:])
if not p.is_file():
raise argparse.ArgumentTypeError("Invalid filepath")
mdstr = p.read_text()
if mdstr[0] != "{":
mdstr = "{" + mdstr + "}"
md_dict = yaml.safe_load(mdstr)
empty = True
# Map the dict to the metadata object
for key, value in md_dict.items():
if hasattr(md, key):
t = get_type(key)
if value is None:
value = REMOVE
elif isinstance(t, tuple):
if value == "":
value = t[0]()
else:
if isinstance(value, str):
value = [value]
if not isinstance(value, Collection):
raise argparse.ArgumentTypeError(f"Invalid syntax for tag '{key}'")
values = list(value)
for idx, v in enumerate(values):
if not isinstance(v, t[1]):
values[idx] = convert_value(t[1], v)
value = t[0](values)
else:
value = convert_value(t, value)
empty = False
setattr(md, key, value)
else:
raise argparse.ArgumentTypeError(f"'{key}' is not a valid tag name")
md.is_empty = empty
except Exception as e:
logger.exception("Unable to read metadata from the commandline '%s'", mdstr)
raise Exception("Unable to read metadata from the commandline") from e
return md

View File

@ -0,0 +1,29 @@
from __future__ import annotations
from typing import NamedTuple
class Replacement(NamedTuple):
find: str
replce: str
strict_only: bool
class Replacements(NamedTuple):
literal_text: list[Replacement]
format_value: list[Replacement]
DEFAULT_REPLACEMENTS = Replacements(
literal_text=[
Replacement(": ", " - ", True),
Replacement(":", "-", True),
],
format_value=[
Replacement(": ", " - ", True),
Replacement(":", "-", True),
Replacement("/", "-", False),
Replacement("//", "--", False),
Replacement("\\", "-", True),
],
)

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to confirm and set options for export to zip"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -19,7 +20,6 @@ import logging
from PyQt5 import QtCore, QtWidgets, uic
from comictaggerlib.settings import ComicTaggerSettings
from comictaggerlib.ui import ui_path
logger = logging.getLogger(__name__)
@ -32,18 +32,17 @@ class ExportConflictOpts:
class ExportWindow(QtWidgets.QDialog):
def __init__(self, parent: QtWidgets.QWidget, settings: ComicTaggerSettings, msg: str) -> None:
def __init__(self, parent: QtWidgets.QWidget, msg: str) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "exportwindow.ui", self)
with (ui_path / "exportwindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.label.setText(msg)
self.setWindowFlags(
QtCore.Qt.WindowType(self.windowFlags() & ~QtCore.Qt.WindowType.WindowContextHelpButtonHint)
)
self.settings = settings
self.cbxDeleteOriginal.setChecked(False)
self.cbxAddToList.setChecked(True)
self.radioDontCreate.setChecked(True)

View File

@ -1,6 +1,7 @@
"""Functions for renaming files based on metadata"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -16,58 +17,50 @@
from __future__ import annotations
import calendar
import datetime
import logging
import os
import pathlib
import string
from typing import Any, NamedTuple, cast
from collections.abc import Collection, Iterable, Mapping, Sequence, Sized
from typing import Any, cast
from pathvalidate import Platform, normalize_platform, sanitize_filename
from comicapi.comicarchive import ComicArchive
from comicapi.genericmetadata import GenericMetadata
from comicapi.issuestring import IssueString
from comictaggerlib.defaults import DEFAULT_REPLACEMENTS, Replacement, Replacements
logger = logging.getLogger(__name__)
class Replacement(NamedTuple):
find: str
replce: str
strict_only: bool
class Replacements(NamedTuple):
literal_text: list[Replacement]
format_value: list[Replacement]
REPLACEMENTS = Replacements(
literal_text=[
Replacement(": ", " - ", True),
Replacement(":", "-", True),
],
format_value=[
Replacement(": ", " - ", True),
Replacement(":", "-", True),
Replacement("/", "-", False),
Replacement("\\", "-", True),
],
)
def get_rename_dir(ca: ComicArchive, rename_dir: str | pathlib.Path | None) -> pathlib.Path:
folder = ca.path.parent.absolute()
if rename_dir is not None:
if isinstance(rename_dir, str):
rename_dir = rename_dir.strip()
folder = pathlib.Path(rename_dir).absolute()
rename_dir = pathlib.Path(rename_dir.strip())
folder = rename_dir.absolute()
return folder
def _isnamedtupleinstance(x: Any) -> bool: # pragma: no cover
t = type(x)
b = t.__bases__
if len(b) != 1 or b[0] != tuple:
return False
f = getattr(t, "_fields", None)
if not isinstance(f, tuple):
return False
return all(isinstance(n, str) for n in f)
class MetadataFormatter(string.Formatter):
def __init__(
self, smart_cleanup: bool = False, platform: str = "auto", replacements: Replacements = REPLACEMENTS
self, smart_cleanup: bool = False, platform: str = "auto", replacements: Replacements = DEFAULT_REPLACEMENTS
) -> None:
super().__init__()
self.smart_cleanup = smart_cleanup
@ -79,7 +72,36 @@ class MetadataFormatter(string.Formatter):
return ""
return cast(str, super().format_field(value, format_spec))
def convert_field(self, value: Any, conversion: str) -> str:
def convert_field(self, value: Any, conversion: str | None) -> str:
if value is None:
return ""
if isinstance(value, Iterable) and not isinstance(value, (str, tuple)):
if conversion == "C":
if isinstance(value, Sized):
return str(len(value))
return ""
if conversion and conversion.isdecimal():
if not isinstance(value, Collection):
return ""
i = int(conversion) - 1
if i < 0:
i = 0
if i < len(value):
try:
return sorted(value)[i]
except Exception:
...
return list(value)[i]
return ""
if conversion == "j":
conversion = "s"
try:
return ", ".join(list(self.convert_field(v, conversion) for v in sorted(value) if v is not None))
except Exception:
...
return ", ".join(list(self.convert_field(v, conversion) for v in value if v is not None))
if not conversion:
return cast(str, super().convert_field(value, conversion))
if conversion == "u":
return str(value).upper()
if conversion == "l":
@ -90,6 +112,8 @@ class MetadataFormatter(string.Formatter):
return str(value).swapcase()
if conversion == "t":
return str(value).title()
if conversion.isdecimal():
return ""
return cast(str, super().convert_field(value, conversion))
def handle_replacements(self, string: str, replacements: list[Replacement]) -> str:
@ -118,8 +142,8 @@ class MetadataFormatter(string.Formatter):
def _vformat(
self,
format_string: str,
args: list[Any],
kwargs: dict[str, Any],
args: Sequence[Any],
kwargs: Mapping[str, Any],
used_args: set[Any],
recursion_depth: int,
auto_arg_index: int = 0,
@ -129,7 +153,6 @@ class MetadataFormatter(string.Formatter):
result = []
lstrip = False
for literal_text, field_name, format_spec, conversion in self.parse(format_string):
# output the literal text
if literal_text:
if lstrip:
@ -161,13 +184,17 @@ class MetadataFormatter(string.Formatter):
# given the field_name, find the object it references
# and the argument it came from
obj, arg_used = self.get_field(field_name, args, kwargs)
used_args.add(arg_used)
try:
obj, arg_used = self.get_field(field_name, args, kwargs)
used_args.add(arg_used)
except Exception:
obj = None
obj = self.none_replacement(obj, replacement, r)
# do any conversion on the resulting object
obj = self.convert_field(obj, conversion) # type: ignore
obj = self.convert_field(obj, conversion)
if r == "-":
obj = self.none_replacement(obj, replacement, r)
# expand the format spec, if needed
format_spec, _ = self._vformat(
@ -200,16 +227,25 @@ class MetadataFormatter(string.Formatter):
class FileRenamer:
def __init__(self, metadata: GenericMetadata | None, platform: str = "auto") -> None:
def __init__(
self,
metadata: GenericMetadata | None,
platform: str = "auto",
replacements: Replacements = DEFAULT_REPLACEMENTS,
) -> None:
self.template = "{publisher}/{series}/{series} v{volume} #{issue} (of {issue_count}) ({year})"
self.smart_cleanup = True
self.issue_zero_padding = 3
self.metadata = metadata or GenericMetadata()
self.move = False
self.platform = platform
self.replacements = replacements
self.original_name = ""
self.move_only = False
def set_metadata(self, metadata: GenericMetadata) -> None:
def set_metadata(self, metadata: GenericMetadata, original_name: str) -> None:
self.metadata = metadata
self.original_name = original_name
def set_issue_zero_padding(self, count: int) -> None:
self.issue_zero_padding = count
@ -221,30 +257,58 @@ class FileRenamer:
self.template = template
def determine_name(self, ext: str) -> str:
class Default(dict):
class Default(dict[str, Any]):
def __missing__(self, key: str) -> str:
return "{" + key + "}"
md = self.metadata
# padding for issue
md.issue = IssueString(md.issue).as_string(pad=self.issue_zero_padding)
template = self.template
new_name = ""
fmt = MetadataFormatter(self.smart_cleanup, platform=self.platform)
fmt = MetadataFormatter(self.smart_cleanup, platform=self.platform, replacements=self.replacements)
md_dict = vars(md)
for role in ["writer", "penciller", "inker", "colorist", "letterer", "cover artist", "editor"]:
md_dict.update(
dict(
month_name=None,
month_abbr=None,
date=None,
genre=None,
story_arc=None,
series_group=None,
web_link=None,
character=None,
team=None,
location=None,
)
)
md_dict["issue"] = IssueString(md.issue).as_string(pad=self.issue_zero_padding)
for role in ["writer", "penciller", "inker", "colorist", "letterer", "cover artist", "editor", "translator"]:
md_dict[role] = md.get_primary_credit(role)
if (isinstance(md.month, int) or isinstance(md.month, str) and md.month.isdigit()) and 0 < int(md.month) < 13:
md_dict["month_name"] = calendar.month_name[int(md.month)]
md_dict["month_abbr"] = calendar.month_abbr[int(md.month)]
else:
md_dict["month_name"] = ""
md_dict["month_abbr"] = ""
if md.year is not None and datetime.MINYEAR <= md.year <= datetime.MAXYEAR:
md_dict["date"] = datetime.datetime(year=md.year, month=md.month or 1, day=md.day or 1)
if md.genres:
md_dict["genre"] = sorted(md.genres)[0]
if md.story_arcs:
md_dict["story_arc"] = md.story_arcs[0]
if md.series_groups:
md_dict["series_group"] = md.series_groups[0]
if md.web_links:
md_dict["web_link"] = md.web_links[0]
if md.characters:
md_dict["character"] = sorted(md.characters)[0]
if md.teams:
md_dict["team"] = sorted(md.teams)[0]
if md.locations:
md_dict["location"] = sorted(md.locations)[0]
new_basename = ""
for component in pathlib.PureWindowsPath(template).parts:
@ -253,11 +317,9 @@ class FileRenamer:
).strip()
new_name = os.path.join(new_name, new_basename)
new_name += ext
new_basename += ext
# remove padding
md.issue = IssueString(md.issue).as_string()
if self.move_only:
new_folder = os.path.join(new_name, os.path.splitext(self.original_name)[0])
return new_folder + ext
if self.move:
return new_name.strip()
return new_basename.strip()
return new_name.strip() + ext
return new_basename.strip() + ext

View File

@ -1,6 +1,7 @@
"""A PyQt5 widget for managing list of comic archive files"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -24,53 +25,38 @@ from PyQt5 import QtCore, QtWidgets, uic
from comicapi import utils
from comicapi.comicarchive import ComicArchive
from comictaggerlib.ctsettings import ct_ns
from comictaggerlib.graphics import graphics_path
from comictaggerlib.optionalmsgdialog import OptionalMessageDialog
from comictaggerlib.settings import ComicTaggerSettings
from comictaggerlib.settingswindow import linuxRarHelp, macRarHelp, windowsRarHelp
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import center_window_on_parent, reduce_widget_font_size
from comictaggerlib.ui.qtutils import center_window_on_parent
logger = logging.getLogger(__name__)
class FileTableWidgetItem(QtWidgets.QTableWidgetItem):
def __lt__(self, other: object) -> bool:
return self.data(QtCore.Qt.ItemDataRole.UserRole) < other.data(QtCore.Qt.ItemDataRole.UserRole) # type: ignore
class FileInfo:
def __init__(self, ca: ComicArchive) -> None:
self.ca: ComicArchive = ca
class FileSelectionList(QtWidgets.QWidget):
selectionChanged = QtCore.pyqtSignal(QtCore.QVariant)
listCleared = QtCore.pyqtSignal()
fileColNum = 0
CRFlagColNum = 1
CBLFlagColNum = 2
typeColNum = 3
readonlyColNum = 4
folderColNum = 5
MDFlagColNum = 1
typeColNum = 2
readonlyColNum = 3
folderColNum = 4
dataColNum = fileColNum
def __init__(
self,
parent: QtWidgets.QWidget,
settings: ComicTaggerSettings,
dirty_flag_verification: Callable[[str, str], bool],
self, parent: QtWidgets.QWidget, config: ct_ns, dirty_flag_verification: Callable[[str, str], bool]
) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "fileselectionlist.ui", self)
with (ui_path / "fileselectionlist.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.settings = settings
self.config = config
reduce_widget_font_size(self.twList)
self.twList.setColumnCount(6)
self.twList.horizontalHeader().setMinimumSectionSize(50)
self.twList.currentItemChanged.connect(self.current_item_changed_cb)
self.currentItem = None
@ -83,7 +69,7 @@ class FileSelectionList(QtWidgets.QWidget):
self.separator.setSeparator(True)
select_all_action.setShortcut("Ctrl+A")
remove_action.setShortcut("Ctrl+X")
remove_action.setShortcut("Backspace" if platform.system() == "Darwin" else "Delete")
select_all_action.triggered.connect(self.select_all)
remove_action.triggered.connect(self.remove_selection)
@ -110,10 +96,14 @@ class FileSelectionList(QtWidgets.QWidget):
self.dirty_flag = modified
def select_all(self) -> None:
self.twList.setRangeSelected(QtWidgets.QTableWidgetSelectionRange(0, 0, self.twList.rowCount() - 1, 5), True)
self.twList.setRangeSelected(
QtWidgets.QTableWidgetSelectionRange(0, 0, self.twList.rowCount() - 1, self.twList.columnCount() - 1), True
)
def deselect_all(self) -> None:
self.twList.setRangeSelected(QtWidgets.QTableWidgetSelectionRange(0, 0, self.twList.rowCount() - 1, 5), False)
self.twList.setRangeSelected(
QtWidgets.QTableWidgetSelectionRange(0, 0, self.twList.rowCount() - 1, self.twList.columnCount() - 1), False
)
def remove_archive_list(self, ca_list: list[ComicArchive]) -> None:
self.twList.setSortingEnabled(False)
@ -139,8 +129,8 @@ class FileSelectionList(QtWidgets.QWidget):
def get_archive_by_row(self, row: int) -> ComicArchive | None:
if row >= 0:
fi: FileInfo = self.twList.item(row, FileSelectionList.dataColNum).data(QtCore.Qt.ItemDataRole.UserRole)
return fi.ca
ca: ComicArchive = self.twList.item(row, FileSelectionList.dataColNum).data(QtCore.Qt.ItemDataRole.UserRole)
return ca
return None
def get_current_archive(self) -> ComicArchive | None:
@ -183,38 +173,42 @@ class FileSelectionList(QtWidgets.QWidget):
self.listCleared.emit()
def add_path_list(self, pathlist: list[str]) -> None:
if not pathlist:
return
filelist = utils.get_recursive_filelist(pathlist)
# we now have a list of files to add
# Prog dialog on Linux flakes out for small range, so scale up
progdialog = QtWidgets.QProgressDialog("", "Cancel", 0, len(filelist), parent=self)
progdialog.setWindowTitle("Adding Files")
progdialog.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal)
progdialog.setMinimumDuration(300)
center_window_on_parent(progdialog)
progdialog = None
if len(filelist) < 3:
# Prog dialog on Linux flakes out for small range, so scale up
progdialog = QtWidgets.QProgressDialog("", "Cancel", 0, len(filelist), parent=self)
progdialog.setWindowTitle("Adding Files")
progdialog.setWindowModality(QtCore.Qt.WindowModality.WindowModal)
progdialog.setMinimumDuration(300)
progdialog.show()
center_window_on_parent(progdialog)
QtCore.QCoreApplication.processEvents()
first_added = None
rar_added = False
rar_added_ro = False
self.twList.setSortingEnabled(False)
for idx, f in enumerate(filelist):
QtCore.QCoreApplication.processEvents()
if progdialog.wasCanceled():
break
progdialog.setValue(idx + 1)
progdialog.setLabelText(f)
center_window_on_parent(progdialog)
if progdialog is not None:
if progdialog.wasCanceled():
break
progdialog.setValue(idx + 1)
progdialog.setLabelText(f)
QtCore.QCoreApplication.processEvents()
row = self.add_path_item(f)
if row is not None:
ca = self.get_archive_by_row(row)
if ca and ca.is_rar():
rar_added = True
if first_added is None:
rar_added_ro = bool(ca and ca.archiver.name() == "RAR" and not ca.archiver.is_writable())
if first_added is None and row != -1:
first_added = row
progdialog.hide()
if progdialog is not None:
progdialog.hide()
QtCore.QCoreApplication.processEvents()
if first_added is not None:
@ -227,15 +221,14 @@ class FileSelectionList(QtWidgets.QWidget):
else:
QtWidgets.QMessageBox.information(self, "File/Folder Open", "No readable comic archives were found.")
if rar_added and not utils.which(self.settings.rar_exe_path or "rar"):
if rar_added_ro:
self.rar_ro_message()
self.twList.setSortingEnabled(True)
# Adjust column size
self.twList.resizeColumnsToContents()
self.twList.setColumnWidth(FileSelectionList.CRFlagColNum, 35)
self.twList.setColumnWidth(FileSelectionList.CBLFlagColNum, 35)
self.twList.setColumnWidth(FileSelectionList.MDFlagColNum, 35)
self.twList.setColumnWidth(FileSelectionList.readonlyColNum, 35)
self.twList.setColumnWidth(FileSelectionList.typeColNum, 45)
if self.twList.columnWidth(FileSelectionList.fileColNum) > 250:
@ -258,7 +251,7 @@ class FileSelectionList(QtWidgets.QWidget):
self,
"RAR Files are Read-Only",
"It looks like you have opened a RAR/CBR archive,\n"
"however ComicTagger cannot currently write to them without the rar program and are marked read only!\n\n"
"however ComicTagger cannot write to them without the rar program and are marked read only!\n\n"
f"{rar_help}",
)
self.rar_ro_shown = True
@ -281,23 +274,20 @@ class FileSelectionList(QtWidgets.QWidget):
if self.is_list_dupe(path):
return self.get_current_list_row(path)
ca = ComicArchive(path, self.settings.rar_exe_path, str(graphics_path / "nocover.png"))
ca = ComicArchive(path, str(graphics_path / "nocover.png"))
if ca.seems_to_be_a_comic_archive():
row: int = self.twList.rowCount()
self.twList.insertRow(row)
fi = FileInfo(ca)
filename_item = QtWidgets.QTableWidgetItem()
folder_item = QtWidgets.QTableWidgetItem()
cix_item = FileTableWidgetItem()
cbi_item = FileTableWidgetItem()
readonly_item = FileTableWidgetItem()
md_item = QtWidgets.QTableWidgetItem()
readonly_item = QtWidgets.QTableWidgetItem()
type_item = QtWidgets.QTableWidgetItem()
filename_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
filename_item.setData(QtCore.Qt.ItemDataRole.UserRole, fi)
filename_item.setData(QtCore.Qt.ItemDataRole.UserRole, ca)
self.twList.setItem(row, FileSelectionList.fileColNum, filename_item)
folder_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
@ -306,13 +296,9 @@ class FileSelectionList(QtWidgets.QWidget):
type_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, FileSelectionList.typeColNum, type_item)
cix_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
cix_item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.twList.setItem(row, FileSelectionList.CRFlagColNum, cix_item)
cbi_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
cbi_item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.twList.setItem(row, FileSelectionList.CBLFlagColNum, cbi_item)
md_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
md_item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.twList.setItem(row, FileSelectionList.MDFlagColNum, md_item)
readonly_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
readonly_item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
@ -325,69 +311,45 @@ class FileSelectionList(QtWidgets.QWidget):
def update_row(self, row: int) -> None:
if row >= 0:
fi: FileInfo = self.twList.item(row, FileSelectionList.dataColNum).data(QtCore.Qt.ItemDataRole.UserRole)
ca: ComicArchive = self.twList.item(row, FileSelectionList.dataColNum).data(QtCore.Qt.ItemDataRole.UserRole)
filename_item = self.twList.item(row, FileSelectionList.fileColNum)
folder_item = self.twList.item(row, FileSelectionList.folderColNum)
cix_item = self.twList.item(row, FileSelectionList.CRFlagColNum)
cbi_item = self.twList.item(row, FileSelectionList.CBLFlagColNum)
md_item = self.twList.item(row, FileSelectionList.MDFlagColNum)
type_item = self.twList.item(row, FileSelectionList.typeColNum)
readonly_item = self.twList.item(row, FileSelectionList.readonlyColNum)
item_text = os.path.split(fi.ca.path)[0]
item_text = os.path.split(ca.path)[0]
folder_item.setText(item_text)
folder_item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
item_text = os.path.split(fi.ca.path)[1]
item_text = os.path.split(ca.path)[1]
filename_item.setText(item_text)
filename_item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
if fi.ca.is_sevenzip():
item_text = "7Z"
elif fi.ca.is_zip():
item_text = "ZIP"
elif fi.ca.is_rar():
item_text = "RAR"
else:
item_text = ""
item_text = ca.archiver.name()
type_item.setText(item_text)
type_item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
if fi.ca.has_cix():
cix_item.setCheckState(QtCore.Qt.CheckState.Checked)
cix_item.setData(QtCore.Qt.ItemDataRole.UserRole, True)
else:
cix_item.setData(QtCore.Qt.ItemDataRole.UserRole, False)
cix_item.setCheckState(QtCore.Qt.CheckState.Unchecked)
md_item.setText(", ".join(x for x in ca.get_supported_tags() if ca.has_tags(x)))
if fi.ca.has_cbi():
cbi_item.setCheckState(QtCore.Qt.CheckState.Checked)
cbi_item.setData(QtCore.Qt.ItemDataRole.UserRole, True)
else:
cbi_item.setData(QtCore.Qt.ItemDataRole.UserRole, False)
cbi_item.setCheckState(QtCore.Qt.CheckState.Unchecked)
if not fi.ca.is_writable():
if not ca.is_writable():
readonly_item.setCheckState(QtCore.Qt.CheckState.Checked)
readonly_item.setData(QtCore.Qt.ItemDataRole.UserRole, True)
readonly_item.setText(" ")
else:
readonly_item.setData(QtCore.Qt.ItemDataRole.UserRole, False)
readonly_item.setCheckState(QtCore.Qt.CheckState.Unchecked)
# Reading these will force them into the ComicArchive's cache
try:
fi.ca.read_cix()
except Exception:
pass
fi.ca.has_cbi()
# This is a nbsp it sorts after a space ' '
readonly_item.setText("\xa0")
def get_selected_archive_list(self) -> list[ComicArchive]:
ca_list: list[ComicArchive] = []
for r in range(self.twList.rowCount()):
item = self.twList.item(r, FileSelectionList.dataColNum)
if item.isSelected():
fi: FileInfo = item.data(QtCore.Qt.ItemDataRole.UserRole)
ca_list.append(fi.ca)
ca: ComicArchive = item.data(QtCore.Qt.ItemDataRole.UserRole)
ca_list.append(ca)
return ca_list

View File

@ -1,5 +1,5 @@
from __future__ import annotations
import pathlib
import importlib.resources
graphics_path = pathlib.Path(__file__).parent
graphics_path = importlib.resources.files(__package__)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg
version="1.1"
id="Capa_1"
x="0px"
y="0px"
viewBox="0 0 469.333 469.333"
style="enable-background:new 0 0 469.333 469.333;"
xml:space="preserve"
sodipodi:docname="eye.svg"
inkscape:version="1.2.2 (b0a8486541, 2022-12-01)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"><defs
id="defs45" /><sodipodi:namedview
id="namedview43"
pagecolor="#505050"
bordercolor="#eeeeee"
borderopacity="1"
inkscape:showpageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#505050"
showgrid="false"
inkscape:zoom="2.1882117"
inkscape:cx="234.6665"
inkscape:cy="234.6665"
inkscape:window-width="2560"
inkscape:window-height="1361"
inkscape:window-x="0"
inkscape:window-y="42"
inkscape:window-maximized="1"
inkscape:current-layer="Capa_1" />
<g
id="g10"
style="fill:#333333">
<g
id="g8"
style="fill:#333333">
<g
id="g6"
style="fill:#333333">
<path
d="M234.667,170.667c-35.307,0-64,28.693-64,64s28.693,64,64,64s64-28.693,64-64S269.973,170.667,234.667,170.667z"
id="path2"
style="fill:#333333" />
<path
d="M234.667,74.667C128,74.667,36.907,141.013,0,234.667c36.907,93.653,128,160,234.667,160 c106.773,0,197.76-66.347,234.667-160C432.427,141.013,341.44,74.667,234.667,74.667z M234.667,341.333 c-58.88,0-106.667-47.787-106.667-106.667S175.787,128,234.667,128s106.667,47.787,106.667,106.667 S293.547,341.333,234.667,341.333z"
id="path4"
style="fill:#333333" />
</g>
</g>
</g>
<g
id="g12">
</g>
<g
id="g14">
</g>
<g
id="g16">
</g>
<g
id="g18">
</g>
<g
id="g20">
</g>
<g
id="g22">
</g>
<g
id="g24">
</g>
<g
id="g26">
</g>
<g
id="g28">
</g>
<g
id="g30">
</g>
<g
id="g32">
</g>
<g
id="g34">
</g>
<g
id="g36">
</g>
<g
id="g38">
</g>
<g
id="g40">
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -0,0 +1,24 @@
<RCC>
<qresource prefix="graphics">
<file>about.png</file>
<file>app.png</file>
<file>auto.png</file>
<file>autotag.png</file>
<file>browse.png</file>
<file>clear.png</file>
<file>down.png</file>
<file>eye.svg</file>
<file>hidden.svg</file>
<file>left.png</file>
<file>longbox.png</file>
<file>nocover.png</file>
<file>open.png</file>
<file>parse.png</file>
<file>popup_bg.png</file>
<file>right.png</file>
<file>save.png</file>
<file>search.png</file>
<file>tags.png</file>
<file>up.png</file>
</qresource>
</RCC>

View File

@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg
version="1.1"
id="Capa_1"
x="0px"
y="0px"
viewBox="0 0 469.44 469.44"
style="enable-background:new 0 0 469.44 469.44;"
xml:space="preserve"
sodipodi:docname="hidden.svg"
inkscape:version="1.2.2 (b0a8486541, 2022-12-01)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"><defs
id="defs47" /><sodipodi:namedview
id="namedview45"
pagecolor="#505050"
bordercolor="#eeeeee"
borderopacity="1"
inkscape:showpageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#505050"
showgrid="false"
inkscape:zoom="2.187713"
inkscape:cx="234.72"
inkscape:cy="234.72"
inkscape:window-width="2560"
inkscape:window-height="1361"
inkscape:window-x="0"
inkscape:window-y="42"
inkscape:window-maximized="1"
inkscape:current-layer="Capa_1" />
<g
id="g12"
style="fill:#333333">
<g
id="g10"
style="fill:#333333">
<g
id="g8"
style="fill:#333333">
<path
d="M231.147,160.373l67.2,67.2l0.32-3.52c0-35.307-28.693-64-64-64L231.147,160.373z"
id="path2"
style="fill:#333333" />
<path
d="M234.667,117.387c58.88,0,106.667,47.787,106.667,106.667c0,13.76-2.773,26.88-7.573,38.933l62.4,62.4 c32.213-26.88,57.6-61.653,73.28-101.333c-37.013-93.653-128-160-234.773-160c-29.867,0-58.453,5.333-85.013,14.933l46.08,45.973 C207.787,120.267,220.907,117.387,234.667,117.387z"
id="path4"
style="fill:#333333" />
<path
d="M21.333,59.253l48.64,48.64l9.707,9.707C44.48,145.12,16.64,181.707,0,224.053c36.907,93.653,128,160,234.667,160 c33.067,0,64.64-6.4,93.547-18.027l9.067,9.067l62.187,62.293l27.2-27.093L48.533,32.053L21.333,59.253z M139.307,177.12 l32.96,32.96c-0.96,4.587-1.6,9.173-1.6,13.973c0,35.307,28.693,64,64,64c4.8,0,9.387-0.64,13.867-1.6l32.96,32.96 c-14.187,7.04-29.973,11.307-46.827,11.307C175.787,330.72,128,282.933,128,224.053C128,207.2,132.267,191.413,139.307,177.12z"
id="path6"
style="fill:#333333" />
</g>
</g>
</g>
<g
id="g14">
</g>
<g
id="g16">
</g>
<g
id="g18">
</g>
<g
id="g20">
</g>
<g
id="g22">
</g>
<g
id="g24">
</g>
<g
id="g26">
</g>
<g
id="g28">
</g>
<g
id="g30">
</g>
<g
id="g32">
</g>
<g
id="g34">
</g>
<g
id="g36">
</g>
<g
id="g38">
</g>
<g
id="g40">
</g>
<g
id="g42">
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.6 KiB

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

154
comictaggerlib/gui.py Normal file
View File

@ -0,0 +1,154 @@
from __future__ import annotations
import logging.handlers
import os
import platform
import sys
import traceback
import types
import settngs
from comictaggerlib.ctsettings import ct_ns
from comictaggerlib.graphics import graphics_path
from comictalker.comictalker import ComicTalker
logger = logging.getLogger("comictagger")
try:
qt_available = True
from PyQt5 import QtCore, QtGui, QtWidgets
def show_exception_box(log_msg: str) -> None:
"""Checks if a QApplication instance is available and shows a messagebox with the exception message.
If unavailable (non-console application), log an additional notice.
"""
if QtWidgets.QApplication.instance() is not None:
errorbox = QtWidgets.QMessageBox()
errorbox.setStandardButtons(
QtWidgets.QMessageBox.StandardButton.Abort | QtWidgets.QMessageBox.StandardButton.Ignore
)
errorbox.setText(log_msg)
if errorbox.exec() == QtWidgets.QMessageBox.StandardButton.Abort:
QtWidgets.QApplication.exit(1)
else:
logger.warning("Exception ignored")
else:
logger.debug("No QApplication instance available.")
class UncaughtHook(QtCore.QObject):
_exception_caught = QtCore.pyqtSignal(object)
def __init__(self) -> None:
super().__init__()
# this registers the exception_hook() function as hook with the Python interpreter
sys.excepthook = self.exception_hook
# connect signal to execute the message box function always on main thread
self._exception_caught.connect(show_exception_box)
def exception_hook(
self, exc_type: type[BaseException], exc_value: BaseException, exc_traceback: types.TracebackType | None
) -> None:
"""Function handling uncaught exceptions.
It is triggered each time an uncaught exception occurs.
"""
if issubclass(exc_type, KeyboardInterrupt):
# ignore keyboard interrupt to support console applications
sys.__excepthook__(exc_type, exc_value, exc_traceback)
else:
exc_info = (exc_type, exc_value, exc_traceback)
trace_back = "".join(traceback.format_tb(exc_traceback))
log_msg = f"{exc_type.__name__}: {exc_value}\n\n{trace_back}"
logger.critical("Uncaught exception: %s: %s", exc_type.__name__, exc_value, exc_info=exc_info)
# trigger message box show
self._exception_caught.emit(f"Oops. An unexpected error occurred:\n{log_msg}")
qt_exception_hook = UncaughtHook()
from comictaggerlib.taggerwindow import TaggerWindow
try:
# needed here to initialize QWebEngine
from PyQt5.QtWebEngineWidgets import QWebEngineView # noqa: F401
qt_webengine_available = True
except ImportError:
qt_webengine_available = False
class Application(QtWidgets.QApplication):
openFileRequest = QtCore.pyqtSignal(QtCore.QUrl, name="openfileRequest")
# Handles "Open With" from Finder on macOS
def event(self, event: QtCore.QEvent) -> bool:
if event.type() == QtCore.QEvent.FileOpen:
logger.info(event.url().toLocalFile())
self.openFileRequest.emit(event.url())
return True
return super().event(event)
except ImportError as e:
def show_exception_box(log_msg: str) -> None: ...
logger.exception("Qt unavailable")
qt_available = False
import_error = e
def open_tagger_window(
talkers: dict[str, ComicTalker], config: settngs.Config[ct_ns], error: tuple[str, bool] | None
) -> None:
os.environ["QtWidgets.QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
args = [sys.argv[0]]
if config[0].Runtime_Options__darkmode:
args.extend(["-platform", "windows:darkmode=2"])
app = Application(args)
if error is not None:
show_exception_box(error[0])
if error[1]:
raise SystemExit(1)
# needed to catch initial open file events (macOS)
app.openFileRequest.connect(lambda x: config[0].Runtime_Options__files.append(x.toLocalFile()))
# The window Icon needs to be set here. It's also set in taggerwindow.ui but it doesn't seem to matter
app.setWindowIcon(QtGui.QIcon(":/graphics/app.png"))
if platform.system() == "Windows":
# For pure python, tell windows that we're not python,
# so we can have our own taskbar icon
import ctypes
myappid = "comictagger" # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) # type: ignore[attr-defined]
# force close of console window
swp_hidewindow = 0x0080
console_wnd = ctypes.windll.kernel32.GetConsoleWindow() # type: ignore[attr-defined]
if console_wnd != 0:
ctypes.windll.user32.SetWindowPos(console_wnd, None, 0, 0, 0, 0, swp_hidewindow) # type: ignore[attr-defined]
if platform.system() != "Linux":
img = QtGui.QPixmap()
img.loadFromData((graphics_path / "tags.png").read_bytes())
splash = QtWidgets.QSplashScreen(img)
splash.show()
splash.raise_()
QtWidgets.QApplication.processEvents()
try:
tagger_window = TaggerWindow(config[0].Runtime_Options__files, config, talkers)
tagger_window.show()
# Catch open file events (macOS)
app.openFileRequest.connect(tagger_window.open_file_event)
if platform.system() != "Linux":
splash.finish(tagger_window)
sys.exit(app.exec())
except Exception:
logger.exception("GUI mode failed")
QtWidgets.QMessageBox.critical(
QtWidgets.QMainWindow(), "Error", "Unhandled exception in app:\n" + traceback.format_exc()
)

View File

@ -1,6 +1,7 @@
"""A class to manage fetching and caching of images by URL"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -18,56 +19,60 @@ from __future__ import annotations
import datetime
import logging
import os
import pathlib
import shutil
import sqlite3 as lite
import tempfile
import requests
from comictaggerlib import ctversion
from comictaggerlib.settings import ComicTaggerSettings
from typing import TYPE_CHECKING
try:
from PyQt5 import QtCore, QtNetwork
qt_available = True
import niquests as requests
except ImportError:
qt_available = False
import requests
from comictaggerlib import ctversion
if TYPE_CHECKING:
from PyQt5 import QtCore, QtNetwork
logger = logging.getLogger(__name__)
class ImageFetcherException(Exception):
...
class ImageFetcherException(Exception): ...
def fetch_complete(image_data: bytes | QtCore.QByteArray) -> None:
...
def fetch_complete(url: str, image_data: bytes | QtCore.QByteArray) -> None: ...
class ImageFetcher:
image_fetch_complete = fetch_complete
qt_available = True
def __init__(self) -> None:
self.settings_folder = ComicTaggerSettings.get_settings_folder()
self.db_file = os.path.join(self.settings_folder, "image_url_cache.db")
self.cache_folder = os.path.join(self.settings_folder, "image_cache")
def __init__(self, cache_folder: pathlib.Path) -> None:
self.db_file = cache_folder / "image_url_cache.db"
self.cache_folder = cache_folder / "image_cache"
self.user_data = None
self.fetched_url = ""
if self.qt_available:
try:
from PyQt5 import QtNetwork
self.qt_available = True
except ImportError:
self.qt_available = False
if not os.path.exists(self.db_file):
self.create_image_db()
if qt_available:
if self.qt_available:
self.nam = QtNetwork.QNetworkAccessManager()
def clear_cache(self) -> None:
os.unlink(self.db_file)
if os.path.isdir(self.cache_folder):
shutil.rmtree(self.cache_folder)
self.cache_folder.mkdir(parents=True, exist_ok=True)
def fetch(self, url: str, blocking: bool = False) -> bytes:
"""
@ -81,22 +86,24 @@ class ImageFetcher:
# first look in the DB
image_data = self.get_image_from_cache(url)
if blocking or not qt_available:
# Async for retrieving covers seems to work well
if blocking or not self.qt_available:
if not image_data:
try:
image_data = requests.get(url, headers={"user-agent": "comictagger/" + ctversion.version}).content
# save the image to the cache
self.add_image_to_cache(self.fetched_url, image_data)
except Exception as e:
logger.exception("Fetching url failed: %s")
raise ImageFetcherException("Network Error!") from e
# save the image to the cache
self.add_image_to_cache(self.fetched_url, image_data)
return image_data
if qt_available:
if self.qt_available:
from PyQt5 import QtCore, QtNetwork
# if we found it, just emit the signal asap
if image_data:
ImageFetcher.image_fetch_complete(QtCore.QByteArray(image_data))
ImageFetcher.image_fetch_complete(url, QtCore.QByteArray(image_data))
return b""
# didn't find it. look online
@ -112,12 +119,11 @@ class ImageFetcher:
image_data = reply.readAll()
# save the image to the cache
self.add_image_to_cache(self.fetched_url, image_data)
self.add_image_to_cache(reply.request().url().toString(), image_data)
ImageFetcher.image_fetch_complete(image_data)
ImageFetcher.image_fetch_complete(reply.request().url().toString(), image_data)
def create_image_db(self) -> None:
# this will wipe out any existing version
open(self.db_file, "wb").close()
@ -126,19 +132,14 @@ class ImageFetcher:
shutil.rmtree(self.cache_folder)
os.makedirs(self.cache_folder)
con = lite.connect(self.db_file)
# create tables
with con:
with lite.connect(self.db_file) as con:
cur = con.cursor()
cur.execute("CREATE TABLE Images(url TEXT,filename TEXT,timestamp TEXT,PRIMARY KEY (url))")
def add_image_to_cache(self, url: str, image_data: bytes | QtCore.QByteArray) -> None:
con = lite.connect(self.db_file)
with con:
with lite.connect(self.db_file) as con:
cur = con.cursor()
timestamp = datetime.datetime.now()
@ -150,9 +151,7 @@ class ImageFetcher:
cur.execute("INSERT or REPLACE INTO Images VALUES(?, ?, ?)", (url, filename, timestamp))
def get_image_from_cache(self, url: str) -> bytes:
con = lite.connect(self.db_file)
with con:
with lite.connect(self.db_file) as con:
cur = con.cursor()
cur.execute("SELECT filename FROM Images WHERE url=?", [url])

215
comictaggerlib/imagehasher.py Executable file → Normal file
View File

@ -1,6 +1,7 @@
"""A class to manage creating image content hashes, and calculate hamming distances"""
#
# Copyright 2013 Anthony Beville
# Copyright 2013 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -16,8 +17,11 @@
from __future__ import annotations
import io
import itertools
import logging
from functools import reduce
import math
from collections.abc import Sequence
from statistics import median
from typing import TypeVar
try:
@ -30,13 +34,19 @@ logger = logging.getLogger(__name__)
class ImageHasher:
def __init__(self, path: str | None = None, data: bytes = b"", width: int = 8, height: int = 8) -> None:
def __init__(
self, path: str | None = None, image: Image | None = None, data: bytes = b"", width: int = 8, height: int = 8
) -> None:
self.width = width
self.height = height
if path is None and not data:
if path is None and not data and not image:
raise OSError
if image is not None:
self.image = image
return
try:
if path is not None:
self.image = Image.open(path)
@ -57,115 +67,104 @@ class ImageHasher:
pixels = list(image.getdata())
avg = sum(pixels) / len(pixels)
def compare_value_to_avg(i: int) -> int:
return 1 if i > avg else 0
diff = "".join(str(int(p > avg)) for p in pixels)
bitlist = list(map(compare_value_to_avg, pixels))
# build up an int value from the bit list, one bit at a time
def set_bit(x: int, idx_val: tuple[int, int]) -> int:
(idx, val) = idx_val
return x | (val << idx)
result = reduce(set_bit, enumerate(bitlist), 0)
result = int(diff, 2)
return result
def average_hash2(self) -> None:
"""
# Got this one from somewhere on the net. Not a clue how the 'convolve2d' works!
def difference_hash(self) -> int:
try:
image = self.image.resize((self.width + 1, self.height), Image.Resampling.LANCZOS).convert("L")
except Exception:
logger.exception("difference_hash error")
return 0
from numpy import array
from scipy.signal import convolve2d
im = self.image.resize((self.width, self.height), Image.ANTIALIAS).convert('L')
in_data = array((im.getdata())).reshape(self.width, self.height)
filt = array([[0,1,0],[1,-4,1],[0,1,0]])
filt_data = convolve2d(in_data,filt,mode='same',boundary='symm').flatten()
result = reduce(lambda x, (y, z): x | (z << y),
enumerate(map(lambda i: 0 if i < 0 else 1, filt_data)),
0)
return result
"""
def dct_average_hash(self) -> None:
"""
# Algorithm source: http://syntaxcandy.blogspot.com/2012/08/perceptual-hash.html
1. Reduce size. Like Average Hash, pHash starts with a small image.
However, the image is larger than 8x8; 32x32 is a good size. This
is really done to simplify the DCT computation and not because it
is needed to reduce the high frequencies.
2. Reduce color. The image is reduced to a grayscale just to further
simplify the number of computations.
3. Compute the DCT. The DCT separates the image into a collection of
frequencies and scalars. While JPEG uses an 8x8 DCT, this algorithm
uses a 32x32 DCT.
4. Reduce the DCT. This is the magic step. While the DCT is 32x32,
just keep the top-left 8x8. Those represent the lowest frequencies in
the picture.
5. Compute the average value. Like the Average Hash, compute the mean DCT
value (using only the 8x8 DCT low-frequency values and excluding the first
term since the DC coefficient can be significantly different from the other
values and will throw off the average). Thanks to David Starkweather for the
added information about pHash. He wrote: "the dct hash is based on the low 2D
DCT coefficients starting at the second from lowest, leaving out the first DC
term. This excludes completely flat image information (i.e. solid colors) from
being included in the hash description."
6. Further reduce the DCT. This is the magic step. Set the 64 hash bits to 0 or
1 depending on whether each of the 64 DCT values is above or below the average
value. The result doesn't tell us the actual low frequencies; it just tells us
the very-rough relative scale of the frequencies to the mean. The result will not
vary as long as the overall structure of the image remains the same; this can
survive gamma and color histogram adjustments without a problem.
7. Construct the hash. Set the 64 bits into a 64-bit integer. The order does not
matter, just as long as you are consistent.
import numpy
import scipy.fftpack
numpy.set_printoptions(threshold=10000, linewidth=200, precision=2, suppress=True)
# Step 1,2
im = self.image.resize((32, 32), Image.ANTIALIAS).convert("L")
in_data = numpy.asarray(im)
# Step 3
dct = scipy.fftpack.dct(in_data.astype(float))
# Step 4
# Just skip the top and left rows when slicing, as suggested somewhere else...
lofreq_dct = dct[1:9, 1:9].flatten()
# Step 5
avg = (lofreq_dct.sum()) / (lofreq_dct.size)
median = numpy.median(lofreq_dct)
thresh = avg
# Step 6
def compare_value_to_thresh(i):
return (1 if i > thresh else 0)
bitlist = map(compare_value_to_thresh, lofreq_dct)
#Step 7
def set_bit(x, (idx, val)):
return (x | (val << idx))
result = reduce(set_bit, enumerate(bitlist), long(0))
pixels = list(image.getdata())
diff = ""
for y in range(self.height):
for x in range(self.width):
idx = x + (self.width + 1 * y)
diff += str(int(pixels[idx] < pixels[idx + 1]))
result = int(diff, 2)
return result
def p_hash(self) -> int:
"""
Pure python version of Perceptual Hash computation of https://github.com/JohannesBuchner/imagehash/tree/master
Implementation follows http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
"""
def generate_dct2(block: Sequence[Sequence[float]], axis: int = 0) -> list[list[float]]:
def dct1(block: Sequence[float]) -> list[float]:
"""Perform 1D Discrete Cosine Transform (DCT) on a given block."""
N = len(block)
dct_block = [0.0] * N
for k in range(N):
sum_val = 0.0
for n in range(N):
cos_val = math.cos(math.pi * k * (2 * n + 1) / (2 * N))
sum_val += block[n] * cos_val
dct_block[k] = sum_val
return dct_block
"""Perform 2D Discrete Cosine Transform (DCT) on a given block along the specified axis."""
rows = len(block)
cols = len(block[0])
dct_block = [[0.0] * cols for _ in range(rows)]
if axis == 0:
# Apply 1D DCT on each row
for i in range(rows):
dct_block[i] = dct1(block[i])
elif axis == 1:
# Apply 1D DCT on each column
for j in range(cols):
column = [block[i][j] for i in range(rows)]
dct_column = dct1(column)
for i in range(rows):
dct_block[i][j] = dct_column[i]
else:
raise ValueError("Invalid axis value. Must be either 0 or 1.")
return dct_block
def convert_image_to_ndarray(image: Image.Image) -> Sequence[Sequence[float]]:
width, height = image.size
pixels2 = []
for y in range(height):
row = []
for x in range(width):
pixel = image.getpixel((x, y))
row.append(pixel)
pixels2.append(row)
return pixels2
highfreq_factor = 4
img_size = 8 * highfreq_factor
try:
image = self.image.convert("L").resize((img_size, img_size), Image.Resampling.LANCZOS)
except Exception:
logger.exception("p_hash error converting to greyscale and resizing")
return 0
pixels = convert_image_to_ndarray(image)
dct = generate_dct2(generate_dct2(pixels, axis=0), axis=1)
dctlowfreq = list(itertools.chain.from_iterable(row[:8] for row in dct[:8]))
med = median(dctlowfreq)
# Convert to a bit string
diff = "".join(str(int(item > med)) for item in dctlowfreq)
result = int(diff, 2)
return result
# accepts 2 hashes (longs or hex strings) and returns the hamming distance
@ -173,12 +172,14 @@ class ImageHasher:
@staticmethod
def hamming_distance(h1: T, h2: T) -> int:
if isinstance(h1, int) or isinstance(h2, int):
if isinstance(h1, int):
n1 = h1
else:
n1 = int(h1, 16)
if isinstance(h2, int):
n2 = h2
else:
# convert hex strings to ints
n1 = int(h1, 16)
n2 = int(h2, 16)
# xor the two numbers

View File

@ -1,6 +1,7 @@
"""A PyQT4 widget to display a popup image"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -16,20 +17,39 @@
from __future__ import annotations
import logging
import platform
from PyQt5 import QtCore, QtGui, QtWidgets, sip, uic
from comictaggerlib.graphics import graphics_path
from comictaggerlib.ui import ui_path
logger = logging.getLogger(__name__)
def clickable(widget: QtWidgets.QWidget) -> QtCore.pyqtBoundSignal:
"""Allow a label to be clickable"""
class Filter(QtCore.QObject):
clicked = QtCore.pyqtSignal()
def eventFilter(self, obj: QtCore.QObject, event: QtCore.QEvent) -> bool:
if obj == widget:
if event.type() == QtCore.QEvent.Type.MouseButtonPress:
self.clicked.emit()
return True
return False
flt = Filter(widget)
widget.installEventFilter(flt)
return flt.clicked
class ImagePopup(QtWidgets.QDialog):
def __init__(self, parent: QtWidgets.QWidget, image_pixmap: QtGui.QPixmap) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "imagepopup.ui", self)
with (ui_path / "imagepopup.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CursorShape.WaitCursor))
@ -38,28 +58,36 @@ class ImagePopup(QtWidgets.QDialog):
self.imagePixmap = image_pixmap
screen_size = QtGui.QGuiApplication.primaryScreen().geometry()
screen = QtWidgets.QApplication.primaryScreen()
screen_size = screen.geometry()
if platform.system() == "Darwin":
screen_size = screen.availableGeometry()
QtWidgets.QApplication.primaryScreen()
self.resize(screen_size.width(), screen_size.height())
self.move(0, 0)
# This is a total hack. Uses a snapshot of the desktop, and overlays a
# translucent screen over it. Probably can do it better by setting opacity of a widget
# TODO: macOS denies this
screen = QtWidgets.QApplication.primaryScreen()
self.desktopBg = screen.grabWindow(sip.voidptr(0), 0, 0, screen_size.width(), screen_size.height())
bg = QtGui.QPixmap(str(graphics_path / "popup_bg.png"))
self.clientBgPixmap = bg.scaled(
screen_size.width(),
screen_size.height(),
QtCore.Qt.AspectRatioMode.IgnoreAspectRatio,
QtCore.Qt.SmoothTransformation,
)
self.setMask(self.clientBgPixmap.mask())
self.apply_image_pixmap()
self.showFullScreen()
self.raise_()
if platform.system() == "Darwin":
self.lblImage: QtWidgets.QLabel
splash = QtWidgets.QSplashScreen(self.lblImage.pixmap())
clickable(splash).connect(lambda *x: splash.close())
splash.show()
else:
# This is a total hack. Uses a snapshot of the desktop, and overlays a
# translucent screen over it. Probably can do it better by setting opacity of a widget
# TODO: macOS denies this
self.desktopBg = screen.grabWindow(sip.voidptr(0), 0, 0, screen_size.width(), screen_size.height())
bg = QtGui.QPixmap(":/graphics/popup_bg.png")
self.clientBgPixmap = bg.scaled(
screen_size.width(),
screen_size.height(),
QtCore.Qt.AspectRatioMode.IgnoreAspectRatio,
QtCore.Qt.SmoothTransformation,
)
self.setMask(self.clientBgPixmap.mask())
self.showFullScreen()
self.raise_()
QtWidgets.QApplication.restoreOverrideCursor()
def paintEvent(self, event: QtGui.QPaintEvent) -> None:

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to select specific issue from list"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -17,15 +18,15 @@ from __future__ import annotations
import logging
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5 import QtCore, QtGui, QtWidgets
from comicapi.genericmetadata import GenericMetadata
from comicapi.issuestring import IssueString
from comictaggerlib.comicvinetalker import ComicVineTalker, ComicVineTalkerException
from comictaggerlib.coverimagewidget import CoverImageWidget
from comictaggerlib.resulttypes import CVIssuesResults
from comictaggerlib.settings import ComicTaggerSettings
from comictaggerlib.ctsettings import ct_ns
from comictaggerlib.seriesselectionwindow import SelectionWindow
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import reduce_widget_font_size
from comictalker.comictalker import ComicTalker, RLCallBack, TalkerError
logger = logging.getLogger(__name__)
@ -38,147 +39,149 @@ class IssueNumberTableWidgetItem(QtWidgets.QTableWidgetItem):
return (IssueString(self_str).as_float() or 0) < (IssueString(other_str).as_float() or 0)
class IssueSelectionWindow(QtWidgets.QDialog):
volume_id = 0
class QueryThread(QtCore.QThread):
def __init__(
self, parent: QtWidgets.QWidget, settings: ComicTaggerSettings, series_id: int, issue_number: str
self,
talker: ComicTalker,
series_id: str,
finish: QtCore.pyqtSignal,
on_ratelimit: QtCore.pyqtSignal,
) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "issueselectionwindow.ui", self)
self.coverWidget = CoverImageWidget(self.coverImageContainer, CoverImageWidget.AltCoverMode)
gridlayout = QtWidgets.QGridLayout(self.coverImageContainer)
gridlayout.addWidget(self.coverWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
reduce_widget_font_size(self.twList)
reduce_widget_font_size(self.teDescription, 1)
self.setWindowFlags(
QtCore.Qt.WindowType(
self.windowFlags()
| QtCore.Qt.WindowType.WindowSystemMenuHint
| QtCore.Qt.WindowType.WindowMaximizeButtonHint
)
)
super().__init__()
self.series_id = series_id
self.issue_id: int | None = None
self.settings = settings
self.url_fetch_thread = None
self.issue_list: list[CVIssuesResults] = []
self.talker = talker
self.finish = finish
self.on_ratelimit = on_ratelimit
if issue_number is None or issue_number == "":
self.issue_number = "1"
else:
self.issue_number = issue_number
self.initial_id: int | None = None
self.perform_query()
self.twList.resizeColumnsToContents()
self.twList.currentItemChanged.connect(self.current_item_changed)
self.twList.cellDoubleClicked.connect(self.cell_double_clicked)
# now that the list has been sorted, find the initial record, and
# select it
if self.initial_id is None:
self.twList.selectRow(0)
else:
for r in range(0, self.twList.rowCount()):
issue_id = self.twList.item(r, 0).data(QtCore.Qt.ItemDataRole.UserRole)
if issue_id == self.initial_id:
self.twList.selectRow(r)
break
def perform_query(self) -> None:
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CursorShape.WaitCursor))
def run(self) -> None:
# QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CursorShape.WaitCursor))
try:
comic_vine = ComicVineTalker(self.settings.id_series_match_search_thresh)
comic_vine.fetch_volume_data(self.series_id)
self.issue_list = comic_vine.fetch_issues_by_volume(self.series_id)
except ComicVineTalkerException as e:
QtWidgets.QApplication.restoreOverrideCursor()
if e.code == ComicVineTalkerException.RateLimit:
QtWidgets.QMessageBox.critical(self, "Comic Vine Error", ComicVineTalker.get_rate_limit_message())
else:
QtWidgets.QMessageBox.critical(self, "Network Issue", "Could not connect to Comic Vine to list issues!")
issue_list = [
x
for x in self.talker.fetch_issues_in_series(
self.series_id, on_rate_limit=RLCallBack(lambda x, y: self.on_ratelimit.emit(x, y), 10)
)
if x.issue_id is not None
]
except TalkerError as e:
# QtWidgets.QApplication.restoreOverrideCursor()
# QtWidgets.QMessageBox.critical(None, f"{e.source} {e.code_name} Error", f"{e}")
return
# QtWidgets.QApplication.restoreOverrideCursor()
self.finish.emit(issue_list)
class IssueSelectionWindow(SelectionWindow):
ui_file = ui_path / "issueselectionwindow.ui"
CoverImageMode = CoverImageWidget.AltCoverMode
finish = QtCore.pyqtSignal(list)
def __init__(
self,
parent: QtWidgets.QWidget,
config: ct_ns,
talker: ComicTalker,
series_id: str = "",
issue_number: str = "",
) -> None:
super().__init__(parent, config, talker)
self.series_id = series_id
self.issue_list: dict[str, GenericMetadata] = {}
self.issue_number = issue_number
if issue_number is None or issue_number == "":
self.issue_number = "1"
self.initial_id: str = ""
self.leFilter.textChanged.connect(self.filter)
self.finish.connect(self.query_finished)
def showEvent(self, event: QtGui.QShowEvent) -> None:
self.perform_query()
def perform_query(self) -> None: # type: ignore[override]
self.querythread = QueryThread(
self.talker,
self.series_id,
self.finish,
self.ratelimit,
)
self.querythread.start()
def query_finished(self, issues: list[GenericMetadata]) -> None:
self.twList.setRowCount(0)
self.twList.setSortingEnabled(False)
row = 0
for record in self.issue_list:
self.issue_list = {i.issue_id: i for i in issues if i.issue_id is not None}
self.twList.clear()
for row, issue in enumerate(issues):
self.twList.insertRow(row)
self.twList.setItem(row, 0, IssueNumberTableWidgetItem())
self.twList.setItem(row, 1, QtWidgets.QTableWidgetItem())
self.twList.setItem(row, 2, QtWidgets.QTableWidgetItem())
item_text = record["issue_number"]
item = IssueNumberTableWidgetItem(item_text)
item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
item.setData(QtCore.Qt.ItemDataRole.UserRole, record["id"])
item.setData(QtCore.Qt.ItemDataRole.DisplayRole, item_text)
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 0, item)
self.update_row(row, issue)
item_text = record["cover_date"]
if item_text is None:
item_text = ""
# remove the day of "YYYY-MM-DD"
parts = item_text.split("-")
if len(parts) > 1:
item_text = parts[0] + "-" + parts[1]
qtw_item = QtWidgets.QTableWidgetItem(item_text)
qtw_item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
qtw_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 1, qtw_item)
item_text = record["name"]
if item_text is None:
item_text = ""
qtw_item = QtWidgets.QTableWidgetItem(item_text)
qtw_item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
qtw_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 2, qtw_item)
if (
IssueString(record["issue_number"]).as_string().casefold()
== IssueString(self.issue_number).as_string().casefold()
):
self.initial_id = record["id"]
row += 1
if IssueString(issue.issue).as_string().casefold() == IssueString(self.issue_number).as_string().casefold():
self.initial_id = issue.issue_id or ""
self.twList.setSortingEnabled(True)
self.twList.sortItems(0, QtCore.Qt.SortOrder.AscendingOrder)
QtWidgets.QApplication.restoreOverrideCursor()
self.twList: QtWidgets.QTableWidget
if self.initial_id:
for r in range(0, self.twList.rowCount()):
item = self.twList.item(r, 0)
issue_id = item.data(QtCore.Qt.ItemDataRole.UserRole)
if issue_id == self.initial_id:
self.twList.selectRow(r)
self.twList.scrollToItem(item, QtWidgets.QAbstractItemView.ScrollHint.EnsureVisible)
break
def cell_double_clicked(self, r: int, c: int) -> None:
self.accept()
def current_item_changed(self, curr: QtCore.QModelIndex | None, prev: QtCore.QModelIndex | None) -> None:
def update_row(self, row: int, issue: GenericMetadata) -> None: # type: ignore[override]
self.twList.setStyleSheet(self.twList.styleSheet())
item_text = issue.issue or ""
item = self.twList.item(row, 0)
item.setText(item_text)
item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
item.setData(QtCore.Qt.ItemDataRole.UserRole, issue.issue_id)
item.setData(QtCore.Qt.ItemDataRole.DisplayRole, item_text)
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
if curr is None:
return
if prev is not None and prev.row() == curr.row():
return
item_text = ""
if issue.year is not None:
item_text += f"-{issue.year:04}"
if issue.month is not None:
item_text += f"-{issue.month:02}"
self.issue_id = self.twList.item(curr.row(), 0).data(QtCore.Qt.ItemDataRole.UserRole)
qtw_item = self.twList.item(row, 1)
qtw_item.setText(item_text.strip("-"))
qtw_item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
qtw_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
# list selection was changed, update the the issue cover
for record in self.issue_list:
if record["id"] == self.issue_id:
self.issue_number = record["issue_number"]
self.coverWidget.set_issue_id(self.issue_id)
if record["description"] is None:
self.teDescription.setText("")
else:
self.teDescription.setText(record["description"])
item_text = issue.title or ""
qtw_item = self.twList.item(row, 2)
qtw_item.setText(item_text)
qtw_item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
qtw_item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
break
def _fetch(self, row: int) -> GenericMetadata: # type: ignore[override]
self.issue_id = self.twList.item(row, 0).data(QtCore.Qt.ItemDataRole.UserRole)
# list selection was changed, update the issue cover
issue = self.issue_list[self.issue_id]
if not (issue.issue and issue.year and issue.month and issue._cover_image and issue.title):
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CursorShape.WaitCursor))
try:
issue = self.talker.fetch_comic_data(
issue_id=self.issue_id, on_rate_limit=RLCallBack(self.on_ratelimit, 10)
)
except TalkerError:
pass
self.issue_number = issue.issue or ""
self.cover_widget.set_issue_details(self.issue_id, [issue._cover_image or "", *issue._alternate_images])
self.set_description(self.teDescription, issue.description or "")
return issue

57
comictaggerlib/log.py Normal file
View File

@ -0,0 +1,57 @@
from __future__ import annotations
import logging.handlers
import pathlib
import platform
import sys
from comictaggerlib.ctversion import version
logger = logging.getLogger("comictagger")
def get_filename(filename: str) -> str:
filename, _, number = filename.rpartition(".")
return filename.removesuffix("log") + number + ".log"
def get_file_handler(filename: pathlib.Path) -> logging.FileHandler:
file_handler = logging.handlers.RotatingFileHandler(filename, encoding="utf-8", backupCount=10)
file_handler.namer = get_filename
if filename.is_file() and filename.stat().st_size > 0:
file_handler.doRollover()
return file_handler
def setup_logging(verbose: int, log_dir: pathlib.Path) -> None:
logging.getLogger("comicapi").setLevel(logging.DEBUG)
logging.getLogger("comictaggerlib").setLevel(logging.DEBUG)
logging.getLogger("comictalker").setLevel(logging.DEBUG)
log_file = log_dir / "ComicTagger.log"
log_dir.mkdir(parents=True, exist_ok=True)
stream_handler = logging.StreamHandler()
file_handler = get_file_handler(log_file)
if verbose > 1:
stream_handler.setLevel(logging.DEBUG)
elif verbose > 0:
stream_handler.setLevel(logging.INFO)
else:
stream_handler.setLevel(logging.WARNING)
logging.basicConfig(
handlers=[stream_handler, file_handler],
level=logging.WARNING,
format="%(asctime)s | %(name)s | %(levelname)s | %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)
logger.info(
"ComicTagger Version: %s running on: %s PyInstaller: %s",
version,
platform.system(),
"Yes" if getattr(sys, "frozen", None) else "No",
)

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to a text file or log"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -28,7 +29,8 @@ class LogWindow(QtWidgets.QDialog):
def __init__(self, parent: QtWidgets.QWidget) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "logwindow.ui", self)
with (ui_path / "logwindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.setWindowFlags(
QtCore.Qt.WindowType(

441
comictaggerlib/main.py Executable file → Normal file
View File

@ -1,6 +1,7 @@
"""A python app to (automatically) tag comic archives"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -15,226 +16,304 @@
# limitations under the License.
from __future__ import annotations
import argparse
import json
import locale
import logging
import logging.handlers
import os
import pathlib
import platform
import signal
import subprocess
import sys
import traceback
import types
from collections.abc import Collection
from typing import cast
from comicapi import utils
from comictaggerlib import cli
from comictaggerlib.comicvinetalker import ComicVineTalker
import settngs
import comicapi.comicarchive
import comicapi.utils
import comictalker
from comictaggerlib import cli, ctsettings
from comictaggerlib.ctsettings import ct_ns, plugin_finder
from comictaggerlib.ctversion import version
from comictaggerlib.graphics import graphics_path
from comictaggerlib.options import parse_cmd_line
from comictaggerlib.settings import ComicTaggerSettings
from comictaggerlib.log import setup_logging
from comictaggerlib.resulttypes import Action
from comictalker.comictalker import ComicTalker
if sys.version_info < (3, 10):
import importlib_metadata
else:
import importlib.metadata as importlib_metadata
logger = logging.getLogger("comictagger")
logging.getLogger("comicapi").setLevel(logging.DEBUG)
logging.getLogger("comictaggerlib").setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
try:
qt_available = True
from PyQt5 import QtCore, QtGui, QtWidgets
def show_exception_box(log_msg: str) -> None:
"""Checks if a QApplication instance is available and shows a messagebox with the exception message.
If unavailable (non-console application), log an additional notice.
"""
if QtWidgets.QApplication.instance() is not None:
errorbox = QtWidgets.QMessageBox()
errorbox.setText(f"Oops. An unexpected error occurred:\n{log_msg}")
errorbox.exec()
QtWidgets.QApplication.exit(1)
else:
logger.debug("No QApplication instance available.")
def _lang_code_mac() -> str:
"""
stolen from https://github.com/mu-editor/mu
Returns the user's language preference as defined in the Language & Region
preference pane in macOS's System Preferences.
"""
class UncaughtHook(QtCore.QObject):
_exception_caught = QtCore.pyqtSignal(object)
# Uses the shell command `defaults read -g AppleLocale` that prints out a
# language code to standard output. Assumptions about the command:
# - It exists and is in the shell's PATH.
# - It accepts those arguments.
# - It returns a usable language code.
#
# Reference documentation:
# - The man page for the `defaults` command on macOS.
# - The macOS underlying API:
# https://developer.apple.com/documentation/foundation/nsuserdefaults.
def __init__(self) -> None:
super().__init__()
lang_detect_command = "defaults read -g AppleLocale"
# this registers the exception_hook() function as hook with the Python interpreter
sys.excepthook = self.exception_hook
status, output = subprocess.getstatusoutput(lang_detect_command)
if status == 0:
# Command was successful.
lang_code = output
else:
logging.warning("Language detection command failed: %r", output)
lang_code = ""
# connect signal to execute the message box function always on main thread
self._exception_caught.connect(show_exception_box)
def exception_hook(
self, exc_type: type[BaseException], exc_value: BaseException, exc_traceback: types.TracebackType | None
) -> None:
"""Function handling uncaught exceptions.
It is triggered each time an uncaught exception occurs.
"""
if issubclass(exc_type, KeyboardInterrupt):
# ignore keyboard interrupt to support console applications
sys.__excepthook__(exc_type, exc_value, exc_traceback)
else:
exc_info = (exc_type, exc_value, exc_traceback)
log_msg = "\n".join(["".join(traceback.format_tb(exc_traceback)), f"{exc_type.__name__}: {exc_value}"])
logger.critical("Uncaught exception: %s: %s", exc_type.__name__, exc_value, exc_info=exc_info)
# trigger message box show
self._exception_caught.emit(log_msg)
qt_exception_hook = UncaughtHook()
from comictaggerlib.taggerwindow import TaggerWindow
class Application(QtWidgets.QApplication):
openFileRequest = QtCore.pyqtSignal(QtCore.QUrl, name="openfileRequest")
def event(self, event):
if event.type() == QtCore.QEvent.FileOpen:
logger.info(event.url().toLocalFile())
self.openFileRequest.emit(event.url())
return True
return super().event(event)
except ImportError as e:
def show_exception_box(log_msg: str) -> None:
...
logger.error(str(e))
qt_available = False
return lang_code
def rotate(handler: logging.handlers.RotatingFileHandler, filename: pathlib.Path) -> None:
if filename.is_file() and filename.stat().st_size > 0:
handler.doRollover()
def configure_locale() -> None:
if sys.platform == "darwin" and "LANG" not in os.environ:
code = _lang_code_mac()
if code != "":
os.environ["LANG"] = f"{code}.utf-8"
locale.setlocale(locale.LC_ALL, "")
sys.stdout.reconfigure(encoding=sys.getdefaultencoding()) # type: ignore[union-attr]
sys.stderr.reconfigure(encoding=sys.getdefaultencoding()) # type: ignore[union-attr]
sys.stdin.reconfigure(encoding=sys.getdefaultencoding()) # type: ignore[union-attr]
def update_publishers() -> None:
json_file = ComicTaggerSettings.get_settings_folder() / "publishers.json"
def update_publishers(config: settngs.Config[ct_ns]) -> None:
json_file = config[0].Runtime_Options__config.user_config_dir / "publishers.json"
if json_file.exists():
try:
utils.update_publishers(json.loads(json_file.read_text("utf-8")))
comicapi.utils.update_publishers(json.loads(json_file.read_text("utf-8")))
except Exception as e:
logger.exception("Failed to load publishers from %s", json_file)
show_exception_box(str(e))
logger.exception("Failed to load publishers from %s: %s", json_file, e)
def ctmain() -> None:
opts = parse_cmd_line()
settings = ComicTaggerSettings(opts.config_path)
class App:
"""docstring for App"""
os.makedirs(ComicTaggerSettings.get_settings_folder() / "logs", exist_ok=True)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.WARNING)
file_handler = logging.handlers.RotatingFileHandler(
ComicTaggerSettings.get_settings_folder() / "logs" / "ComicTagger.log", encoding="utf-8", backupCount=10
)
rotate(file_handler, ComicTaggerSettings.get_settings_folder() / "logs" / "ComicTagger.log")
logging.basicConfig(
handlers=[
stream_handler,
file_handler,
],
level=logging.WARNING,
format="%(asctime)s | %(name)s | %(levelname)s | %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)
# Need to load setting before anything else
def __init__(self) -> None:
self.config: settngs.Config[ct_ns]
self.initial_arg_parser = ctsettings.initial_commandline_parser()
self.config_load_success = False
self.talkers: dict[str, ComicTalker]
# manage the CV API key
# None comparison is used so that the empty string can unset the value
if opts.cv_api_key is not None or opts.cv_url is not None:
settings.cv_api_key = opts.cv_api_key if opts.cv_api_key is not None else settings.cv_api_key
settings.cv_url = opts.cv_url if opts.cv_url is not None else settings.cv_url
settings.save()
if opts.only_set_cv_key:
print("Key set") # noqa: T201
return
def run(self) -> None:
configure_locale()
conf = self.initialize()
self.initialize_dirs(conf.config)
self.load_plugins(conf)
self.register_settings(conf.enable_quick_tag)
self.config = self.parse_settings(conf.config)
ComicVineTalker.api_key = settings.cv_api_key
ComicVineTalker.api_base_url = settings.cv_url
self.main()
signal.signal(signal.SIGINT, signal.SIG_DFL)
def load_plugins(self, opts: argparse.Namespace) -> None:
local_plugins = plugin_finder.find_plugins(opts.config.user_plugin_dir)
logger.info(
"ComicTagger Version: %s running on: %s PyInstaller: %s",
version,
platform.system(),
"Yes" if getattr(sys, "frozen", None) else "No",
)
comicapi.comicarchive.load_archive_plugins(local_plugins=[p.obj for p in local_plugins.archivers])
comicapi.comicarchive.load_tag_plugins(version=version, local_plugins=[p.obj for p in local_plugins.tags])
self.talkers = comictalker.get_talkers(
version, opts.config.user_cache_dir, local_plugins=[p.obj for p in local_plugins.talkers]
)
logger.debug("Installed Packages")
for pkg in sorted(importlib_metadata.distributions(), key=lambda x: x.name):
logger.debug("%s\t%s", pkg.metadata["Name"], pkg.metadata["Version"])
def list_plugins(
self,
talkers: Collection[comictalker.ComicTalker],
archivers: Collection[type[comicapi.comicarchive.Archiver]],
tags: Collection[comicapi.comicarchive.Tag],
) -> None:
if self.config[0].Runtime_Options__json:
for talker in talkers:
print( # noqa: T201
json.dumps(
{
"type": "talker",
"id": talker.id,
"name": talker.name,
"website": talker.website,
}
)
)
utils.load_publishers()
update_publishers()
for archiver in archivers:
try:
a = archiver()
print( # noqa: T201
json.dumps(
{
"type": "archiver",
"enabled": a.enabled,
"name": a.name(),
"extension": a.extension(),
"exe": a.exe,
}
)
)
except Exception:
print( # noqa: T201
json.dumps(
{
"type": "archiver",
"enabled": archiver.enabled,
"name": "",
"extension": "",
"exe": archiver.exe,
}
)
)
if not qt_available and not opts.no_gui:
opts.no_gui = True
logger.warning("PyQt5 is not available. ComicTagger is limited to command-line mode.")
for tag in tags:
print( # noqa: T201
json.dumps(
{
"type": "tag",
"enabled": tag.enabled,
"name": tag.name(),
"id": tag.id,
}
)
)
else:
print("Metadata Sources: (ID: Name, URL)") # noqa: T201
for talker in talkers:
print(f"{talker.id:<10}: {talker.name:<21}, {talker.website}") # noqa: T201
print("\nComic Archive: (Enabled, Name: extension, exe)") # noqa: T201
for archiver in archivers:
a = archiver()
print(f"{a.enabled!s:<5}, {a.name():<10}: {a.extension():<5}, {a.exe}") # noqa: T201
print("\nTags: (Enabled, ID: Name)") # noqa: T201
for tag in tags:
print(f"{tag.enabled!s:<5}, {tag.id:<10}: {tag.name()}") # noqa: T201
def initialize(self) -> argparse.Namespace:
conf, _ = self.initial_arg_parser.parse_known_intermixed_args()
assert conf is not None
setup_logging(conf.verbose, conf.config.user_log_dir)
return conf
def register_settings(self, enable_quick_tag: bool) -> None:
self.manager = settngs.Manager(
description="A utility for reading and writing metadata to comic archives.\n\n\n"
+ "If no options are given, %(prog)s will run in windowed mode.\nPlease keep the '-v' option separated '-so -v' not '-sov'",
epilog="For more help visit the wiki at: https://github.com/comictagger/comictagger/wiki",
)
ctsettings.register_commandline_settings(self.manager, enable_quick_tag)
ctsettings.register_file_settings(self.manager)
ctsettings.register_plugin_settings(self.manager, getattr(self, "talkers", {}))
def parse_settings(self, config_paths: ctsettings.ComicTaggerPaths, *args: str) -> settngs.Config[ct_ns]:
cfg, self.config_load_success = ctsettings.parse_config(
self.manager, config_paths.user_config_dir / "settings.json", list(args) or None
)
config = cast(settngs.Config[ct_ns], self.manager.get_namespace(cfg, file=True, cmdline=True))
config[0].Runtime_Options__config = config_paths
config = ctsettings.validate_commandline_settings(config, self.manager)
config = ctsettings.validate_file_settings(config)
config = ctsettings.validate_plugin_settings(config, getattr(self, "talkers", {}))
return config
def initialize_dirs(self, paths: ctsettings.ComicTaggerPaths) -> None:
paths.user_config_dir.mkdir(parents=True, exist_ok=True)
paths.user_cache_dir.mkdir(parents=True, exist_ok=True)
paths.user_log_dir.mkdir(parents=True, exist_ok=True)
paths.user_plugin_dir.mkdir(parents=True, exist_ok=True)
logger.debug("user_config_dir: %s", paths.user_config_dir)
logger.debug("user_cache_dir: %s", paths.user_cache_dir)
logger.debug("user_log_dir: %s", paths.user_log_dir)
logger.debug("user_plugin_dir: %s", paths.user_plugin_dir)
def main(self) -> None:
assert self.config is not None
# config already loaded
error = None
if (
not self.config[0].Metadata_Options__cr
and "cr" in comicapi.comicarchive.tags
and comicapi.comicarchive.tags["cr"].enabled
):
comicapi.comicarchive.tags["cr"].enabled = False
if len(self.talkers) < 1:
error = (
"Failed to load any talkers, please re-install and check the log located in '"
+ str(self.config[0].Runtime_Options__config.user_log_dir)
+ "' for more details",
True,
)
signal.signal(signal.SIGINT, signal.SIG_DFL)
logger.debug("Installed Packages")
for pkg in sorted(importlib_metadata.distributions(), key=lambda x: x.name):
logger.debug("%s\t%s", pkg.metadata["Name"], pkg.metadata["Version"])
comicapi.utils.load_publishers()
update_publishers(self.config)
if self.config[0].Commands__command == Action.list_plugins:
self.list_plugins(
list(self.talkers.values()),
comicapi.comicarchive.archivers,
comicapi.comicarchive.tags.values(),
)
return
if self.config[0].Commands__command == Action.save_config:
if self.config_load_success:
settings_path = self.config[0].Runtime_Options__config.user_config_dir / "settings.json"
if self.config_load_success:
ctsettings.save_file(self.config, settings_path)
print("Settings saved") # noqa: T201
return
if not self.config_load_success:
error = (
"Failed to load settings, check the log located in '"
+ str(self.config[0].Runtime_Options__config.user_log_dir)
+ "' for more details",
True,
)
if not self.config[0].Runtime_Options__no_gui:
try:
from comictaggerlib import gui
if not gui.qt_available:
raise gui.import_error
return gui.open_tagger_window(self.talkers, self.config, error)
except ImportError:
self.config[0].Runtime_Options__no_gui = True
logger.warning("PyQt5 is not available. ComicTagger is limited to command-line mode.")
# GUI mode is not available or CLI mode was requested
if error and error[1]:
print(f"A fatal error occurred please check the log for more information: {error[0]}") # noqa: T201
raise SystemExit(1)
if opts.no_gui:
try:
cli.cli_mode(opts, settings)
raise SystemExit(cli.CLI(self.config[0], self.talkers).run())
except Exception:
logger.exception("CLI mode failed")
else:
os.environ["QtWidgets.QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
args = []
if opts.darkmode:
args.extend(["-platform", "windows:darkmode=2"])
args.extend(sys.argv)
app = Application(args)
# needed to catch initial open file events (macOS)
app.openFileRequest.connect(lambda x: opts.files.append(x.toLocalFile()))
if platform.system() == "Darwin":
# Set the MacOS dock icon
app.setWindowIcon(QtGui.QIcon(str(graphics_path / "app.png")))
if platform.system() == "Windows":
# For pure python, tell windows that we're not python,
# so we can have our own taskbar icon
import ctypes
myappid = "comictagger" # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) # type: ignore[attr-defined]
# force close of console window
swp_hidewindow = 0x0080
console_wnd = ctypes.windll.kernel32.GetConsoleWindow() # type: ignore[attr-defined]
if console_wnd != 0:
ctypes.windll.user32.SetWindowPos(console_wnd, None, 0, 0, 0, 0, swp_hidewindow) # type: ignore[attr-defined]
if platform.system() != "Linux":
img = QtGui.QPixmap(str(graphics_path / "tags.png"))
splash = QtWidgets.QSplashScreen(img)
splash.show()
splash.raise_()
QtWidgets.QApplication.processEvents()
try:
tagger_window = TaggerWindow(opts.files, settings, opts=opts)
tagger_window.setWindowIcon(QtGui.QIcon(str(graphics_path / "app.png")))
tagger_window.show()
# Catch open file events (macOS)
app.openFileRequest.connect(tagger_window.open_file_event)
if platform.system() != "Linux":
splash.finish(tagger_window)
sys.exit(app.exec())
except Exception:
logger.exception("GUI mode failed")
QtWidgets.QMessageBox.critical(
QtWidgets.QMainWindow(), "Error", "Unhandled exception in app:\n" + traceback.format_exc()
)
def main() -> None:
App().run()

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to select from automated issue matches"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -22,34 +23,40 @@ from PyQt5 import QtCore, QtWidgets, uic
from comicapi.comicarchive import ComicArchive
from comictaggerlib.coverimagewidget import CoverImageWidget
from comictaggerlib.ctsettings import ct_ns
from comictaggerlib.resulttypes import IssueResult
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import reduce_widget_font_size
from comictalker.comictalker import ComicTalker
logger = logging.getLogger(__name__)
class MatchSelectionWindow(QtWidgets.QDialog):
volume_id = 0
def __init__(self, parent: QtWidgets.QWidget, matches: list[IssueResult], comic_archive: ComicArchive) -> None:
def __init__(
self,
parent: QtWidgets.QWidget,
matches: list[IssueResult],
comic_archive: ComicArchive,
config: ct_ns,
talker: ComicTalker,
) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "matchselectionwindow.ui", self)
with (ui_path / "matchselectionwindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.altCoverWidget = CoverImageWidget(self.altCoverContainer, CoverImageWidget.AltCoverMode)
self.altCoverWidget = CoverImageWidget(
self.altCoverContainer, CoverImageWidget.AltCoverMode, config.Runtime_Options__config.user_cache_dir
)
gridlayout = QtWidgets.QGridLayout(self.altCoverContainer)
gridlayout.addWidget(self.altCoverWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
self.archiveCoverWidget = CoverImageWidget(self.archiveCoverContainer, CoverImageWidget.ArchiveMode)
self.archiveCoverWidget = CoverImageWidget(self.archiveCoverContainer, CoverImageWidget.ArchiveMode, None)
gridlayout = QtWidgets.QGridLayout(self.archiveCoverContainer)
gridlayout.addWidget(self.archiveCoverWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
reduce_widget_font_size(self.twList)
reduce_widget_font_size(self.teDescription, 1)
self.setWindowFlags(
QtCore.Qt.WindowType(
self.windowFlags()
@ -67,7 +74,6 @@ class MatchSelectionWindow(QtWidgets.QDialog):
self.update_data()
def update_data(self) -> None:
self.set_cover_image()
self.populate_table()
self.twList.resizeColumnsToContents()
@ -77,24 +83,22 @@ class MatchSelectionWindow(QtWidgets.QDialog):
self.setWindowTitle(f"Select correct match: {os.path.split(path)[1]}")
def populate_table(self) -> None:
self.twList.setRowCount(0)
self.twList.setSortingEnabled(False)
row = 0
for match in self.matches:
for row, match in enumerate(self.matches):
self.twList.insertRow(row)
item_text = match["series"]
item_text = match.series
item = QtWidgets.QTableWidgetItem(item_text)
item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, item_text)
item.setData(QtCore.Qt.ItemDataRole.UserRole, (match,))
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 0, item)
if match["publisher"] is not None:
item_text = str(match["publisher"])
if match.publisher is not None:
item_text = str(match.publisher)
else:
item_text = "Unknown"
item = QtWidgets.QTableWidgetItem(item_text)
@ -104,10 +108,10 @@ class MatchSelectionWindow(QtWidgets.QDialog):
month_str = ""
year_str = "????"
if match["month"] is not None:
month_str = f"-{int(match['month']):02d}"
if match["year"] is not None:
year_str = str(match["year"])
if match.month is not None:
month_str = f"-{int(match.month):02d}"
if match.year is not None:
year_str = str(match.year)
item_text = year_str + month_str
item = QtWidgets.QTableWidgetItem(item_text)
@ -115,7 +119,7 @@ class MatchSelectionWindow(QtWidgets.QDialog):
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 2, item)
item_text = match["issue_title"]
item_text = match.issue_title
if item_text is None:
item_text = ""
item = QtWidgets.QTableWidgetItem(item_text)
@ -123,8 +127,6 @@ class MatchSelectionWindow(QtWidgets.QDialog):
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twList.setItem(row, 3, item)
row += 1
self.twList.resizeColumnsToContents()
self.twList.setSortingEnabled(True)
self.twList.sortItems(2, QtCore.Qt.SortOrder.AscendingOrder)
@ -136,17 +138,20 @@ class MatchSelectionWindow(QtWidgets.QDialog):
self.accept()
def current_item_changed(self, curr: QtCore.QModelIndex, prev: QtCore.QModelIndex) -> None:
if curr is None:
return
if prev is not None and prev.row() == curr.row():
return
self.altCoverWidget.set_issue_id(self.current_match()["issue_id"])
if self.current_match()["description"] is None:
match = self.current_match()
self.altCoverWidget.set_issue_details(
match.issue_id,
[match.image_url, *match.alt_image_urls],
)
if match.description is None:
self.teDescription.setText("")
else:
self.teDescription.setText(self.current_match()["description"])
self.teDescription.setText(match.description)
def set_cover_image(self) -> None:
self.archiveCoverWidget.set_archive(self.comic_archive)

62
comictaggerlib/md.py Normal file
View File

@ -0,0 +1,62 @@
from __future__ import annotations
from datetime import datetime
from comicapi import merge, utils
from comicapi.comicarchive import ComicArchive
from comicapi.genericmetadata import GenericMetadata
from comictaggerlib import ctversion
from comictaggerlib.cbltransformer import CBLTransformer
from comictaggerlib.ctsettings.settngs_namespace import SettngsNS
from comictalker.talker_utils import cleanup_html
def prepare_metadata(md: GenericMetadata, new_md: GenericMetadata, config: SettngsNS) -> GenericMetadata:
if config.Metadata_Options__apply_transform_on_import:
new_md = CBLTransformer(new_md, config).apply()
final_md = md.copy()
if config.Auto_Tag__clear_tags:
final_md = GenericMetadata()
final_md.overlay(new_md, config.Metadata_Options__metadata_merge, config.Metadata_Options__metadata_merge_lists)
issue_id = ""
if final_md.issue_id:
issue_id = f" [Issue ID {final_md.issue_id}]"
origin = ""
if final_md.data_origin is not None:
origin = f" using info from {final_md.data_origin.name}"
notes = f"Tagged with ComicTagger {ctversion.version}{origin} on {datetime.now():%Y-%m-%d %H:%M:%S}.{issue_id}"
if config.Auto_Tag__auto_imprint:
final_md.fix_publisher()
return final_md.replace(
is_empty=False,
notes=utils.combine_notes(final_md.notes, notes, "Tagged with ComicTagger"),
description=cleanup_html(final_md.description, config.Metadata_Options__remove_html_tables) or None,
)
def read_selected_tags(
tag_ids: list[str], ca: ComicArchive, mode: merge.Mode = merge.Mode.OVERLAY, merge_lists: bool = False
) -> tuple[GenericMetadata, list[str], Exception | None]:
md = GenericMetadata()
error = None
tags_used = []
try:
for tag_id in tag_ids:
metadata = ca.read_tags(tag_id)
if not metadata.is_empty:
md.overlay(
metadata,
mode=mode,
merge_lists=merge_lists,
)
tags_used.append(tag_id)
except Exception as e:
error = e
return md, tags_used, error

View File

@ -10,8 +10,9 @@ said_yes, checked = OptionalMessageDialog.question(self, "QtWidgets.Question",
"Are you sure you wish to do this?",
)
"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -77,10 +78,7 @@ class OptionalMessageDialog(QtWidgets.QDialog):
else:
btnbox_style = QtWidgets.QDialogButtonBox.StandardButton.Ok
self.theButtonBox = QtWidgets.QDialogButtonBox(
btnbox_style,
parent=self,
)
self.theButtonBox = QtWidgets.QDialogButtonBox(btnbox_style, parent=self)
self.theButtonBox.accepted.connect(self.accept)
self.theButtonBox.rejected.connect(self.reject)
@ -96,7 +94,6 @@ class OptionalMessageDialog(QtWidgets.QDialog):
@staticmethod
def msg(parent: QtWidgets.QWidget, title: str, msg: str, checked: bool = False, check_text: str = "") -> bool:
d = OptionalMessageDialog(parent, StyleMessage, title, msg, checked=checked, check_text=check_text)
d.exec()
@ -106,7 +103,6 @@ class OptionalMessageDialog(QtWidgets.QDialog):
def question(
parent: QtWidgets.QWidget, title: str, msg: str, checked: bool = False, check_text: str = ""
) -> tuple[bool, bool]:
d = OptionalMessageDialog(parent, StyleQuestion, title, msg, checked=checked, check_text=check_text)
d.exec()
@ -117,7 +113,6 @@ class OptionalMessageDialog(QtWidgets.QDialog):
def msg_no_checkbox(
parent: QtWidgets.QWidget, title: str, msg: str, checked: bool = False, check_text: str = ""
) -> bool:
d = OptionalMessageDialog(parent, StyleMessage, title, msg, checked=checked, check_text=check_text)
d.theCheckBox.hide()

View File

@ -1,412 +0,0 @@
"""CLI options class for ComicTagger app"""
#
# Copyright 2012-2014 Anthony Beville
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import argparse
import logging
import os
import platform
import sys
from comicapi import utils
from comicapi.comicarchive import MetaDataStyle
from comicapi.genericmetadata import GenericMetadata
from comictaggerlib import ctversion
logger = logging.getLogger(__name__)
def define_args() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="""A utility for reading and writing metadata to comic archives.
If no options are given, %(prog)s will run in windowed mode.""",
epilog="For more help visit the wiki at: https://github.com/comictagger/comictagger/wiki",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--version",
action="store_true",
help="Display version.",
)
commands = parser.add_mutually_exclusive_group()
commands.add_argument(
"-p",
"--print",
action="store_true",
help="""Print out tag info from file. Specify type\n(via -t) to get only info of that tag type.\n\n""",
)
commands.add_argument(
"-d",
"--delete",
action="store_true",
help="Deletes the tag block of specified type (via -t).\n",
)
commands.add_argument(
"-c",
"--copy",
type=metadata_type,
metavar="{CR,CBL,COMET}",
help="Copy the specified source tag block to\ndestination style specified via -t\n(potentially lossy operation).\n\n",
)
commands.add_argument(
"-s",
"--save",
action="store_true",
help="Save out tags as specified type (via -t).\nMust specify also at least -o, -f, or -m.\n\n",
)
commands.add_argument(
"-r",
"--rename",
action="store_true",
help="Rename the file based on specified tag style.",
)
commands.add_argument(
"-e",
"--export-to-zip",
action="store_true",
help="Export RAR archive to Zip format.",
)
commands.add_argument(
"--only-set-cv-key",
action="store_true",
help="Only set the Comic Vine API key and quit.\n\n",
)
parser.add_argument(
"-1",
"--assume-issue-one",
action="store_true",
help="""Assume issue number is 1 if not found (relevant for -s).\n\n""",
)
parser.add_argument(
"--abort-on-conflict",
action="store_true",
help="""Don't export to zip if intended new filename\nexists (otherwise, creates a new unique filename).\n\n""",
)
parser.add_argument(
"-a",
"--auto-imprint",
action="store_true",
help="""Enables the auto imprint functionality.\ne.g. if the publisher is set to 'vertigo' it\nwill be updated to 'DC Comics' and the imprint\nproperty will be set to 'Vertigo'.\n\n""",
)
parser.add_argument(
"--config",
dest="config_path",
help="""Config directory defaults to ~/.ComicTagger\non Linux/Mac and %%APPDATA%% on Windows\n""",
)
parser.add_argument(
"--cv-api-key",
help="Use the given Comic Vine API Key (persisted in settings).",
)
parser.add_argument(
"--cv-url",
help="Use the given Comic Vine URL (persisted in settings).",
)
parser.add_argument(
"--delete-rar",
action="store_true",
dest="delete_after_zip_export",
help="""Delete original RAR archive after successful\nexport to Zip.""",
)
parser.add_argument(
"-f",
"--parse-filename",
"--parsefilename",
action="store_true",
help="""Parse the filename to get some info,\nspecifically series name, issue number,\nvolume, and publication year.\n\n""",
)
parser.add_argument(
"--id",
dest="issue_id",
type=int,
help="""Use the issue ID when searching online.\nOverrides all other metadata.\n\n""",
)
parser.add_argument(
"-t",
"--type",
metavar="{CR,CBL,COMET}",
default=[],
type=metadata_type,
help="""Specify TYPE as either CR, CBL or COMET\n(as either ComicRack, ComicBookLover,\nor CoMet style tags, respectively).\nUse commas for multiple types.\nFor searching the metadata will use the first listed:\neg '-t cbl,cr' with no CBL tags, CR will be used if they exist\n\n""",
)
parser.add_argument(
"-o",
"--online",
action="store_true",
help="""Search online and attempt to identify file\nusing existing metadata and images in archive.\nMay be used in conjunction with -f and -m.\n\n""",
)
parser.add_argument(
"-m",
"--metadata",
default=GenericMetadata(),
type=parse_metadata_from_string,
help="""Explicitly define, as a list, some tags to be used. e.g.:\n"series=Plastic Man, publisher=Quality Comics"\n"series=Kickers^, Inc., issue=1, year=1986"\nName-Value pairs are comma separated. Use a\n"^" to escape an "=" or a ",", as shown in\nthe example above. Some names that can be\nused: series, issue, issue_count, year,\npublisher, title\n\n""",
)
parser.add_argument(
"-i",
"--interactive",
action="store_true",
help="""Interactively query the user when there are\nmultiple matches for an online search.\n\n""",
)
parser.add_argument(
"--no-overwrite",
"--nooverwrite",
action="store_true",
help="""Don't modify tag block if it already exists (relevant for -s or -c).""",
)
parser.add_argument(
"--noabort",
dest="abort_on_low_confidence",
action="store_false",
help="""Don't abort save operation when online match\nis of low confidence.\n\n""",
)
parser.add_argument(
"--nosummary",
dest="show_save_summary",
action="store_false",
help="Suppress the default summary after a save operation.\n\n",
)
parser.add_argument(
"--overwrite",
action="store_true",
help="""Overwrite all existing metadata.\nMay be used in conjunction with -o, -f and -m.\n\n""",
)
parser.add_argument(
"--raw", action="store_true", help="""With -p, will print out the raw tag block(s)\nfrom the file.\n"""
)
parser.add_argument(
"-R",
"--recursive",
action="store_true",
help="Recursively include files in sub-folders.",
)
parser.add_argument(
"-S",
"--script",
help="""Run an "add-on" python script that uses the\nComicTagger library for custom processing.\nScript arguments can follow the script name.\n\n""",
)
parser.add_argument(
"--split-words",
action="store_true",
help="""Splits words before parsing the filename.\ne.g. 'judgedredd' to 'judge dredd'\n\n""",
)
parser.add_argument(
"--terse",
action="store_true",
help="Don't say much (for print mode).",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Be noisy when doing what it does.",
)
parser.add_argument(
"-w",
"--wait-on-cv-rate-limit",
action="store_true",
help="""When encountering a Comic Vine rate limit\nerror, wait and retry query.\n\n""",
)
parser.add_argument(
"-n", "--dryrun", action="store_true", help="Don't actually modify file (only relevant for -d, -s, or -r).\n\n"
)
parser.add_argument(
"--darkmode",
action="store_true",
help="Windows only. Force a dark pallet",
)
parser.add_argument(
"-g",
"--glob",
action="store_true",
help="Windows only. Enable globbing",
)
parser.add_argument("files", nargs="*")
return parser
def metadata_type(types: str) -> list[int]:
result = []
types = types.casefold()
for typ in types.split(","):
typ = typ.strip()
if typ not in MetaDataStyle.short_name:
choices = ", ".join(MetaDataStyle.short_name)
raise argparse.ArgumentTypeError(f"invalid choice: {typ} (choose from {choices.upper()})")
result.append(MetaDataStyle.short_name.index(typ))
return result
def parse_metadata_from_string(mdstr: str) -> GenericMetadata:
"""The metadata string is a comma separated list of name-value pairs
The names match the attributes of the internal metadata struct (for now)
The caret is the special "escape character", since it's not common in
natural language text
example = "series=Kickers^, Inc. ,issue=1, year=1986"
"""
escaped_comma = "^,"
escaped_equals = "^="
replacement_token = "<_~_>"
md = GenericMetadata()
# First, replace escaped commas with with a unique token (to be changed back later)
mdstr = mdstr.replace(escaped_comma, replacement_token)
tmp_list = mdstr.split(",")
md_list = []
for item in tmp_list:
item = item.replace(replacement_token, ",")
md_list.append(item)
# Now build a nice dict from the list
md_dict = {}
for item in md_list:
# Make sure to fix any escaped equal signs
i = item.replace(escaped_equals, replacement_token)
key, value = i.split("=")
value = value.replace(replacement_token, "=").strip()
key = key.strip()
if key.casefold() == "credit":
cred_attribs = value.split(":")
role = cred_attribs[0]
person = cred_attribs[1] if len(cred_attribs) > 1 else ""
primary = len(cred_attribs) > 2
md.add_credit(person.strip(), role.strip(), primary)
else:
md_dict[key] = value
# Map the dict to the metadata object
for key, value in md_dict.items():
if not hasattr(md, key):
raise argparse.ArgumentTypeError(f"'{key}' is not a valid tag name")
else:
md.is_empty = False
setattr(md, key, value)
return md
def launch_script(scriptfile: str, args: list[str]) -> None:
# we were given a script. special case for the args:
# 1. ignore everything before the -S,
# 2. pass all the ones that follow (including script name) to the script
if not os.path.exists(scriptfile):
logger.error("Can't find %s", scriptfile)
else:
# I *think* this makes sense:
# assume the base name of the file is the module name
# add the folder of the given file to the python path import module
dirname = os.path.dirname(scriptfile)
module_name = os.path.splitext(os.path.basename(scriptfile))[0]
sys.path = [dirname] + sys.path
try:
script = __import__(module_name)
# Determine if the entry point exists before trying to run it
if "main" in dir(script):
script.main(args)
else:
logger.error("Can't find entry point 'main()' in module '%s'", module_name)
except Exception:
logger.exception("Script: %s raised an unhandled exception: ", module_name)
sys.exit(0)
def parse_cmd_line() -> argparse.Namespace:
if platform.system() == "Darwin" and getattr(sys, "frozen", False):
# remove the PSN (process serial number) argument from OS/X
input_args = [a for a in sys.argv[1:] if "-psn_0_" not in a]
else:
input_args = sys.argv[1:]
script_args = []
# first check if we're launching a script and split off script args
for n, _ in enumerate(input_args):
if input_args[n] == "--":
break
if input_args[n] in ["-S", "--script"] and n + 1 < len(input_args):
# insert a "--" which will cause getopt to ignore the remaining args
# so they will be passed to the script
script_args = input_args[n + 2 :]
input_args = input_args[: n + 2]
break
parser = define_args()
opts = parser.parse_args(input_args)
if opts.config_path:
opts.config_path = os.path.abspath(opts.config_path)
if opts.version:
parser.exit(
status=1,
message=f"ComicTagger {ctversion.version}: Copyright (c) 2012-2022 ComicTagger Team\n"
"Distributed under Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)\n",
)
opts.no_gui = any(
[
opts.print,
opts.delete,
opts.save,
opts.copy,
opts.rename,
opts.export_to_zip,
opts.only_set_cv_key,
]
)
if opts.script is not None:
launch_script(opts.script, script_args)
if platform.system() == "Windows" and opts.glob:
# no globbing on windows shell, so do it for them
import glob
globs = opts.files
opts.files = []
for item in globs:
opts.files.extend(glob.glob(item))
if opts.only_set_cv_key and opts.cv_api_key is None and opts.cv_url is None:
parser.exit(message="Key not given!\n", status=1)
if not opts.only_set_cv_key and opts.no_gui and not opts.files:
parser.exit(message="Command requires at least one filename!\n", status=1)
if opts.delete and not opts.type:
parser.exit(message="Please specify the type to delete with -t\n", status=1)
if opts.save and not opts.type:
parser.exit(message="Please specify the type to save with -t\n", status=1)
if opts.copy:
if not opts.type:
parser.exit(message="Please specify the type to copy to with -t\n", status=1)
if len(opts.copy) > 1:
parser.exit(message="Please specify only one type to copy to with -c\n", status=1)
opts.copy = opts.copy[0]
if opts.recursive:
opts.file_list = utils.get_recursive_filelist(opts.files)
else:
opts.file_list = opts.files
return opts

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to show pages of a comic archive"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -16,14 +17,12 @@
from __future__ import annotations
import logging
import platform
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from comicapi.comicarchive import ComicArchive
from comicapi.genericmetadata import GenericMetadata
from comictaggerlib.coverimagewidget import CoverImageWidget
from comictaggerlib.graphics import graphics_path
from comictaggerlib.ui import ui_path
logger = logging.getLogger(__name__)
@ -33,9 +32,10 @@ class PageBrowserWindow(QtWidgets.QDialog):
def __init__(self, parent: QtWidgets.QWidget, metadata: GenericMetadata) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "pagebrowser.ui", self)
with (ui_path / "pagebrowser.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.pageWidget = CoverImageWidget(self.pageContainer, CoverImageWidget.ArchiveMode)
self.pageWidget = CoverImageWidget(self.pageContainer, CoverImageWidget.ArchiveMode, None)
gridlayout = QtWidgets.QGridLayout(self.pageContainer)
gridlayout.addWidget(self.pageWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
@ -55,12 +55,8 @@ class PageBrowserWindow(QtWidgets.QDialog):
self.metadata = metadata
self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Close).setDefault(True)
if platform.system() == "Darwin":
self.btnPrev.setText("<<")
self.btnNext.setText(">>")
else:
self.btnPrev.setIcon(QtGui.QIcon(str(graphics_path / "left.png")))
self.btnNext.setIcon(QtGui.QIcon(str(graphics_path / "right.png")))
self.btnPrev.setIcon(QtGui.QIcon(":/graphics/left.png"))
self.btnNext.setIcon(QtGui.QIcon(":/graphics/right.png"))
self.btnNext.clicked.connect(self.next_page)
self.btnPrev.clicked.connect(self.prev_page)
@ -80,7 +76,6 @@ class PageBrowserWindow(QtWidgets.QDialog):
self.pageWidget.clear()
def set_comic_archive(self, ca: ComicArchive) -> None:
self.comic_archive = ca
self.page_count = ca.get_number_of_pages()
self.current_page_num = 0
@ -92,7 +87,6 @@ class PageBrowserWindow(QtWidgets.QDialog):
self.btnPrev.setEnabled(True)
def next_page(self) -> None:
if self.current_page_num + 1 < self.page_count:
self.current_page_num += 1
else:
@ -100,7 +94,6 @@ class PageBrowserWindow(QtWidgets.QDialog):
self.set_page()
def prev_page(self) -> None:
if self.current_page_num - 1 >= 0:
self.current_page_num -= 1
else:
@ -109,9 +102,9 @@ class PageBrowserWindow(QtWidgets.QDialog):
def set_page(self) -> None:
if not self.metadata.is_empty:
archive_page_index = self.metadata.get_archive_page_index(self.current_page_num)
selected_page_index = self.metadata.get_archive_page_index(self.current_page_num)
else:
archive_page_index = self.current_page_num
selected_page_index = self.current_page_num
self.pageWidget.set_page(archive_page_index)
self.setWindowTitle(f"Page Browser - Page {self.current_page_num + 1} (of {self.page_count}) ")
self.pageWidget.set_page(selected_page_index)
self.setWindowTitle(f"Page Browser - Page {self.current_page_num+1} (of {self.page_count})")

View File

@ -1,6 +1,7 @@
"""A PyQt5 widget for editing the page list info"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -17,23 +18,22 @@ from __future__ import annotations
import logging
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5 import QtCore, QtWidgets, uic
from comicapi.comicarchive import ComicArchive, MetaDataStyle
from comicapi.genericmetadata import ImageMetadata, PageType
from comicapi.comicarchive import ComicArchive, tags
from comicapi.genericmetadata import GenericMetadata, PageMetadata, PageType
from comictaggerlib.coverimagewidget import CoverImageWidget
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import enable_widget
logger = logging.getLogger(__name__)
def item_move_events(widget: QtWidgets.QWidget) -> QtCore.pyqtBoundSignal:
class Filter(QtCore.QObject):
mysignal = QtCore.pyqtSignal(str)
def eventFilter(self, obj: QtCore.QObject, event: QtCore.QEvent) -> bool:
if obj == widget:
if event.type() == QtCore.QEvent.Type.ChildRemoved:
self.mysignal.emit("finish")
@ -70,14 +70,24 @@ class PageListEditor(QtWidgets.QWidget):
def __init__(self, parent: QtWidgets.QWidget) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "pagelisteditor.ui", self)
with (ui_path / "pagelisteditor.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.pageWidget = CoverImageWidget(self.pageContainer, CoverImageWidget.ArchiveMode)
self.md_attributes = {
"pages.image_index": [self.btnDown, self.btnUp],
"pages.type": self.cbPageType,
"pages.double_page": self.chkDoublePage,
"pages.bookmark": self.leBookmark,
"pages": self,
}
self.pageWidget = CoverImageWidget(self.pageContainer, CoverImageWidget.ArchiveMode, None)
gridlayout = QtWidgets.QGridLayout(self.pageContainer)
gridlayout.addWidget(self.pageWidget)
gridlayout.setContentsMargins(0, 0, 0, 0)
self.pageWidget.showControls = False
self.blur = False
self.reset_page()
# Add the entries to the page type combobox
@ -98,22 +108,38 @@ class PageListEditor(QtWidgets.QWidget):
item_move_events(self.listWidget).connect(self.item_move_event)
self.cbPageType.activated.connect(self.change_page_type)
self.chkDoublePage.clicked.connect(self.toggle_double_page)
self.cbxBlur.clicked.connect(self._toggle_blur)
self.leBookmark.editingFinished.connect(self.save_bookmark)
self.btnUp.clicked.connect(self.move_current_up)
self.btnDown.clicked.connect(self.move_current_down)
self.btnIdentifyScannerPage.clicked.connect(self.identify_scanner_page)
self.btnIdentifyDoublePage.clicked.connect(self.identify_double_page)
self.pre_move_row = -1
self.first_front_page: int | None = None
self.comic_archive: ComicArchive | None = None
self.pages_list: list[ImageMetadata] = []
self.pages_list: list[PageMetadata] = []
self.tag_ids: list[str] = []
def set_blur(self, blur: bool) -> None:
self.pageWidget.blur = self.blur = blur
self.cbxBlur.setChecked(blur)
self.pageWidget.update_content()
def _toggle_blur(self) -> None:
self.pageWidget.blur = self.blur = not self.blur
self.cbxBlur.setChecked(self.blur)
self.pageWidget.update_content()
def reset_page(self) -> None:
self.pageWidget.clear()
self.cbPageType.setDisabled(True)
self.chkDoublePage.setDisabled(True)
self.leBookmark.setDisabled(True)
self.cbPageType.setEnabled(False)
self.chkDoublePage.setEnabled(False)
self.leBookmark.setEnabled(False)
self.listWidget.clear()
self.comic_archive = None
self.pages_list = []
self.cbxBlur.setChecked(self.blur)
def add_page_type_item(self, text: str, user_data: str, shortcut: str, show_shortcut: bool = True) -> None:
if show_shortcut:
@ -124,8 +150,33 @@ class PageListEditor(QtWidgets.QWidget):
action_item.setShortcut(shortcut)
self.addAction(action_item)
def identify_scanner_page(self) -> None:
if self.comic_archive is None:
return
row = self.comic_archive.get_scanner_page_index()
if row is None:
return
page: PageMetadata = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)
page.type = PageType.Deleted
item = self.listWidget.item(row)
item.setData(QtCore.Qt.ItemDataRole.UserRole, page)
item.setText(self.list_entry_text(page))
self.change_page()
def identify_double_page(self) -> None:
if self.comic_archive is None:
return
md = GenericMetadata(pages=self.get_page_list())
double_pages = [bool(x.double_page) for x in md.pages]
self.comic_archive.apply_archive_info_to_metadata(md, True, True)
self.set_data(self.comic_archive, pages_list=md.pages)
if double_pages != [bool(x.double_page) for x in md.pages]:
self.modified.emit()
def select_page_type_item(self, idx: int) -> None:
if self.cbPageType.isEnabled():
if self.cbPageType.isEnabled() and self.listWidget.count() > 0:
self.cbPageType.setCurrentIndex(idx)
self.change_page_type(idx)
@ -209,7 +260,7 @@ class PageListEditor(QtWidgets.QWidget):
def change_page_type(self, i: int) -> None:
new_type = self.cbPageType.itemData(i)
if self.get_current_page_type() != new_type:
if self.listWidget.count() > 0 and self.get_current_page_type() != new_type:
self.set_current_page_type(new_type)
self.emit_front_cover_change()
self.modified.emit()
@ -221,135 +272,123 @@ class PageListEditor(QtWidgets.QWidget):
i = self.cbPageType.findData(pagetype)
self.cbPageType.setCurrentIndex(i)
self.chkDoublePage.setChecked("DoublePage" in self.listWidget.item(row).data(QtCore.Qt.UserRole)[0])
page: PageMetadata = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)
self.chkDoublePage.setChecked(bool(page.double_page))
if "Bookmark" in self.listWidget.item(row).data(QtCore.Qt.UserRole)[0]:
self.leBookmark.setText(self.listWidget.item(row).data(QtCore.Qt.UserRole)[0]["Bookmark"])
else:
self.leBookmark.setText("")
idx = int(self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)[0]["Image"])
self.leBookmark.setText(page.bookmark)
if self.comic_archive is not None:
self.pageWidget.set_archive(self.comic_archive, idx)
self.pageWidget.set_archive(self.comic_archive, page.archive_index)
def get_first_front_cover(self) -> int:
front_cover = 0
if self.listWidget.count() > 0:
page: PageMetadata = self.listWidget.item(0).data(QtCore.Qt.ItemDataRole.UserRole)
front_cover = page.archive_index
for i in range(self.listWidget.count()):
item = self.listWidget.item(i)
page_dict: ImageMetadata = item.data(QtCore.Qt.ItemDataRole.UserRole)[0]
if "Type" in page_dict and page_dict["Type"] == PageType.FrontCover:
front_cover = int(page_dict["Image"])
page = item.data(QtCore.Qt.ItemDataRole.UserRole)
if page.type == PageType.FrontCover:
front_cover = page.archive_index
break
return front_cover
def get_current_page_type(self) -> str:
row = self.listWidget.currentRow()
page_dict: ImageMetadata = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)[0]
if "Type" in page_dict:
return page_dict["Type"]
return ""
page: PageMetadata = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)
return page.type
def set_current_page_type(self, t: str) -> None:
row = self.listWidget.currentRow()
page_dict: ImageMetadata = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)[0]
rows = self.listWidget.selectionModel().selectedRows()
for index in rows:
row = index.row()
page: PageMetadata = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)
if t == "":
if "Type" in page_dict:
del page_dict["Type"]
else:
page_dict["Type"] = t
page.type = t
item = self.listWidget.item(row)
# wrap the dict in a tuple to keep from being converted to QtWidgets.QStrings
item.setData(QtCore.Qt.ItemDataRole.UserRole, (page_dict,))
item.setText(self.list_entry_text(page_dict))
item = self.listWidget.item(row)
item.setData(QtCore.Qt.ItemDataRole.UserRole, page)
item.setText(self.list_entry_text(page))
def toggle_double_page(self) -> None:
row = self.listWidget.currentRow()
page_dict: ImageMetadata = self.listWidget.item(row).data(QtCore.Qt.UserRole)[0]
rows = self.listWidget.selectionModel().selectedRows()
for index in rows:
row = index.row()
page: PageMetadata = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)
cbx = self.sender()
cbx = self.sender()
if isinstance(cbx, QtWidgets.QCheckBox) and cbx.isChecked():
if "DoublePage" not in page_dict:
page_dict["DoublePage"] = True
if isinstance(cbx, QtWidgets.QCheckBox):
page.double_page = cbx.isChecked()
self.modified.emit()
elif "DoublePage" in page_dict:
del page_dict["DoublePage"]
self.modified.emit()
item = self.listWidget.item(row)
# wrap the dict in a tuple to keep from being converted to QStrings
item.setData(QtCore.Qt.UserRole, (page_dict,))
item.setText(self.list_entry_text(page_dict))
item = self.listWidget.item(row)
item.setData(QtCore.Qt.ItemDataRole.UserRole, page)
item.setText(self.list_entry_text(page))
self.listWidget.setFocus()
def save_bookmark(self) -> None:
row = self.listWidget.currentRow()
page_dict: ImageMetadata = self.listWidget.item(row).data(QtCore.Qt.UserRole)[0]
page: PageMetadata = self.listWidget.item(row).data(QtCore.Qt.ItemDataRole.UserRole)
current_bookmark = ""
if "Bookmark" in page_dict:
current_bookmark = page_dict["Bookmark"]
previous_bookmark = page.bookmark
new_bookmark = self.leBookmark.text().strip()
if self.leBookmark.text().strip():
new_bookmark = str(self.leBookmark.text().strip())
if current_bookmark != new_bookmark:
page_dict["Bookmark"] = new_bookmark
self.modified.emit()
elif current_bookmark != "":
del page_dict["Bookmark"]
self.modified.emit()
if previous_bookmark == new_bookmark:
return
page.bookmark = new_bookmark
self.modified.emit()
item = self.listWidget.item(row)
# wrap the dict in a tuple to keep from being converted to QStrings
item.setData(QtCore.Qt.UserRole, (page_dict,))
item.setText(self.list_entry_text(page_dict))
item.setData(QtCore.Qt.ItemDataRole.UserRole, page)
item.setText(self.list_entry_text(page))
self.listWidget.setFocus()
def set_data(self, comic_archive: ComicArchive, pages_list: list[ImageMetadata]) -> None:
def set_data(self, comic_archive: ComicArchive, pages_list: list[PageMetadata]) -> None:
self.cbxBlur.setChecked(self.blur)
self.comic_archive = comic_archive
self.pages_list = pages_list
if pages_list is not None and len(pages_list) > 0:
self.cbPageType.setDisabled(False)
self.chkDoublePage.setDisabled(False)
self.leBookmark.setDisabled(False)
if pages_list:
self.select_read_tags(self.tag_ids)
else:
self.cbPageType.setEnabled(False)
self.chkDoublePage.setEnabled(False)
self.leBookmark.setEnabled(False)
self.listWidget.itemSelectionChanged.disconnect(self.change_page)
self.listWidget.clear()
for p in pages_list:
for p in sorted(pages_list, key=lambda p: p.display_index):
item = QtWidgets.QListWidgetItem(self.list_entry_text(p))
# wrap the dict in a tuple to keep from being converted to QtWidgets.QStrings
item.setData(QtCore.Qt.ItemDataRole.UserRole, (p,))
item.setData(QtCore.Qt.ItemDataRole.UserRole, p)
self.listWidget.addItem(item)
self.first_front_page = self.get_first_front_cover()
self.listWidget.itemSelectionChanged.connect(self.change_page)
self.listWidget.setCurrentRow(0)
def list_entry_text(self, page_dict: ImageMetadata) -> str:
text = str(int(page_dict["Image"]) + 1)
if "Type" in page_dict:
if page_dict["Type"] in self.pageTypeNames:
text += " (" + self.pageTypeNames[page_dict["Type"]] + ")"
def list_entry_text(self, page: PageMetadata) -> str:
# indexes start at 0 but we display starting at 1. This should be consistent for all indexes in ComicTagger
text = str(int(page.archive_index) + 1)
if page.type:
if page.type.casefold() in {x.casefold() for x in PageType}:
text += " (" + self.pageTypeNames[PageType(page.type)] + ")"
else:
text += " (Error: " + page_dict["Type"] + ")"
if "DoublePage" in page_dict:
text += f" (Unknown: {page.type})"
if page.double_page:
text += ""
if "Bookmark" in page_dict:
if page.bookmark:
text += " 🔖"
return text
def get_page_list(self) -> list[ImageMetadata]:
page_list = []
def get_page_list(self) -> list[PageMetadata]:
page_list: list[PageMetadata] = []
for i in range(self.listWidget.count()):
item = self.listWidget.item(i)
page_list.append(item.data(QtCore.Qt.ItemDataRole.UserRole)[0])
page_list.append(item.data(QtCore.Qt.ItemDataRole.UserRole))
page_list[i].display_index = i
return page_list
def emit_front_cover_change(self) -> None:
@ -357,42 +396,20 @@ class PageListEditor(QtWidgets.QWidget):
self.first_front_page = self.get_first_front_cover()
self.firstFrontCoverChanged.emit(self.first_front_page)
def set_metadata_style(self, data_style: int) -> None:
# depending on the current data style, certain fields are disabled
def select_read_tags(self, tag_ids: list[str]) -> None:
# depending on the current tags, certain fields are disabled
if not tag_ids:
return
inactive_color = QtGui.QColor(255, 170, 150)
active_palette = self.cbPageType.palette()
enabled_widgets = set()
for tag_id in tag_ids:
if not tags[tag_id].enabled:
continue
enabled_widgets.update(tags[tag_id].supported_attributes)
inactive_palette3 = self.cbPageType.palette()
inactive_palette3.setColor(QtGui.QPalette.ColorRole.Base, inactive_color)
self.tag_ids = tag_ids
if data_style == MetaDataStyle.CIX:
self.btnUp.setEnabled(True)
self.btnDown.setEnabled(True)
self.cbPageType.setEnabled(True)
self.chkDoublePage.setEnabled(True)
self.leBookmark.setEnabled(True)
self.listWidget.setEnabled(True)
for md_field, widget in self.md_attributes.items():
enable_widget(widget, md_field in enabled_widgets)
self.leBookmark.setPalette(active_palette)
self.listWidget.setPalette(active_palette)
elif data_style == MetaDataStyle.CBI:
self.btnUp.setEnabled(False)
self.btnDown.setEnabled(False)
self.cbPageType.setEnabled(False)
self.chkDoublePage.setEnabled(False)
self.leBookmark.setEnabled(False)
self.listWidget.setEnabled(False)
self.leBookmark.setPalette(inactive_palette3)
self.listWidget.setPalette(inactive_palette3)
elif data_style == MetaDataStyle.COMET:
pass
# make sure combo is disabled when no list
if self.comic_archive is None:
self.cbPageType.setEnabled(False)
self.chkDoublePage.setEnabled(False)
self.leBookmark.setEnabled(False)
self.listWidget.setDragEnabled(not ("pages.image_index" not in enabled_widgets and "pages" in enabled_widgets))

View File

@ -1,6 +1,7 @@
"""A PyQT4 class to load a page image from a ComicArchive in a background thread"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,6 +1,7 @@
"""A PyQt5 dialog to show ID log and progress"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -20,7 +21,6 @@ import logging
from PyQt5 import QtCore, QtWidgets, uic
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import reduce_widget_font_size
logger = logging.getLogger(__name__)
@ -29,7 +29,8 @@ class IDProgressWindow(QtWidgets.QDialog):
def __init__(self, parent: QtWidgets.QWidget) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "progresswindow.ui", self)
with (ui_path / "progresswindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.setWindowFlags(
QtCore.Qt.WindowType(
@ -38,5 +39,3 @@ class IDProgressWindow(QtWidgets.QDialog):
| QtCore.Qt.WindowType.WindowMaximizeButtonHint
)
)
reduce_widget_font_size(self.textEdit)

391
comictaggerlib/quick_tag.py Normal file
View File

@ -0,0 +1,391 @@
from __future__ import annotations
import argparse
import itertools
import logging
from enum import auto
from io import BytesIO
from typing import Callable, TypedDict, cast
from urllib.parse import urljoin
import requests
import settngs
from comicapi import comicarchive, utils
from comicapi.genericmetadata import GenericMetadata
from comicapi.issuestring import IssueString
from comictaggerlib.ctsettings.settngs_namespace import SettngsNS
from comictaggerlib.imagehasher import ImageHasher
from comictalker import ComicTalker
logger = logging.getLogger(__name__)
__version__ = "0.1"
class HashType(utils.StrEnum):
AHASH = auto()
DHASH = auto()
PHASH = auto()
class SimpleResult(TypedDict):
Distance: int
# Mapping of domains (eg comicvine.gamespot.com) to IDs
IDList: dict[str, list[str]]
class Hash(TypedDict):
Hash: int
Kind: str
class Result(TypedDict):
# Mapping of domains (eg comicvine.gamespot.com) to IDs
IDs: dict[str, list[str]]
Distance: int
Hash: Hash
def ihash(types: str) -> list[HashType]:
result: list[HashType] = []
types = types.casefold()
choices = ", ".join(HashType)
for typ in utils.split(types, ","):
if typ not in list(HashType):
raise argparse.ArgumentTypeError(f"invalid choice: {typ} (choose from {choices.upper()})")
result.append(HashType[typ.upper()])
if not result:
raise argparse.ArgumentTypeError(f"invalid choice: {types} (choose from {choices.upper()})")
return result
def settings(manager: settngs.Manager) -> None:
manager.add_setting(
"--url",
"-u",
default="https://comic-hasher.narnian.us",
type=utils.parse_url,
help="Website to use for searching cover hashes",
)
manager.add_setting(
"--max",
default=8,
type=int,
help="Maximum score to allow. Lower score means more accurate",
)
manager.add_setting(
"--simple",
default=False,
action=argparse.BooleanOptionalAction,
help="Whether to retrieve simple results or full results",
)
manager.add_setting(
"--aggressive-filtering",
default=False,
action=argparse.BooleanOptionalAction,
help="Will filter out worse matches if better matches are found",
)
manager.add_setting(
"--hash",
default="ahash, dhash, phash",
type=ihash,
help="Pick what hashes you want to use to search (default: %(default)s)",
)
manager.add_setting(
"--exact-only",
default=True,
action=argparse.BooleanOptionalAction,
help="Skip non-exact matches if we have exact matches",
)
class QuickTag:
def __init__(
self, url: utils.Url, domain: str, talker: ComicTalker, config: SettngsNS, output: Callable[[str], None]
):
self.output = output
self.url = url
self.talker = talker
self.domain = domain
self.config = config
def id_comic(
self,
ca: comicarchive.ComicArchive,
tags: GenericMetadata,
simple: bool,
hashes: set[HashType],
exact_only: bool,
interactive: bool,
aggressive_filtering: bool,
max_hamming_distance: int,
) -> GenericMetadata | None:
if not ca.seems_to_be_a_comic_archive():
raise Exception(f"{ca.path} is not an archive")
from PIL import Image
cover_index = tags.get_cover_page_index_list()[0]
cover_image = Image.open(BytesIO(ca.get_page(cover_index)))
self.output(f"Tagging: {ca.path}")
self.output("hashing cover")
phash = dhash = ahash = ""
hasher = ImageHasher(image=cover_image)
if HashType.AHASH in hashes:
ahash = hex(hasher.average_hash())[2:]
if HashType.DHASH in hashes:
dhash = hex(hasher.difference_hash())[2:]
if HashType.PHASH in hashes:
phash = hex(hasher.p_hash())[2:]
logger.info(f"Searching with {ahash=}, {dhash=}, {phash=}")
self.output("Searching hashes")
results = self.SearchHashes(simple, max_hamming_distance, ahash, dhash, phash, exact_only)
logger.debug(f"{results=}")
if simple:
filtered_simple_results = self.filter_simple_results(
cast(list[SimpleResult], results), interactive, aggressive_filtering
)
metadata_simple_results = self.get_simple_results(filtered_simple_results)
chosen_result = self.display_simple_results(metadata_simple_results, tags, interactive)
else:
filtered_results = self.filter_results(cast(list[Result], results), interactive, aggressive_filtering)
metadata_results = self.get_results(filtered_results)
chosen_result = self.display_results(metadata_results, tags, interactive)
return self.talker.fetch_comic_data(issue_id=chosen_result.issue_id, on_rate_limit=None)
def SearchHashes(
self, simple: bool, max_hamming_distance: int, ahash: str, dhash: str, phash: str, exact_only: bool
) -> list[SimpleResult] | list[Result]:
resp = requests.get(
urljoin(self.url.url, "/match_cover_hash"),
params={
"simple": str(simple),
"max": str(max_hamming_distance),
"ahash": ahash,
"dhash": dhash,
"phash": phash,
"exactOnly": str(exact_only),
},
)
if resp.status_code != 200:
try:
text = resp.json()["msg"]
except Exception:
text = resp.text
if text == "No hashes found":
return []
logger.error("message from server: %s", text)
raise Exception(f"Failed to retrieve results from the server: {text}")
return resp.json()["results"]
def get_mds(self, results: list[SimpleResult] | list[Result]) -> list[GenericMetadata]:
md_results: list[GenericMetadata] = []
results.sort(key=lambda r: r["Distance"])
all_ids = set()
for res in results:
all_ids.update(res.get("IDList", res.get("IDs", {})).get(self.domain, [])) # type: ignore[attr-defined]
self.output(f"Retrieving basic {self.talker.name} data")
# Try to do a bulk feth of basic issue data
if hasattr(self.talker, "fetch_comics"):
md_results = self.talker.fetch_comics(issue_ids=list(all_ids), on_rate_limit=None)
else:
for md_id in all_ids:
md_results.append(self.talker.fetch_comic_data(issue_id=md_id, on_rate_limit=None))
return md_results
def get_simple_results(self, results: list[SimpleResult]) -> list[tuple[int, GenericMetadata]]:
md_results = []
mds = self.get_mds(results)
# Re-associate the md to the distance
for res in results:
for md in mds:
if md.issue_id in res["IDList"].get(self.domain, []):
md_results.append((res["Distance"], md))
return md_results
def get_results(self, results: list[Result]) -> list[tuple[int, Hash, GenericMetadata]]:
md_results = []
mds = self.get_mds(results)
# Re-associate the md to the distance
for res in results:
for md in mds:
if md.issue_id in res["IDs"].get(self.domain, []):
md_results.append((res["Distance"], res["Hash"], md))
return md_results
def filter_simple_results(
self, results: list[SimpleResult], interactive: bool, aggressive_filtering: bool
) -> list[SimpleResult]:
# If there is a single exact match return it
exact = [r for r in results if r["Distance"] == 0]
if len(exact) == 1:
logger.info("Exact result found. Ignoring any others")
return exact
# If ther are more than 4 results and any are better than 6 return the first group of results
if len(results) > 4:
dist: list[tuple[int, list[SimpleResult]]] = []
filtered_results: list[SimpleResult] = []
for distance, group in itertools.groupby(results, key=lambda r: r["Distance"]):
dist.append((distance, list(group)))
if aggressive_filtering and dist[0][0] < 6:
logger.info(f"Aggressive filtering is enabled. Dropping matches above {dist[0]}")
for _, res in dist[:1]:
filtered_results.extend(res)
logger.debug(f"{filtered_results=}")
return filtered_results
return results
def filter_results(self, results: list[Result], interactive: bool, aggressive_filtering: bool) -> list[Result]:
ahash_results = sorted([r for r in results if r["Hash"]["Kind"] == "ahash"], key=lambda r: r["Distance"])
dhash_results = sorted([r for r in results if r["Hash"]["Kind"] == "dhash"], key=lambda r: r["Distance"])
phash_results = sorted([r for r in results if r["Hash"]["Kind"] == "phash"], key=lambda r: r["Distance"])
hash_results = [phash_results, dhash_results, ahash_results]
# If any of the hash types have a single exact match return it. Prefer phash for no particular reason
for hashed_result in hash_results:
exact = [r for r in hashed_result if r["Distance"] == 0]
if len(exact) == 1:
logger.info(f"Exact {exact[0]['Hash']['Kind']} result found. Ignoring any others")
return exact
results_filtered = False
# If any of the hash types have more than 4 results and they have results better than 6 return the first group of results for each hash type
for i, hashed_results in enumerate(hash_results):
filtered_results: list[Result] = []
if len(hashed_results) > 4:
dist: list[tuple[int, list[Result]]] = []
for distance, group in itertools.groupby(hashed_results, key=lambda r: r["Distance"]):
dist.append((distance, list(group)))
if aggressive_filtering and dist[0][0] < 6:
logger.info(
f"Aggressive filtering is enabled. Dropping {dist[0][1][0]['Hash']['Kind']} matches above {dist[0][0]}"
)
for _, res in dist[:1]:
filtered_results.extend(res)
if filtered_results:
hash_results[i] = filtered_results
results_filtered = True
if results_filtered:
logger.debug(f"filtered_results={list(itertools.chain(*hash_results))}")
return list(itertools.chain(*hash_results))
def display_simple_results(
self, md_results: list[tuple[int, GenericMetadata]], tags: GenericMetadata, interactive: bool
) -> GenericMetadata:
if len(md_results) < 1:
return GenericMetadata()
if len(md_results) == 1 and md_results[0][0] <= 4:
self.output("Found a single match <=4. Assuming it's correct")
return md_results[0][1]
series_match: list[GenericMetadata] = []
for score, md in md_results:
if (
score < 10
and tags.series
and md.series
and utils.titles_match(tags.series, md.series)
and IssueString(tags.issue).as_string() == IssueString(md.issue).as_string()
):
series_match.append(md)
if len(series_match) == 1:
self.output(f"Found match with series name {series_match[0].series!r}")
return series_match[0]
if not interactive:
return GenericMetadata()
md_results.sort(key=lambda r: (r[0], len(r[1].publisher or "")))
for counter, r in enumerate(md_results, 1):
self.output(
" {:2}. score: {} [{:15}] ({:02}/{:04}) - {} #{} - {}".format(
counter,
r[0],
r[1].publisher,
r[1].month or 0,
r[1].year or 0,
r[1].series,
r[1].issue,
r[1].title,
),
)
while True:
i = input(
f'Please select a result to tag the comic with or "q" to quit: [1-{len(md_results)}] ',
).casefold()
if i.isdigit() and int(i) in range(1, len(md_results) + 1):
break
if i == "q":
logger.warning("User quit without saving metadata")
return GenericMetadata()
return md_results[int(i) - 1][1]
def display_results(
self,
md_results: list[tuple[int, Hash, GenericMetadata]],
tags: GenericMetadata,
interactive: bool,
) -> GenericMetadata:
if len(md_results) < 1:
return GenericMetadata()
if len(md_results) == 1 and md_results[0][0] <= 4:
self.output("Found a single match <=4. Assuming it's correct")
return md_results[0][2]
series_match: dict[str, tuple[int, Hash, GenericMetadata]] = {}
for score, cover_hash, md in md_results:
if (
score < 10
and tags.series
and md.series
and utils.titles_match(tags.series, md.series)
and IssueString(tags.issue).as_string() == IssueString(md.issue).as_string()
):
assert md.issue_id
series_match[md.issue_id] = (score, cover_hash, md)
if len(series_match) == 1:
score, cover_hash, md = list(series_match.values())[0]
self.output(f"Found {cover_hash['Kind']} {score=} match with series name {md.series!r}")
return md
if not interactive:
return GenericMetadata()
md_results.sort(key=lambda r: (r[0], len(r[2].publisher or ""), r[1]["Kind"]))
for counter, r in enumerate(md_results, 1):
self.output(
" {:2}. score: {} {}: {:064b} [{:15}] ({:02}/{:04}) - {} #{} - {}".format(
counter,
r[0],
r[1]["Kind"],
r[1]["Hash"],
r[2].publisher or "",
r[2].month or 0,
r[2].year or 0,
r[2].series or "",
r[2].issue or "",
r[2].title or "",
),
)
while True:
i = input(
f'Please select a result to tag the comic with or "q" to quit: [1-{len(md_results)}] ',
).casefold()
if i.isdigit() and int(i) in range(1, len(md_results) + 1):
break
if i == "q":
self.output("User quit without saving metadata")
return GenericMetadata()
return md_results[int(i) - 1][2]

View File

@ -1,6 +1,7 @@
"""A PyQT4 dialog to confirm rename"""
#
# Copyright 2012-2014 Anthony Beville
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -17,16 +18,18 @@ from __future__ import annotations
import logging
import settngs
from PyQt5 import QtCore, QtWidgets, uic
from comicapi import utils
from comicapi.comicarchive import ComicArchive, MetaDataStyle
from comicapi.comicarchive import ComicArchive, tags
from comicapi.genericmetadata import GenericMetadata
from comictaggerlib.ctsettings import ct_ns
from comictaggerlib.filerenamer import FileRenamer, get_rename_dir
from comictaggerlib.settings import ComicTaggerSettings
from comictaggerlib.settingswindow import SettingsWindow
from comictaggerlib.ui import ui_path
from comictaggerlib.ui.qtutils import center_window_on_parent
from comictalker.comictalker import ComicTalker
logger = logging.getLogger(__name__)
@ -36,13 +39,16 @@ class RenameWindow(QtWidgets.QDialog):
self,
parent: QtWidgets.QWidget,
comic_archive_list: list[ComicArchive],
data_style: int,
settings: ComicTaggerSettings,
read_tag_ids: list[str],
config: settngs.Config[ct_ns],
talkers: dict[str, ComicTalker],
) -> None:
super().__init__(parent)
uic.loadUi(ui_path / "renamewindow.ui", self)
self.label.setText(f"Preview (based on {MetaDataStyle.name[data_style]} tags):")
with (ui_path / "renamewindow.ui").open(encoding="utf-8") as uifile:
uic.loadUi(uifile, self)
self.label.setText(f"Preview (based on {', '.join(tags[tag].name() for tag in read_tag_ids)} tags):")
self.setWindowFlags(
QtCore.Qt.WindowType(
@ -52,42 +58,48 @@ class RenameWindow(QtWidgets.QDialog):
)
)
self.settings = settings
self.config = config
self.talkers = talkers
self.comic_archive_list = comic_archive_list
self.data_style = data_style
self.read_tag_ids = read_tag_ids
self.rename_list: list[str] = []
self.btnSettings.clicked.connect(self.modify_settings)
platform = "universal" if self.settings.rename_strict else "auto"
self.renamer = FileRenamer(None, platform=platform)
platform = "universal" if self.config[0].File_Rename__strict_filenames else "auto"
self.renamer = FileRenamer(None, platform=platform, replacements=self.config[0].File_Rename__replacements)
self.do_preview()
def config_renamer(self, ca: ComicArchive, md: GenericMetadata | None = None) -> str:
self.renamer.set_template(self.settings.rename_template)
self.renamer.set_issue_zero_padding(self.settings.rename_issue_number_padding)
self.renamer.set_smart_cleanup(self.settings.rename_use_smart_string_cleanup)
def config_renamer(self, ca: ComicArchive, md: GenericMetadata = GenericMetadata()) -> str:
self.renamer.set_template(self.config[0].File_Rename__template)
self.renamer.set_issue_zero_padding(self.config[0].File_Rename__issue_number_padding)
self.renamer.set_smart_cleanup(self.config[0].File_Rename__use_smart_string_cleanup)
self.renamer.replacements = self.config[0].File_Rename__replacements
self.renamer.move_only = self.config[0].File_Rename__only_move
new_ext = ca.path.suffix # default
if self.settings.rename_extension_based_on_archive:
if ca.is_sevenzip():
new_ext = ".cb7"
elif ca.is_zip():
new_ext = ".cbz"
elif ca.is_rar():
new_ext = ".cbr"
if self.config[0].File_Rename__auto_extension:
new_ext = ca.extension()
if md is None or md.is_empty:
md, _, error = self.parent().read_selected_tags(self.read_tag_ids, ca)
if error is not None:
logger.error("Failed to load tags from %s: %s", ca.path, error)
QtWidgets.QMessageBox.warning(
self,
"Read Failed!",
f"One or more of the read tags failed to load for {ca.path}, check log for details",
)
if md is None:
md = ca.read_metadata(self.data_style)
if md.is_empty:
md = ca.metadata_from_filename(
self.settings.complicated_parser,
self.settings.remove_c2c,
self.settings.remove_fcbd,
self.settings.remove_publisher,
self.config[0].Filename_Parsing__filename_parser,
self.config[0].Filename_Parsing__remove_c2c,
self.config[0].Filename_Parsing__remove_fcbd,
self.config[0].Filename_Parsing__remove_publisher,
)
self.renamer.set_metadata(md)
self.renamer.move = self.settings.rename_move_dir
self.renamer.set_metadata(md, ca.path.name)
self.renamer.move = self.config[0].File_Rename__move
return new_ext
def do_preview(self) -> None:
@ -100,7 +112,7 @@ class RenameWindow(QtWidgets.QDialog):
try:
new_name = self.renamer.determine_name(new_ext)
except ValueError as e:
logger.exception("Invalid format string: %s", self.settings.rename_template)
logger.exception("Invalid format string: %s", self.config[0].File_Rename__template)
QtWidgets.QMessageBox.critical(
self,
"Invalid format string!",
@ -114,7 +126,7 @@ class RenameWindow(QtWidgets.QDialog):
return
except Exception as e:
logger.exception(
"Formatter failure: %s metadata: %s", self.settings.rename_template, self.renamer.metadata
"Formatter failure: %s metadata: %s", self.config[0].File_Rename__template, self.renamer.metadata
)
QtWidgets.QMessageBox.critical(
self,
@ -125,6 +137,7 @@ class RenameWindow(QtWidgets.QDialog):
"<a href='https://github.com/comictagger/comictagger'>"
"https://github.com/comictagger/comictagger</a>",
)
return
row = self.twList.rowCount()
self.twList.insertRow(row)
@ -161,7 +174,7 @@ class RenameWindow(QtWidgets.QDialog):
self.twList.setSortingEnabled(True)
def modify_settings(self) -> None:
settingswin = SettingsWindow(self, self.settings)
settingswin = SettingsWindow(self, self.config, self.talkers)
settingswin.setModal(True)
settingswin.show_rename_tab()
settingswin.exec()
@ -169,7 +182,6 @@ class RenameWindow(QtWidgets.QDialog):
self.do_preview()
def accept(self) -> None:
prog_dialog = QtWidgets.QProgressDialog("", "Cancel", 0, len(self.rename_list), self)
prog_dialog.setWindowTitle("Renaming Archives")
prog_dialog.setWindowModality(QtCore.Qt.WindowModality.WindowModal)
@ -178,18 +190,19 @@ class RenameWindow(QtWidgets.QDialog):
QtCore.QCoreApplication.processEvents()
try:
for idx, comic in enumerate(zip(self.comic_archive_list, self.rename_list)):
for idx, comic in enumerate(zip(self.comic_archive_list, self.rename_list), 1):
QtCore.QCoreApplication.processEvents()
if prog_dialog.wasCanceled():
break
idx += 1
prog_dialog.setValue(idx)
prog_dialog.setLabelText(comic[1])
center_window_on_parent(prog_dialog)
QtCore.QCoreApplication.processEvents()
folder = get_rename_dir(comic[0], self.settings.rename_dir if self.settings.rename_move_dir else None)
folder = get_rename_dir(
comic[0],
self.config[0].File_Rename__dir if self.config[0].File_Rename__move else None,
)
full_path = folder / comic[1]
@ -197,7 +210,7 @@ class RenameWindow(QtWidgets.QDialog):
logger.info("%s: Filename is already good!", comic[1])
continue
if not comic[0].is_writable(check_rar_status=False):
if not comic[0].is_writable(check_archive_status=False):
continue
comic[0].rename(utils.unique_file(full_path))

View File

@ -1,162 +1,97 @@
from __future__ import annotations
from typing_extensions import NotRequired, Required, TypedDict
import dataclasses
import pathlib
from enum import auto
from comicapi.comicarchive import ComicArchive
from comicapi import utils
from comicapi.genericmetadata import GenericMetadata
class IssueResult(TypedDict):
@dataclasses.dataclass
class IssueResult:
series: str
distance: int
issue_number: str
cv_issue_count: int
issue_count: int | None
url_image_hash: int
issue_title: str
issue_id: int # int?
volume_id: int # int?
issue_id: str
series_id: str
month: int | None
year: int | None
publisher: str | None
image_url: str
thumb_url: str
page_url: str
alt_image_urls: list[str]
description: str
def __str__(self) -> str:
return f"series: {self.series}; series id: {self.series_id}; issue number: {self.issue_number}; issue id: {self.issue_id}; published: {self.month} {self.year}"
class Action(utils.StrEnum):
gui = auto()
print = auto()
delete = auto()
copy = auto()
save = auto()
rename = auto()
export = auto()
save_config = auto()
list_plugins = auto()
class MatchStatus(utils.StrEnum):
good_match = auto()
no_match = auto()
multiple_match = auto()
low_confidence_match = auto()
class Status(utils.StrEnum):
success = auto()
match_failure = auto()
write_failure = auto()
fetch_data_failure = auto()
existing_tags = auto()
read_failure = auto()
write_permission_failure = auto()
rename_failure = auto()
@dataclasses.dataclass
class OnlineMatchResults:
def __init__(self) -> None:
self.good_matches: list[str] = []
self.no_matches: list[str] = []
self.multiple_matches: list[MultipleMatch] = []
self.low_confidence_matches: list[MultipleMatch] = []
self.write_failures: list[str] = []
self.fetch_data_failures: list[str] = []
good_matches: list[Result] = dataclasses.field(default_factory=list)
no_matches: list[Result] = dataclasses.field(default_factory=list)
multiple_matches: list[Result] = dataclasses.field(default_factory=list)
low_confidence_matches: list[Result] = dataclasses.field(default_factory=list)
write_failures: list[Result] = dataclasses.field(default_factory=list)
fetch_data_failures: list[Result] = dataclasses.field(default_factory=list)
class MultipleMatch:
def __init__(self, ca: ComicArchive, match_list: list[IssueResult]) -> None:
self.ca: ComicArchive = ca
self.matches: list[IssueResult] = match_list
@dataclasses.dataclass
class Result:
action: Action
status: Status | None
original_path: pathlib.Path
renamed_path: pathlib.Path | None = None
class SelectDetails(TypedDict):
image_url: str | None
thumb_image_url: str | None
cover_date: str | None
site_detail_url: str | None
online_results: list[IssueResult] = dataclasses.field(default_factory=list)
match_status: MatchStatus | None = None
md: GenericMetadata | None = None
class CVResult(TypedDict):
error: str
limit: int
offset: int
number_of_page_results: int
number_of_total_results: int
status_code: int
results: (
CVIssuesResults
| CVIssueDetailResults
| CVVolumeResults
| list[CVIssuesResults]
| list[CVVolumeResults]
| list[CVIssueDetailResults]
)
version: str
tags_read: list[str] = dataclasses.field(default_factory=list)
tags_deleted: list[str] = dataclasses.field(default_factory=list)
tags_written: list[str] = dataclasses.field(default_factory=list)
class CVImage(TypedDict, total=False):
icon_url: str
medium_url: str
screen_url: str
screen_large_url: str
small_url: str
super_url: Required[str]
thumb_url: str
tiny_url: str
original_url: str
image_tags: str
class CVVolume(TypedDict):
api_detail_url: str
id: int
name: str
site_detail_url: str
class CVIssuesResults(TypedDict):
cover_date: str
description: str
id: int
image: CVImage
issue_number: str
name: str
site_detail_url: str
volume: NotRequired[CVVolume]
aliases: str
class CVPublisher(TypedDict, total=False):
api_detail_url: str
id: int
name: Required[str]
class CVVolumeResults(TypedDict):
count_of_issues: int
description: NotRequired[str]
id: int
image: NotRequired[CVImage]
name: str
publisher: CVPublisher
start_year: str
resource_type: NotRequired[str]
aliases: NotRequired[str | None]
class CVCredits(TypedDict):
api_detail_url: str
id: int
name: str
site_detail_url: str
class CVPersonCredits(TypedDict):
api_detail_url: str
id: int
name: str
site_detail_url: str
role: str
class CVIssueDetailResults(TypedDict):
aliases: None
api_detail_url: str
character_credits: list[CVCredits]
character_died_in: None
concept_credits: list[CVCredits]
cover_date: str
date_added: str
date_last_updated: str
deck: None
description: str
first_appearance_characters: None
first_appearance_concepts: None
first_appearance_locations: None
first_appearance_objects: None
first_appearance_storyarcs: None
first_appearance_teams: None
has_staff_review: bool
id: int
image: CVImage
issue_number: str
location_credits: list[CVCredits]
name: str
object_credits: list[CVCredits]
person_credits: list[CVPersonCredits]
site_detail_url: str
store_date: str
story_arc_credits: list[CVCredits]
team_credits: list[CVCredits]
team_disbanded_in: None
volume: CVVolume
def __str__(self) -> str:
if len(self.online_results) == 0:
matches = None
elif len(self.online_results) == 1:
matches = str(self.online_results[0])
else:
matches = "\n" + "".join([f" - {x}" for x in self.online_results])
path_str = utils.path_to_short_str(self.original_path, self.renamed_path)
return f"{path_str}: {matches}"

Some files were not shown because too many files have changed in this diff Show More