Download:
child 6:bddc9023ac66
parent 4:4b0145336c46
5:754ae6ae2c54
Anton Shestakov <av6@dwimlabs.net>, Tue, 10 May 2016 17:17:47 +0800
rollbot: ability to specify a config file (YAML) with jid, password and nick

2 файлов изменено, 27 вставок(+), 7 удалений(-) [+]
requirements.txt file | annotate | diff | comparison | revisions
rollbot.py file | annotate | diff | comparison | revisions
--- a/requirements.txt Tue May 10 14:33:06 2016 +0800
+++ b/requirements.txt Tue May 10 17:17:47 2016 +0800
@@ -1,1 +1,2 @@
+PyYAML==3.11
sleekxmpp==1.3.1
--- a/rollbot.py Tue May 10 14:33:06 2016 +0800
+++ b/rollbot.py Tue May 10 17:17:47 2016 +0800
@@ -1,8 +1,9 @@
#!/usr/bin/env python
import logging
import sys
-from argparse import ArgumentParser
+from argparse import ArgumentParser, FileType, SUPPRESS
+import yaml
import sleekxmpp
@@ -38,12 +39,21 @@
self.plugin['xep_0045'].joinMUC(room, self.nick, password=password, wait=True)
-def main():
- parser = ArgumentParser()
+def lookup(key, args, config, default=None):
+ if hasattr(args, key):
+ return getattr(args, key)
+ return config.get(key, default)
+
- parser.add_argument('-j', '--jid', help='JID to use')
- parser.add_argument('-p', '--password', help='password to use')
- parser.add_argument('-n', '--nick', help='MUC nickname', default='rollbot')
+def main():
+ parser = ArgumentParser(argument_default=SUPPRESS)
+
+ parser.add_argument('-c', '--config', help='configuration file (YAML)', type=FileType('r'))
+
+ group = parser.add_argument_group('main configuration', 'options that can be specified in the configuration file')
+ group.add_argument('-j', '--jid', help='JID to use')
+ group.add_argument('-p', '--password', help='password to use')
+ group.add_argument('-n', '--nick', help='MUC nickname')
group = parser.add_mutually_exclusive_group()
group.add_argument('-q', '--quiet', help='set logging to ERROR', action='store_const', dest='loglevel', const=logging.ERROR, default=logging.INFO)
@@ -53,7 +63,16 @@
logging.basicConfig(level=args.loglevel, format='%(asctime)s %(levelname)-8s %(message)s')
- rollbot = RollBot(args.jid, args.password, args.nick)
+ if hasattr(args, 'config'):
+ config = yaml.safe_load(args.config)
+ else:
+ config = {}
+
+ jid = lookup('jid', args, config)
+ password = lookup('password', args, config)
+ nick = lookup('nick', args, config, default='rollbot')
+
+ rollbot = RollBot(jid, password, nick)
rollbot.auto_authorize = True
rollbot.auto_subscribe = True
rollbot.register_plugin('xep_0030') # Service Discovery