diff options
| author | jason | 2016-08-18 19:30:35 -0600 |
|---|---|---|
| committer | jason | 2016-08-18 19:30:35 -0600 |
| commit | 4518daba36678f52330513ec1fec82a44340fa81 (patch) | |
| tree | f5a95fa686516f1898c7ec66b44ad2ad583bbc95 | |
| parent | a427cd070d2246264e555b3d4779ebde8acd2e08 (diff) | |
| download | warmachine-ng-4518daba36678f52330513ec1fec82a44340fa81.tar.gz warmachine-ng-4518daba36678f52330513ec1fec82a44340fa81.zip | |
Remove the hard-coded config file path
- added a dynamic config file path
| -rw-r--r-- | README.org | 17 | ||||
| -rwxr-xr-x | bin/dbolla | 14 | ||||
| -rw-r--r-- | warmachine/addons/base.py | 1 | ||||
| -rw-r--r-- | warmachine/addons/standup.py | 15 | ||||
| -rw-r--r-- | warmachine/connections/base.py | 3 | ||||
| -rw-r--r-- | warmachine/connections/irc.py | 1 |
6 files changed, 39 insertions, 12 deletions
| @@ -30,6 +30,8 @@ you must call ~super()~. | |||
| 30 | ** ~self._loop~ | 30 | ** ~self._loop~ |
| 31 | This is given to you access to the asyncio loop. This is useful for scheduling | 31 | This is given to you access to the asyncio loop. This is useful for scheduling |
| 32 | tasks to be run later. | 32 | tasks to be run later. |
| 33 | ** ~self.config_dir~ | ||
| 34 | The directory to store any configuration files and other junk in. | ||
| 33 | ** ~self.log~ | 35 | ** ~self.log~ |
| 34 | This is a logger that you should use when logging messages. | 36 | This 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 |
| 77 | To write a new connection protocol you must inherit from | 79 | To 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 |
| 79 | must implement to support the plugins. | 81 | must implement to support the plugins. If you override ~__init__~ you must call |
| 82 | ~super()~. | ||
| 80 | ** ~__config_prefix__~ | 83 | ** ~__config_prefix__~ |
| 81 | This global is used to decide which connection to use when it is found in the | 84 | This global is used to decide which connection to use when it is found in the |
| 82 | config file. E.g. IRC uses ~'irc'~ and Slack uses ~'slack'~. It should be | 85 | config file. E.g. IRC uses ~'irc'~ and Slack uses ~'slack'~. It should be |
| 83 | defined somewhere near the top of your file. | 86 | defined somewhere near the top of your file. |
| 84 | ** ~connect()~ | 87 | ** ~self.config_dir~ |
| 88 | The directory to store any configuration files and other junk in. | ||
| 89 | ** ~self.connect()~ | ||
| 85 | D'bolla uses python 3's asyncio to manage multiple connections concurrently thus | 90 | D'bolla uses python 3's asyncio to manage multiple connections concurrently thus |
| 86 | you should use ~asyncio.open_connection~ to create your connection. Once you | 91 | you should use ~asyncio.open_connection~ to create your connection. Once you |
| 87 | have successfully connected you must set ~self.status~ to | 92 | have 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 |
| 89 | to use. | 94 | to use. |
| 90 | ** ~read()~ | 95 | ** ~self.read()~ |
| 91 | This method is constantly checked in a loop by the ~Bot~ class. When a message | 96 | This method is constantly checked in a loop by the ~Bot~ class. When a message |
| 92 | is returned it is passed into the ~recv_msg~ method in all loaded plugins. This | 97 | is returned it is passed into the ~recv_msg~ method in all loaded plugins. This |
| 93 | return value should be formatted in the following format: | 98 | return 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)~ |
| 103 | This method is used by plugins to send a message to a channel or user. | 108 | This method is used by plugins to send a message to a channel or user. |
| 104 | ** ~id~ | 109 | ** ~self.id~ |
| 105 | This should return a unique id used to identify this particular connection. This | 110 | This should return a unique id used to identify this particular connection. This |
| 106 | is used by plugins when saving state. As an example, the IRC connection uses | 111 | is used by plugins when saving state. As an example, the IRC connection uses |
| 107 | something like this: | 112 | something 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)~ |
| 119 | This method should return a list of all users (including the bot) for the | 124 | This method should return a list of all users (including the bot) for the |
| 120 | connection. | 125 | connection. |
| @@ -43,6 +43,13 @@ log_config = { | |||
| 43 | class Bot(object): | 43 | class 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 | |||
| 2 | from datetime import datetime, timedelta | 2 | from datetime import datetime, timedelta |
| 3 | import functools | 3 | import functools |
| 4 | import json | 4 | import json |
| 5 | import os | ||
| 5 | from pprint import pformat | 6 | from pprint import pformat |
| 6 | 7 | ||
| 7 | from .base import WarMachinePlugin | 8 | from .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 | ||
| 5 | class Connection(object): | 5 | class 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 | ||
| 8 | class AioIRC(Connection): | 8 | class 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 | ||