aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.org17
-rwxr-xr-xbin/dbolla14
-rw-r--r--warmachine/addons/base.py1
-rw-r--r--warmachine/addons/standup.py15
-rw-r--r--warmachine/connections/base.py3
-rw-r--r--warmachine/connections/irc.py1
6 files changed, 39 insertions, 12 deletions
diff --git a/README.org b/README.org
index 4d8bf43..b317e13 100644
--- a/README.org
+++ b/README.org
@@ -30,6 +30,8 @@ you must call ~super()~.
30** ~self._loop~ 30** ~self._loop~
31This is given to you access to the asyncio loop. This is useful for scheduling 31This is given to you access to the asyncio loop. This is useful for scheduling
32tasks to be run later. 32tasks to be run later.
33** ~self.config_dir~
34The directory to store any configuration files and other junk in.
33** ~self.log~ 35** ~self.log~
34This is a logger that you should use when logging messages. 36This is a logger that you should use when logging messages.
35** ~recv_msg(channel, message)~ 37** ~recv_msg(channel, message)~
@@ -76,18 +78,21 @@ you may want to do such as create configuration files that don't exist yet.
76* Writing a Connection 78* Writing a Connection
77To write a new connection protocol you must inherit from 79To write a new connection protocol you must inherit from
78~warmachine.connections.base.Connection~. This class defines an interface you 80~warmachine.connections.base.Connection~. This class defines an interface you
79must implement to support the plugins. 81must implement to support the plugins. If you override ~__init__~ you must call
82~super()~.
80** ~__config_prefix__~ 83** ~__config_prefix__~
81This global is used to decide which connection to use when it is found in the 84This global is used to decide which connection to use when it is found in the
82config file. E.g. IRC uses ~'irc'~ and Slack uses ~'slack'~. It should be 85config file. E.g. IRC uses ~'irc'~ and Slack uses ~'slack'~. It should be
83defined somewhere near the top of your file. 86defined somewhere near the top of your file.
84** ~connect()~ 87** ~self.config_dir~
88The directory to store any configuration files and other junk in.
89** ~self.connect()~
85D'bolla uses python 3's asyncio to manage multiple connections concurrently thus 90D'bolla uses python 3's asyncio to manage multiple connections concurrently thus
86you should use ~asyncio.open_connection~ to create your connection. Once you 91you should use ~asyncio.open_connection~ to create your connection. Once you
87have successfully connected you must set ~self.status~ to 92have successfully connected you must set ~self.status~ to
88~warmachine.connections.base.CONNECTED~. This indicates the connection is ready 93~warmachine.connections.base.CONNECTED~. This indicates the connection is ready
89to use. 94to use.
90** ~read()~ 95** ~self.read()~
91This method is constantly checked in a loop by the ~Bot~ class. When a message 96This method is constantly checked in a loop by the ~Bot~ class. When a message
92is returned it is passed into the ~recv_msg~ method in all loaded plugins. This 97is returned it is passed into the ~recv_msg~ method in all loaded plugins. This
93return value should be formatted in the following format: 98return value should be formatted in the following format:
@@ -99,9 +104,9 @@ return value should be formatted in the following format:
99 'message': 'The message that was received', 104 'message': 'The message that was received',
100} 105}
101#+END_SRC 106#+END_SRC
102** ~say(message, destination)~ 107** ~self.say(message, destination)~
103This method is used by plugins to send a message to a channel or user. 108This method is used by plugins to send a message to a channel or user.
104** ~id~ 109** ~self.id~
105This should return a unique id used to identify this particular connection. This 110This should return a unique id used to identify this particular connection. This
106is used by plugins when saving state. As an example, the IRC connection uses 111is used by plugins when saving state. As an example, the IRC connection uses
107something like this: 112something like this:
@@ -115,6 +120,6 @@ def id(self):
115 value = '{}-{}'.format(self.host, self.nick) 120 value = '{}-{}'.format(self.host, self.nick)
116 return md5(value.encode()).hexdigest() 121 return md5(value.encode()).hexdigest()
117#+END_SRC 122#+END_SRC
118** ~get_users_by_channel(channel)~ 123** ~self.get_users_by_channel(channel)~
119This method should return a list of all users (including the bot) for the 124This method should return a list of all users (including the bot) for the
120connection. 125connection.
diff --git a/bin/dbolla b/bin/dbolla
index e00143b..5bef051 100755
--- a/bin/dbolla
+++ b/bin/dbolla
@@ -43,6 +43,13 @@ log_config = {
43class Bot(object): 43class Bot(object):
44 def __init__(self, settings): 44 def __init__(self, settings):
45 self.log = logging.getLogger(self.__class__.__name__) 45 self.log = logging.getLogger(self.__class__.__name__)
46
47 self.config_dir = os.path.expanduser('~/.warmachine')
48 if not os.path.exists(self.config_dir):
49 self.log.info('Creating config directory: {}'.format(
50 self.config_dir))
51 os.makedirs(self.config_dir)
52
46 self._loop = asyncio.get_event_loop() 53 self._loop = asyncio.get_event_loop()
47 54
48 self.settings = settings 55 self.settings = settings
@@ -55,8 +62,6 @@ class Bot(object):
55 self.load_plugin('warmachine.addons.giphy.GiphySearch') 62 self.load_plugin('warmachine.addons.giphy.GiphySearch')
56 self.load_plugin('warmachine.addons.standup.StandUpPlugin') 63 self.load_plugin('warmachine.addons.standup.StandUpPlugin')
57 64
58 # TODO: Ensure the config directory has been created
59
60 def start(self): 65 def start(self):
61 for connection in self.connections: 66 for connection in self.connections:
62 t = asyncio.ensure_future(connection.connect()) 67 t = asyncio.ensure_future(connection.connect())
@@ -65,6 +70,7 @@ class Bot(object):
65 self._loop.run_forever() 70 self._loop.run_forever()
66 71
67 def add_connection(self, connection): 72 def add_connection(self, connection):
73 connection.config_dir = self.config_dir
68 self.connections[connection] = {} 74 self.connections[connection] = {}
69 75
70 def on_connect(self, connection, task): 76 def on_connect(self, connection, task):
@@ -104,9 +110,9 @@ class Bot(object):
104 mod = import_module(mod_path) 110 mod = import_module(mod_path)
105 111
106 if hasattr(mod, cls_name): 112 if hasattr(mod, cls_name):
107 cls = getattr(mod, cls_name)() 113 obj = getattr(mod, cls_name)(config_dir=self.config_dir)
108 114
109 self.loaded_plugins.append(cls) 115 self.loaded_plugins.append(obj)
110 116
111 def reload_plugin(self, path): 117 def reload_plugin(self, path):
112 """ 118 """
diff --git a/warmachine/addons/base.py b/warmachine/addons/base.py
index 0a02df7..d67500f 100644
--- a/warmachine/addons/base.py
+++ b/warmachine/addons/base.py
@@ -6,6 +6,7 @@ class WarMachinePlugin(object):
6 def __init__(self, *args, **kwargs): 6 def __init__(self, *args, **kwargs):
7 self._loop = asyncio.get_event_loop() 7 self._loop = asyncio.get_event_loop()
8 self.log = logging.getLogger(self.__class__.__name__) 8 self.log = logging.getLogger(self.__class__.__name__)
9 self.config_dir = kwargs.pop('config_dir', None)
9 10
10 def recv_msg(self, *args, **kwargs): 11 def recv_msg(self, *args, **kwargs):
11 """ 12 """
diff --git a/warmachine/addons/standup.py b/warmachine/addons/standup.py
index 6ee6378..aa7fda6 100644
--- a/warmachine/addons/standup.py
+++ b/warmachine/addons/standup.py
@@ -2,6 +2,7 @@ import asyncio
2from datetime import datetime, timedelta 2from datetime import datetime, timedelta
3import functools 3import functools
4import json 4import json
5import os
5from pprint import pformat 6from pprint import pformat
6 7
7from .base import WarMachinePlugin 8from .base import WarMachinePlugin
@@ -20,6 +21,7 @@ class StandUpPlugin(WarMachinePlugin):
20 !standup-schedules 21 !standup-schedules
21 !standup-waiting_replies 22 !standup-waiting_replies
22 """ 23 """
24 SETTINGS_FILENAME = 'standup_schedules.json'
23 def __init__(self, *args, **kwargs): 25 def __init__(self, *args, **kwargs):
24 super().__init__(*args, **kwargs) 26 super().__init__(*args, **kwargs)
25 27
@@ -38,6 +40,15 @@ class StandUpPlugin(WarMachinePlugin):
38 self.users_awaiting_reply = {} 40 self.users_awaiting_reply = {}
39 self.log.info('Loaded standup plugin') 41 self.log.info('Loaded standup plugin')
40 42
43 self.settings_file = os.path.join(
44 self.config_dir, self.SETTINGS_FILENAME)
45
46 if not os.path.exists(self.settings_file):
47 self.log.info('Creating standup config file: {}'.format(
48 self.settings_file))
49 with open(self.settings_file, 'w') as f:
50 f.write('{}')
51
41 def on_connect(self, connection): 52 def on_connect(self, connection):
42 self.load_schedule(connection) 53 self.load_schedule(connection)
43 54
@@ -416,7 +427,7 @@ class StandUpPlugin(WarMachinePlugin):
416 data[channel][key] = self.standup_schedules[channel][key] 427 data[channel][key] = self.standup_schedules[channel][key]
417 428
418 data = {connection.id: data} 429 data = {connection.id: data}
419 with open('/home/jason/.warmachine/standup_schedules.json', 'w') as f: 430 with open(self.settings_file, 'w') as f:
420 f.write(json.dumps(data)) 431 f.write(json.dumps(data))
421 432
422 self.log.info('Schedules saved to disk') 433 self.log.info('Schedules saved to disk')
@@ -425,7 +436,7 @@ class StandUpPlugin(WarMachinePlugin):
425 """ 436 """
426 Load the channel schedules from a file. 437 Load the channel schedules from a file.
427 """ 438 """
428 with open('/home/jason/.warmachine/standup_schedules.json', 'r') as f: 439 with open(self.settings_file, 'r') as f:
429 try: 440 try:
430 data = json.loads(f.read()) 441 data = json.loads(f.read())
431 except Exception as e: 442 except Exception as e:
diff --git a/warmachine/connections/base.py b/warmachine/connections/base.py
index e787e35..56ce108 100644
--- a/warmachine/connections/base.py
+++ b/warmachine/connections/base.py
@@ -3,6 +3,9 @@ CONNECTED = 'Connected'
3 3
4 4
5class Connection(object): 5class Connection(object):
6 def __init__(self):
7 self.config_dir = None
8
6 def connect(self, *args, **kwargs): 9 def connect(self, *args, **kwargs):
7 """ 10 """
8 This is called by the main start method. It should prepare your 11 This is called by the main start method. It should prepare your
diff --git a/warmachine/connections/irc.py b/warmachine/connections/irc.py
index 86068d7..542ba62 100644
--- a/warmachine/connections/irc.py
+++ b/warmachine/connections/irc.py
@@ -7,6 +7,7 @@ from ..utils.decorators import memoize
7 7
8class AioIRC(Connection): 8class AioIRC(Connection):
9 def __init__(self, host, port): 9 def __init__(self, host, port):
10 super().__init__()
10 self._loop = asyncio.get_event_loop() 11 self._loop = asyncio.get_event_loop()
11 self.log = logging.getLogger(self.__class__.__name__) 12 self.log = logging.getLogger(self.__class__.__name__)
12 13