Przeglądaj źródła

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

tags/v0.1^2
Ben Kurtovic 11 lat temu
rodzic
commit
5856ce3a2e
2 zmienionych plików z 29 dodań i 7 usunięć
  1. +2
    -1
      earwigbot/config/__init__.py
  2. +27
    -6
      earwigbot/config/script.py

+ 2
- 1
earwigbot/config/__init__.py Wyświetl plik

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

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

if not path.isdir(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:
msg = "log_dir ({0}) exists but is not a directory!"
print msg.format(log_dir)


+ 27
- 6
earwigbot/config/script.py Wyświetl plik

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

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

__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):
"""A script to guide a user through the creation of a new config file."""
WIDTH = 79
@@ -118,8 +127,8 @@ class ConfigScript(object):
subdirectory. Error logs are kept for a month whereas
normal logs are kept for a week. If you disable this,
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):
print
@@ -284,7 +293,15 @@ class ConfigScript(object):
else:
chan_question = "Watcher channels to join"
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"

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

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:
yaml.dump(self.data, stream=stream, default_flow_style=False)

def make_new(self):
"""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_components()
self._set_wiki()


Ładowanie…
Anuluj
Zapisz