Browse Source

Create 'rules.py' template; do a check before we start that we can write to the config file.

tags/v0.1^2
Ben Kurtovic 12 years ago
parent
commit
5856ce3a2e
2 changed files with 29 additions and 7 deletions
  1. +2
    -1
      earwigbot/config/__init__.py
  2. +27
    -6
      earwigbot/config/script.py

+ 2
- 1
earwigbot/config/__init__.py View File

@@ -25,6 +25,7 @@ from hashlib import sha256
import logging import logging
import logging.handlers import logging.handlers
from os import mkdir, path from os import mkdir, path
import stat


try: try:
from Crypto.Cipher import Blowfish from Crypto.Cipher import Blowfish
@@ -139,7 +140,7 @@ class BotConfig(object):


if not path.isdir(log_dir): if not path.isdir(log_dir):
if not path.exists(log_dir): if not path.exists(log_dir):
mkdir(log_dir, 0700)
mkdir(log_dir, stat.S_IWUSR|stat.S_IRUSR|stat.S_IXUSR)
else: else:
msg = "log_dir ({0}) exists but is not a directory!" msg = "log_dir ({0}) exists but is not a directory!"
print msg.format(log_dir) print msg.format(log_dir)


+ 27
- 6
earwigbot/config/script.py View File

@@ -22,7 +22,7 @@


from collections import OrderedDict from collections import OrderedDict
from getpass import getpass from getpass import getpass
from os import chmod
from os import chmod, path
import re import re
import stat import stat
from textwrap import fill, wrap from textwrap import fill, wrap
@@ -41,6 +41,15 @@ from earwigbot import exceptions


__all__ = ["ConfigScript"] __all__ = ["ConfigScript"]


RULES_TEMPLATE = """"# -*- coding: utf-8 -*-

def process(bot, rc):
\"\"\"Given a Bot() object and an RC() object, return a list of channels
to report this event to. Also, start any wiki bot tasks within this
function if necessary.\"\"\"
pass
"""

class ConfigScript(object): class ConfigScript(object):
"""A script to guide a user through the creation of a new config file.""" """A script to guide a user through the creation of a new config file."""
WIDTH = 79 WIDTH = 79
@@ -118,8 +127,8 @@ class ConfigScript(object):
subdirectory. Error logs are kept for a month whereas subdirectory. Error logs are kept for a month whereas
normal logs are kept for a week. If you disable this, normal logs are kept for a week. If you disable this,
the bot will still print logs to stdout.""") the bot will still print logs to stdout.""")
question = "Enable logging?"
self.data["metadata"]["enableLogging"] = self._ask_bool(question)
logging = self._ask_bool("Enable logging?")
self.data["metadata"]["enableLogging"] = logging


def _set_components(self): def _set_components(self):
print print
@@ -284,7 +293,15 @@ class ConfigScript(object):
else: else:
chan_question = "Watcher channels to join" chan_question = "Watcher channels to join"
watcher["channels"] = self._ask_list(chan_question) watcher["channels"] = self._ask_list(chan_question)
# create rules.py
self._print("""I am now creating a blank 'rules.py' file, which
will determine how the bot handles messages received
from the IRC watcher. It contains a process()
function that takes a Bot object allowing you to
start tasks and an RC object that holds the message
from the watcher. See the documentation for
details.""")
with open(path.join(self.config.root_dir, "rules.py"), "w") as fp:
fp.write(RULES_TEMPLATE)


self.data["irc"]["version"] = "EarwigBot - $1 - Python/$2 https://github.com/earwig/earwigbot" self.data["irc"]["version"] = "EarwigBot - $1 - Python/$2 https://github.com/earwig/earwigbot"


@@ -302,13 +319,17 @@ class ConfigScript(object):
pass pass


def _save(self): def _save(self):
open(self.config.path, "w").close()
chmod(self.config.path, stat.S_IRUSR|stat.S_IWUSR)
with open(self.config.path, "w") as stream: with open(self.config.path, "w") as stream:
yaml.dump(self.data, stream=stream, default_flow_style=False) yaml.dump(self.data, stream=stream, default_flow_style=False)


def make_new(self): def make_new(self):
"""Make a new config file based on the user's input.""" """Make a new config file based on the user's input."""
try:
open(self.config.path, "w").close()
chmod(self.config.path, stat.S_IRUSR|stat.S_IWUSR)
except IOError:
print "I can't seem to write to the config file:"
raise
self._set_metadata() self._set_metadata()
self._set_components() self._set_components()
self._set_wiki() self._set_wiki()


Loading…
Cancel
Save