This repository has been archived by the owner on Aug 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add the concept of storage backends, not fully fleshed out at this point, but a good starting point - Add a generic serializer - Move mention_nick to the GuildMember object (I'm not sure this was a good idea, but we'll see) - Add a default config loader to the bot - Fix some Python 2.x/3.x unicode stuff - Start tracking greenlets on the Plugin level, this will help with reloading when its fully completed - Fix manhole locals being basically empty (sans the bot if relevant) - Add Channel.delete_messages_bulk - Add GuildMember.owner to check if the member owns the server
- Loading branch information
1 parent
e3140b6
commit 7d53702
Showing
17 changed files
with
310 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from disco.bot.bot import Bot, BotConfig | ||
from disco.bot.plugin import Plugin | ||
from disco.util.config import Config | ||
|
||
__all__ = ['Bot', 'BotConfig', 'Plugin'] | ||
__all__ = ['Bot', 'BotConfig', 'Plugin', 'Config'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from .memory import MemoryBackend | ||
from .disk import DiskBackend | ||
|
||
|
||
BACKENDS = { | ||
'memory': MemoryBackend, | ||
'disk': DiskBackend, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
class BaseStorageBackend(object): | ||
def base(self): | ||
return self.storage | ||
|
||
def __getitem__(self, key): | ||
return self.storage[key] | ||
|
||
def __setitem__(self, key, value): | ||
self.storage[key] = value | ||
|
||
def __delitem__(self, key): | ||
del self.storage[key] | ||
|
||
|
||
class StorageDict(dict): | ||
def ensure(self, name): | ||
if not dict.__contains__(self, name): | ||
dict.__setitem__(self, name, StorageDict()) | ||
return dict.__getitem__(self, name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os | ||
|
||
from .base import BaseStorageBackend, StorageDict | ||
|
||
|
||
class DiskBackend(BaseStorageBackend): | ||
def __init__(self, config): | ||
self.format = config.get('format', 'json') | ||
self.path = config.get('path', 'storage') + '.' + self.format | ||
self.storage = StorageDict() | ||
|
||
@staticmethod | ||
def get_format_functions(fmt): | ||
if fmt == 'json': | ||
from json import loads, dumps | ||
return (loads, dumps) | ||
elif fmt == 'yaml': | ||
from pyyaml import load, dump | ||
return (load, dump) | ||
raise Exception('Unsupported format type {}'.format(fmt)) | ||
|
||
def load(self): | ||
if not os.path.exists(self.path): | ||
return | ||
|
||
decode, _ = self.get_format_functions(self.format) | ||
|
||
with open(self.path, 'r') as f: | ||
self.storage = decode(f.read()) | ||
|
||
def dump(self): | ||
_, encode = self.get_format_functions(self.format) | ||
|
||
with open(self.path, 'w') as f: | ||
f.write(encode(self.storage)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from .base import BaseStorageBackend, StorageDict | ||
|
||
|
||
class MemoryBackend(BaseStorageBackend): | ||
def __init__(self): | ||
self.storage = StorageDict() | ||
|
||
def base(self): | ||
return self.storage | ||
|
||
def __getitem__(self, key): | ||
return self.storage[key] | ||
|
||
def __setitem__(self, key, value): | ||
self.storage[key] = value | ||
|
||
def __delitem__(self, key): | ||
del self.storage[key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from .backends import BACKENDS | ||
|
||
|
||
class Storage(object): | ||
def __init__(self, ctx, config): | ||
self.ctx = ctx | ||
self.backend = BACKENDS[config.backend] | ||
# TODO: autosave | ||
# config.autosave config.autosave_interval | ||
|
||
@property | ||
def guild(self): | ||
return self.backend.base().ensure('guilds').ensure(self.ctx['guild'].id) | ||
|
||
@property | ||
def channel(self): | ||
return self.backend.base().ensure('channels').ensure(self.ctx['channel'].id) | ||
|
||
@property | ||
def user(self): | ||
return self.backend.base().ensure('users').ensure(self.ctx['user'].id) |
Oops, something went wrong.