# Plugin event handling

**URL:** <https://discourse.beets.io/t/plugin-event-handling/1287>\
**Category:** Help\
**Created:** [May 21, 2020, 2:52pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287 "2020-05-21T14:52:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 21, 2020, 2:52pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/1 "2020-05-21T14:52:49Z")

</div>

I have been using the hook plugin to run operations of files when changed or first imported. For changed files I use the after\_write event. That event is fired multiple times during an import, so the operation is repeated. I am considering writing a plugin to do it instead, but the plugin would need to know if an import operation is currently in progress.  
Is there a function or variable plugins can gain access to that will tell me whether an import is in progress? This way I can tell my plugin to ignore the write event during imports.

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 21, 2020, 4:01pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/2 "2020-05-21T16:01:29Z")

</div>

Yo—I don’t think that is feasible (it would require too much global, mutable state). Maybe a good way to do it would be to only trigger the action if the file is actually in the library directory?

(Or maybe more details on what you’re doing would help shake out other ideas?)

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 21, 2020, 4:54pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/3 "2020-05-21T16:54:54Z")

</div>

That method might reduce the multiple writes, but there must at least be one call in the library directory and then the event for a finished import is fired. This would still have at least 2 runs which is better.  
I am writing a plugin to keep a par set for each song and update it when the file changes.

In a related vein, if I only call item.store(), will beets keep the changes and write them to disk at the end? If so, will that only happen on import?

The only other idea I had was to add a cached writing system for all plugins. Any time a plugin calls item.write() beets could hold the operation in queue until the end of the import and essentially make one write and thus fire one write event. I’m sure that would be a lot of work though.

I will give your idea a try and see how many write events fire during an import.

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 21, 2020, 5:35pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/4 "2020-05-21T17:35:00Z")

</div>

> [@phirestalker](#):
>
> In a related vein, if I only call item.store(), will beets keep the changes and write them to disk at the end? If so, will that only happen on import?

`store()` only ever affects the database. The importer has a stage (at the end) that writes updates from the database to the files. You can also do `write()` to do that immediately, but you wouldn’t want to do that in an import stage.

Well-behaved plugins already sort of do a version of what you’re proposing. They just do `store()` and rely on the importer pipeline to `write()` just once at the very end. If a plugin doesn’t do that, it should probably be fixed.

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 21, 2020, 6:56pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/5 "2020-05-21T18:56:05Z")

</div>

I ran an import without my new par2 plugin with verbose. after\_write is fired twice for each file (once after the file is moved and once for embedart plugin. Then, the album\_imported event is fired and my script runs again.  
How can I narrow those down even with a plugin without knowing if an import is happening?  
The only thing I seem to be able to do right now is only listen for the after\_write event to only have my script run twice for every file instead of 3 times.

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 21, 2020, 7:16pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/6 "2020-05-21T19:16:49Z")

</div>

Hard to say exactly—maybe try disabling plugins and enabling them one at a time?

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 21, 2020, 9:31pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/7 "2020-05-21T21:31:06Z")

</div>

In my previous message I mentioned the write is coming from the embedart plugin.

Are there any exposed methods or properties that can be combined logically to determine that an import may be happening? Through ui, or session, or task perhaps?  
What is the easiest way to determine what methods and properties a plugin has access to (like a whole list for ui, session, task, etc)?

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 21, 2020, 10:10pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/8 "2020-05-21T22:10:35Z")

</div>

Oops, sorry, missed that! Yeah, makes sense that embedart would need to write files. 🙂

I don’t think there’s a good way to get a notion of where an event is “coming from,” nor whether or not an import is currently active. I think this is unlikely to work because “is an import currently happening” is not represented in global, mutable state—it’s just like any other function you might call in beets.

As far as what is available to a plugin, the place to look is the source code—for example, look at the list of methods on `ImportTask`, for example:

> <https://github.com/beetbox/beets/blob/469541df2e6863c6c3e2ede3fc32d1984d2739a1/beets/importer.py#L429>

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 21, 2020, 10:35pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/9 "2020-05-21T22:35:28Z")

</div>

I’ve got a crazy idea. What if I disable the listener for after\_write in the function for the import\_started or whatever event. Then, at the end of the function for album\_imported event I set the listener for after\_write again?  
Can you think of any issue that might arise from this?  
Would it even work like that?  
Can I even unregister a listener?

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 22, 2020, 11:21am UTC](https://discourse.beets.io/t/plugin-event-handling/1287/10 "2020-05-22T11:21:28Z")

</div>

Sure! You need not even unregister your listener—you could just set a Boolean flag in your own plugin, and check that flag when you get the `after_write` event (and do nothing if it’s true).

This will probably work, but it will run into issues if there are ever two things happening concurrently in beets. Probably nothing to worry about.

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 22, 2020, 12:56pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/11 "2020-05-22T12:56:23Z")

</div>

That’s good. I thought about doing that, but I was convinced that a new instance of my plugin class would be created each time one of the listeners was called.  
Thank you, I will try this.

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 23, 2020, 2:51pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/12 "2020-05-23T14:51:30Z")

</div>

I have another question. Is it safe to change directory in my plugn? Will it mess up operations from other plugins that come after mine?

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 23, 2020, 3:06pm UTC](https://discourse.beets.io/t/plugin-event-handling/1287/13 "2020-05-23T15:06:36Z")

</div>

I don’t know—most things in beets use absolute paths, so _maybe_ it would be fine? But if I were you, I’d avoid it if possible because there’s so much other code running that it’s usually a good idea not to change global, mutable state (like the cwd).

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 25, 2020, 12:07am UTC](https://discourse.beets.io/t/plugin-event-handling/1287/14 "2020-05-25T00:07:12Z")

</div>

I was able to get it working without changing the working directory.  
I am running on macOS and pydoc is not finding beets.ui. I installed from git with python3 setup.py install. Did I need some options to make it install the docs? How do I get them?

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 25, 2020, 12:24am UTC](https://discourse.beets.io/t/plugin-event-handling/1287/15 "2020-05-25T00:24:45Z")

</div>

That’s mysterious. My understanding of pydoc is that it just uses Python modules themselves and looks up their docstrings—there should be nothing else to install, so long as you can `import` a given module successfully.

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 25, 2020, 12:25am UTC](https://discourse.beets.io/t/plugin-event-handling/1287/16 "2020-05-25T00:25:57Z")

</div>

OK, got pydoc working.

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 25, 2020, 12:47am UTC](https://discourse.beets.io/t/plugin-event-handling/1287/17 "2020-05-25T00:47:51Z")

</div>

What is the correct way to send an irrecoverable error up so that the plugin does not load?

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 27, 2020, 1:00am UTC](https://discourse.beets.io/t/plugin-event-handling/1287/18 "2020-05-27T01:00:48Z")

</div>

Setting a variable to prevent the after\_write event funtion from running did not work. It must create a new instance of the plugin class each time a trigger is called like I thought. Any other ideas?  
Would it be possible to add a function to de-register a listener?

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 27, 2020, 1:53am UTC](https://discourse.beets.io/t/plugin-event-handling/1287/19 "2020-05-27T01:53:34Z")

</div>

Hmm; it definitely doesn’t do that… it’s the same plugin instance across events. Maybe something else went wrong?

---

<div class="post-metadata">

**Author:** ![phirestalker](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@phirestalker](https://discourse.beets.io/u/phirestalker)\
**Post date:** [May 27, 2020, 1:55am UTC](https://discourse.beets.io/t/plugin-event-handling/1287/20 "2020-05-27T01:55:47Z")

</div>

I erroneously tried to add an import stage, but that makes the par2 files in the source directory. I am going to try it again soon to be sure I got it right.

Also, I would like to check for existence of an executable file in the init function. If the file is not found I want it to raise a configuration error, preventing the plugin from loading. How can I do this?

[Next page](https://discourse.beets.io/t/plugin-event-handling/1287.md?page=2)
