소스 검색

Move config to a JSON file

Move config to a JSON file in order to allow for complex
configuration of the ethercalc data model.

Change-Id: I0d18bc6aa3fd8ea1739c64a3d488145435bebb4f
changes/54/478954/1
Thierry Carrez 2 년 전
부모
커밋
39e17d012c
5개의 변경된 파일42개의 추가작업 그리고 52개의 파일을 삭제
  1. +11
    -15
      README.rst
  2. +0
    -11
      config.ini.sample
  3. +10
    -0
      config.json.sample
  4. +20
    -25
      ptgbot/bot.py
  5. +1
    -1
      ptgbot/db.py

+ 11
- 15
README.rst 파일 보기

@@ -34,27 +34,23 @@ Example::
Testing
=======

Copy config.ini.sample to config.ini::
Copy config.json.sample to config.json::

cp config.ini.sample config.ini
cp config.json.sample config.json

Edit config.ini contents, for example::
Edit config.json contents, for example::

[ircbot]
nick=ptgbot
pass=
server=irc.freenode.net
port=6667
channels=testptg

[db]
filename=html/ptg.json
ethercalc=
cells=
{
"irc_nick": "ptgbot",
"irc_server": "irc.freenode.net",
"irc_port": 6667,
"irc_channel": "#testptg",
"db_filename": "html/ptg.json",
}

In one terminal, run the bot::

tox -evenv -- ptgbot -d config.ini
tox -evenv -- ptgbot -d config.json

Join that channel and give a command to the bot::



+ 0
- 11
config.ini.sample 파일 보기

@@ -1,11 +0,0 @@
[ircbot]
nick=NICKNAME
pass=PASSWORD
server=irc.freenode.net
port=6667
channels=foo,bar

[db]
filename=html/ptg.json
ethercalc=https://ethercalc.openstack.org/_/MyDocumentName/cells
cells=E9 E10

+ 10
- 0
config.json.sample 파일 보기

@@ -0,0 +1,10 @@
{
"irc_nick": "NICK",
"irc_pass": "PASS",
"irc_server": "irc.freenode.net",
"irc_port": 6667,
"irc_channel": "#mychannel",
"db_filename": "html/ptg.json",
"ethercalc_url": "https://ethercalc.openstack.org/_/YOURDOC/cells",
"ethercalc_cells": ["E9", "E10", "E11"]
}

+ 20
- 25
ptgbot/bot.py 파일 보기

@@ -16,9 +16,9 @@
# limitations under the License.

import argparse
import configparser
import daemon
import irc.bot
import json
import logging.config
import os
import time
@@ -43,7 +43,7 @@ ANTI_FLOOD_SLEEP = 2
class PTGBot(irc.bot.SingleServerIRCBot):
log = logging.getLogger("ptgbot.bot")

def __init__(self, nickname, password, server, port, channels, db):
def __init__(self, nickname, password, server, port, channel, db):
if port == 6697:
factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
irc.bot.SingleServerIRCBot.__init__(self,
@@ -56,7 +56,7 @@ class PTGBot(irc.bot.SingleServerIRCBot):
nickname, nickname)
self.nickname = nickname
self.password = password
self.channel_list = channels
self.channel = channel
self.identify_msg_cap = False
self.data = db

@@ -77,10 +77,9 @@ class PTGBot(irc.bot.SingleServerIRCBot):
if (self.password):
self.log.debug("Identifying to nickserv")
c.privmsg("nickserv", "identify %s " % self.password)
for channel in self.channel_list:
self.log.info("Joining %s" % channel)
c.join(channel)
time.sleep(ANTI_FLOOD_SLEEP)
self.log.info("Joining %s" % self.channel)
c.join(self.channel)
time.sleep(ANTI_FLOOD_SLEEP)

def on_cap(self, c, e):
self.log.debug("Received cap response %s" % repr(e.arguments))
@@ -164,11 +163,11 @@ class PTGBot(irc.bot.SingleServerIRCBot):


def start(configpath):
config = configparser.RawConfigParser()
config.read(configpath)
with open(configpath, 'r') as fp:
config = json.load(fp)

if config.has_option('ircbot', 'log_config'):
log_config = config.get('ircbot', 'log_config')
if 'log_config' in config:
log_config = config['log_config']
fp = os.path.expanduser(log_config)
if not os.path.exists(fp):
raise Exception("Unable to read logging config file at %s" % fp)
@@ -176,27 +175,23 @@ def start(configpath):
else:
logging.basicConfig(level=logging.DEBUG)

channels = ['#' + name.strip() for name in
config.get('ircbot', 'channels').split(',')]

db = ptgbot.db.PTGDataBase(
config.get('db', 'filename'),
config.get('db', 'ethercalc'),
config.get('db', 'cells'))
bot = PTGBot(config.get('ircbot', 'nick'),
config.get('ircbot', 'pass'),
config.get('ircbot', 'server'),
config.getint('ircbot', 'port'),
channels,
config['db_filename'],
config.get('ethercalc_url'),
config.get('ethercalc_cells'))
bot = PTGBot(config['irc_nick'],
config.get('irc_pass', ''),
config['irc_server'],
config['irc_port'],
config['irc_channel'],
db)
bot.start()


def main():
parser = argparse.ArgumentParser(description='PTG bot.')
parser.add_argument('configfile', nargs=1,
help='specify the config file')
parser.add_argument('configfile', help='specify the config file')
parser.add_argument('-d', dest='nodaemon', action='store_true',
help='do not run as a daemon')
args = parser.parse_args()


+ 1
- 1
ptgbot/db.py 파일 보기

@@ -26,7 +26,7 @@ class PTGDataBase():
def __init__(self, filename, ethercalc_url, ethercalc_cells):
self.filename = filename
self.ethercalc_url = ethercalc_url
self.ethercalc_cells = ethercalc_cells.split(' ')
self.ethercalc_cells = ethercalc_cells
if os.path.isfile(filename):
with open(filename, 'r') as fp:
self.data = json.load(fp)


불러오는 중...
취소
저장