Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #57 from HairyFotr/patch-settings
Browse files Browse the repository at this point in the history
Refactor settings
  • Loading branch information
anze3db authored Sep 6, 2016
2 parents 6f4f6d1 + db411ee commit 9a5ffb9
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
src/error_log
error_log
tests/.cache/*
venv/*
*.pyc
*~
Expand Down
17 changes: 16 additions & 1 deletion requirements-analysis.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
pycodestyle
pydocstyle

pep8
pyflakes
pep257

pyflakes
bashate
bandit

flake8
mastool
hacking
flake8-commas
flake8-coding
flake8-debugger
flake8-pep3101
flake8-string-format
flake8-comprehensions
flake8-tuple
flake8-deprecated
flake8-builtins
flake8-mutable
12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
requests==2.10.0
Pillow==3.3.0
requests==2.11.1
Pillow==3.3.1
tweepy==3.5.0
gdata==2.0.18
pytest==2.9.2
pytest==3.0.1
mock==2.0.0
lxml==3.6.0
coverage==4.1
lxml==3.6.4
coverage==4.2
coveralls==1.1
Pafy==0.5.1
Pafy==0.5.2
8 changes: 6 additions & 2 deletions run_analysis
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/bin/sh
export PYTHONPATH=src/:$PYTHONPATH
. venv/bin/activate
pip install -r requirements-analysis.txt -U
pip install -r requirements-analysis.txt -U --pre

pycodestyle --ignore=W503 src tests
pep8 --ignore=W503 src tests

pydocstyle --ignore=D100,D101,D102,D103,D104,D200,D205,D211,D400 src tests
pep257 --ignore=D100,D101,D102,D103,D104,D200,D205,D211,D400 src tests

pyflakes src tests
flake8 --ignore=W503,H101,H301,H306,H404,H405,M005 src tests
flake8 --ignore=W503,H101,H301,H306,H404,H405,M005,C101,C103 src tests
bandit -r src
bashate run run_daemon run_analysis run_tests setup_venv
38 changes: 17 additions & 21 deletions src/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@

class Bot(asynchat.async_chat):

def __init__(self, debug=True):
def __init__(self):
asynchat.async_chat.__init__(self)
self.known_users = {}
self.buffer = ''
self.set_terminator('\r\n')
self.nicks = settings.BOT_NICKS
self.nick_num = 0
self.nick = self.nicks[0]
self.realname = settings.BOT_NAME
self.channels = settings.CHANNELS
self.ident = 'botko'
self.debug = debug
self.nick = settings.NICKS[self.nick_num]
self.logic = logic.BotLogic(self)
self.ac_in_buffer_size = self.ac_out_buffer_size = 8192 # 2*default
self.uptime = time()
self.start_time = time()

def print_debug(self, text):
if settings.DEBUG:
print(text)

def write(self, text):
if self.debug:
print('> %s' % text)
self.print_debug('> %s' % text)
self.push(text + '\r\n')

def say(self, text, channel):
Expand All @@ -44,16 +42,17 @@ def say(self, text, channel):
return line

def set_nick(self):
self.nick = self.nicks[self.nick_num]
self.nick = settings.NICKS[self.nick_num]
self.write('NICK %s' % self.nick)

def next_nick(self):
self.nick_num = (self.nick_num + 1) % len(self.nicks)
self.nick_num = (self.nick_num + 1) % len(settings.NICKS)
self.set_nick()

def handle_connect(self):
self.set_nick()
self.write('USER %s iw 0 :%s' % (self.ident, self.realname))
self.write('USER %s iw 0 :%s' %
(settings.IDENT, settings.REAL_NAME))

def collect_incoming_data(self, data):
self.buffer += data
Expand All @@ -62,30 +61,27 @@ def log_error(self, text):
with open("error.log", "a") as f:
f.write(str(datetime.now()) + '\n')
f.write('Error: ' + text + '\n')
if self.debug:
print(text)
self.print_debug(text)

from traceback import format_exception
from sys import exc_info
ex_type, ex_val, ex_tb = exc_info()
ex_text = ''.join(format_exception(ex_type, ex_val, ex_tb, 10))
f.write(ex_text + '\n')
if self.debug:
print(ex_text)
self.print_debug(ex_text)

def found_terminator(self):
line = self.buffer
self.buffer = ''
if self.debug:
print("< " + line)
self.print_debug("< " + line)
self.logic.new_input(line)

def run(self, host, port):
def run(self):
def handler(signal, frame):
pass

self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))
self.connect((settings.IRC_SERVER, settings.IRC_PORT))

random.seed()
# set the signal handler for shouting random messages
Expand Down
4 changes: 3 additions & 1 deletion src/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from plugins.psywerx_groups import PsywerxGroups
from plugins.psywerx_karma import PsywerxKarma

import settings


class BotLogic(object):

Expand Down Expand Up @@ -64,7 +66,7 @@ def self_input(self, channel, msg, line):

def handle_end_motd(self, line):
# after server MOTD, join desired channel
for channel in self.bot.channels:
for channel in settings.CHANNELS:
self.bot.known_users[channel] = {}
self.bot.write('JOIN ' + channel)

Expand Down
14 changes: 8 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@


def run_bot():
import settings
from handler import Bot

while True:
try:
# initialize
# initialize and run
botko = Bot()
# and run
botko.run(settings.IRC_SERVER, settings.IRC_PORT)
botko.run()

except Exception:
# log the error
Expand All @@ -30,6 +28,10 @@ def run_bot():
sleep(10)


def print_usage():
print("usage: %s start|stop|restart|nodaemon" % sys.argv[0])


class BotkoDaemon(Daemon):

def run(self):
Expand All @@ -47,9 +49,9 @@ def run(self):
elif sys.argv[1] == 'nodaemon':
run_bot()
else:
print("Unknown command")
print_usage()
sys.exit(2)
sys.exit(0)
else:
print("usage: %s start|stop|restart" % sys.argv[0])
print_usage()
sys.exit(2)
3 changes: 2 additions & 1 deletion src/plugins/psywerx_groups.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from base import PsywerxPlugin
import settings
import re
import json

Expand Down Expand Up @@ -67,7 +68,7 @@ def _handle_mentions(self, channel, nick, msg):
if group[-1] == "'":
group = group[:-1]

if nick.lower() == '_haibot_':
if re.match(settings.BOTS_REGEX, nick):
continue

params = {'group': group}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _uptime(self, tokens, channel):
self.bot.log_error('Could not get server uptime.')
server_uptime = "unknown"

bot_uptime_seconds = int(time() - self.bot.uptime)
bot_uptime_seconds = int(time() - self.bot.start_time)
bot_uptime = str(timedelta(seconds=bot_uptime_seconds))
self.bot.say("My uptime: %s, server uptime: %s"
% (bot_uptime, server_uptime),
Expand Down
18 changes: 9 additions & 9 deletions src/settings.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Created on Oct 9, 2011"""
TOKEN = '16edde56d1801c65ec96a4d607a67d89'
SERVER_URL = 'http://localhost:8000/'
BOT_NICKS = ['botko', 'botko_', '_botko_', 'botko__']
BOT_NAME = 'botko'
CHANNELS = ['#smotko-testing', '#smotko-testing2']
NICKS = ['botko', 'botko_', '_botko_', 'botko__']
REAL_NAME = 'botko'
IDENT = 'botko'
CHANNELS = ['#botko-testing', '#botko-testing2']
IRC_SERVER = 'irc.freenode.org'
IRC_PORT = 6667
COOKIEZ_URL = 'http://www.fortunefortoday.com/getfortuneonly.php'
SIMON_USERS = ['smotko', 'edofic']

DEBUG = True

BOTS_REGEX = '_.+_'

PSYWERX = {
'TOKEN': '16edde56d1801c65ec96a4d607a67d89',
Expand All @@ -18,5 +18,5 @@
'consumer_key': '',
'consumer_secret': '',
'access_token_key': '',
'access_token_secret': ''
'access_token_secret': '',
}
2 changes: 1 addition & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def setUp(self):
self.bot.known_users = {"#psywerx": {"smotko": "smotko",
"test1": "test1",
"test2": "test2"}}
self.bot.uptime = time() - 100
self.bot.start_time = time() - 100

self.fixtures_dir = os.path.join(BASE_DIR, 'fixtures/')

Expand Down

0 comments on commit 9a5ffb9

Please sign in to comment.