Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistence of objects in plugins #1433

Open
1 of 5 tasks
raspbeguy opened this issue Jul 8, 2020 · 0 comments
Open
1 of 5 tasks

Persistence of objects in plugins #1433

raspbeguy opened this issue Jul 8, 2020 · 0 comments

Comments

@raspbeguy
Copy link
Contributor

raspbeguy commented Jul 8, 2020

I am...

  • Reporting a bug
  • Suggesting a new feature
  • Requesting help with running my bot
  • Requesting help writing plugins
  • Here about something else

I am running...

Issue description

I wrote a soundbox plugin that enables you to play sounds from a given set. Find the full code here https://gist.github.com/raspbeguy/b65e26430af9f6a5a308f7cc1a1556c2

A sound is a small object:

class Sound():
    def __init__(self, filename, aliases):
        self.filename = filename
        self.aliases = aliases
        self.counter = 0

    def play(self, sounds_path):
        playsound("{}/{}".format(sounds_path,self.filename))
        self.counter += 1

    def addAliases(self, aliases):
        self.aliases = list(set(self.aliases + aliases))

    def getAliases(self):
        return self.aliases

    def getFilename(self):
        return self.filename

    def __str__(self):
        return "{}\t{}".format(self.filename, ", ".join(self.aliases))

    def __eq__(self, other):
        if not isinstance(other, Sound):
            return False
        return self.filename == other.getFilename

In the plugin class, I'm trying to use a persistent attribute self['SOUNDS'] to store the list of the sounds.

Here is the function to add a sound:

@botcmd(split_args_with=None)
def soundbox_add(self, msg, args):
    """Add a sound"""
    sound = Sound(args[0],args[1:])
    with self.mutable('SOUNDS') as sounds_list:
        sounds_list.append(sound)
    return "Sound was successfully added."

I also have a function to list the sounds:

@botcmd
def soundbox_list(self, msg, args):
    """List sounds"""
    for sound in self['SOUNDS']:
        yield str(sound)
    if len(self['SOUNDS']) == 0:
        return "No sounds yet"

I can add sounds with no problem, I can list them. But when I restart errbot the sound is sometimes not persisted. I say "sometimes" because I didn't find yet a pattern that makes it persist and sometimes not.

Steps to reproduce

!soundbox list

It shows:

file.mp3   something, stuff

Then add a sound

!soundbox add somefile.mp3 alias1 alias2

You can then see that is was successful by typing:

 !soundbox list

It shows:

file.mp3   something, stuff
somefile.mp3 alias1, alias2

Then restart errbot:

systemctl restart errbot

Print the list again:

 !soundbox list

It shows:

file.mp3   something, stuff

The sound that we added is gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants