From 6a9f284a8aad59392d697399f8f25ef4cc5d33d0 Mon Sep 17 00:00:00 2001 From: Alex Parella Date: Thu, 20 Nov 2014 23:46:51 -0800 Subject: [PATCH] Allow for reading in of settings from external configuration file --- bot.py | 27 +++++++++++++++++++++++++-- settings.ini | 4 ++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 settings.ini diff --git a/bot.py b/bot.py index 0d8d5ea..e8f7254 100644 --- a/bot.py +++ b/bot.py @@ -3,11 +3,12 @@ # Import some necessary libraries. import socket, sys, time, csv, Queue, random, re, pdb, select from threading import Thread +import ConfigParser # Some basic variables used to configure the bot. server = "irc.freenode.net" -channel = "#openhatch" # Please use #openhatch-bots rather than #openhatch for testing -botnick = "WelcomeBot" +channel = "#openhatch-bots" # Please use #openhatch-bots rather than #openhatch for testing +botnick = "ParserWelcomeBot" channel_greeters = ['shauna', 'paulproteus', 'marktraceur'] hello_list = [r'hello', r'hi', r'hey', r'yo', r'sup'] help_list = [r'help', r'info', r'faq', r'explain yourself'] @@ -58,6 +59,27 @@ class NewComer(object): ### Startup Functions ### ######################### +# Parse configuration file and load settings +def parsesettings(): + parser = ConfigParser.ConfigParser() + parser.read("settings.ini") + settings = {} + for section in parser.sections(): + for option in parser.options(section): + settings[option] = parser.get(section, option) + + # Needs to be a cleaner way to do this + global channel + global botnick + global server + global channel_greeter + global hello_list + global help_lis + + channel = settings["channel"] + botnick = settings["botnick"] + server = settings["server"] + # Creates a socket that will be used to send and receive messages, # then connects the socket to an IRC server and joins the channel. def irc_start(): # pragma: no cover (this excludes this function from testing) @@ -223,6 +245,7 @@ def pong(ircsock): ########################## def main(): + parsesettings() ircsock = irc_start() join_irc(ircsock) WelcomeBot = Bot() diff --git a/settings.ini b/settings.ini new file mode 100644 index 0000000..78164e2 --- /dev/null +++ b/settings.ini @@ -0,0 +1,4 @@ +[Configurations] +channel: #openhatch-bots +botnick: welcomebot +server: irc.freenode.net