Source

DailyPromptBot / minibotd.py

Full commit
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# The Daily Prompt Mini-Bot - A Shut Up and Write Project
# Version 0.0.1a
# Author: Marc-Alexandre Chan <laogeodritt at arenthil.net>
#-------------------------------------------------------------------------------
#
# Copyright (c) 2012 Marc-Alexandre Chan. Licensed under the GNU GPL version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#-------------------------------------------------------------------------------

""" The Daily Prompt: Minibot (daemon mode) bootstrap script. Use this script to
run the Minibot in daemon mode (UNIX and UNIX-like systems only!). This script
accepts an argument that can take one of three values: 'start', 'restart' or
'stop'. """

from minibot import DailyPromptMinibot
from minibot.errors import InvalidRedditError, ConfigValueError,\
    InvalidUserPass, HTTPError, SQLAlchemyError, DBAPIError,\
    ConfigFileError, LogFileError
from daemon.runner import DaemonRunner, DaemonRunnerInvalidActionError,\
    DaemonRunnerStartFailureError, DaemonRunnerStopFailureError
import sys

def main():
    try:
        minibot_app = DailyPromptMinibot('minibot.ini')
    except (ConfigFileError, LogFileError) as e:
        sys.exit(repr(e))
    except (IOError, OSError) as e:
        sys.exit("IOError: " + repr(e))
    except (ConfigValueError, ValueError) as e:
        sys.exit("Invalid configuration value: " + repr(e))

    try:
        daemon = DaemonRunner(minibot_app)
        if daemon.action != 'stop':
            minibot_app.init_resources()
        daemon.do_action()
    except DaemonRunnerInvalidActionError as e:
        sys.exit(str(e))
    except DaemonRunnerStartFailureError as e:
        sys.exit("Failed to start daemon: " + repr(e))
    except DaemonRunnerStopFailureError as e:
        sys.exit("Failed to end daemon: " + repr(e))
    except InvalidUserPass as e:
        sys.exit("Error logging into Reddit account: " + repr(e))
    except HTTPError as e:
        sys.exit("An error occurred accessing Reddit: " + repr(e))
    except (SQLAlchemyError, DBAPIError):
        raise # let Python handle it. This line just documents the possibility

if __name__ == '__main__':
    main()