Skip to content

Commit efe6781

Browse files
authored
Merge pull request #2863 from phw/plugin-api-reorganize-imports
Plugin API reorganize imports
2 parents f34da4b + 8bc7c2e commit efe6781

File tree

10 files changed

+1031
-989
lines changed

10 files changed

+1031
-989
lines changed

docs/PLUGINSV3/API.md

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,29 @@ class MyWidget(QWidget):
6969

7070
## Class References
7171

72-
The following classes are available through the `api` object:
73-
74-
- `api.Album` - Album object
75-
- `api.Track` - Track object
76-
- `api.File` - File base class (for custom formats)
77-
- `api.Cluster` - Cluster object
78-
- `api.Metadata` - Metadata container for tags
79-
- `api.BaseAction` - Base class for UI actions
80-
- `api.OptionsPage` - Base class for options pages
81-
- `api.CoverArtImage` - Cover art image object
82-
- `api.CoverArtProvider` - Base class for cover art providers
83-
- `api.ProviderOptions` - Base class for cover art provider option pages
72+
The following classes are available through the `api` module:
73+
74+
- `Album` - Album object
75+
- `Track` - Track object
76+
- `File` - File base class (for custom formats)
77+
- `Cluster` - Cluster object
78+
- `Metadata` - Metadata container for tags
79+
- `BaseAction` - Base class for UI actions
80+
- `OptionsPage` - Base class for options pages
81+
- `CoverArtImage` - Cover art image object
82+
- `CoverArtProvider` - Base class for cover art providers
83+
- `ProviderOptions` - Base class for cover art provider option pages
84+
- `ScriptParser` - A parser for parsing tagger scripts
8485

8586
**Example**:
8687
```python
87-
from picard.plugin3.api import BaseAction, OptionsPage, File, CoverArtProvider
88-
from picard.metadata import Metadata
88+
from picard.plugin3.api import BaseAction, OptionsPage, File, CoverArtProvider, Metadata
8989

9090
class MyProvider(CoverArtProvider):
9191
NAME = "My Provider"
9292

9393
class MyFormat(File):
94+
NAME = "Custom Format"
9495
EXTENSIONS = [".custom"]
9596

9697
def _load(self, filename):
@@ -99,10 +100,9 @@ class MyFormat(File):
99100
return metadata
100101
```
101102

102-
**Note**: Classes that are considered part of the API `Metadata`, `Track`, `Album`, etc.
103-
should be accessed via their aliases defined in `PluginApi`, e.g. `PluginApi.Track`.
104-
This is also the case for classes meant for inheritance (`BaseAction`, `OptionsPage`,
105-
`File`, `CoverArtProvider`).
103+
**Note**: Classes that are considered part of the API like `Metadata`, `Track`, `Album`,
104+
etc. should be imported directly from `picard.plugin3.api` instead from elsewhere
105+
in Picard.
106106

107107
---
108108

@@ -216,9 +216,9 @@ def enable(api):
216216

217217
**In OptionsPage:**
218218
```python
219-
from picard.plugin3 import PluginApi
219+
from picard.plugin3.api import PluginApi, OptionsPage
220220

221-
class MyOptionsPage(PluginApi.OptionsPage):
221+
class MyOptionsPage(OptionsPage):
222222
def __init__(self):
223223
super().__init__()
224224
# Initialize the UI here
@@ -385,8 +385,7 @@ Mark a string for translation extraction without translating it immediately.
385385
This is a marker function that allows you to define translatable strings at module level or in data structures before the API is available. At runtime, it simply returns the key (or tuple for plurals) unchanged.
386386

387387
```python
388-
from picard.plugin3 import PluginApi
389-
from picard.plugin3.api import t_
388+
from picard.plugin3.api import PluginApi, BaseAction, t_
390389

391390
# Define translatable strings at module level
392391
ERROR_MESSAGES = {
@@ -398,8 +397,8 @@ ERROR_MESSAGES = {
398397
FILE_COUNT = t_('files.count', '{n} file', '{n} files')
399398

400399
# Use in class definitions
401-
class MyAction(PluginApi.BaseAction):
402-
NAME = "My Custom Action"
400+
class MyAction(BaseAction):
401+
TITLE = "My Custom Action"
403402

404403
def __init__(self):
405404
super().__init__()
@@ -617,10 +616,10 @@ Plugin Tools menu actions:
617616
Register menu actions for different object types.
618617

619618
```python
620-
from picard.plugin3 import PluginApi
619+
from picard.plugin3.api import BaseAction
621620

622-
class MyAction(PluginApi.BaseAction):
623-
NAME = "My Custom Action"
621+
class MyAction(BaseAction):
622+
TITLE = "My Custom Action"
624623

625624
def callback(self, objs):
626625
for obj in objs:
@@ -650,9 +649,9 @@ the class to access the `PluginApi` instance of the plugin.
650649
Register a settings page in Picard's options dialog.
651650

652651
```python
653-
from picard.plugin3 import PluginApi
652+
from picard.plugin3.api import OptionsPage
654653

655-
class MyOptionsPage(PluginApi.OptionsPage):
654+
class MyOptionsPage(OptionsPage):
656655
NAME = "my_plugin"
657656
TITLE = "My Plugin"
658657
PARENT = "plugins"
@@ -691,9 +690,9 @@ def enable(api):
691690
Register a custom cover art provider.
692691

693692
```python
694-
from picard.plugin3 import PluginApi
693+
from picard.plugin3.api import CoverArtProvider
695694

696-
class MyProvider(PluginApi.CoverArtProvider):
695+
class MyProvider(CoverArtProvider):
697696
NAME = "My Provider"
698697

699698
def queue_images(self):
@@ -773,9 +772,9 @@ def enable(api):
773772
Register support for a custom file format.
774773

775774
```python
776-
from picard.plugin3 import PluginApi
775+
from picard.plugin3.api import File
777776

778-
class MyFormat(PluginApi.File):
777+
class MyFormat(File):
779778
EXTENSIONS = [".myformat"]
780779
NAME = "My Format"
781780

@@ -905,9 +904,9 @@ def enable(api):
905904

906905
**For Classes**:
907906
```python
908-
from picard.plugin3 import PluginApi
907+
from picard.plugin3.api import OptionsPage
909908

910-
class MyPage(PluginApi.OptionsPage):
909+
class MyPage(OptionsPage):
911910
def load(self):
912911
self.api.logger.info("Loading")
913912

@@ -948,12 +947,12 @@ def enable(api):
948947

949948
```python
950949
from PyQt6.QtWidgets import QCheckBox
951-
from picard.plugin3 import PluginApi
950+
from picard.plugin3.api import PluginApi, BaseAction, OptionsPage, t_
952951

953952

954-
class MyOptionsPage(PluginApi.OptionsPage):
953+
class MyOptionsPage(OptionsPage):
955954
NAME = "example"
956-
TITLE = "Example Plugin"
955+
TITLE = t_("Example Plugin")
957956
PARENT = "plugins"
958957

959958
def __init__(self):
@@ -981,8 +980,8 @@ def on_file_saved(api, file):
981980
api.logger.info(f"Saved: {file.filename}")
982981

983982

984-
class MyAction(PluginApi.BaseAction):
985-
NAME = "Example Action"
983+
class MyAction(BaseAction):
984+
TITLE = t_("Example Action")
986985

987986
def callback(self, objs):
988987
self.api.logger.info(f"Action on {len(objs)} objects")
@@ -1011,8 +1010,8 @@ def enable(api):
10111010
4. **Handle errors gracefully**: Wrap risky operations in try/except
10121011
5. **Set priorities wisely**: Only use non-zero priorities when order matters
10131012
6. **Pass api to parent classes**: You can use `self.api` in classes inherited from
1014-
base classes meant to be subclassed like `PluginApi.BaseAction` or
1015-
`PluginApi.OptionsPage`.
1013+
base classes meant to be subclassed and imported from `picard.plugin3.api`,
1014+
like `BaseAction` or `OptionsPage`.
10161015

10171016
---
10181017

docs/PLUGINSV3/MANIFEST.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ api = ["3.0"]
2929

3030
```python
3131
# __init__.py
32-
from picard.plugin3 import PluginApi
32+
from picard.plugin3.api import PluginApi
3333

3434
def enable(api: PluginApi):
3535
"""Called when plugin is enabled"""
@@ -416,7 +416,7 @@ Vollständige deutsche Beschreibung hier...
416416
Every plugin must have an `__init__.py` file with an `enable()` function:
417417

418418
```python
419-
from picard.plugin3 import PluginApi
419+
from picard.plugin3.api import PluginApi
420420

421421
def enable(api: PluginApi):
422422
"""
@@ -571,7 +571,7 @@ def enable(api: PluginApi):
571571

572572
```python
573573
# __init__.py
574-
from picard.plugin3 import PluginApi
574+
from picard.plugin3.api import PluginApi
575575

576576
def enable(api: PluginApi):
577577
@api.on_album_metadata_loaded
@@ -584,7 +584,7 @@ def enable(api: PluginApi):
584584

585585
```python
586586
# __init__.py
587-
from picard.plugin3 import PluginApi
587+
from picard.plugin3.api import PluginApi
588588

589589
def enable(api: PluginApi):
590590
@api.register_cover_art_provider
@@ -599,7 +599,7 @@ def enable(api: PluginApi):
599599

600600
```python
601601
# __init__.py
602-
from picard.plugin3 import PluginApi
602+
from picard.plugin3.api import PluginApi
603603

604604
def enable(api: PluginApi):
605605
@api.register_album_action("Export to CSV")

docs/PLUGINSV3/TRANSLATIONS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Simple flat JSON structure with dot notation for namespacing:
5656
### Usage in Plugin Code
5757

5858
```python
59-
from picard.plugin3 import PluginApi
59+
from picard.plugin3.api import PluginApi
6060
from picard.i18n import gettext as _ # Picard's translations
6161

6262
def enable(api: PluginApi):
@@ -102,7 +102,7 @@ FILE_COUNT = t_('files.count', '{n} file', '{n} files')
102102

103103
# Use in class definitions
104104
class MyAction(BaseAction):
105-
NAME = "My Custom Action"
105+
TITLE = t_("My Custom Action")
106106

107107
def enable(api):
108108
# Translate at runtime
@@ -666,7 +666,7 @@ fr = "Soumettez votre musique sur ListenBrainz"
666666

667667
### __init__.py
668668
```python
669-
from picard.plugin3 import PluginApi
669+
from picard.plugin3.api import PluginApi
670670
from picard.i18n import gettext as _
671671

672672
def enable(api: PluginApi):

docs/Plugin2to3MigrationGuide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ register_file_action(action)
340340
from picard.plugin3.api import BaseAction
341341

342342
class MyAction(BaseAction):
343-
NAME = 'My Action'
343+
TITLE = 'My Action'
344344

345345
def __init__(self, api=None):
346346
super().__init__()

picard/plugin3/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
GitReferenceError,
2525
GitRepositoryError,
2626
)
27-
from picard.plugin3.api import PluginApi
2827

2928

3029
__all__ = [
31-
'PluginApi',
3230
'GitBackendError',
3331
'GitRepositoryError',
3432
'GitReferenceError',

0 commit comments

Comments
 (0)