From 2929380e12a21f265a50d10a8f78d17896c06262 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 25 Mar 2012 00:18:41 -0400 Subject: [PATCH 01/12] Preliminary documentation updates --- README.md | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3ecc02b..da0cf1f 100644 --- a/README.md +++ b/README.md @@ -22,18 +22,17 @@ integration between bot parts, and easier maintenance. # Installation -## Dependencies - -EarwigBot uses the MySQL library -[oursql](http://packages.python.org/oursql/) (>= 0.9.2) for communicating with -MediaWiki databases, and some tasks use their own tables for storage. -Additionally, the afc_history task uses -[matplotlib](http://matplotlib.sourceforge.net/) and -[numpy](http://numpy.scipy.org/) for graphing AfC statistics. Neither of these -modules are required for the main bot itself. - -`earwigbot.wiki.copyright` requires access to a search engine for detecting -copyright violations. Currently, -[Yahoo! BOSS](http://developer.yahoo.com/search/boss/) is the only engine -supported, and this requires -[oauth2](https://github.com/simplegeo/python-oauth2). +## From stable + +## From development + +# Setup + +It's recommended to run the bot's unit tests before installing. Run +`python -m unittest discover tests` from the project's root directory. + +*Note:* some unit tests require an internet connection. + +# Customizing + +# Hacking From d8a78675185ad8acc74f0054a159a8e36bdaf1e5 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Thu, 5 Apr 2012 23:50:36 -0400 Subject: [PATCH 02/12] Documentation for installation process --- README.md | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index da0cf1f..1820d4b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ was carried out in May, and the bot has been running consistently ever since [several ongoing tasks](http://en.wikipedia.org/wiki/User:EarwigBot#Tasks), ranging from statistics generation to category cleanup, and on-demand tasks such as WikiProject template tagging. Since it started running, the bot has -made over 45,000 edits. +made over 50,000 edits. A project to rewrite it from scratch began in early April 2011, thus moving away from the Pywikipedia framework and allowing for less overall code, better @@ -22,9 +22,40 @@ integration between bot parts, and easier maintenance. # Installation -## From stable +This package contains the core `earwigbot`, abstracted enough that it should be +usable and customizable by anyone running a bot on a MediaWiki site. Since it's +component-based, the IRC components can be disabled if desired. IRC commands +and bot tasks specific to +[my instance of EarwigBot](http://en.wikipedia.org/wiki/User:EarwigBot) are +available from the package +[earwigbot-plugins](https://github.com/earwig/earwigbot-plugins). -## From development +## Latest release (v0.1) + +EarwigBot is available from the [Python Package Index](http://pypi.python.org), +so you can install the latest release with `pip install earwigbot` +([get pip](http://pypi.python.org/pypi/pip)). + +You can also install it from source directly: + + curl -Lo earwigbot.tgz "https://github.com/earwig/earwigbot/tarball/v0.1" + tar -xf earwigbot.tgz + cd earwig-earwigbot-* + python setup.py install # may require root, or use --user switch to install locally + cd .. + rm -r earwigbot.tgz earwig-earwigbot-* + +## Development version + +You can install the development version of the bot from `git`, probably on the +`develop` branch which contains (usually) working code. `master` contains the +latest release. EarwigBot uses +[git flow](http://nvie.com/posts/a-successful-git-branching-model/), so you're +free to browse by tags or by new features (`feature/*` branches). + + git clone git://github.com/earwig/earwigbot.git earwigbot + cd earwigbot + python setup.py develop # may require root, or use --user switch to install locally # Setup From b20b6ba0922be47ca9c53e9af7a250a4f76c9cfc Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 6 Apr 2012 22:07:41 -0400 Subject: [PATCH 03/12] Markdown -> reStructuredText --- README.md | 69 ----------------------------------------------- README.rst | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 69 deletions(-) delete mode 100644 README.md create mode 100644 README.rst diff --git a/README.md b/README.md deleted file mode 100644 index 1820d4b..0000000 --- a/README.md +++ /dev/null @@ -1,69 +0,0 @@ -[EarwigBot](http://en.wikipedia.org/wiki/User:EarwigBot) is a -[Python](http://python.org/) robot that edits -[Wikipedia](http://en.wikipedia.org/) and interacts with people over -[IRC](http://en.wikipedia.org/wiki/Internet_Relay_Chat). - -# History - -Development began, based on the -[Pywikipedia framework](http://pywikipediabot.sourceforge.net/), in early 2009. -Approval for its fist task, a -[copyright violation detector](http://en.wikipedia.org/wiki/Wikipedia:Bots/Requests_for_approval/EarwigBot_1), -was carried out in May, and the bot has been running consistently ever since -(with the exception of Jan/Feb 2011). It currently handles -[several ongoing tasks](http://en.wikipedia.org/wiki/User:EarwigBot#Tasks), -ranging from statistics generation to category cleanup, and on-demand tasks -such as WikiProject template tagging. Since it started running, the bot has -made over 50,000 edits. - -A project to rewrite it from scratch began in early April 2011, thus moving -away from the Pywikipedia framework and allowing for less overall code, better -integration between bot parts, and easier maintenance. - -# Installation - -This package contains the core `earwigbot`, abstracted enough that it should be -usable and customizable by anyone running a bot on a MediaWiki site. Since it's -component-based, the IRC components can be disabled if desired. IRC commands -and bot tasks specific to -[my instance of EarwigBot](http://en.wikipedia.org/wiki/User:EarwigBot) are -available from the package -[earwigbot-plugins](https://github.com/earwig/earwigbot-plugins). - -## Latest release (v0.1) - -EarwigBot is available from the [Python Package Index](http://pypi.python.org), -so you can install the latest release with `pip install earwigbot` -([get pip](http://pypi.python.org/pypi/pip)). - -You can also install it from source directly: - - curl -Lo earwigbot.tgz "https://github.com/earwig/earwigbot/tarball/v0.1" - tar -xf earwigbot.tgz - cd earwig-earwigbot-* - python setup.py install # may require root, or use --user switch to install locally - cd .. - rm -r earwigbot.tgz earwig-earwigbot-* - -## Development version - -You can install the development version of the bot from `git`, probably on the -`develop` branch which contains (usually) working code. `master` contains the -latest release. EarwigBot uses -[git flow](http://nvie.com/posts/a-successful-git-branching-model/), so you're -free to browse by tags or by new features (`feature/*` branches). - - git clone git://github.com/earwig/earwigbot.git earwigbot - cd earwigbot - python setup.py develop # may require root, or use --user switch to install locally - -# Setup - -It's recommended to run the bot's unit tests before installing. Run -`python -m unittest discover tests` from the project's root directory. - -*Note:* some unit tests require an internet connection. - -# Customizing - -# Hacking diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..bf45f14 --- /dev/null +++ b/README.rst @@ -0,0 +1,90 @@ +EarwigBot +========= + +EarwigBot_ is a Python_ robot that edits Wikipedia_ and interacts with people +over IRC_. + +History +------- + +Development began, based on the `Pywikipedia framework`_, in early 2009. +Approval for its fist task, a `copyright violation detector`_, was carried out +in May, and the bot has been running consistently ever since (with the +exception of Jan/Feb 2011). It currently handles `several ongoing tasks`_ +ranging from statistics generation to category cleanup, and on-demand tasks +such as WikiProject template tagging. Since it started running, the bot has +made over 50,000 edits. + +A project to rewrite it from scratch began in early April 2011, thus moving +away from the Pywikipedia framework and allowing for less overall code, better +integration between bot parts, and easier maintenance. + +Installation +------------ + +This package contains the core ``earwigbot``, abstracted enough that it should +be usable and customizable by anyone running a bot on a MediaWiki site. Since +it is component-based, the IRC components can be disabled if desired. IRC +commands and bot tasks specific to `my instance of EarwigBot`_ are available +from the package `earwigbot-plugins`_. + +Latest release (v0.1) +~~~~~~~~~~~~~~~~~~~~~ + +EarwigBot is available from the `Python Package Index`_, so you can install the +latest release with ``pip install earwigbot`` (`get pip`_). + +You can also install it from source [1]_ directly:: + + curl -Lo earwigbot.tgz "https://github.com/earwig/earwigbot/tarball/v0.1" + tar -xf earwigbot.tgz + cd earwig-earwigbot-* + python setup.py install + cd .. + rm -r earwigbot.tgz earwig-earwigbot-* + +Development version +~~~~~~~~~~~~~~~~~~~ + +You can install the development version of the bot from ``git`` by using +setuptools/distribute's ``develop`` command [1]_, probably on the ``develop`` +branch which contains (usually) working code. ``master`` contains the latest +release. EarwigBot uses `git flow`_, so you're free to +browse by tags or by new features (``feature/*`` branches):: + + git clone git://github.com/earwig/earwigbot.git earwigbot + cd earwigbot + python setup.py develop + +Setup +----- + +It's recommended to run the bot's unit tests before installing. Run +``python setup.py test`` from the project's root directory. + +*Note:* some unit tests require an internet connection. + +Customizing +----------- + +Hacking +------- + +Footnotes +--------- + +.. _EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot +.. _Python: http://python.org/ +.. _Wikipedia: http://en.wikipedia.org/ +.. _IRC: http://en.wikipedia.org/wiki/Internet_Relay_Chat +.. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ +.. _copyright violation detector: http://en.wikipedia.org/wiki/Wikipedia:Bots/Requests_for_approval/EarwigBot_1 +.. _several ongoing tasks: http://en.wikipedia.org/wiki/User:EarwigBot#Tasks +.. _my instance of EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot +.. _earwigbot-plugins: https://github.com/earwig/earwigbot-plugins +.. _Python Package Index: http://pypi.python.org +.. _get pip: http://pypi.python.org/pypi/pip +.. _git flow: http://nvie.com/posts/a-successful-git-branching-model/ + +.. [1] ``python setup.py install``/``develop`` may require root, or use the + ``--user`` switch to install for the current user only From 9eb85f1b6ffcfe81b2ad4cd683059fa55a6e9838 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 10 Apr 2012 20:24:16 -0400 Subject: [PATCH 04/12] Update docs, including README.rst (almost done) --- README.rst | 233 ++++++++++++++++++++++++++++++++++++----- earwigbot/__init__.py | 15 +-- earwigbot/commands/__init__.py | 20 ++-- earwigbot/commands/chanops.py | 8 +- earwigbot/config.py | 5 +- earwigbot/tasks/__init__.py | 25 +++-- 6 files changed, 245 insertions(+), 61 deletions(-) diff --git a/README.rst b/README.rst index bf45f14..854ad08 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,9 @@ EarwigBot ========= EarwigBot_ is a Python_ robot that edits Wikipedia_ and interacts with people -over IRC_. +over IRC_. This file provides a basic overview of how to install and setup the +bot; more detailed information is located in the ``docs/`` directory (available +online at PyPI_). History ------- @@ -17,7 +19,10 @@ made over 50,000 edits. A project to rewrite it from scratch began in early April 2011, thus moving away from the Pywikipedia framework and allowing for less overall code, better -integration between bot parts, and easier maintenance. +integration between bot parts, and easier maintenance. Thanks to abstraction of +the core bot from tasks specific to my instance of it, the bot core can now be +used by other people with little additional work. How to take full advantage of +it is explained below. Installation ------------ @@ -25,8 +30,14 @@ Installation This package contains the core ``earwigbot``, abstracted enough that it should be usable and customizable by anyone running a bot on a MediaWiki site. Since it is component-based, the IRC components can be disabled if desired. IRC -commands and bot tasks specific to `my instance of EarwigBot`_ are available -from the package `earwigbot-plugins`_. +commands and bot tasks specific to `my instance of EarwigBot`_ that I don't +feel the average user will need are available from the repository +`earwigbot-plugins`_. + +It's recommended to run the bot's unit tests before installing. Run ``python +setup.py test`` from the project's root directory. Note that some +tests require an internet connection, and others may take a while to run. +Coverage is currently rather incomplete. Latest release (v0.1) ~~~~~~~~~~~~~~~~~~~~~ @@ -36,7 +47,7 @@ latest release with ``pip install earwigbot`` (`get pip`_). You can also install it from source [1]_ directly:: - curl -Lo earwigbot.tgz "https://github.com/earwig/earwigbot/tarball/v0.1" + curl -Lo earwigbot.tgz https://github.com/earwig/earwigbot/tarball/v0.1 tar -xf earwigbot.tgz cd earwig-earwigbot-* python setup.py install @@ -59,32 +70,206 @@ browse by tags or by new features (``feature/*`` branches):: Setup ----- -It's recommended to run the bot's unit tests before installing. Run -``python setup.py test`` from the project's root directory. +The bot stores its data in a "working directory", including its config file and +some databases. This is also the location where you will place custom IRC +commands and bot tasks, which will be explained later. It doesn't matter where +this directory is, as long as the bot can write to it. + +Start the bot with ``earwigbot path/to/working/dir``, or just ``earwigbot`` if +the working directory is the current directory. It will notice that no +``config.yml`` file exists and take you through the setup process. + +There is currently no way to edit the ``config.yml`` file from within the bot +after it has been created, but YAML is a very straightforward format, so you +should be able to make any necessary changes yourself. Check out the +`explanation of YAML`_ on Wikipedia for help. -*Note:* some unit tests require an internet connection. +After setup, the bot will start. This means it will connect to the IRC servers +it has been configured for, schedule bot tasks to run at specific times, and +then wait for instructions (as commands on IRC). For a list of commands, say +"``!help``" (commands are messages prefixed with an exclamation mark). + +You can stop the bot at any time with Control+C, same as you stop a normal +Python program, and it will exit safely. You can also use the "``!quit``" +command on IRC for the same purpose. Customizing ----------- -Hacking -------- +The bot's directory contains a ``commands`` subdirectory and a ``tasks`` +subdirectory. Custom IRC commands can be placed in the former, whereas custom +wiki bot tasks go into the latter. Developing custom modules is explained +below, and in more detail through the bot's documentation on PyPI_. + +You can easily reload commands and tasks without restarting the bot by using +"``!reload``". + +Note that custom commands will override built-in commands and tasks with the +same name. + +``Bot`` and ``BotConfig`` +~~~~~~~~~~~~~~~~~~~~~~~~~ + +- ``bot.wiki``: entry into `the Wiki Toolset`_, explained below. + +XXX: TODO + +Custom IRC commands +~~~~~~~~~~~~~~~~~~~ + +Custom commands are subclasses of `earwigbot.commands.BaseCommand`_ that +override ``BaseCommand``'s ``process()`` (and optionally ``check()``) methods. + +``BaseCommand``'s ``__doc__``-strings should explain what each attribute and +method is for and what they should be overridden with, but these are the +basics: + +- Class attribute ``name`` is the name of the command. This must be specified. + +- Class attribute ``hooks`` is a list of the "IRC events" that this command + might respond to. It defaults to ``["msg"]``, but options include + ``"msg_private"`` (for private messages only), ``"msg_public"`` (for channel + messages only), and ``"join"`` (for when a user joins a channel). See the + afc_status_ plugin for a command that responds to other hook types. + +- Method ``check()`` is passed a ``Data`` [2]_ object, and should return + ``True`` if you want to respond to this message, or ``False`` otherwise. The + default behavior is to return ``True`` only if ``data.is_command`` is + ``True`` and ``data.command == self.name``, which is suitable for most cases. + A common, straightforward reason for overriding is if a command has aliases + (see chanops_ for an example). Note that by returning ``True``, you prevent + any other commands from responding to this message. + +- Method ``process()`` is passed the same ``Data`` object as ``check()``, but + only if ``check()`` returned ``True``. This is where the bulk of your command + goes. To respond to IRC messages, there are a number of methods of + ``BaseCommand`` at your disposal. See the the test_ command for a simple + example, or look in BaseCommand's ``__init__`` method for the full list. + The most common ones are ``self.say(chan_or_user, msg)``, + ``self.reply(data, msg)`` (convenience function; sends a reply to the + issuer of the command in the channel it was received), + ``self.action(chan_or_user, msg)``, ``self.notice(chan_or_user, msg)``, + ``self.join(chan)``, and ``self.part(chan)``. + +It's important to name the command class ``Command`` within the file, or else +the bot might not recognize it as a command. The name of the file doesn't +really matter and need not match the command's name, but this is recommended +for readability. + +The bot has a wide selection of built-in commands and plugins to act as sample +code and/or to give ideas. Start with test_, and then check out chanops_ and +afc_status_ for some more complicated scripts! + +Custom bot tasks +~~~~~~~~~~~~~~~~ + +Custom tasks are subclasses of `earwigbot.tasks.BaseTask`_ that override +``BaseTask``'s ``run()`` (and optionally ``setup()``) methods. + +``BaseTask``'s ``_doc__``-strings should explain what each attribute and method +is for and what they should be overridden with, but these are the basics: + +- Class attribute ``name`` is the name of the task. This must be specified. + +- Class attribute ``number`` can be used to store an optional "task number", + possibly for use in edit summaries (to be generated with ``make_summary()``). + For example, EarwigBot's ``config.wiki["summary"]`` is + ``"([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2"``, which the + task class's ``make_summary(comment)`` method will take and replace ``$1`` + with the task number and ``$2`` with the details of the edit. Additionally, + ``shutoff_enabled()`` (which checks whether the bot has been told to stop + on-wiki by checking the content of a particular page) can check a different + page for each task using similar variables. EarwigBot's + ``config.wiki["shutoff"]["page"]`` is ``"User:$1/Shutoff/Task $2"``; ``$1`` + is substituted with the bot's username, and ``$2`` is substituted with the + task number, so, e.g., task #14 checks the page + ``[[User:EarwigBot/Shutoff/Task 14]].`` If the page's content does *not* + match ``config.wiki["shutoff"]["disabled"]`` (``"run"`` by default), then + shutoff is considered to be *enabled* and ``shutoff_enabled()`` will return + ``True``, indicating the task should not run. If you don't intend to use + either of these methods, feel free to leave this attribute blank. + +- Method ``setup()`` is called *once* with no arguments immediately after the + task is first loaded. Does nothing by default; treat it like an + ``__init__()`` if you want (``__init__()`` does things by default and a + dedicated setup method is easier than asking people to use ``super``). + +- Method ``run()`` is called with any number of keyword arguments every time + the task is executed (by ``bot.tasks.start(task_name, **kwargs)``, usually). + This is where the bulk of the task's code goes. For interfacing with + MediaWiki sites, read up on `the Wiki Toolset`_ below. + +Tasks have access to ``config.tasks[task_name]`` for config information, which +is a node in ``config.yml`` like every other attribute of ``bot.config``. This +can be used to store, for example, edit summaries, or templates to append to +user talk pages, so that these can be easily changed without modifying the task +itself. + +It's important to name the task class ``Task`` within the file, or else the bot +might not recognize it as a task. The name of the file doesn't really matter +and need not match the task's name, but this is recommended for readability. + +See the built-in wikiproject_tagger_ task for a relatively straightforward +task, or the afc_statistics_ plugin for a more complicated one. + +The Wiki Toolset +~~~~~~~~~~~~~~~~ + +EarwigBot's answer to the `Pywikipedia framework`_ is the Wiki Toolset +(``earwigbot.wiki``), which you will mainly access through ``bot.wiki``. + +XXX: TODO + +Tips +---- + +- Logging_ is a fantastic way to track the bot's progress + +- You can run a task by itself instead of the entire bot with ``earwigbot + path/to/working/dir --task task_name``. Footnotes --------- -.. _EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot -.. _Python: http://python.org/ -.. _Wikipedia: http://en.wikipedia.org/ -.. _IRC: http://en.wikipedia.org/wiki/Internet_Relay_Chat -.. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ -.. _copyright violation detector: http://en.wikipedia.org/wiki/Wikipedia:Bots/Requests_for_approval/EarwigBot_1 -.. _several ongoing tasks: http://en.wikipedia.org/wiki/User:EarwigBot#Tasks -.. _my instance of EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot -.. _earwigbot-plugins: https://github.com/earwig/earwigbot-plugins -.. _Python Package Index: http://pypi.python.org -.. _get pip: http://pypi.python.org/pypi/pip -.. _git flow: http://nvie.com/posts/a-successful-git-branching-model/ - .. [1] ``python setup.py install``/``develop`` may require root, or use the - ``--user`` switch to install for the current user only + ``--user`` switch to install for the current user only. + +.. [2] ``Data`` objects are instances of ``earwigbot.irc.Data`` that contain + information about a single message sent on IRC. Their useful attributes + are ``chan`` (channel the message was sent from, equal to ``nick`` if + it's a private message), ``nick`` (nickname of the sender), ``ident`` + (ident_ of the sender), ``host`` (hostname of the sender), ``msg`` (text + of the sent message), ``is_command`` (boolean telling whether or not + this message is a bot command, i.e., whether it is prefixed by ``!``), + ``command`` (if the message is a command, this is the name of the + command used), and ``args`` (if the message is a command, this is a list + of the command arguments - for example, if issuing "``!part ##earwig + Goodbye guys``", ``args`` will equal ``["##earwig", "Goodbye", + "guys"]``). Note that not all ``Data`` objects will have all of these + attributes: ``Data`` objects generated by private messages will, but + ones generated by joins will only have ``chan``, ``nick``, ``ident``, + and ``host``. + +.. _EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot +.. _Python: http://python.org/ +.. _Wikipedia: http://en.wikipedia.org/ +.. _IRC: http://en.wikipedia.org/wiki/Internet_Relay_Chat +.. _PyPI: http://packages.python.org/earwigbot +.. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ +.. _copyright violation detector: http://en.wikipedia.org/wiki/Wikipedia:Bots/Requests_for_approval/EarwigBot_1 +.. _several ongoing tasks: http://en.wikipedia.org/wiki/User:EarwigBot#Tasks +.. _my instance of EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot +.. _earwigbot-plugins: https://github.com/earwig/earwigbot-plugins +.. _Python Package Index: http://pypi.python.org +.. _get pip: http://pypi.python.org/pypi/pip +.. _git flow: http://nvie.com/posts/a-successful-git-branching-model/ +.. _explanation of YAML: http://en.wikipedia.org/wiki/YAML +.. _earwigbot.commands.BaseCommand: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/__init__.py +.. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py +.. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py +.. _test: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/test.py +.. _earwigbot.tasks.BaseTask: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/__init__.py +.. _wikiproject_tagger: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/wikiproject_tagger.py +.. _afc_statistics: https://github.com/earwig/earwigbot-plugins/blob/develop/tasks/afc_statistics.py +.. _logging: http://docs.python.org/library/logging.html +.. _ident: http://en.wikipedia.org/wiki/Ident diff --git a/earwigbot/__init__.py b/earwigbot/__init__.py index 39f0938..2210e45 100644 --- a/earwigbot/__init__.py +++ b/earwigbot/__init__.py @@ -22,9 +22,10 @@ """ EarwigBot is a Python robot that edits Wikipedia and interacts with people over -IRC. - http://earwig.github.com/earwig/earwigbot +IRC. - https://github.com/earwig/earwigbot -See README.md for a basic overview, or the docs/ directory for details. +See README.rst for an overview, or the docs/ directory for details. This +documentation is also available online at http://packages.python.org/earwigbot. """ __author__ = "Ben Kurtovic" @@ -35,18 +36,18 @@ __email__ = "ben.kurtovic@verizon.net" __release__ = False if not __release__: - def _add_git_commit_id_to_version(version): + def _add_git_commit_id_to_version_string(version): from git import Repo from os.path import split, dirname path = split(dirname(__file__))[0] commit_id = Repo(path).head.object.hexsha return version + ".git+" + commit_id[:8] try: - __version__ = _add_git_commit_id_to_version(__version__) + __version__ = _add_git_commit_id_to_version_string(__version__) except Exception: pass finally: - del _add_git_commit_id_to_version + del _add_git_commit_id_to_version_string -from earwigbot import (blowfish, bot, commands, config, irc, managers, tasks, - util, wiki) +from earwigbot import blowfish, bot, config, managers, util # Modules +from earwigbot import commands, irc, tasks, wiki # Subpackages diff --git a/earwigbot/commands/__init__.py b/earwigbot/commands/__init__.py index 1dd745d..3e03f71 100644 --- a/earwigbot/commands/__init__.py +++ b/earwigbot/commands/__init__.py @@ -20,20 +20,18 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -""" -EarwigBot's IRC Commands - -This package provides the IRC "commands" used by the bot's front-end component. -This module contains the BaseCommand class (import with -`from earwigbot.commands import BaseCommand`), whereas the package contains -various built-in commands. Additional commands can be installed as plugins in -the bot's working directory. -""" - __all__ = ["BaseCommand"] class BaseCommand(object): - """A base class for commands on IRC. + """ + EarwigBot's Base IRC Command + + This package provides built-in IRC "commands" used by the bot's front-end + component. Additional commands can be installed as plugins in the bot's + working directory. + + This class (import with `from earwigbot.commands import BaseCommand`), + can be subclassed to create custom IRC commands. This docstring is reported to the user when they use !help . """ diff --git a/earwigbot/commands/chanops.py b/earwigbot/commands/chanops.py index 9783411..a343aee 100644 --- a/earwigbot/commands/chanops.py +++ b/earwigbot/commands/chanops.py @@ -29,9 +29,7 @@ class Command(BaseCommand): def check(self, data): cmnds = ["chanops", "voice", "devoice", "op", "deop", "join", "part"] - if data.is_command and data.command in cmnds: - return True - return False + return data.is_command and data.command in cmnds def process(self, data): if data.command == "chanops": @@ -77,11 +75,11 @@ class Command(BaseCommand): reason = None if data.args: if data.args[0].startswith("#"): - # !part #channel reason for parting + # "!part #channel reason for parting" channel = data.args[0] if data.args[1:]: reason = " ".join(data.args[1:]) - else: # !part reason for parting; assume current channel + else: # "!part reason for parting"; assume current channel reason = " ".join(data.args) msg = "Requested by {0}".format(data.nick) diff --git a/earwigbot/config.py b/earwigbot/config.py index 59392a1..107f6b2 100644 --- a/earwigbot/config.py +++ b/earwigbot/config.py @@ -150,7 +150,10 @@ class BotConfig(object): #else: # is_encrypted = False raise NotImplementedError() - # yaml.dumps() + # yaml.dumps() config.yml file (self._config_path) + # Create root_dir/, root_dir/commands/, root_dir/tasks/ + # Give a reasonable message after config has been created regarding + # what to do next... @property def root_dir(self): diff --git a/earwigbot/tasks/__init__.py b/earwigbot/tasks/__init__.py index bfa7ef9..7f2f867 100644 --- a/earwigbot/tasks/__init__.py +++ b/earwigbot/tasks/__init__.py @@ -20,24 +20,23 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -""" -EarwigBot's Bot Tasks - -This package provides the wiki bot "tasks" EarwigBot runs. This module contains -the BaseTask class (import with `from earwigbot.tasks import BaseTask`), -whereas the package contains various built-in tasks. Additional tasks can be -installed as plugins in the bot's working directory. - -To run a task, use bot.tasks.start(name, **kwargs). **kwargs get passed to the -Task's run() function. -""" - from earwigbot import wiki __all__ = ["BaseTask"] class BaseTask(object): - """A base class for bot tasks that edit Wikipedia.""" + """ + EarwigBot's Base Bot Task + + This package provides built-in wiki bot "tasks" EarwigBot runs. Additional + tasks can be installed as plugins in the bot's working directory. + + This class (import with `from earwigbot.tasks import BaseTask`) can be + subclassed to create custom bot tasks. + + To run a task, use bot.tasks.start(name, **kwargs). **kwargs get passed to + the Task's run() function. + """ name = None number = 0 From 80091b2de3634f7c03f6b26e9697e9304a65567b Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 29 Apr 2012 01:24:43 -0400 Subject: [PATCH 05/12] Updating README and class docstrings throughout wiki toolset * Much of the documentation doesn't match the code; code will be updated in develop soon --- README.rst | 297 ++++++++++++++++++++++++++++++++++++++++----- earwigbot/irc/frontend.py | 8 +- earwigbot/managers.py | 4 +- earwigbot/wiki/category.py | 2 +- earwigbot/wiki/page.py | 22 ++-- earwigbot/wiki/site.py | 12 +- earwigbot/wiki/user.py | 23 ++-- 7 files changed, 306 insertions(+), 62 deletions(-) diff --git a/README.rst b/README.rst index 854ad08..5824805 100644 --- a/README.rst +++ b/README.rst @@ -19,10 +19,7 @@ made over 50,000 edits. A project to rewrite it from scratch began in early April 2011, thus moving away from the Pywikipedia framework and allowing for less overall code, better -integration between bot parts, and easier maintenance. Thanks to abstraction of -the core bot from tasks specific to my instance of it, the bot core can now be -used by other people with little additional work. How to take full advantage of -it is explained below. +integration between bot parts, and easier maintenance. Installation ------------ @@ -71,9 +68,9 @@ Setup ----- The bot stores its data in a "working directory", including its config file and -some databases. This is also the location where you will place custom IRC -commands and bot tasks, which will be explained later. It doesn't matter where -this directory is, as long as the bot can write to it. +databases. This is also the location where you will place custom IRC commands +and bot tasks, which will be explained later. It doesn't matter where this +directory is, as long as the bot can write to it. Start the bot with ``earwigbot path/to/working/dir``, or just ``earwigbot`` if the working directory is the current directory. It will notice that no @@ -90,19 +87,16 @@ then wait for instructions (as commands on IRC). For a list of commands, say "``!help``" (commands are messages prefixed with an exclamation mark). You can stop the bot at any time with Control+C, same as you stop a normal -Python program, and it will exit safely. You can also use the "``!quit``" -command on IRC for the same purpose. +Python program, and it will try to exit safely. You can also use the +"``!quit``" command on IRC. Customizing ----------- -The bot's directory contains a ``commands`` subdirectory and a ``tasks`` -subdirectory. Custom IRC commands can be placed in the former, whereas custom -wiki bot tasks go into the latter. Developing custom modules is explained -below, and in more detail through the bot's documentation on PyPI_. - -You can easily reload commands and tasks without restarting the bot by using -"``!reload``". +The bot's working directory contains a ``commands`` subdirectory and a +``tasks`` subdirectory. Custom IRC commands can be placed in the former, +whereas custom wiki bot tasks go into the latter. Developing custom modules is +explained below, and in more detail through the bot's documentation on PyPI_. Note that custom commands will override built-in commands and tasks with the same name. @@ -110,9 +104,55 @@ same name. ``Bot`` and ``BotConfig`` ~~~~~~~~~~~~~~~~~~~~~~~~~ -- ``bot.wiki``: entry into `the Wiki Toolset`_, explained below. +`earwigbot.bot.Bot`_ is EarwigBot's main class. You don't have to instantiate +this yourself, but it's good to be familiar with its attributes and methods, +because it is the main way to communicate with other parts of the bot. A +``Bot`` object is accessible as an attribute of commands and tasks (i.e., +``self.bot``). + +The most useful attributes are: + +- ``bot.config``: an instance of ``BotConfig``, for accessing the bot's + configuration data (see below). + +- ``bot.commands``: the bot's ``CommandManager``, which is used internally to + run IRC commands (through ``bot.commands.call()``, which you shouldn't have + to use); you can safely reload all commands with ``bot.commands.load()``. + +- ``bot.tasks``: the bot's ``TaskManager``, which can be used to start tasks + with ``bot.tasks.start(task_name, **kwargs)``. ``bot.tasks.load()`` can be + used to safely reload all tasks. + +- ``bot.frontend`` / ``bot.watcher``: instances of ``earwigbot.irc.Frontend`` + and ``earwigbot.irc.Watcher``, respectively, which represent the bot's + connections to these two servers; you can, for example, send a message to the + frontend with ``bot.frontend.say(chan, msg)`` (more on communicating with IRC + below). + +- ``bot.wiki``: interface with `the Wiki Toolset`_ (see below). -XXX: TODO +- Finally, ``bot.restart()`` (restarts IRC components and reloads config, + commands, and tasks) and ``bot.stop()`` can be used almost anywhere. Both + take an optional "reason" that will be logged and used as the quit message + when disconnecting from IRC. + +`earwigbot.config.BotConfig`_ stores configuration information for the bot. Its +``__doc__``string explains what each attribute is used for, but essentially +each "node" (one of ``config.components``, ``wiki``, ``tasks``, ``irc``, or +``metadata``) maps to a section of the bot's ``config.yml`` file. For example, +if ``config.yml`` includes something like:: + + irc: + frontend: + nick: MyAwesomeBot + channels: + - "##earwigbot" + - "#channel" + - "#other-channel" + +...then ``config.irc["frontend"]["nick"]`` will be ``"MyAwesomeBot"`` and +``config.irc["frontend"]["channels"]`` will be ``["##earwigbot", "#channel", +"#other-channel"]``. Custom IRC commands ~~~~~~~~~~~~~~~~~~~ @@ -120,9 +160,8 @@ Custom IRC commands Custom commands are subclasses of `earwigbot.commands.BaseCommand`_ that override ``BaseCommand``'s ``process()`` (and optionally ``check()``) methods. -``BaseCommand``'s ``__doc__``-strings should explain what each attribute and -method is for and what they should be overridden with, but these are the -basics: +``BaseCommand``'s docstrings should explain what each attribute andmethod is +for and what they should be overridden with, but these are the basics: - Class attribute ``name`` is the name of the command. This must be specified. @@ -145,6 +184,7 @@ basics: goes. To respond to IRC messages, there are a number of methods of ``BaseCommand`` at your disposal. See the the test_ command for a simple example, or look in BaseCommand's ``__init__`` method for the full list. + The most common ones are ``self.say(chan_or_user, msg)``, ``self.reply(data, msg)`` (convenience function; sends a reply to the issuer of the command in the channel it was received), @@ -158,7 +198,7 @@ for readability. The bot has a wide selection of built-in commands and plugins to act as sample code and/or to give ideas. Start with test_, and then check out chanops_ and -afc_status_ for some more complicated scripts! +afc_status_ for some more complicated scripts. Custom bot tasks ~~~~~~~~~~~~~~~~ @@ -166,8 +206,8 @@ Custom bot tasks Custom tasks are subclasses of `earwigbot.tasks.BaseTask`_ that override ``BaseTask``'s ``run()`` (and optionally ``setup()``) methods. -``BaseTask``'s ``_doc__``-strings should explain what each attribute and method -is for and what they should be overridden with, but these are the basics: +``BaseTask``'s docstrings should explain what each attribute and method is for +and what they should be overridden with, but these are the basics: - Class attribute ``name`` is the name of the task. This must be specified. @@ -176,10 +216,11 @@ is for and what they should be overridden with, but these are the basics: For example, EarwigBot's ``config.wiki["summary"]`` is ``"([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2"``, which the task class's ``make_summary(comment)`` method will take and replace ``$1`` - with the task number and ``$2`` with the details of the edit. Additionally, - ``shutoff_enabled()`` (which checks whether the bot has been told to stop - on-wiki by checking the content of a particular page) can check a different - page for each task using similar variables. EarwigBot's + with the task number and ``$2`` with the details of the edit. + + Additionally, ``shutoff_enabled()`` (which checks whether the bot has been + told to stop on-wiki by checking the content of a particular page) can check + a different page for each task using similar variables. EarwigBot's ``config.wiki["shutoff"]["page"]`` is ``"User:$1/Shutoff/Task $2"``; ``$1`` is substituted with the bot's username, and ``$2`` is substituted with the task number, so, e.g., task #14 checks the page @@ -192,7 +233,8 @@ is for and what they should be overridden with, but these are the basics: - Method ``setup()`` is called *once* with no arguments immediately after the task is first loaded. Does nothing by default; treat it like an ``__init__()`` if you want (``__init__()`` does things by default and a - dedicated setup method is easier than asking people to use ``super``). + dedicated setup method is often easier than overriding ``__init__()`` and + using ``super``). - Method ``run()`` is called with any number of keyword arguments every time the task is executed (by ``bot.tasks.start(task_name, **kwargs)``, usually). @@ -218,16 +260,201 @@ The Wiki Toolset EarwigBot's answer to the `Pywikipedia framework`_ is the Wiki Toolset (``earwigbot.wiki``), which you will mainly access through ``bot.wiki``. -XXX: TODO +``bot.wiki`` provides three methods for the management of Sites - +``get_site()``, ``add_site()``, and ``remove_site()``. Sites are objects that +simply represent a MediaWiki site. A single instance of EarwigBot (i.e. a +single *working directory*) is expected to relate to a single site or group of +sites using the same login info (like all WMF wikis with CentralAuth). + +Load your default site (the one that you picked during setup) with +``site = bot.wiki.get_site()``. + +Dealing with other sites +^^^^^^^^^^^^^^^^^^^^^^^^ + +*Skip this section if you're only working with one site.* + +If a site is *already known to the bot* (meaning that it is stored in the +``sites.db`` file, which includes just your default wiki at first), you can +load a site with ``site = bot.wiki.get_site(name)``, where ``name`` might be +``"enwiki"`` or ``"frwiktionary"`` (you can also do +``site = bot.wiki.get_site(project="wikipedia", lang="en")``). Recall that not +giving any arguments to ``get_site()`` will return the default site. + +``add_site()`` is used to add new sites to the sites database. It may be called +with similar arguments as ``get_site()``, but the difference is important. +``get_site()`` only needs enough information to identify the site in its +database, which is usually just its name; the database stores all other +necessary connection info. With ``add_site()``, you need to provide enough +connection info so the toolset can successfully access the site's API/SQL +databases and store that information for later. That might not be much; for +WMF wikis, you can usually use code like this:: + + project, lang = "wikipedia", "es" + try: + site = bot.wiki.get_site(project=project, lang=lang) + except earwigbot.SiteNotFoundError: + # Load site info from http://es.wikipedia.org/w/api.php: + site = bot.wiki.add_site(project=project, lang=lang) + +This works because EarwigBot assumes that the URL for the site is +``"//{lang}.{project}.org"`` and the API is at ``/w/api.php``; this might +change if you're dealing with non-WMF wikis, where the code might look +something more like:: + + project, lang = "mywiki", "it" + try: + site = bot.wiki.get_site(project=project, lang=lang) + except earwigbot.SiteNotFoundError: + Load site info from http://mysite.net/mywiki/it/s/api.php: + base_url = "http://mysite.net/" + project + "/" + lang + db_name = lang + project + "_p" + sql = {host: "sql.mysite.net", db: db_name} + site = bot.wiki.add_site(base_url=base_url, script_path="/s", sql=sql) + +``remove_site()`` does the opposite of ``add_site()``: give it a site's name +or a project/lang pair like ``get_site()`` takes, and it'll remove that site +from the sites database. + +Sites +^^^^^ + +``Site`` objects provide the following attributes: + +- ``name``: the site's name (or "wikiid"), like ``"enwiki"`` +- ``project``: the site's project name, like ``"wikipedia"`` +- ``lang``: the site's language code, like ``"en"`` +- ``domain``: the site's web domain, like ``"en.wikipedia.org"`` + +and the following methods: + +- ``api_query(**kwargs)``: does an API query with the given keyword arguments + as params +- ``sql_query(query, params=(), ...)``: does an SQL query and yields its + results (as a generator) +- ``get_replag()``: returns the estimated database replication lag (if we have + the site's SQL connection info) +- ``namespace_id_to_name(id, all=False)``: given a namespace ID, returns the + primary associated namespace name (or a list of all names when ``all`` is + ``True``) +- ``namespace_name_to_id(name)``: given a namespace name, returns the + associated namespace ID +- ``get_page(title, follow_redirects=False)``: returns a ``Page`` object for + the given title (or a ``Category`` object if the page's namespace is + "``Category:``") +- ``get_category(catname, follow_redirects=False)``: returns a ``Category`` + object for the given title (sans namespace) +- ``get_user(username)``: returns a ``User`` object for the given username + +Pages (and Categories) +^^^^^^^^^^^^^^^^^^^^^^ + +Create ``Page`` objects with ``site.get_page(title)``, +``page.toggle_talk()``, ``user.get_userpage()``, or ``user.get_talkpage()``. +They provide the following attributes: + +- ``title``: the page's title, or pagename +- ``exists``: whether the page exists +- ``pageid``: an integer ID representing the page +- ``url``: the page's URL +- ``namespace``: the page's namespace as an integer +- ``protection``: the page's current protection status +- ``is_talkpage``: ``True`` if the page is a talkpage, else ``False`` +- ``is_redirect``: ``True`` if the page is a redirect, else ``False`` + +and the following methods: + +- ``reload()``: forcibly reload the page's attributes (emphasis on *reload* - + this is only necessary if there is reason to believe they have changed) +- ``toggle_talk(...)``: returns a content page's talk page, or vice versa +- ``get()``: returns page content +- ``get_redirect_target()``: if the page is a redirect, returns its destination +- ``get_creator()``: returns a ``User`` object representing the first user to + edit the page +- ``edit(text, summary, minor=False, bot=True, force=False)``: replaces the + page's content with ``text`` or creates a new page +- ``add_section(text, title, minor=False, bot=True, force=False)``: adds a new + section named ``title`` at the bottom of the page +- ``copyvio_check(...)``: checks the page for copyright violations +- ``copyvio_compare(url, ...)``: checks the page like ``copyvio_check()``, but + against a specific URL + +Additionally, ``Category`` objects (created with ``site.get_category(name)`` or +``site.get_page(title)`` where ``title`` is in the ``Category:`` namespace) +provide the following additional method: + +- ``get_members(use_sql=False, limit=None)``: returns a list of page titles in + the category (limit is ``50`` by default if using the API) + +Users +^^^^^ + +Create ``User`` objects with ``site.get_user(name)`` or +``page.get_creator()``. They provide the following attributes: + +- ``name``: the user's username +- ``exists``: ``True`` if the user exists, or ``False`` if they do not +- ``userid``: an integer ID representing the user +- ``blockinfo``: information about any current blocks on the user (``False`` if + no block, or a dict of ``{"by": blocking_user, "reason": block_reason, + "expiry": block_expire_time}``) +- ``groups``: a list of the user's groups +- ``rights``: a list of the user's rights +- ``editcount``: the number of edits made by the user +- ``registration``: the time the user registered as a ``time.struct_time`` +- ``emailable``: ``True`` if you can email the user, ``False`` if you cannot +- ``gender``: the user's gender (``"male"``, ``"female"``, or ``"unknown"``) + +and the following methods: + +- ``reload()``: forcibly reload the user's attributes (emphasis on *reload* - + this is only necessary if there is reason to believe they have changed) +- ``get_userpage()``: returns a ``Page`` object representing the user's + userpage +- ``get_talkpage()``: returns a ``Page`` object representing the user's + talkpage + +Additional features +^^^^^^^^^^^^^^^^^^^ + +Not all aspects of the toolset are covered here. Explore `its code and +docstrings`_ to learn how to use it in a more hands-on fashion. For reference, +``bot.wiki`` is an instance of ``earwigbot.wiki.SitesDB`` tied to the +``sites.db`` file in the bot's working directory. Tips ---- -- Logging_ is a fantastic way to track the bot's progress +- Logging_ is a fantastic way to monitor the bot's progress as it runs. It has + a slew of built-in loggers, and enabling log retention (so logs are saved to + ``logs/`` in the working directory) is highly recommended. In the normal + setup, there are three log files, each of which "rotate" at a specific time + (``filename.log`` becomes ``filename.log.2012-04-10``, for example). The + ``debug.log`` file rotates every hour, and maintains six hours of logs of + every level (``DEBUG`` and up). ``bot.log`` rotates every day at midnight, + and maintains seven days of non-debug logs (``INFO`` and up). Finally, + ``error.log`` rotates every Sunday night, and maintains four weeks of logs + indicating unexpected events (``WARNING`` and up). + + To use logging in your commands or tasks (recommended), ``BaseCommand`` and + ``BaseTask`` provide ``logger`` attributes configured for the specific + command or task. If you're working with other classes, ``bot.logger`` is the + root logger (``logging.getLogger("earwigbot")`` by default), so you can use + ``getChild`` to make your logger. For example, task loggers are essentially + ``bot.logger.getChild("tasks").getChild(task.name)``. + +- A very useful IRC command is "``!reload``", which reloads all commands and + tasks without restarting the bot. [3]_ Combined with using the `!git plugin`_ + for pulling repositories from IRC, this can provide a seamless command/task + development workflow if the bot runs on an external server and you set up + its working directory as a git repo. - You can run a task by itself instead of the entire bot with ``earwigbot path/to/working/dir --task task_name``. +- Questions, comments, or suggestions about the documentation? `Let me know`_ + so I can improve it for other people. + Footnotes --------- @@ -250,6 +477,9 @@ Footnotes ones generated by joins will only have ``chan``, ``nick``, ``ident``, and ``host``. +.. [3] In reality, all this does is call ``bot.commands.load()`` and + ``bot.tasks.load()``! + .. _EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot .. _Python: http://python.org/ .. _Wikipedia: http://en.wikipedia.org/ @@ -264,6 +494,8 @@ Footnotes .. _get pip: http://pypi.python.org/pypi/pip .. _git flow: http://nvie.com/posts/a-successful-git-branching-model/ .. _explanation of YAML: http://en.wikipedia.org/wiki/YAML +.. _earwigbot.bot.Bot: https://github.com/earwig/earwigbot/blob/develop/earwigbot/bot.py +.. _earwigbot.config.BotConfig: https://github.com/earwig/earwigbot/blob/develop/earwigbot/config.py .. _earwigbot.commands.BaseCommand: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/__init__.py .. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py .. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py @@ -271,5 +503,8 @@ Footnotes .. _earwigbot.tasks.BaseTask: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/__init__.py .. _wikiproject_tagger: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/wikiproject_tagger.py .. _afc_statistics: https://github.com/earwig/earwigbot-plugins/blob/develop/tasks/afc_statistics.py +.. _its code and docstrings: https://github.com/earwig/earwigbot/tree/develop/earwigbot/wiki .. _logging: http://docs.python.org/library/logging.html +.. _Let me know: ben.kurtovic@verizon.net +.. _!git plugin: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/git.py .. _ident: http://en.wikipedia.org/wiki/Ident diff --git a/earwigbot/irc/frontend.py b/earwigbot/irc/frontend.py index 2065ff1..b13f5b0 100644 --- a/earwigbot/irc/frontend.py +++ b/earwigbot/irc/frontend.py @@ -57,7 +57,7 @@ class Frontend(IRCConnection): data.nick, data.ident, data.host = self.sender_regex.findall(line[0])[0] data.chan = line[2] data.parse_args() - self.bot.commands.check("join", data) + self.bot.commands.call("join", data) elif line[1] == "PRIVMSG": data.nick, data.ident, data.host = self.sender_regex.findall(line[0])[0] @@ -69,13 +69,13 @@ class Frontend(IRCConnection): # This is a privmsg to us, so set 'chan' as the nick of the # sender, then check for private-only command hooks: data.chan = data.nick - self.bot.commands.check("msg_private", data) + self.bot.commands.call("msg_private", data) else: # Check for public-only command hooks: - self.bot.commands.check("msg_public", data) + self.bot.commands.call("msg_public", data) # Check for command hooks that apply to all messages: - self.bot.commands.check("msg", data) + self.bot.commands.call("msg", data) elif line[0] == "PING": # If we are pinged, pong back self.pong(line[1]) diff --git a/earwigbot/managers.py b/earwigbot/managers.py index 293894c..62d1304 100644 --- a/earwigbot/managers.py +++ b/earwigbot/managers.py @@ -147,8 +147,8 @@ class CommandManager(_ResourceManager): base = super(CommandManager, self) base.__init__(bot, "commands", "Command", BaseCommand) - def check(self, hook, data): - """Given an IRC event, check if there's anything we can respond to.""" + def call(self, hook, data): + """Given a hook type and a Data object, respond appropriately.""" self.lock.acquire() for command in self._resources.itervalues(): if hook in command.hooks and command._wrap_check(data): diff --git a/earwigbot/wiki/category.py b/earwigbot/wiki/category.py index 67426f4..bcc8cb1 100644 --- a/earwigbot/wiki/category.py +++ b/earwigbot/wiki/category.py @@ -35,7 +35,7 @@ class Category(Page): because it accepts category names without the namespace prefix. Public methods: - members -- returns a list of page titles in the category + get_members -- returns a list of page titles in the category """ def __repr__(self): diff --git a/earwigbot/wiki/page.py b/earwigbot/wiki/page.py index 957878e..8772d53 100644 --- a/earwigbot/wiki/page.py +++ b/earwigbot/wiki/page.py @@ -38,19 +38,23 @@ class Page(CopyrightMixin): about the page, getting page content, and so on. Category is a subclass of Page with additional methods. + Attributes: + title -- the page's title, or pagename + exists -- whether the page exists + pageid -- an integer ID representing the page + url -- the page's URL + namespace -- the page's namespace as an integer + protection -- the page's current protection status + is_talkpage -- True if the page is a talkpage, else False + is_redirect -- True if the page is a redirect, else False + Public methods: - title -- returns the page's title, or pagename - exists -- returns whether the page exists - pageid -- returns an integer ID representing the page - url -- returns the page's URL - namespace -- returns the page's namespace as an integer - protection -- returns the page's current protection status - creator -- returns the page's creator (first user to edit) - is_talkpage -- returns True if the page is a talkpage, else False - is_redirect -- returns True if the page is a redirect, else False + reload -- forcibly reload the page's attributes toggle_talk -- returns a content page's talk page, or vice versa get -- returns page content get_redirect_target -- if the page is a redirect, returns its destination + get_creator -- returns a User object representing the first person + to edit the page edit -- replaces the page's content or creates a new page add_section -- adds a new section at the bottom of the page copyvio_check -- checks the page for copyright violations diff --git a/earwigbot/wiki/site.py b/earwigbot/wiki/site.py index 1f4d277..4154fe0 100644 --- a/earwigbot/wiki/site.py +++ b/earwigbot/wiki/site.py @@ -55,16 +55,18 @@ class Site(object): instances, tools.add_site() for adding new ones to config, and tools.del_site() for removing old ones from config, should suffice. + Attributes: + name -- the site's name (or "wikiid"), like "enwiki" + project -- the site's project name, like "wikipedia" + lang -- the site's language code, like "en" + domain -- the site's web domain, like "en.wikipedia.org" + Public methods: - name -- returns our name (or "wikiid"), like "enwiki" - project -- returns our project name, like "wikipedia" - lang -- returns our language code, like "en" - domain -- returns our web domain, like "en.wikipedia.org" api_query -- does an API query with the given kwargs as params sql_query -- does an SQL query and yields its results get_replag -- returns the estimated database replication lag namespace_id_to_name -- given a namespace ID, returns associated name(s) - namespace_name_to_id -- given a namespace name, returns associated id + namespace_name_to_id -- given a namespace name, returns the associated ID get_page -- returns a Page object for the given title get_category -- returns a Category object for the given title get_user -- returns a User object for the given username diff --git a/earwigbot/wiki/user.py b/earwigbot/wiki/user.py index 2747e2d..6c51051 100644 --- a/earwigbot/wiki/user.py +++ b/earwigbot/wiki/user.py @@ -36,17 +36,20 @@ class User(object): information about the user, such as editcount and user rights, methods for returning the user's userpage and talkpage, etc. + Attributes: + name -- the user's username + exists -- True if the user exists, or False if they do not + userid -- an integer ID representing the user + blockinfo -- information about any current blocks on the user + groups -- a list of the user's groups + rights -- a list of the user's rights + editcount -- the number of edits made by the user + registration -- the time the user registered as a time.struct_time + emailable -- True if you can email the user, False if you cannot + gender -- the user's gender ("male", "female", or "unknown") + Public methods: - name -- returns the user's username - exists -- returns True if the user exists, False if they do not - userid -- returns an integer ID representing the user - blockinfo -- returns information about a current block on the user - groups -- returns a list of the user's groups - rights -- returns a list of the user's rights - editcount -- returns the number of edits made by the user - registration -- returns the time the user registered as a time.struct_time - emailable -- returns True if you can email the user, False if you cannot - gender -- returns the user's gender ("male", "female", or "unknown") + reload -- forcibly reload the user's attributes get_userpage -- returns a Page object representing the user's userpage get_talkpage -- returns a Page object representing the user's talkpage """ From dfb4183e699c00a4365de8f7eec7f646e54361af Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 29 Apr 2012 02:29:36 -0400 Subject: [PATCH 06/12] Adding docs/ --- .gitignore | 5 ++ README.rst | 12 +-- docs/Makefile | 153 +++++++++++++++++++++++++++++++ docs/conf.py | 242 ++++++++++++++++++++++++++++++++++++++++++++++++++ docs/customizing.rst | 192 +++++++++++++++++++++++++++++++++++++++ docs/index.rst | 48 ++++++++++ docs/installation.rst | 53 +++++++++++ docs/setup.rst | 27 ++++++ docs/tips.rst | 41 +++++++++ docs/toolset.rst | 170 +++++++++++++++++++++++++++++++++++ 10 files changed, 937 insertions(+), 6 deletions(-) create mode 100644 docs/Makefile create mode 100644 docs/conf.py create mode 100644 docs/customizing.rst create mode 100644 docs/index.rst create mode 100644 docs/installation.rst create mode 100644 docs/setup.rst create mode 100644 docs/tips.rst create mode 100644 docs/toolset.rst diff --git a/.gitignore b/.gitignore index 93e373c..c40c476 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ *.pyc + +*.egg *.egg-info +build +docs/_build + .DS_Store diff --git a/README.rst b/README.rst index 5824805..8bc850e 100644 --- a/README.rst +++ b/README.rst @@ -255,7 +255,7 @@ See the built-in wikiproject_tagger_ task for a relatively straightforward task, or the afc_statistics_ plugin for a more complicated one. The Wiki Toolset -~~~~~~~~~~~~~~~~ +---------------- EarwigBot's answer to the `Pywikipedia framework`_ is the Wiki Toolset (``earwigbot.wiki``), which you will mainly access through ``bot.wiki``. @@ -270,7 +270,7 @@ Load your default site (the one that you picked during setup) with ``site = bot.wiki.get_site()``. Dealing with other sites -^^^^^^^^^^^^^^^^^^^^^^^^ +~~~~~~~~~~~~~~~~~~~~~~~~ *Skip this section if you're only working with one site.* @@ -317,7 +317,7 @@ or a project/lang pair like ``get_site()`` takes, and it'll remove that site from the sites database. Sites -^^^^^ +~~~~~ ``Site`` objects provide the following attributes: @@ -347,7 +347,7 @@ and the following methods: - ``get_user(username)``: returns a ``User`` object for the given username Pages (and Categories) -^^^^^^^^^^^^^^^^^^^^^^ +~~~~~~~~~~~~~~~~~~~~~~ Create ``Page`` objects with ``site.get_page(title)``, ``page.toggle_talk()``, ``user.get_userpage()``, or ``user.get_talkpage()``. @@ -387,7 +387,7 @@ provide the following additional method: the category (limit is ``50`` by default if using the API) Users -^^^^^ +~~~~~ Create ``User`` objects with ``site.get_user(name)`` or ``page.get_creator()``. They provide the following attributes: @@ -415,7 +415,7 @@ and the following methods: talkpage Additional features -^^^^^^^^^^^^^^^^^^^ +~~~~~~~~~~~~~~~~~~~ Not all aspects of the toolset are covered here. Explore `its code and docstrings`_ to learn how to use it in a more hands-on fashion. For reference, diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..80e5ec7 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,153 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + -rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/EarwigBot.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/EarwigBot.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/EarwigBot" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/EarwigBot" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..92b2d74 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,242 @@ +# -*- coding: utf-8 -*- +# +# EarwigBot documentation build configuration file, created by +# sphinx-quickstart on Sun Apr 29 01:42:25 2012. +# +# This file is execfile()d with the current directory set to its containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys, os + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +sys.path.insert(0, os.path.abspath('..')) + +# -- General configuration ----------------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be extensions +# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.coverage', 'sphinx.ext.viewcode'] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'EarwigBot' +copyright = u'2009, 2010, 2011, 2012 by Ben Kurtovic' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '0.1' +# The full version, including alpha/beta/rc tags. +release = '0.1.dev' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +#language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + + +# -- Options for HTML output --------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'nature' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'EarwigBotdoc' + + +# -- Options for LaTeX output -------------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +#'preamble': '', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass [howto/manual]). +latex_documents = [ + ('index', 'EarwigBot.tex', u'EarwigBot Documentation', + u'Ben Kurtovic', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output -------------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'earwigbot', u'EarwigBot Documentation', + [u'Ben Kurtovic'], 1) +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------------ + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'EarwigBot', u'EarwigBot Documentation', + u'Ben Kurtovic', 'EarwigBot', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' diff --git a/docs/customizing.rst b/docs/customizing.rst new file mode 100644 index 0000000..5a18d5b --- /dev/null +++ b/docs/customizing.rst @@ -0,0 +1,192 @@ +Customizing +=========== + +The bot's working directory contains a ``commands`` subdirectory and a +``tasks`` subdirectory. Custom IRC commands can be placed in the former, +whereas custom wiki bot tasks go into the latter. Developing custom modules is +explained in detail in this documentation. + +Note that custom commands will override built-in commands and tasks with the +same name. + +``Bot`` and ``BotConfig`` +------------------------- + +`earwigbot.bot.Bot`_ is EarwigBot's main class. You don't have to instantiate +this yourself, but it's good to be familiar with its attributes and methods, +because it is the main way to communicate with other parts of the bot. A +``Bot`` object is accessible as an attribute of commands and tasks (i.e., +``self.bot``). + +The most useful attributes are: + +- ``bot.config``: an instance of ``BotConfig``, for accessing the bot's + configuration data (see below). + +- ``bot.commands``: the bot's ``CommandManager``, which is used internally to + run IRC commands (through ``bot.commands.call()``, which you shouldn't have + to use); you can safely reload all commands with ``bot.commands.load()``. + +- ``bot.tasks``: the bot's ``TaskManager``, which can be used to start tasks + with ``bot.tasks.start(task_name, **kwargs)``. ``bot.tasks.load()`` can be + used to safely reload all tasks. + +- ``bot.frontend`` / ``bot.watcher``: instances of ``earwigbot.irc.Frontend`` + and ``earwigbot.irc.Watcher``, respectively, which represent the bot's + connections to these two servers; you can, for example, send a message to the + frontend with ``bot.frontend.say(chan, msg)`` (more on communicating with IRC + below). + +- ``bot.wiki``: interface with the :doc:`Wiki Toolset `. + +- Finally, ``bot.restart()`` (restarts IRC components and reloads config, + commands, and tasks) and ``bot.stop()`` can be used almost anywhere. Both + take an optional "reason" that will be logged and used as the quit message + when disconnecting from IRC. + +`earwigbot.config.BotConfig`_ stores configuration information for the bot. Its +``__doc__``string explains what each attribute is used for, but essentially +each "node" (one of ``config.components``, ``wiki``, ``tasks``, ``irc``, or +``metadata``) maps to a section of the bot's ``config.yml`` file. For example, +if ``config.yml`` includes something like:: + + irc: + frontend: + nick: MyAwesomeBot + channels: + - "##earwigbot" + - "#channel" + - "#other-channel" + +...then ``config.irc["frontend"]["nick"]`` will be ``"MyAwesomeBot"`` and +``config.irc["frontend"]["channels"]`` will be ``["##earwigbot", "#channel", +"#other-channel"]``. + +Custom IRC commands +------------------- + +Custom commands are subclasses of `earwigbot.commands.BaseCommand`_ that +override ``BaseCommand``'s ``process()`` (and optionally ``check()``) methods. + +``BaseCommand``'s docstrings should explain what each attribute andmethod is +for and what they should be overridden with, but these are the basics: + +- Class attribute ``name`` is the name of the command. This must be specified. + +- Class attribute ``hooks`` is a list of the "IRC events" that this command + might respond to. It defaults to ``["msg"]``, but options include + ``"msg_private"`` (for private messages only), ``"msg_public"`` (for channel + messages only), and ``"join"`` (for when a user joins a channel). See the + afc_status_ plugin for a command that responds to other hook types. + +- Method ``check()`` is passed a ``Data`` [1]_ object, and should return + ``True`` if you want to respond to this message, or ``False`` otherwise. The + default behavior is to return ``True`` only if ``data.is_command`` is + ``True`` and ``data.command == self.name``, which is suitable for most cases. + A common, straightforward reason for overriding is if a command has aliases + (see chanops_ for an example). Note that by returning ``True``, you prevent + any other commands from responding to this message. + +- Method ``process()`` is passed the same ``Data`` object as ``check()``, but + only if ``check()`` returned ``True``. This is where the bulk of your command + goes. To respond to IRC messages, there are a number of methods of + ``BaseCommand`` at your disposal. See the the test_ command for a simple + example, or look in BaseCommand's ``__init__`` method for the full list. + + The most common ones are ``self.say(chan_or_user, msg)``, + ``self.reply(data, msg)`` (convenience function; sends a reply to the + issuer of the command in the channel it was received), + ``self.action(chan_or_user, msg)``, ``self.notice(chan_or_user, msg)``, + ``self.join(chan)``, and ``self.part(chan)``. + +It's important to name the command class ``Command`` within the file, or else +the bot might not recognize it as a command. The name of the file doesn't +really matter and need not match the command's name, but this is recommended +for readability. + +The bot has a wide selection of built-in commands and plugins to act as sample +code and/or to give ideas. Start with test_, and then check out chanops_ and +afc_status_ for some more complicated scripts. + +Custom bot tasks +---------------- + +Custom tasks are subclasses of `earwigbot.tasks.BaseTask`_ that override +``BaseTask``'s ``run()`` (and optionally ``setup()``) methods. + +``BaseTask``'s docstrings should explain what each attribute and method is for +and what they should be overridden with, but these are the basics: + +- Class attribute ``name`` is the name of the task. This must be specified. + +- Class attribute ``number`` can be used to store an optional "task number", + possibly for use in edit summaries (to be generated with ``make_summary()``). + For example, EarwigBot's ``config.wiki["summary"]`` is + ``"([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2"``, which the + task class's ``make_summary(comment)`` method will take and replace ``$1`` + with the task number and ``$2`` with the details of the edit. + + Additionally, ``shutoff_enabled()`` (which checks whether the bot has been + told to stop on-wiki by checking the content of a particular page) can check + a different page for each task using similar variables. EarwigBot's + ``config.wiki["shutoff"]["page"]`` is ``"User:$1/Shutoff/Task $2"``; ``$1`` + is substituted with the bot's username, and ``$2`` is substituted with the + task number, so, e.g., task #14 checks the page + ``[[User:EarwigBot/Shutoff/Task 14]].`` If the page's content does *not* + match ``config.wiki["shutoff"]["disabled"]`` (``"run"`` by default), then + shutoff is considered to be *enabled* and ``shutoff_enabled()`` will return + ``True``, indicating the task should not run. If you don't intend to use + either of these methods, feel free to leave this attribute blank. + +- Method ``setup()`` is called *once* with no arguments immediately after the + task is first loaded. Does nothing by default; treat it like an + ``__init__()`` if you want (``__init__()`` does things by default and a + dedicated setup method is often easier than overriding ``__init__()`` and + using ``super``). + +- Method ``run()`` is called with any number of keyword arguments every time + the task is executed (by ``bot.tasks.start(task_name, **kwargs)``, usually). + This is where the bulk of the task's code goes. For interfacing with + MediaWiki sites, read up on the :doc:`Wiki Toolset `. + +Tasks have access to ``config.tasks[task_name]`` for config information, which +is a node in ``config.yml`` like every other attribute of ``bot.config``. This +can be used to store, for example, edit summaries, or templates to append to +user talk pages, so that these can be easily changed without modifying the task +itself. + +It's important to name the task class ``Task`` within the file, or else the bot +might not recognize it as a task. The name of the file doesn't really matter +and need not match the task's name, but this is recommended for readability. + +See the built-in wikiproject_tagger_ task for a relatively straightforward +task, or the afc_statistics_ plugin for a more complicated one. + +.. rubric:: Footnotes + +.. [1] ``Data`` objects are instances of ``earwigbot.irc.Data`` that contain + information about a single message sent on IRC. Their useful attributes + are ``chan`` (channel the message was sent from, equal to ``nick`` if + it's a private message), ``nick`` (nickname of the sender), ``ident`` + (ident_ of the sender), ``host`` (hostname of the sender), ``msg`` (text + of the sent message), ``is_command`` (boolean telling whether or not + this message is a bot command, i.e., whether it is prefixed by ``!``), + ``command`` (if the message is a command, this is the name of the + command used), and ``args`` (if the message is a command, this is a list + of the command arguments - for example, if issuing "``!part ##earwig + Goodbye guys``", ``args`` will equal ``["##earwig", "Goodbye", + "guys"]``). Note that not all ``Data`` objects will have all of these + attributes: ``Data`` objects generated by private messages will, but + ones generated by joins will only have ``chan``, ``nick``, ``ident``, + and ``host``. + +.. _earwigbot.bot.Bot: https://github.com/earwig/earwigbot/blob/develop/earwigbot/bot.py +.. _earwigbot.config.BotConfig: https://github.com/earwig/earwigbot/blob/develop/earwigbot/config.py +.. _earwigbot.commands.BaseCommand: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/__init__.py +.. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py +.. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py +.. _test: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/test.py +.. _earwigbot.tasks.BaseTask: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/__init__.py +.. _wikiproject_tagger: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/wikiproject_tagger.py +.. _afc_statistics: https://github.com/earwig/earwigbot-plugins/blob/develop/tasks/afc_statistics.py +.. _ident: http://en.wikipedia.org/wiki/Ident diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..c5aa2e9 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,48 @@ +EarwigBot v0.1 Documentation +============================ + +EarwigBot_ is a Python_ robot that edits Wikipedia_ and interacts with people +over IRC_. + +History +------- + +Development began, based on the `Pywikipedia framework`_, in early 2009. +Approval for its fist task, a `copyright violation detector`_, was carried out +in May, and the bot has been running consistently ever since (with the +exception of Jan/Feb 2011). It currently handles `several ongoing tasks`_ +ranging from statistics generation to category cleanup, and on-demand tasks +such as WikiProject template tagging. Since it started running, the bot has +made over 50,000 edits. + +A project to rewrite it from scratch began in early April 2011, thus moving +away from the Pywikipedia framework and allowing for less overall code, better +integration between bot parts, and easier maintenance. + +.. _EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot +.. _Python: http://python.org/ +.. _Wikipedia: http://en.wikipedia.org/ +.. _IRC: http://en.wikipedia.org/wiki/Internet_Relay_Chat +.. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ +.. _copyright violation detector: http://en.wikipedia.org/wiki/Wikipedia:Bots/Requests_for_approval/EarwigBot_1 +.. _several ongoing tasks: http://en.wikipedia.org/wiki/User:EarwigBot#Tasks + +Contents +-------- + +.. toctree:: + :maxdepth: 3 + + installation + setup + customizing + toolset + tips + +Indices and tables +------------------ + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` + diff --git a/docs/installation.rst b/docs/installation.rst new file mode 100644 index 0000000..39996c9 --- /dev/null +++ b/docs/installation.rst @@ -0,0 +1,53 @@ +Installation +============ + +This package contains the core ``earwigbot``, abstracted enough that it should +be usable and customizable by anyone running a bot on a MediaWiki site. Since +it is component-based, the IRC components can be disabled if desired. IRC +commands and bot tasks specific to `my instance of EarwigBot`_ that I don't +feel the average user will need are available from the repository +`earwigbot-plugins`_. + +It's recommended to run the bot's unit tests before installing. Run ``python +setup.py test`` from the project's root directory. Note that some +tests require an internet connection, and others may take a while to run. +Coverage is currently rather incomplete. + +Latest release (v0.1) +--------------------- + +EarwigBot is available from the `Python Package Index`_, so you can install the +latest release with ``pip install earwigbot`` (`get pip`_). + +You can also install it from source [1]_ directly:: + + curl -Lo earwigbot.tgz https://github.com/earwig/earwigbot/tarball/v0.1 + tar -xf earwigbot.tgz + cd earwig-earwigbot-* + python setup.py install + cd .. + rm -r earwigbot.tgz earwig-earwigbot-* + +Development version +------------------- + +You can install the development version of the bot from ``git`` by using +setuptools/distribute's ``develop`` command [1]_, probably on the ``develop`` +branch which contains (usually) working code. ``master`` contains the latest +release. EarwigBot uses `git flow`_, so you're free to +browse by tags or by new features (``feature/*`` branches):: + + git clone git://github.com/earwig/earwigbot.git earwigbot + cd earwigbot + python setup.py develop + +.. rubric:: Footnotes + +.. [1] ``python setup.py install``/``develop`` may require root, or use the + ``--user`` switch to install for the current user only. + +.. _my instance of EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot +.. _earwigbot-plugins: https://github.com/earwig/earwigbot-plugins +.. _Python Package Index: http://pypi.python.org +.. _get pip: http://pypi.python.org/pypi/pip +.. _git flow: http://nvie.com/posts/a-successful-git-branching-model/ diff --git a/docs/setup.rst b/docs/setup.rst new file mode 100644 index 0000000..e4feb85 --- /dev/null +++ b/docs/setup.rst @@ -0,0 +1,27 @@ +Setup +===== + +The bot stores its data in a "working directory", including its config file and +databases. This is also the location where you will place custom IRC commands +and bot tasks, which will be explained later. It doesn't matter where this +directory is, as long as the bot can write to it. + +Start the bot with ``earwigbot path/to/working/dir``, or just ``earwigbot`` if +the working directory is the current directory. It will notice that no +``config.yml`` file exists and take you through the setup process. + +There is currently no way to edit the ``config.yml`` file from within the bot +after it has been created, but YAML is a very straightforward format, so you +should be able to make any necessary changes yourself. Check out the +`explanation of YAML`_ on Wikipedia for help. + +After setup, the bot will start. This means it will connect to the IRC servers +it has been configured for, schedule bot tasks to run at specific times, and +then wait for instructions (as commands on IRC). For a list of commands, say +"``!help``" (commands are messages prefixed with an exclamation mark). + +You can stop the bot at any time with :kbd:`Control-c`, same as you stop a +normal Python program, and it will try to exit safely. You can also use the +"``!quit``" command on IRC. + +.. _explanation of YAML: http://en.wikipedia.org/wiki/YAML diff --git a/docs/tips.rst b/docs/tips.rst new file mode 100644 index 0000000..7a4e34a --- /dev/null +++ b/docs/tips.rst @@ -0,0 +1,41 @@ +Tips +==== + +- Logging_ is a fantastic way to monitor the bot's progress as it runs. It has + a slew of built-in loggers, and enabling log retention (so logs are saved to + ``logs/`` in the working directory) is highly recommended. In the normal + setup, there are three log files, each of which "rotate" at a specific time + (``filename.log`` becomes ``filename.log.2012-04-10``, for example). The + ``debug.log`` file rotates every hour, and maintains six hours of logs of + every level (``DEBUG`` and up). ``bot.log`` rotates every day at midnight, + and maintains seven days of non-debug logs (``INFO`` and up). Finally, + ``error.log`` rotates every Sunday night, and maintains four weeks of logs + indicating unexpected events (``WARNING`` and up). + + To use logging in your commands or tasks (recommended), ``BaseCommand`` and + ``BaseTask`` provide ``logger`` attributes configured for the specific + command or task. If you're working with other classes, ``bot.logger`` is the + root logger (``logging.getLogger("earwigbot")`` by default), so you can use + ``getChild`` to make your logger. For example, task loggers are essentially + ``bot.logger.getChild("tasks").getChild(task.name)``. + +- A very useful IRC command is "``!reload``", which reloads all commands and + tasks without restarting the bot. [1]_ Combined with using the `!git plugin`_ + for pulling repositories from IRC, this can provide a seamless command/task + development workflow if the bot runs on an external server and you set up + its working directory as a git repo. + +- You can run a task by itself instead of the entire bot with ``earwigbot + path/to/working/dir --task task_name``. + +- Questions, comments, or suggestions about the documentation? `Let me know`_ + so I can improve it for other people. + +.. rubric:: Footnotes + +.. [1] In reality, all this does is call ``bot.commands.load()`` and + ``bot.tasks.load()``! + +.. _logging: http://docs.python.org/library/logging.html +.. _Let me know: ben.kurtovic@verizon.net +.. _!git plugin: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/git.py diff --git a/docs/toolset.rst b/docs/toolset.rst new file mode 100644 index 0000000..6b8a719 --- /dev/null +++ b/docs/toolset.rst @@ -0,0 +1,170 @@ +The Wiki Toolset +================ + +EarwigBot's answer to the `Pywikipedia framework`_ is the Wiki Toolset +(``earwigbot.wiki``), which you will mainly access through ``bot.wiki``. + +``bot.wiki`` provides three methods for the management of Sites - +``get_site()``, ``add_site()``, and ``remove_site()``. Sites are objects that +simply represent a MediaWiki site. A single instance of EarwigBot (i.e. a +single *working directory*) is expected to relate to a single site or group of +sites using the same login info (like all WMF wikis with CentralAuth). + +Load your default site (the one that you picked during setup) with +``site = bot.wiki.get_site()``. + +Dealing with other sites +~~~~~~~~~~~~~~~~~~~~~~~~ + +*Skip this section if you're only working with one site.* + +If a site is *already known to the bot* (meaning that it is stored in the +``sites.db`` file, which includes just your default wiki at first), you can +load a site with ``site = bot.wiki.get_site(name)``, where ``name`` might be +``"enwiki"`` or ``"frwiktionary"`` (you can also do +``site = bot.wiki.get_site(project="wikipedia", lang="en")``). Recall that not +giving any arguments to ``get_site()`` will return the default site. + +``add_site()`` is used to add new sites to the sites database. It may be called +with similar arguments as ``get_site()``, but the difference is important. +``get_site()`` only needs enough information to identify the site in its +database, which is usually just its name; the database stores all other +necessary connection info. With ``add_site()``, you need to provide enough +connection info so the toolset can successfully access the site's API/SQL +databases and store that information for later. That might not be much; for +WMF wikis, you can usually use code like this:: + + project, lang = "wikipedia", "es" + try: + site = bot.wiki.get_site(project=project, lang=lang) + except earwigbot.SiteNotFoundError: + # Load site info from http://es.wikipedia.org/w/api.php: + site = bot.wiki.add_site(project=project, lang=lang) + +This works because EarwigBot assumes that the URL for the site is +``"//{lang}.{project}.org"`` and the API is at ``/w/api.php``; this might +change if you're dealing with non-WMF wikis, where the code might look +something more like:: + + project, lang = "mywiki", "it" + try: + site = bot.wiki.get_site(project=project, lang=lang) + except earwigbot.SiteNotFoundError: + Load site info from http://mysite.net/mywiki/it/s/api.php: + base_url = "http://mysite.net/" + project + "/" + lang + db_name = lang + project + "_p" + sql = {host: "sql.mysite.net", db: db_name} + site = bot.wiki.add_site(base_url=base_url, script_path="/s", sql=sql) + +``remove_site()`` does the opposite of ``add_site()``: give it a site's name +or a project/lang pair like ``get_site()`` takes, and it'll remove that site +from the sites database. + +Sites +~~~~~ + +``Site`` objects provide the following attributes: + +- ``name``: the site's name (or "wikiid"), like ``"enwiki"`` +- ``project``: the site's project name, like ``"wikipedia"`` +- ``lang``: the site's language code, like ``"en"`` +- ``domain``: the site's web domain, like ``"en.wikipedia.org"`` + +and the following methods: + +- ``api_query(**kwargs)``: does an API query with the given keyword arguments + as params +- ``sql_query(query, params=(), ...)``: does an SQL query and yields its + results (as a generator) +- ``get_replag()``: returns the estimated database replication lag (if we have + the site's SQL connection info) +- ``namespace_id_to_name(id, all=False)``: given a namespace ID, returns the + primary associated namespace name (or a list of all names when ``all`` is + ``True``) +- ``namespace_name_to_id(name)``: given a namespace name, returns the + associated namespace ID +- ``get_page(title, follow_redirects=False)``: returns a ``Page`` object for + the given title (or a ``Category`` object if the page's namespace is + "``Category:``") +- ``get_category(catname, follow_redirects=False)``: returns a ``Category`` + object for the given title (sans namespace) +- ``get_user(username)``: returns a ``User`` object for the given username + +Pages (and Categories) +~~~~~~~~~~~~~~~~~~~~~~ + +Create ``Page`` objects with ``site.get_page(title)``, +``page.toggle_talk()``, ``user.get_userpage()``, or ``user.get_talkpage()``. +They provide the following attributes: + +- ``title``: the page's title, or pagename +- ``exists``: whether the page exists +- ``pageid``: an integer ID representing the page +- ``url``: the page's URL +- ``namespace``: the page's namespace as an integer +- ``protection``: the page's current protection status +- ``is_talkpage``: ``True`` if the page is a talkpage, else ``False`` +- ``is_redirect``: ``True`` if the page is a redirect, else ``False`` + +and the following methods: + +- ``reload()``: forcibly reload the page's attributes (emphasis on *reload* - + this is only necessary if there is reason to believe they have changed) +- ``toggle_talk(...)``: returns a content page's talk page, or vice versa +- ``get()``: returns page content +- ``get_redirect_target()``: if the page is a redirect, returns its destination +- ``get_creator()``: returns a ``User`` object representing the first user to + edit the page +- ``edit(text, summary, minor=False, bot=True, force=False)``: replaces the + page's content with ``text`` or creates a new page +- ``add_section(text, title, minor=False, bot=True, force=False)``: adds a new + section named ``title`` at the bottom of the page +- ``copyvio_check(...)``: checks the page for copyright violations +- ``copyvio_compare(url, ...)``: checks the page like ``copyvio_check()``, but + against a specific URL + +Additionally, ``Category`` objects (created with ``site.get_category(name)`` or +``site.get_page(title)`` where ``title`` is in the ``Category:`` namespace) +provide the following additional method: + +- ``get_members(use_sql=False, limit=None)``: returns a list of page titles in + the category (limit is ``50`` by default if using the API) + +Users +~~~~~ + +Create ``User`` objects with ``site.get_user(name)`` or +``page.get_creator()``. They provide the following attributes: + +- ``name``: the user's username +- ``exists``: ``True`` if the user exists, or ``False`` if they do not +- ``userid``: an integer ID representing the user +- ``blockinfo``: information about any current blocks on the user (``False`` if + no block, or a dict of ``{"by": blocking_user, "reason": block_reason, + "expiry": block_expire_time}``) +- ``groups``: a list of the user's groups +- ``rights``: a list of the user's rights +- ``editcount``: the number of edits made by the user +- ``registration``: the time the user registered as a ``time.struct_time`` +- ``emailable``: ``True`` if you can email the user, ``False`` if you cannot +- ``gender``: the user's gender (``"male"``, ``"female"``, or ``"unknown"``) + +and the following methods: + +- ``reload()``: forcibly reload the user's attributes (emphasis on *reload* - + this is only necessary if there is reason to believe they have changed) +- ``get_userpage()``: returns a ``Page`` object representing the user's + userpage +- ``get_talkpage()``: returns a ``Page`` object representing the user's + talkpage + +Additional features +~~~~~~~~~~~~~~~~~~~ + +Not all aspects of the toolset are covered here. Explore `its code and +docstrings`_ to learn how to use it in a more hands-on fashion. For reference, +``bot.wiki`` is an instance of ``earwigbot.wiki.SitesDB`` tied to the +``sites.db`` file in the bot's working directory. + +.. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ +.. _its code and docstrings: https://github.com/earwig/earwigbot/tree/develop/earwigbot/wiki From 035f7b80fcb4ca61324e6f1c49e92a56054ae22a Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 29 Apr 2012 03:32:31 -0400 Subject: [PATCH 07/12] Use Sphinx magic; typo fixes --- README.rst | 10 +- docs/customizing.rst | 326 ++++++++++++++++++++++++++++----------------------- docs/setup.rst | 13 +- 3 files changed, 193 insertions(+), 156 deletions(-) diff --git a/README.rst b/README.rst index 8bc850e..0952483 100644 --- a/README.rst +++ b/README.rst @@ -137,10 +137,10 @@ The most useful attributes are: when disconnecting from IRC. `earwigbot.config.BotConfig`_ stores configuration information for the bot. Its -``__doc__``string explains what each attribute is used for, but essentially -each "node" (one of ``config.components``, ``wiki``, ``tasks``, ``irc``, or -``metadata``) maps to a section of the bot's ``config.yml`` file. For example, -if ``config.yml`` includes something like:: +docstring explains what each attribute is used for, but essentially each "node" +(one of ``config.components``, ``wiki``, ``tasks``, ``irc``, and ``metadata``) +maps to a section of the bot's ``config.yml`` file. For example, if +``config.yml`` includes something like:: irc: frontend: @@ -160,7 +160,7 @@ Custom IRC commands Custom commands are subclasses of `earwigbot.commands.BaseCommand`_ that override ``BaseCommand``'s ``process()`` (and optionally ``check()``) methods. -``BaseCommand``'s docstrings should explain what each attribute andmethod is +``BaseCommand``'s docstrings should explain what each attribute and method is for and what they should be overridden with, but these are the basics: - Class attribute ``name`` is the name of the command. This must be specified. diff --git a/docs/customizing.rst b/docs/customizing.rst index 5a18d5b..04594dc 100644 --- a/docs/customizing.rst +++ b/docs/customizing.rst @@ -1,54 +1,63 @@ Customizing =========== -The bot's working directory contains a ``commands`` subdirectory and a -``tasks`` subdirectory. Custom IRC commands can be placed in the former, +The bot's working directory contains a :file:`commands` subdirectory and a +:file:`tasks` subdirectory. Custom IRC commands can be placed in the former, whereas custom wiki bot tasks go into the latter. Developing custom modules is explained in detail in this documentation. Note that custom commands will override built-in commands and tasks with the same name. -``Bot`` and ``BotConfig`` -------------------------- +:py:class:`~earwigbot.bot.Bot` and :py:class:`~earwigbot.bot.BotConfig` +----------------------------------------------------------------------- -`earwigbot.bot.Bot`_ is EarwigBot's main class. You don't have to instantiate -this yourself, but it's good to be familiar with its attributes and methods, -because it is the main way to communicate with other parts of the bot. A -``Bot`` object is accessible as an attribute of commands and tasks (i.e., -``self.bot``). +:py:class:`earwigbot.bot.Bot` is EarwigBot's main class. You don't have to +instantiate this yourself, but it's good to be familiar with its attributes and +methods, because it is the main way to communicate with other parts of the bot. +A :py:class:`~earwigbot.bot.Bot` object is accessible as an attribute of +commands and tasks (i.e., :py:attr:`self.bot`). The most useful attributes are: -- ``bot.config``: an instance of ``BotConfig``, for accessing the bot's - configuration data (see below). - -- ``bot.commands``: the bot's ``CommandManager``, which is used internally to - run IRC commands (through ``bot.commands.call()``, which you shouldn't have - to use); you can safely reload all commands with ``bot.commands.load()``. - -- ``bot.tasks``: the bot's ``TaskManager``, which can be used to start tasks - with ``bot.tasks.start(task_name, **kwargs)``. ``bot.tasks.load()`` can be - used to safely reload all tasks. - -- ``bot.frontend`` / ``bot.watcher``: instances of ``earwigbot.irc.Frontend`` - and ``earwigbot.irc.Watcher``, respectively, which represent the bot's - connections to these two servers; you can, for example, send a message to the - frontend with ``bot.frontend.say(chan, msg)`` (more on communicating with IRC - below). - -- ``bot.wiki``: interface with the :doc:`Wiki Toolset `. - -- Finally, ``bot.restart()`` (restarts IRC components and reloads config, - commands, and tasks) and ``bot.stop()`` can be used almost anywhere. Both - take an optional "reason" that will be logged and used as the quit message - when disconnecting from IRC. - -`earwigbot.config.BotConfig`_ stores configuration information for the bot. Its -``__doc__``string explains what each attribute is used for, but essentially -each "node" (one of ``config.components``, ``wiki``, ``tasks``, ``irc``, or -``metadata``) maps to a section of the bot's ``config.yml`` file. For example, -if ``config.yml`` includes something like:: +- :py:attr:`~earwigbot.bot.Bot.config`: an instance of + :py:class:`~earwigbot.bot.BotConfig`, for accessing the bot's configuration + data (see below). + +- :py:attr:`~earwigbot.bot.Bot.commands`: the bot's + :py:class:`~earwigbot.managers.CommandManager`, which is used internally to + run IRC commands (through :py:meth:`bot.commands.call`, which you shouldn't + have to use); you can safely reload all commands with + :py:meth:`~earwigbot.bot.Bot.commands.load`. + +- :py:attr:`~earwigbot.bot.Bot.tasks`: the bot's + :py:class:`~earwigbot.managers.TaskManager`, which can be used to start tasks + with :py:attr:`~earwigbot.bot.Bot.tasks.start(task_name, **kwargs)`. + :py:meth:`~earwigbot.bot.Bot.tasks.load` can be used to safely reload all + tasks. + +- :py:attr:`~earwigbot.bot.Bot.frontend` / + :py:attr:`~earwigbot.bot.Bot.watcher`: instances of + :py:class:`earwigbot.irc.Frontend` and :py:class:`earwigbot.irc.Watcher`, + respectively, which represent the bot's connections to these two servers; you + can, for example, send a message to the frontend with + :py:attr:`~earwigbot.bot.Bot.frontend.say(chan, msg)` (more on communicating + with IRC below). + +- :py:attr:`~earwigbot.bot.Bot.wiki`: interface with the + :doc:`Wiki Toolset `. + +- Finally, :py:meth:`~earwigbot.bot.Bot.restart` (restarts IRC components and + reloads config, commands, and tasks) and :py:meth:`~earwigbot.bot.Bot.stop` + can be used almost anywhere. Both take an optional "reason" that will be + logged and used as the quit message when disconnecting from IRC. + +:py:class:`earwigbot.config.BotConfig` stores configuration information for the +bot. Its docstring explains what each attribute is used for, but essentially +each "node" (one of :py:attr:`config.components`, :py:attr:`wiki`, +:py:attr:`tasks`, :py:attr:`tasks`, or :py:attr:`metadata`) maps to a section +of the bot's :file:`config.yml` file. For example, if :file:`config.yml` +includes something like:: irc: frontend: @@ -58,51 +67,63 @@ if ``config.yml`` includes something like:: - "#channel" - "#other-channel" -...then ``config.irc["frontend"]["nick"]`` will be ``"MyAwesomeBot"`` and -``config.irc["frontend"]["channels"]`` will be ``["##earwigbot", "#channel", -"#other-channel"]``. +...then :py:attr:`config.irc["frontend"]["nick"]` will be ``"MyAwesomeBot"`` +and :py:attr:`config.irc["frontend"]["channels"]` will be +``["##earwigbot", "#channel", "#other-channel"]``. Custom IRC commands ------------------- -Custom commands are subclasses of `earwigbot.commands.BaseCommand`_ that -override ``BaseCommand``'s ``process()`` (and optionally ``check()``) methods. - -``BaseCommand``'s docstrings should explain what each attribute andmethod is -for and what they should be overridden with, but these are the basics: - -- Class attribute ``name`` is the name of the command. This must be specified. - -- Class attribute ``hooks`` is a list of the "IRC events" that this command - might respond to. It defaults to ``["msg"]``, but options include - ``"msg_private"`` (for private messages only), ``"msg_public"`` (for channel - messages only), and ``"join"`` (for when a user joins a channel). See the - afc_status_ plugin for a command that responds to other hook types. - -- Method ``check()`` is passed a ``Data`` [1]_ object, and should return - ``True`` if you want to respond to this message, or ``False`` otherwise. The - default behavior is to return ``True`` only if ``data.is_command`` is - ``True`` and ``data.command == self.name``, which is suitable for most cases. - A common, straightforward reason for overriding is if a command has aliases - (see chanops_ for an example). Note that by returning ``True``, you prevent - any other commands from responding to this message. - -- Method ``process()`` is passed the same ``Data`` object as ``check()``, but - only if ``check()`` returned ``True``. This is where the bulk of your command - goes. To respond to IRC messages, there are a number of methods of - ``BaseCommand`` at your disposal. See the the test_ command for a simple - example, or look in BaseCommand's ``__init__`` method for the full list. - - The most common ones are ``self.say(chan_or_user, msg)``, - ``self.reply(data, msg)`` (convenience function; sends a reply to the +Custom commands are subclasses of :py:class:`earwigbot.commands.BaseCommand` +that override :py:class:`~earwigbot.commands.BaseCommand`'s +:py:meth:`~earwigbot.commands.BaseCommand.process` (and optionally +:py:meth:`~earwigbot.commands.BaseCommand.check`) methods. + +:py:class:`~earwigbot.commands.BaseCommand`'s docstrings should explain what +each attribute and method is for and what they should be overridden with, but +these are the basics: + +- Class attribute :py:attr:`~earwigbot.commands.BaseCommand.name` is the name + of the command. This must be specified. + +- Class attribute :py:attr:`~earwigbot.commands.BaseCommand.hooks` is a list of + the "IRC events" that this command might respond to. It defaults to + ``["msg"]``, but options include ``"msg_private"`` (for private messages + only), ``"msg_public"`` (for channel messages only), and ``"join"`` (for when + a user joins a channel). See the afc_status_ plugin for a command that + responds to other hook types. + +- Method :py:meth:`~earwigbot.commands.BaseCommand.check` is passed a + :py:class:`~earwigbot.irc.Data` [1]_ object, and should return ``True`` if + you want to respond to this message, or ``False`` otherwise. The default + behavior is to return ``True`` only if + :py:attr:`data.is_command` is ``True`` and :py:attr:`data.command` == + :py:attr:`~earwigbot.commands.BaseCommand.name`, which is suitable for most + cases. A common, straightforward reason for overriding is if a command has + aliases (see chanops_ for an example). Note that by returning ``True``, you + prevent any other commands from responding to this message. + +- Method :py:meth:`~earwigbot.commands.BaseCommand.process` is passed the same + :py:class:`~earwigbot.irc.Data` object as + :py:meth:`~earwigbot.commands.BaseCommand.check`, but only if + :py:meth:`~earwigbot.commands.BaseCommand.check` returned ``True``. This is + where the bulk of your command goes. To respond to IRC messages, there are a + number of methods of :py:class:`~earwigbot.commands.BaseCommand` at your + disposal. See the the test_ command for a simple example, or look in + :py:class:`~earwigbot.commands.BaseCommand`'s + :py:meth:`~earwigbot.commands.BaseCommand.__init__` method for the full list. + + The most common ones are :py:attr:`self.say(chan_or_user, msg)`, + :py:attr:`self.reply(data, msg)` (convenience function; sends a reply to the issuer of the command in the channel it was received), - ``self.action(chan_or_user, msg)``, ``self.notice(chan_or_user, msg)``, - ``self.join(chan)``, and ``self.part(chan)``. + :py:attr:`self.action(chan_or_user, msg)`, + :py:attr:`self.notice(chan_or_user, msg)`, :py:attr:`self.join(chan)`, and + :py:attr:`self.part(chan)`. -It's important to name the command class ``Command`` within the file, or else -the bot might not recognize it as a command. The name of the file doesn't -really matter and need not match the command's name, but this is recommended -for readability. +It's important to name the command class :py:class:`Command` within the file, +or else the bot might not recognize it as a command. The name of the file +doesn't really matter and need not match the command's name, but this is +recommended for readability. The bot has a wide selection of built-in commands and plugins to act as sample code and/or to give ideas. Start with test_, and then check out chanops_ and @@ -111,82 +132,97 @@ afc_status_ for some more complicated scripts. Custom bot tasks ---------------- -Custom tasks are subclasses of `earwigbot.tasks.BaseTask`_ that override -``BaseTask``'s ``run()`` (and optionally ``setup()``) methods. +Custom tasks are subclasses of :py:class:`earwigbot.tasks.BaseTask` that +override :py:class:`~earwigbot.tasks.BaseTask`'s +:py:meth:`~earwigbot.tasks.BaseTask.run` (and optionally +:py:meth:`~earwigbot.tasks.BaseTask.setup`) methods. -``BaseTask``'s docstrings should explain what each attribute and method is for -and what they should be overridden with, but these are the basics: +:py:class:`~earwigbot.tasks.BaseTask`'s docstrings should explain what each +attribute and method is for and what they should be overridden with, but these +are the basics: -- Class attribute ``name`` is the name of the task. This must be specified. +- Class attribute :py:attr:`~earwigbot.tasks.BaseTask.name` is the name of the + task. This must be specified. -- Class attribute ``number`` can be used to store an optional "task number", - possibly for use in edit summaries (to be generated with ``make_summary()``). - For example, EarwigBot's ``config.wiki["summary"]`` is +- Class attribute :py:attr:`~earwigbot.tasks.BaseTask.number` can be used to + store an optional "task number", possibly for use in edit summaries (to be + generated with :py:meth:`~earwigbot.tasks.BaseTask.make_summary`). For + example, EarwigBot's :py:attr:`config.wiki["summary"]` is ``"([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2"``, which the - task class's ``make_summary(comment)`` method will take and replace ``$1`` - with the task number and ``$2`` with the details of the edit. + task class's :py:attr:`~earwigbot.tasks.BaseTask.make_summary(comment)` + method will take and replace ``$1`` with the task number and ``$2`` with the + details of the edit. - Additionally, ``shutoff_enabled()`` (which checks whether the bot has been - told to stop on-wiki by checking the content of a particular page) can check - a different page for each task using similar variables. EarwigBot's - ``config.wiki["shutoff"]["page"]`` is ``"User:$1/Shutoff/Task $2"``; ``$1`` - is substituted with the bot's username, and ``$2`` is substituted with the - task number, so, e.g., task #14 checks the page - ``[[User:EarwigBot/Shutoff/Task 14]].`` If the page's content does *not* - match ``config.wiki["shutoff"]["disabled"]`` (``"run"`` by default), then - shutoff is considered to be *enabled* and ``shutoff_enabled()`` will return - ``True``, indicating the task should not run. If you don't intend to use - either of these methods, feel free to leave this attribute blank. - -- Method ``setup()`` is called *once* with no arguments immediately after the - task is first loaded. Does nothing by default; treat it like an - ``__init__()`` if you want (``__init__()`` does things by default and a - dedicated setup method is often easier than overriding ``__init__()`` and - using ``super``). - -- Method ``run()`` is called with any number of keyword arguments every time - the task is executed (by ``bot.tasks.start(task_name, **kwargs)``, usually). - This is where the bulk of the task's code goes. For interfacing with - MediaWiki sites, read up on the :doc:`Wiki Toolset `. - -Tasks have access to ``config.tasks[task_name]`` for config information, which -is a node in ``config.yml`` like every other attribute of ``bot.config``. This -can be used to store, for example, edit summaries, or templates to append to -user talk pages, so that these can be easily changed without modifying the task -itself. - -It's important to name the task class ``Task`` within the file, or else the bot -might not recognize it as a task. The name of the file doesn't really matter -and need not match the task's name, but this is recommended for readability. + Additionally, :py:meth:`~earwigbot.tasks.BaseTask.shutoff_enabled` (which + checks whether the bot has been told to stop on-wiki by checking the content + of a particular page) can check a different page for each task using similar + variables. EarwigBot's :py:attr:`config.wiki["shutoff"]["page"]` is + ``"User:$1/Shutoff/Task $2"``; ``$1`` is substituted with the bot's username, + and ``$2`` is substituted with the task number, so, e.g., task #14 checks the + page ``[[User:EarwigBot/Shutoff/Task 14]].`` If the page's content does *not* + match :py:attr:`config.wiki["shutoff"]["disabled"]` (``"run"`` by default), + then shutoff is considered to be *enabled* and + :py:meth:`~earwigbot.tasks.BaseTask.shutoff_enabled` will return ``True``, + indicating the task should not run. If you don't intend to use either of + these methods, feel free to leave this attribute blank. + +- Method :py:meth:`~earwigbot.tasks.BaseTask.setup` is called *once* with no + arguments immediately after the task is first loaded. Does nothing by + default; treat it like an :py:meth:`__init__` if you want + (:py:meth:`~earwigbot.tasks.BaseTask.__init__` does things by default and a + dedicated setup method is often easier than overriding + :py:meth:`~earwigbot.tasks.BaseTask.__init__` and using :py:obj:`super`). + +- Method :py:meth:`~earwigbot.tasks.BaseTask.run` is called with any number of + keyword arguments every time the task is executed (by + :py:attr:`bot.tasks.start(task_name, **kwargs)`, usually). This is where the + bulk of the task's code goes. For interfacing with MediaWiki sites, read up + on the :doc:`Wiki Toolset `. + +Tasks have access to :py:attr:`config.tasks[task_name]` for config information, +which is a node in :file:`config.yml` like every other attribute of +:py:attr:`bot.config`. This can be used to store, for example, edit summaries, +or templates to append to user talk pages, so that these can be easily changed +without modifying the task itself. + +It's important to name the task class :py:class:`Task` within the file, or else +the bot might not recognize it as a task. The name of the file doesn't really +matter and need not match the task's name, but this is recommended for +readability. See the built-in wikiproject_tagger_ task for a relatively straightforward task, or the afc_statistics_ plugin for a more complicated one. .. rubric:: Footnotes -.. [1] ``Data`` objects are instances of ``earwigbot.irc.Data`` that contain - information about a single message sent on IRC. Their useful attributes - are ``chan`` (channel the message was sent from, equal to ``nick`` if - it's a private message), ``nick`` (nickname of the sender), ``ident`` - (ident_ of the sender), ``host`` (hostname of the sender), ``msg`` (text - of the sent message), ``is_command`` (boolean telling whether or not - this message is a bot command, i.e., whether it is prefixed by ``!``), - ``command`` (if the message is a command, this is the name of the - command used), and ``args`` (if the message is a command, this is a list - of the command arguments - for example, if issuing "``!part ##earwig - Goodbye guys``", ``args`` will equal ``["##earwig", "Goodbye", - "guys"]``). Note that not all ``Data`` objects will have all of these - attributes: ``Data`` objects generated by private messages will, but - ones generated by joins will only have ``chan``, ``nick``, ``ident``, - and ``host``. - -.. _earwigbot.bot.Bot: https://github.com/earwig/earwigbot/blob/develop/earwigbot/bot.py -.. _earwigbot.config.BotConfig: https://github.com/earwig/earwigbot/blob/develop/earwigbot/config.py -.. _earwigbot.commands.BaseCommand: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/__init__.py -.. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py -.. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py -.. _test: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/test.py -.. _earwigbot.tasks.BaseTask: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/__init__.py -.. _wikiproject_tagger: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/wikiproject_tagger.py -.. _afc_statistics: https://github.com/earwig/earwigbot-plugins/blob/develop/tasks/afc_statistics.py -.. _ident: http://en.wikipedia.org/wiki/Ident +.. [1] :py:class:`~earwigbot.irc.Data` objects are instances of + :py:class:`earwigbot.irc.Data` that contain information about a single + message sent on IRC. Their useful attributes are + :py:attr:`~earwigbot.irc.Data.chan` (channel the message was sent from, + equal to :py:attr:`~earwigbot.irc.Data.nick` if it's a private message), + :py:attr:`~earwigbot.irc.Data.nick` (nickname of the sender), + :py:attr:`~earwigbot.irc.Data.ident` (ident_ of the sender), + :py:attr:`~earwigbot.irc.Data.host` (hostname of the sender), + :py:attr:`~earwigbot.irc.Data.msg` (text of the sent message), + :py:attr:`~earwigbot.irc.Data.is_command` (boolean telling whether or + not this message is a bot command, e.g., whether it is prefixed by + ``!``), :py:attr:`~earwigbot.irc.Data.command` (if the message is a + command, this is the name of the command used), and + :py:attr:`~earwigbot.irc.Data.args` (if the message is a command, this + is a list of the command arguments - for example, if issuing + "``!part ##earwig Goodbye guys``", :py:attr:`~earwigbot.irc.Data.args` + will equal ``["##earwig", "Goodbye", "guys"]``). Note that not all + :py:class:`~earwigbot.irc.Data` objects will have all of these + attributes: :py:class:`~earwigbot.irc.Data` objects generated by private + messages will, but ones generated by joins will only have + :py:attr:`~earwigbot.irc.Data.chan`, + :py:attr:`~earwigbot.irc.Data.nick`, + :py:attr:`~earwigbot.irc.Data.ident`, + and :py:attr:`~earwigbot.irc.Data.host`. + +.. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py +.. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py +.. _test: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/test.py +.. _wikiproject_tagger: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/wikiproject_tagger.py +.. _afc_statistics: https://github.com/earwig/earwigbot-plugins/blob/develop/tasks/afc_statistics.py +.. _ident: http://en.wikipedia.org/wiki/Ident diff --git a/docs/setup.rst b/docs/setup.rst index e4feb85..ea01b49 100644 --- a/docs/setup.rst +++ b/docs/setup.rst @@ -6,13 +6,14 @@ databases. This is also the location where you will place custom IRC commands and bot tasks, which will be explained later. It doesn't matter where this directory is, as long as the bot can write to it. -Start the bot with ``earwigbot path/to/working/dir``, or just ``earwigbot`` if -the working directory is the current directory. It will notice that no -``config.yml`` file exists and take you through the setup process. +Start the bot with :command:`earwigbot path/to/working/dir`, or just +:command:`earwigbot` if the working directory is the current directory. It will +notice that no :file:`config.yml` file exists and take you through the setup +process. -There is currently no way to edit the ``config.yml`` file from within the bot -after it has been created, but YAML is a very straightforward format, so you -should be able to make any necessary changes yourself. Check out the +There is currently no way to edit the :file:`config.yml` file from within the +bot after it has been created, but YAML is a very straightforward format, so +you should be able to make any necessary changes yourself. Check out the `explanation of YAML`_ on Wikipedia for help. After setup, the bot will start. This means it will connect to the IRC servers From c51c83e94eebff460276b8c689bc8d422e7f7acd Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 29 Apr 2012 03:55:33 -0400 Subject: [PATCH 08/12] More documentation updates --- docs/customizing.rst | 41 +++++++++++++++++++++++------------------ docs/index.rst | 1 - docs/installation.rst | 35 ++++++++++++++++++----------------- docs/setup.rst | 2 +- docs/tips.rst | 51 ++++++++++++++++++++++++++++----------------------- docs/toolset.rst | 4 ++-- 6 files changed, 72 insertions(+), 62 deletions(-) diff --git a/docs/customizing.rst b/docs/customizing.rst index 04594dc..3d5bac6 100644 --- a/docs/customizing.rst +++ b/docs/customizing.rst @@ -26,14 +26,16 @@ The most useful attributes are: - :py:attr:`~earwigbot.bot.Bot.commands`: the bot's :py:class:`~earwigbot.managers.CommandManager`, which is used internally to - run IRC commands (through :py:meth:`bot.commands.call`, which you shouldn't - have to use); you can safely reload all commands with - :py:meth:`~earwigbot.bot.Bot.commands.load`. + run IRC commands (through + :py:meth:`commands.call() `, which + you shouldn't have to use); you can safely reload all commands with + :py:meth:`commands.load() `. - :py:attr:`~earwigbot.bot.Bot.tasks`: the bot's :py:class:`~earwigbot.managers.TaskManager`, which can be used to start tasks - with :py:attr:`~earwigbot.bot.Bot.tasks.start(task_name, **kwargs)`. - :py:meth:`~earwigbot.bot.Bot.tasks.load` can be used to safely reload all + with :py:meth:`tasks.start(task_name, **kwargs) + `. :py:meth:`tasks.load() + ` can be used to safely reload all tasks. - :py:attr:`~earwigbot.bot.Bot.frontend` / @@ -41,8 +43,8 @@ The most useful attributes are: :py:class:`earwigbot.irc.Frontend` and :py:class:`earwigbot.irc.Watcher`, respectively, which represent the bot's connections to these two servers; you can, for example, send a message to the frontend with - :py:attr:`~earwigbot.bot.Bot.frontend.say(chan, msg)` (more on communicating - with IRC below). + :py:meth:`frontend.say(chan, msg) ` (more on + communicating with IRC below). - :py:attr:`~earwigbot.bot.Bot.wiki`: interface with the :doc:`Wiki Toolset `. @@ -113,12 +115,14 @@ these are the basics: :py:class:`~earwigbot.commands.BaseCommand`'s :py:meth:`~earwigbot.commands.BaseCommand.__init__` method for the full list. - The most common ones are :py:attr:`self.say(chan_or_user, msg)`, - :py:attr:`self.reply(data, msg)` (convenience function; sends a reply to the + The most common ones are :py:meth:`say(chan_or_user, msg) + `, :py:meth:`reply(data, msg) + ` (convenience function; sends a reply to the issuer of the command in the channel it was received), - :py:attr:`self.action(chan_or_user, msg)`, - :py:attr:`self.notice(chan_or_user, msg)`, :py:attr:`self.join(chan)`, and - :py:attr:`self.part(chan)`. + :py:meth:`action(chan_or_user, msg) `, + :py:meth:`notice(chan_or_user, msg) `, + :py:meth:`join(chan) `, and + :py:meth:`part(chan) `. It's important to name the command class :py:class:`Command` within the file, or else the bot might not recognize it as a command. The name of the file @@ -149,9 +153,9 @@ are the basics: generated with :py:meth:`~earwigbot.tasks.BaseTask.make_summary`). For example, EarwigBot's :py:attr:`config.wiki["summary"]` is ``"([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2"``, which the - task class's :py:attr:`~earwigbot.tasks.BaseTask.make_summary(comment)` - method will take and replace ``$1`` with the task number and ``$2`` with the - details of the edit. + task class's :py:meth:`make_summary(comment) + ` method will take and replace + ``$1`` with the task number and ``$2`` with the details of the edit. Additionally, :py:meth:`~earwigbot.tasks.BaseTask.shutoff_enabled` (which checks whether the bot has been told to stop on-wiki by checking the content @@ -175,9 +179,10 @@ are the basics: - Method :py:meth:`~earwigbot.tasks.BaseTask.run` is called with any number of keyword arguments every time the task is executed (by - :py:attr:`bot.tasks.start(task_name, **kwargs)`, usually). This is where the - bulk of the task's code goes. For interfacing with MediaWiki sites, read up - on the :doc:`Wiki Toolset `. + :py:meth:`tasks.start(task_name, **kwargs) + `, usually). This is where the bulk of + the task's code goes. For interfacing with MediaWiki sites, read up on the + :doc:`Wiki Toolset `. Tasks have access to :py:attr:`config.tasks[task_name]` for config information, which is a node in :file:`config.yml` like every other attribute of diff --git a/docs/index.rst b/docs/index.rst index c5aa2e9..987eca2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -45,4 +45,3 @@ Indices and tables * :ref:`genindex` * :ref:`modindex` * :ref:`search` - diff --git a/docs/installation.rst b/docs/installation.rst index 39996c9..857329d 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -1,23 +1,23 @@ Installation ============ -This package contains the core ``earwigbot``, abstracted enough that it should -be usable and customizable by anyone running a bot on a MediaWiki site. Since -it is component-based, the IRC components can be disabled if desired. IRC +This package contains the core :py:mod:`earwigbot`, abstracted enough that it +should be usable and customizable by anyone running a bot on a MediaWiki site. +Since it is component-based, the IRC components can be disabled if desired. IRC commands and bot tasks specific to `my instance of EarwigBot`_ that I don't feel the average user will need are available from the repository `earwigbot-plugins`_. -It's recommended to run the bot's unit tests before installing. Run ``python -setup.py test`` from the project's root directory. Note that some -tests require an internet connection, and others may take a while to run. +It's recommended to run the bot's unit tests before installing. Run +:command:`python setup.py test` from the project's root directory. Note that +some tests require an internet connection, and others may take a while to run. Coverage is currently rather incomplete. Latest release (v0.1) --------------------- EarwigBot is available from the `Python Package Index`_, so you can install the -latest release with ``pip install earwigbot`` (`get pip`_). +latest release with :command:`pip install earwigbot` (`get pip`_). You can also install it from source [1]_ directly:: @@ -31,11 +31,11 @@ You can also install it from source [1]_ directly:: Development version ------------------- -You can install the development version of the bot from ``git`` by using -setuptools/distribute's ``develop`` command [1]_, probably on the ``develop`` -branch which contains (usually) working code. ``master`` contains the latest -release. EarwigBot uses `git flow`_, so you're free to -browse by tags or by new features (``feature/*`` branches):: +You can install the development version of the bot from :command:`git` by using +setuptools/`distribute`_'s :command:`develop` command [1]_, probably on the +``develop`` branch which contains (usually) working code. ``master`` contains +the latest release. EarwigBot uses `git flow`_, so you're free to browse by +tags or by new features (``feature/*`` branches):: git clone git://github.com/earwig/earwigbot.git earwigbot cd earwigbot @@ -46,8 +46,9 @@ browse by tags or by new features (``feature/*`` branches):: .. [1] ``python setup.py install``/``develop`` may require root, or use the ``--user`` switch to install for the current user only. -.. _my instance of EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot -.. _earwigbot-plugins: https://github.com/earwig/earwigbot-plugins -.. _Python Package Index: http://pypi.python.org -.. _get pip: http://pypi.python.org/pypi/pip -.. _git flow: http://nvie.com/posts/a-successful-git-branching-model/ +.. _my instance of EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot +.. _earwigbot-plugins: https://github.com/earwig/earwigbot-plugins +.. _Python Package Index: http://pypi.python.org +.. _get pip: http://pypi.python.org/pypi/pip +.. _distribute: http://pypi.python.org/pypi/distribute +.. _git flow: http://nvie.com/posts/a-successful-git-branching-model/ diff --git a/docs/setup.rst b/docs/setup.rst index ea01b49..81523df 100644 --- a/docs/setup.rst +++ b/docs/setup.rst @@ -25,4 +25,4 @@ You can stop the bot at any time with :kbd:`Control-c`, same as you stop a normal Python program, and it will try to exit safely. You can also use the "``!quit``" command on IRC. -.. _explanation of YAML: http://en.wikipedia.org/wiki/YAML +.. _explanation of YAML: http://en.wikipedia.org/wiki/YAML diff --git a/docs/tips.rst b/docs/tips.rst index 7a4e34a..6836b7c 100644 --- a/docs/tips.rst +++ b/docs/tips.rst @@ -3,21 +3,24 @@ Tips - Logging_ is a fantastic way to monitor the bot's progress as it runs. It has a slew of built-in loggers, and enabling log retention (so logs are saved to - ``logs/`` in the working directory) is highly recommended. In the normal + :file:`logs/` in the working directory) is highly recommended. In the normal setup, there are three log files, each of which "rotate" at a specific time - (``filename.log`` becomes ``filename.log.2012-04-10``, for example). The - ``debug.log`` file rotates every hour, and maintains six hours of logs of - every level (``DEBUG`` and up). ``bot.log`` rotates every day at midnight, - and maintains seven days of non-debug logs (``INFO`` and up). Finally, - ``error.log`` rotates every Sunday night, and maintains four weeks of logs - indicating unexpected events (``WARNING`` and up). - - To use logging in your commands or tasks (recommended), ``BaseCommand`` and - ``BaseTask`` provide ``logger`` attributes configured for the specific - command or task. If you're working with other classes, ``bot.logger`` is the - root logger (``logging.getLogger("earwigbot")`` by default), so you can use - ``getChild`` to make your logger. For example, task loggers are essentially - ``bot.logger.getChild("tasks").getChild(task.name)``. + (:file:`filename.log` becomes :file:`filename.log.2012-04-10`, for example). + The :file:`debug.log` file rotates every hour, and maintains six hours of + logs of every level (``DEBUG`` and up). :file:`bot.log` rotates every day at + midnight, and maintains seven days of non-debug logs (``INFO`` and up). + Finally, :file:`error.log` rotates every Sunday night, and maintains four + weeks of logs indicating unexpected events (``WARNING`` and up). + + To use logging in your commands or tasks (recommended), + :py:class:~earwigbot.commands.BaseCommand` and + :py:class:~earwigbot.tasks.BaseTask` provide :py:attr:`logger` attributes + configured for the specific command or task. If you're working with other + classes, :py:attr:`bot.logger` is the root logger + (:py:obj:`logging.getLogger("earwigbot")` by default), so you can use + :py:func:`~logging.Logger.getChild` to make your logger. For example, task + loggers are essentially + :py:attr:`bot.logger.getChild("tasks").getChild(task.name) `. - A very useful IRC command is "``!reload``", which reloads all commands and tasks without restarting the bot. [1]_ Combined with using the `!git plugin`_ @@ -25,17 +28,19 @@ Tips development workflow if the bot runs on an external server and you set up its working directory as a git repo. -- You can run a task by itself instead of the entire bot with ``earwigbot - path/to/working/dir --task task_name``. +- You can run a task by itself instead of the entire bot with + :command:`earwigbot path/to/working/dir --task task_name`. -- Questions, comments, or suggestions about the documentation? `Let me know`_ - so I can improve it for other people. +- Questions, comments, or suggestions about the documentation? `Let me know`_, + or `create an issue`_ so I can improve it for other people. .. rubric:: Footnotes -.. [1] In reality, all this does is call ``bot.commands.load()`` and - ``bot.tasks.load()``! +.. [1] In reality, all this does is call :py:meth:`bot.commands.load() + ` and + :py:meth:`bot.tasks.load() `! -.. _logging: http://docs.python.org/library/logging.html -.. _Let me know: ben.kurtovic@verizon.net -.. _!git plugin: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/git.py +.. _logging: http://docs.python.org/library/logging.html +.. _!git plugin: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/git.py +.. _Let me know: ben.kurtovic@verizon.net +.. _create an issue: https://github.com/earwig/earwigbot/issues diff --git a/docs/toolset.rst b/docs/toolset.rst index 6b8a719..dba7700 100644 --- a/docs/toolset.rst +++ b/docs/toolset.rst @@ -166,5 +166,5 @@ docstrings`_ to learn how to use it in a more hands-on fashion. For reference, ``bot.wiki`` is an instance of ``earwigbot.wiki.SitesDB`` tied to the ``sites.db`` file in the bot's working directory. -.. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ -.. _its code and docstrings: https://github.com/earwig/earwigbot/tree/develop/earwigbot/wiki +.. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ +.. _its code and docstrings: https://github.com/earwig/earwigbot/tree/develop/earwigbot/wiki From 17e29a1b84893128117c88a49af85e494f9d8e42 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 29 Apr 2012 04:25:14 -0400 Subject: [PATCH 09/12] Updates to wiki toolset documentation --- README.rst | 2 +- docs/toolset.rst | 226 +++++++++++++++++++++++++++++++++---------------------- 2 files changed, 135 insertions(+), 93 deletions(-) diff --git a/README.rst b/README.rst index 0952483..cafdc9f 100644 --- a/README.rst +++ b/README.rst @@ -306,7 +306,7 @@ something more like:: try: site = bot.wiki.get_site(project=project, lang=lang) except earwigbot.SiteNotFoundError: - Load site info from http://mysite.net/mywiki/it/s/api.php: + # Load site info from http://mysite.net/mywiki/it/s/api.php: base_url = "http://mysite.net/" + project + "/" + lang db_name = lang + project + "_p" sql = {host: "sql.mysite.net", db: db_name} diff --git a/docs/toolset.rst b/docs/toolset.rst index dba7700..7e85a50 100644 --- a/docs/toolset.rst +++ b/docs/toolset.rst @@ -2,13 +2,16 @@ The Wiki Toolset ================ EarwigBot's answer to the `Pywikipedia framework`_ is the Wiki Toolset -(``earwigbot.wiki``), which you will mainly access through ``bot.wiki``. +(:py:mod:`earwigbot.wiki`), which you will mainly access through +:py:attr:`bot.wiki `. -``bot.wiki`` provides three methods for the management of Sites - -``get_site()``, ``add_site()``, and ``remove_site()``. Sites are objects that -simply represent a MediaWiki site. A single instance of EarwigBot (i.e. a -single *working directory*) is expected to relate to a single site or group of -sites using the same login info (like all WMF wikis with CentralAuth). +:py:attr:`bot.wiki ` provides three methods for the +management of Sites - :py:meth:`~earwigbot.wiki.SiteDB.get_site`, +:py:meth:`~earwigbot.wiki.SiteDB.add_site`, and +:py:meth:`~earwigbot.wiki.SiteDB.remove_site`. Sites are objects that simply +represent a MediaWiki site. A single instance of EarwigBot (i.e. a single +*working directory*) is expected to relate to a single site or group of sites +using the same login info (like all WMF wikis with CentralAuth). Load your default site (the one that you picked during setup) with ``site = bot.wiki.get_site()``. @@ -19,20 +22,22 @@ Dealing with other sites *Skip this section if you're only working with one site.* If a site is *already known to the bot* (meaning that it is stored in the -``sites.db`` file, which includes just your default wiki at first), you can +:file:`sites.db` file, which includes just your default wiki at first), you can load a site with ``site = bot.wiki.get_site(name)``, where ``name`` might be ``"enwiki"`` or ``"frwiktionary"`` (you can also do ``site = bot.wiki.get_site(project="wikipedia", lang="en")``). Recall that not giving any arguments to ``get_site()`` will return the default site. -``add_site()`` is used to add new sites to the sites database. It may be called -with similar arguments as ``get_site()``, but the difference is important. -``get_site()`` only needs enough information to identify the site in its -database, which is usually just its name; the database stores all other -necessary connection info. With ``add_site()``, you need to provide enough -connection info so the toolset can successfully access the site's API/SQL -databases and store that information for later. That might not be much; for -WMF wikis, you can usually use code like this:: +:py:meth:`~earwigbot.wiki.SiteDB.add_site` is used to add new sites to the sites +database. It may be called with similar arguments as +:py:meth:`~earwigbot.wiki.SiteDB.get_site`, but the difference is important. +:py:meth:`~earwigbot.wiki.SiteDB.get_site` only needs enough information to +identify the site in its database, which is usually just its name; the database +stores all other necessary connection info. With +:py:meth:`~earwigbot.wiki.SiteDB.add_site`, you need to provide enough connection +info so the toolset can successfully access the site's API/SQL databases and +store that information for later. That might not be much; for WMF wikis, you +can usually use code like this:: project, lang = "wikipedia", "es" try: @@ -50,121 +55,158 @@ something more like:: try: site = bot.wiki.get_site(project=project, lang=lang) except earwigbot.SiteNotFoundError: - Load site info from http://mysite.net/mywiki/it/s/api.php: + # Load site info from http://mysite.net/mywiki/it/s/api.php: base_url = "http://mysite.net/" + project + "/" + lang db_name = lang + project + "_p" sql = {host: "sql.mysite.net", db: db_name} site = bot.wiki.add_site(base_url=base_url, script_path="/s", sql=sql) -``remove_site()`` does the opposite of ``add_site()``: give it a site's name -or a project/lang pair like ``get_site()`` takes, and it'll remove that site -from the sites database. +:py:meth:`~earwigbot.wiki.SiteDB.remove_site` does the opposite of +:py:meth:`~earwigbot.wiki.SiteDB.add_site`: give it a site's name or a +project/lang pair like :py:meth:`~earwigbot.wiki.SiteDB.get_site` takes, and +it'll remove that site from the sites database. Sites ~~~~~ -``Site`` objects provide the following attributes: +:py:class:`earwigbot.wiki.Site` objects provide the following attributes: -- ``name``: the site's name (or "wikiid"), like ``"enwiki"`` -- ``project``: the site's project name, like ``"wikipedia"`` -- ``lang``: the site's language code, like ``"en"`` -- ``domain``: the site's web domain, like ``"en.wikipedia.org"`` +- :py:attr:`~earwigbot.wiki.Site.name`: the site's name (or "wikiid"), like + ``"enwiki"`` +- :py:attr:`~earwigbot.wiki.Site.project`: the site's project name, like + ``"wikipedia"`` +- :py:attr:`~earwigbot.wiki.Site.lang`: the site's language code, like ``"en"`` +- :py:attr:`~earwigbot.wiki.Site.domain`: the site's web domain, like + ``"en.wikipedia.org"`` and the following methods: -- ``api_query(**kwargs)``: does an API query with the given keyword arguments - as params -- ``sql_query(query, params=(), ...)``: does an SQL query and yields its - results (as a generator) -- ``get_replag()``: returns the estimated database replication lag (if we have - the site's SQL connection info) -- ``namespace_id_to_name(id, all=False)``: given a namespace ID, returns the - primary associated namespace name (or a list of all names when ``all`` is +- :py:meth:`api_query(**kwargs) `: does an API + query with the given keyword arguments as params +- :py:meth:`sql_query(query, params=(), ...) `: + does an SQL query and yields its results (as a generator) +- :py:meth:`~earwigbot.wiki.Site.get_replag`: returns the estimated database + replication lag (if we have the site's SQL connection info) +- :py:meth:`namespace_id_to_name(id, all=False) + `: given a namespace ID, returns + the primary associated namespace name (or a list of all names when ``all`` is ``True``) -- ``namespace_name_to_id(name)``: given a namespace name, returns the - associated namespace ID -- ``get_page(title, follow_redirects=False)``: returns a ``Page`` object for - the given title (or a ``Category`` object if the page's namespace is - "``Category:``") -- ``get_category(catname, follow_redirects=False)``: returns a ``Category`` - object for the given title (sans namespace) -- ``get_user(username)``: returns a ``User`` object for the given username +- :py:meth:`namespace_name_to_id(name) + `: given a namespace name, returns + the associated namespace ID +- :py:meth:`get_page(title, follow_redirects=False) + `: returns a ``Page`` object for the given + title (or a :py:class:`~earwigbot.wiki.Category` object if the page's + namespace is "``Category:``") +- :py:meth:`get_category(catname, follow_redirects=False) + `: returns a ``Category`` object for the + given title (sans namespace) +- :py:meth:`get_user(username) `: returns a + :py:class:`~earwigbot.wiki.User` object for the given username Pages (and Categories) ~~~~~~~~~~~~~~~~~~~~~~ -Create ``Page`` objects with ``site.get_page(title)``, -``page.toggle_talk()``, ``user.get_userpage()``, or ``user.get_talkpage()``. -They provide the following attributes: - -- ``title``: the page's title, or pagename -- ``exists``: whether the page exists -- ``pageid``: an integer ID representing the page -- ``url``: the page's URL -- ``namespace``: the page's namespace as an integer -- ``protection``: the page's current protection status -- ``is_talkpage``: ``True`` if the page is a talkpage, else ``False`` -- ``is_redirect``: ``True`` if the page is a redirect, else ``False`` +Create :py:class:`earwigbot.wiki.Page` objects with +:py:meth:`site.get_page(title) `, +:py:meth:`page.toggle_talk() `, +:py:meth:`user.get_userpage() `, or +:py:meth:`user.get_talkpage() `. They provide +the following attributes: + +- :py:attr:`~earwigbot.wiki.Page.title`: the page's title, or pagename +- :py:attr:`~earwigbot.wiki.Page.exists`: whether the page exists +- :py:attr:`~earwigbot.wiki.Page.pageid`: an integer ID representing the page +- :py:attr:`~earwigbot.wiki.Page.url`: the page's URL +- :py:attr:`~earwigbot.wiki.Page.namespace`: the page's namespace as an integer +- :py:attr:`~earwigbot.wiki.Page.protection`: the page's current protection + status +- :py:attr:`~earwigbot.wiki.Page.is_talkpage`: ``True`` if the page is a + talkpage, else ``False`` +- :py:attr:`~earwigbot.wiki.Page.is_redirect`: ``True`` if the page is a + redirect, else ``False`` and the following methods: -- ``reload()``: forcibly reload the page's attributes (emphasis on *reload* - - this is only necessary if there is reason to believe they have changed) -- ``toggle_talk(...)``: returns a content page's talk page, or vice versa -- ``get()``: returns page content -- ``get_redirect_target()``: if the page is a redirect, returns its destination -- ``get_creator()``: returns a ``User`` object representing the first user to - edit the page -- ``edit(text, summary, minor=False, bot=True, force=False)``: replaces the - page's content with ``text`` or creates a new page -- ``add_section(text, title, minor=False, bot=True, force=False)``: adds a new - section named ``title`` at the bottom of the page -- ``copyvio_check(...)``: checks the page for copyright violations -- ``copyvio_compare(url, ...)``: checks the page like ``copyvio_check()``, but +- :py:meth:`~earwigbot.wiki.Page.reload`: forcibly reload the page's attributes + (emphasis on *reload* - this is only necessary if there is reason to believe + they have changed) +- :py:meth:`toggle_talk(...) `: returns a + content page's talk page, or vice versa +- :py:meth:`~earwigbot.wiki.Page.get`: returns page content +- :py:meth:`~earwigbot.wiki.Page.get_redirect_target`: if the page is a + redirect, returns its destination +- :py:meth:`~earwigbot.wiki.Page.get_creator`: returns a + :py:class:`~earwigbot.wiki.User` object representing the first user to edit + the page +- :py:meth:`edit(text, summary, minor=False, bot=True, force=False) + `: replaces the page's content with ``text`` or + creates a new page +- :py:meth:`add_section(text, title, minor=False, bot=True, force=False) + `: adds a new section named ``title`` at the + bottom of the page +- :py:meth:`copyvio_check(...) + `: checks the page for + copyright violations +- :py:meth:`copyvio_compare(url, ...) + `: checks the page like + :py:meth:`~earwigbot.wiki.copyvios.CopyvioMixin.copyvio_check`, but against a specific URL -Additionally, ``Category`` objects (created with ``site.get_category(name)`` or -``site.get_page(title)`` where ``title`` is in the ``Category:`` namespace) -provide the following additional method: +Additionally, :py:class:`~earwigbot.wiki.Category` objects (created with +:py:meth:`site.get_category(name) ` or +:py:meth:`site.get_page(title) ` where ``title`` +is in the ``Category:`` namespace) provide the following additional method: -- ``get_members(use_sql=False, limit=None)``: returns a list of page titles in - the category (limit is ``50`` by default if using the API) +- :py:meth:`get_members(use_sql=False, limit=None) + `: returns a list of page titles in the + category (limit is ``50`` by default if using the API) Users ~~~~~ -Create ``User`` objects with ``site.get_user(name)`` or -``page.get_creator()``. They provide the following attributes: - -- ``name``: the user's username -- ``exists``: ``True`` if the user exists, or ``False`` if they do not -- ``userid``: an integer ID representing the user -- ``blockinfo``: information about any current blocks on the user (``False`` if - no block, or a dict of ``{"by": blocking_user, "reason": block_reason, +Create :py:class:`earwigbot.wiki.User` objects with +:py:meth:`site.get_user(name) ` or +:py:meth:`page.get_creator() `. They provide +the following attributes: + +- :py:attr:`~earwigbot.wiki.User.name`: the user's username +- :py:attr:`~earwigbot.wiki.User.exists`: ``True`` if the user exists, or + ``False`` if they do not +- :py:attr:`~earwigbot.wiki.User.userid`: an integer ID representing the user +- :py:attr:`~earwigbot.wiki.User.blockinfo`: information about any current + blocks on the user (``False`` if no block, or a dict of + ``{"by": blocking_user, "reason": block_reason, "expiry": block_expire_time}``) -- ``groups``: a list of the user's groups -- ``rights``: a list of the user's rights -- ``editcount``: the number of edits made by the user -- ``registration``: the time the user registered as a ``time.struct_time`` -- ``emailable``: ``True`` if you can email the user, ``False`` if you cannot -- ``gender``: the user's gender (``"male"``, ``"female"``, or ``"unknown"``) +- :py:attr:`~earwigbot.wiki.User.groups`: a list of the user's groups +- :py:attr:`~earwigbot.wiki.User.rights`: a list of the user's rights +- :py:attr:`~earwigbot.wiki.User.editcount`: the number of edits made by the + user +- :py:attr:`~earwigbot.wiki.User.registration`: the time the user registered as + a :py:obj:`time.struct_time` +- :py:attr:`~earwigbot.wiki.User.emailable`: ``True`` if you can email the + user, ``False`` if you cannot +- :py:attr:`~earwigbot.wiki.User.gender`: the user's gender (``"male"``, + ``"female"``, or ``"unknown"``) and the following methods: -- ``reload()``: forcibly reload the user's attributes (emphasis on *reload* - - this is only necessary if there is reason to believe they have changed) -- ``get_userpage()``: returns a ``Page`` object representing the user's - userpage -- ``get_talkpage()``: returns a ``Page`` object representing the user's - talkpage +- :py:meth:`~earwigbot.wiki.User.reload`: forcibly reload the user's attributes + (emphasis on *reload* - this is only necessary if there is reason to believe + they have changed) +- :py:meth:`~earwigbot.wiki.User.get_userpage`: returns a + :py:class:`~earwigbot.wiki.Page` object representing the user's userpage +- :py:meth:`~earwigbot.wiki.User.get_talkpage`: returns a + :py:class:`~earwigbot.wiki.Page` object representing the user's talkpage Additional features ~~~~~~~~~~~~~~~~~~~ Not all aspects of the toolset are covered here. Explore `its code and docstrings`_ to learn how to use it in a more hands-on fashion. For reference, -``bot.wiki`` is an instance of ``earwigbot.wiki.SitesDB`` tied to the -``sites.db`` file in the bot's working directory. +:py:attr:`bot.wiki ` is an instance of +:py:class:`earwigbot.wiki.SitesDB` tied to the :file:`sites.db` file in the +bot's working directory. .. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ .. _its code and docstrings: https://github.com/earwig/earwigbot/tree/develop/earwigbot/wiki From e4e9356d54f6755ece0b4f488ef472721dc0bad8 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 29 Apr 2012 04:48:50 -0400 Subject: [PATCH 10/12] API docs --- docs/api/earwigbot.commands.rst | 163 ++++++++++++++++++++++++++++++++++++++++ docs/api/earwigbot.irc.rst | 51 +++++++++++++ docs/api/earwigbot.rst | 61 +++++++++++++++ docs/api/earwigbot.tasks.rst | 91 ++++++++++++++++++++++ docs/api/earwigbot.wiki.rst | 75 ++++++++++++++++++ docs/api/modules.rst | 7 ++ docs/customizing.rst | 4 +- docs/index.rst | 3 +- earwigbot/bot.py | 1 + earwigbot/config.py | 2 + earwigbot/tasks/__init__.py | 5 +- earwigbot/tasks/blptag.py | 4 +- 12 files changed, 460 insertions(+), 7 deletions(-) create mode 100644 docs/api/earwigbot.commands.rst create mode 100644 docs/api/earwigbot.irc.rst create mode 100644 docs/api/earwigbot.rst create mode 100644 docs/api/earwigbot.tasks.rst create mode 100644 docs/api/earwigbot.wiki.rst create mode 100644 docs/api/modules.rst diff --git a/docs/api/earwigbot.commands.rst b/docs/api/earwigbot.commands.rst new file mode 100644 index 0000000..544a88d --- /dev/null +++ b/docs/api/earwigbot.commands.rst @@ -0,0 +1,163 @@ +commands Package +================ + +:mod:`commands` Package +----------------------- + +.. automodule:: earwigbot.commands + :members: + :undoc-members: + :show-inheritance: + +:mod:`_old` Module +------------------ + +.. automodule:: earwigbot.commands._old + :members: + :undoc-members: + :show-inheritance: + +:mod:`afc_report` Module +------------------------ + +.. automodule:: earwigbot.commands.afc_report + :members: + :undoc-members: + :show-inheritance: + +:mod:`afc_status` Module +------------------------ + +.. automodule:: earwigbot.commands.afc_status + :members: + :undoc-members: + :show-inheritance: + +:mod:`calc` Module +------------------ + +.. automodule:: earwigbot.commands.calc + :members: + :undoc-members: + :show-inheritance: + +:mod:`chanops` Module +--------------------- + +.. automodule:: earwigbot.commands.chanops + :members: + :undoc-members: + :show-inheritance: + +:mod:`crypt` Module +------------------- + +.. automodule:: earwigbot.commands.crypt + :members: + :undoc-members: + :show-inheritance: + +:mod:`ctcp` Module +------------------ + +.. automodule:: earwigbot.commands.ctcp + :members: + :undoc-members: + :show-inheritance: + +:mod:`editcount` Module +----------------------- + +.. automodule:: earwigbot.commands.editcount + :members: + :undoc-members: + :show-inheritance: + +:mod:`git` Module +----------------- + +.. automodule:: earwigbot.commands.git + :members: + :undoc-members: + :show-inheritance: + +:mod:`help` Module +------------------ + +.. automodule:: earwigbot.commands.help + :members: + :undoc-members: + :show-inheritance: + +:mod:`link` Module +------------------ + +.. automodule:: earwigbot.commands.link + :members: + :undoc-members: + :show-inheritance: + +:mod:`praise` Module +-------------------- + +.. automodule:: earwigbot.commands.praise + :members: + :undoc-members: + :show-inheritance: + +:mod:`quit` Module +------------------ + +.. automodule:: earwigbot.commands.quit + :members: + :undoc-members: + :show-inheritance: + +:mod:`registration` Module +-------------------------- + +.. automodule:: earwigbot.commands.registration + :members: + :undoc-members: + :show-inheritance: + +:mod:`remind` Module +-------------------- + +.. automodule:: earwigbot.commands.remind + :members: + :undoc-members: + :show-inheritance: + +:mod:`replag` Module +-------------------- + +.. automodule:: earwigbot.commands.replag + :members: + :undoc-members: + :show-inheritance: + +:mod:`rights` Module +-------------------- + +.. automodule:: earwigbot.commands.rights + :members: + :undoc-members: + :show-inheritance: + +:mod:`test` Module +------------------ + +.. automodule:: earwigbot.commands.test + :members: + :undoc-members: + :show-inheritance: + +:mod:`threads` Module +--------------------- + +.. automodule:: earwigbot.commands.threads + :members: + :undoc-members: + :show-inheritance: + diff --git a/docs/api/earwigbot.irc.rst b/docs/api/earwigbot.irc.rst new file mode 100644 index 0000000..93f327d --- /dev/null +++ b/docs/api/earwigbot.irc.rst @@ -0,0 +1,51 @@ +irc Package +=========== + +:mod:`irc` Package +------------------ + +.. automodule:: earwigbot.irc + :members: + :undoc-members: + :show-inheritance: + +:mod:`connection` Module +------------------------ + +.. automodule:: earwigbot.irc.connection + :members: + :undoc-members: + :show-inheritance: + +:mod:`data` Module +------------------ + +.. automodule:: earwigbot.irc.data + :members: + :undoc-members: + :show-inheritance: + +:mod:`frontend` Module +---------------------- + +.. automodule:: earwigbot.irc.frontend + :members: + :undoc-members: + :show-inheritance: + +:mod:`rc` Module +---------------- + +.. automodule:: earwigbot.irc.rc + :members: + :undoc-members: + :show-inheritance: + +:mod:`watcher` Module +--------------------- + +.. automodule:: earwigbot.irc.watcher + :members: + :undoc-members: + :show-inheritance: + diff --git a/docs/api/earwigbot.rst b/docs/api/earwigbot.rst new file mode 100644 index 0000000..f74e44f --- /dev/null +++ b/docs/api/earwigbot.rst @@ -0,0 +1,61 @@ +earwigbot Package +================= + +:mod:`earwigbot` Package +------------------------ + +.. automodule:: earwigbot.__init__ + :members: + :undoc-members: + :show-inheritance: + +:mod:`blowfish` Module +---------------------- + +.. automodule:: earwigbot.blowfish + :members: + :undoc-members: + :show-inheritance: + +:mod:`bot` Module +----------------- + +.. automodule:: earwigbot.bot + :members: + :undoc-members: + :show-inheritance: + +:mod:`config` Module +-------------------- + +.. automodule:: earwigbot.config + :members: + :undoc-members: + :show-inheritance: + +:mod:`managers` Module +---------------------- + +.. automodule:: earwigbot.managers + :members: + :undoc-members: + :show-inheritance: + +:mod:`util` Module +------------------ + +.. automodule:: earwigbot.util + :members: + :undoc-members: + :show-inheritance: + +Subpackages +----------- + +.. toctree:: + + earwigbot.commands + earwigbot.irc + earwigbot.tasks + earwigbot.wiki + diff --git a/docs/api/earwigbot.tasks.rst b/docs/api/earwigbot.tasks.rst new file mode 100644 index 0000000..47cbfe6 --- /dev/null +++ b/docs/api/earwigbot.tasks.rst @@ -0,0 +1,91 @@ +tasks Package +============= + +:mod:`tasks` Package +-------------------- + +.. automodule:: earwigbot.tasks + :members: + :undoc-members: + :show-inheritance: + +:mod:`afc_catdelink` Module +--------------------------- + +.. automodule:: earwigbot.tasks.afc_catdelink + :members: + :undoc-members: + :show-inheritance: + +:mod:`afc_copyvios` Module +-------------------------- + +.. automodule:: earwigbot.tasks.afc_copyvios + :members: + :undoc-members: + :show-inheritance: + +:mod:`afc_dailycats` Module +--------------------------- + +.. automodule:: earwigbot.tasks.afc_dailycats + :members: + :undoc-members: + :show-inheritance: + +:mod:`afc_history` Module +------------------------- + +.. automodule:: earwigbot.tasks.afc_history + :members: + :undoc-members: + :show-inheritance: + +:mod:`afc_statistics` Module +---------------------------- + +.. automodule:: earwigbot.tasks.afc_statistics + :members: + :undoc-members: + :show-inheritance: + +:mod:`afc_undated` Module +------------------------- + +.. automodule:: earwigbot.tasks.afc_undated + :members: + :undoc-members: + :show-inheritance: + +:mod:`blptag` Module +-------------------- + +.. automodule:: earwigbot.tasks.blptag + :members: + :undoc-members: + :show-inheritance: + +:mod:`feed_dailycats` Module +---------------------------- + +.. automodule:: earwigbot.tasks.feed_dailycats + :members: + :undoc-members: + :show-inheritance: + +:mod:`wikiproject_tagger` Module +-------------------------------- + +.. automodule:: earwigbot.tasks.wikiproject_tagger + :members: + :undoc-members: + :show-inheritance: + +:mod:`wrongmime` Module +----------------------- + +.. automodule:: earwigbot.tasks.wrongmime + :members: + :undoc-members: + :show-inheritance: + diff --git a/docs/api/earwigbot.wiki.rst b/docs/api/earwigbot.wiki.rst new file mode 100644 index 0000000..9938da8 --- /dev/null +++ b/docs/api/earwigbot.wiki.rst @@ -0,0 +1,75 @@ +wiki Package +============ + +:mod:`wiki` Package +------------------- + +.. automodule:: earwigbot.wiki + :members: + :undoc-members: + :show-inheritance: + +:mod:`category` Module +---------------------- + +.. automodule:: earwigbot.wiki.category + :members: + :undoc-members: + :show-inheritance: + +:mod:`constants` Module +----------------------- + +.. automodule:: earwigbot.wiki.constants + :members: + :undoc-members: + :show-inheritance: + +:mod:`copyright` Module +----------------------- + +.. automodule:: earwigbot.wiki.copyright + :members: + :undoc-members: + :show-inheritance: + +:mod:`exceptions` Module +------------------------ + +.. automodule:: earwigbot.wiki.exceptions + :members: + :undoc-members: + :show-inheritance: + +:mod:`page` Module +------------------ + +.. automodule:: earwigbot.wiki.page + :members: + :undoc-members: + :show-inheritance: + +:mod:`site` Module +------------------ + +.. automodule:: earwigbot.wiki.site + :members: + :undoc-members: + :show-inheritance: + +:mod:`sitesdb` Module +--------------------- + +.. automodule:: earwigbot.wiki.sitesdb + :members: + :undoc-members: + :show-inheritance: + +:mod:`user` Module +------------------ + +.. automodule:: earwigbot.wiki.user + :members: + :undoc-members: + :show-inheritance: + diff --git a/docs/api/modules.rst b/docs/api/modules.rst new file mode 100644 index 0000000..7c4c110 --- /dev/null +++ b/docs/api/modules.rst @@ -0,0 +1,7 @@ +earwigbot +========= + +.. toctree:: + :maxdepth: 4 + + earwigbot diff --git a/docs/customizing.rst b/docs/customizing.rst index 3d5bac6..a1ab2ee 100644 --- a/docs/customizing.rst +++ b/docs/customizing.rst @@ -21,8 +21,8 @@ commands and tasks (i.e., :py:attr:`self.bot`). The most useful attributes are: - :py:attr:`~earwigbot.bot.Bot.config`: an instance of - :py:class:`~earwigbot.bot.BotConfig`, for accessing the bot's configuration - data (see below). + :py:class:`~earwigbot.config.BotConfig`, for accessing the bot's + configuration data (see below). - :py:attr:`~earwigbot.bot.Bot.commands`: the bot's :py:class:`~earwigbot.managers.CommandManager`, which is used internally to diff --git a/docs/index.rst b/docs/index.rst index 987eca2..8d446dc 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -31,13 +31,14 @@ Contents -------- .. toctree:: - :maxdepth: 3 + :maxdepth: 2 installation setup customizing toolset tips + API Reference Indices and tables ------------------ diff --git a/earwigbot/bot.py b/earwigbot/bot.py index bc8fbc4..4ba1992 100644 --- a/earwigbot/bot.py +++ b/earwigbot/bot.py @@ -38,6 +38,7 @@ class Bot(object): EarwigBot has three components that can run independently of each other: an IRC front-end, an IRC watcher, and a wiki scheduler. + * The IRC front-end runs on a normal IRC server and expects users to interact with it/give it commands. * The IRC watcher runs on a wiki recent-changes server and listens for diff --git a/earwigbot/config.py b/earwigbot/config.py index 107f6b2..0220542 100644 --- a/earwigbot/config.py +++ b/earwigbot/config.py @@ -40,6 +40,7 @@ class BotConfig(object): from scratch at the inital bot run. BotConfig has a few properties and functions, including the following: + * config.root_dir - bot's working directory; contains config.yml, logs/ * config.path - path to the bot's config file * config.components - enabled components @@ -50,6 +51,7 @@ class BotConfig(object): * config.schedule() - tasks scheduled to run at a given time BotConfig also has some functions used in config loading: + * config.load() - loads and parses our config file, returning True if passwords are stored encrypted or False otherwise; can also be used to easily reload config diff --git a/earwigbot/tasks/__init__.py b/earwigbot/tasks/__init__.py index 7f2f867..d830f1e 100644 --- a/earwigbot/tasks/__init__.py +++ b/earwigbot/tasks/__init__.py @@ -34,8 +34,9 @@ class BaseTask(object): This class (import with `from earwigbot.tasks import BaseTask`) can be subclassed to create custom bot tasks. - To run a task, use bot.tasks.start(name, **kwargs). **kwargs get passed to - the Task's run() function. + To run a task, use :py:meth:`bot.tasks.start(name, **kwargs) + `. ``**kwargs`` get passed to the + Task's run() function. """ name = None number = 0 diff --git a/earwigbot/tasks/blptag.py b/earwigbot/tasks/blptag.py index 76f80be..695dce6 100644 --- a/earwigbot/tasks/blptag.py +++ b/earwigbot/tasks/blptag.py @@ -25,8 +25,8 @@ from earwigbot.tasks import BaseTask __all__ = ["Task"] class Task(BaseTask): - """A task to add |blp=yes to {{WPB}} or {{WPBS}} when it is used along with - {{WP Biography}}.""" + """A task to add |blp=yes to ``{{WPB}}`` or ``{{WPBS}}`` when it is used + along with ``{{WP Biography}}``.""" name = "blptag" def setup(self): From 4aac14c3e13b0d4df060d38ebd75c6f343410c78 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 5 May 2012 02:36:42 -0400 Subject: [PATCH 11/12] Fixed some references, etc --- docs/customizing.rst | 83 +++++++++++++++++++++++++++------------------------ docs/installation.rst | 5 ++-- docs/toolset.rst | 4 +-- 3 files changed, 49 insertions(+), 43 deletions(-) diff --git a/docs/customizing.rst b/docs/customizing.rst index a1ab2ee..dde9b67 100644 --- a/docs/customizing.rst +++ b/docs/customizing.rst @@ -29,22 +29,24 @@ The most useful attributes are: run IRC commands (through :py:meth:`commands.call() `, which you shouldn't have to use); you can safely reload all commands with - :py:meth:`commands.load() `. + :py:meth:`commands.load() `. - :py:attr:`~earwigbot.bot.Bot.tasks`: the bot's :py:class:`~earwigbot.managers.TaskManager`, which can be used to start tasks with :py:meth:`tasks.start(task_name, **kwargs) `. :py:meth:`tasks.load() - ` can be used to safely reload all + ` can be used to safely reload all tasks. - :py:attr:`~earwigbot.bot.Bot.frontend` / :py:attr:`~earwigbot.bot.Bot.watcher`: instances of - :py:class:`earwigbot.irc.Frontend` and :py:class:`earwigbot.irc.Watcher`, + :py:class:`earwigbot.irc.Frontend ` and + :py:class:`earwigbot.irc.Watcher `, respectively, which represent the bot's connections to these two servers; you can, for example, send a message to the frontend with - :py:meth:`frontend.say(chan, msg) ` (more on - communicating with IRC below). + :py:meth:`frontend.say(chan, msg) + ` (more on communicating with IRC + below). - :py:attr:`~earwigbot.bot.Bot.wiki`: interface with the :doc:`Wiki Toolset `. @@ -96,8 +98,8 @@ these are the basics: responds to other hook types. - Method :py:meth:`~earwigbot.commands.BaseCommand.check` is passed a - :py:class:`~earwigbot.irc.Data` [1]_ object, and should return ``True`` if - you want to respond to this message, or ``False`` otherwise. The default + :py:class:`~earwigbot.irc.data.Data` [1]_ object, and should return ``True`` + if you want to respond to this message, or ``False`` otherwise. The default behavior is to return ``True`` only if :py:attr:`data.is_command` is ``True`` and :py:attr:`data.command` == :py:attr:`~earwigbot.commands.BaseCommand.name`, which is suitable for most @@ -106,7 +108,7 @@ these are the basics: prevent any other commands from responding to this message. - Method :py:meth:`~earwigbot.commands.BaseCommand.process` is passed the same - :py:class:`~earwigbot.irc.Data` object as + :py:class:`~earwigbot.irc.data.Data` object as :py:meth:`~earwigbot.commands.BaseCommand.check`, but only if :py:meth:`~earwigbot.commands.BaseCommand.check` returned ``True``. This is where the bulk of your command goes. To respond to IRC messages, there are a @@ -116,13 +118,15 @@ these are the basics: :py:meth:`~earwigbot.commands.BaseCommand.__init__` method for the full list. The most common ones are :py:meth:`say(chan_or_user, msg) - `, :py:meth:`reply(data, msg) - ` (convenience function; sends a reply to the - issuer of the command in the channel it was received), - :py:meth:`action(chan_or_user, msg) `, - :py:meth:`notice(chan_or_user, msg) `, - :py:meth:`join(chan) `, and - :py:meth:`part(chan) `. + `, :py:meth:`reply(data, msg) + ` (convenience function; sends + a reply to the issuer of the command in the channel it was received), + :py:meth:`action(chan_or_user, msg) + `, + :py:meth:`notice(chan_or_user, msg) + `, :py:meth:`join(chan) + `, and + :py:meth:`part(chan) `. It's important to name the command class :py:class:`Command` within the file, or else the bot might not recognize it as a command. The name of the file @@ -200,30 +204,31 @@ task, or the afc_statistics_ plugin for a more complicated one. .. rubric:: Footnotes -.. [1] :py:class:`~earwigbot.irc.Data` objects are instances of - :py:class:`earwigbot.irc.Data` that contain information about a single - message sent on IRC. Their useful attributes are - :py:attr:`~earwigbot.irc.Data.chan` (channel the message was sent from, - equal to :py:attr:`~earwigbot.irc.Data.nick` if it's a private message), - :py:attr:`~earwigbot.irc.Data.nick` (nickname of the sender), - :py:attr:`~earwigbot.irc.Data.ident` (ident_ of the sender), - :py:attr:`~earwigbot.irc.Data.host` (hostname of the sender), - :py:attr:`~earwigbot.irc.Data.msg` (text of the sent message), - :py:attr:`~earwigbot.irc.Data.is_command` (boolean telling whether or - not this message is a bot command, e.g., whether it is prefixed by - ``!``), :py:attr:`~earwigbot.irc.Data.command` (if the message is a - command, this is the name of the command used), and - :py:attr:`~earwigbot.irc.Data.args` (if the message is a command, this - is a list of the command arguments - for example, if issuing - "``!part ##earwig Goodbye guys``", :py:attr:`~earwigbot.irc.Data.args` - will equal ``["##earwig", "Goodbye", "guys"]``). Note that not all - :py:class:`~earwigbot.irc.Data` objects will have all of these - attributes: :py:class:`~earwigbot.irc.Data` objects generated by private - messages will, but ones generated by joins will only have - :py:attr:`~earwigbot.irc.Data.chan`, - :py:attr:`~earwigbot.irc.Data.nick`, - :py:attr:`~earwigbot.irc.Data.ident`, - and :py:attr:`~earwigbot.irc.Data.host`. +.. [1] :py:class:`~earwigbot.irc.data.Data` objects are instances of + :py:class:`earwigbot.irc.Data ` that contain + information about a single message sent on IRC. Their useful attributes + are :py:attr:`~earwigbot.irc.data.Data.chan` (channel the message was + sent from, equal to :py:attr:`~earwigbot.irc.data.Data.nick` if it's a + private message), :py:attr:`~earwigbot.irc.data.Data.nick` (nickname of + the sender), :py:attr:`~earwigbot.irc.data.Data.ident` (ident_ of the + sender), :py:attr:`~earwigbot.irc.data.Data.host` (hostname of the + sender), :py:attr:`~earwigbot.irc.data.Data.msg` (text of the sent + message), :py:attr:`~earwigbot.irc.data.Data.is_command` (boolean + telling whether or not this message is a bot command, e.g., whether it + is prefixed by ``!``), :py:attr:`~earwigbot.irc.data.Data.command` (if + the message is a command, this is the name of the command used), and + :py:attr:`~earwigbot.irc.data.Data.args` (if the message is a command, + this is a list of the command arguments - for example, if issuing + "``!part ##earwig Goodbye guys``", + :py:attr:`~earwigbot.irc.data.Data.args` will equal + ``["##earwig", "Goodbye", "guys"]``). Note that not all + :py:class:`~earwigbot.irc.data.Data` objects will have all of these + attributes: :py:class:`~earwigbot.irc.data.Data` objects generated by + private messages will, but ones generated by joins will only have + :py:attr:`~earwigbot.irc.data.Data.chan`, + :py:attr:`~earwigbot.irc.data.Data.nick`, + :py:attr:`~earwigbot.irc.data.Data.ident`, + and :py:attr:`~earwigbot.irc.data.Data.host`. .. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py .. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py diff --git a/docs/installation.rst b/docs/installation.rst index 857329d..12fc907 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -43,8 +43,9 @@ tags or by new features (``feature/*`` branches):: .. rubric:: Footnotes -.. [1] ``python setup.py install``/``develop`` may require root, or use the - ``--user`` switch to install for the current user only. +.. [1] :command:`python setup.py install`/:command:`develop` may require root, + or use the :command:`--user` switch to install for the current user + only. .. _my instance of EarwigBot: http://en.wikipedia.org/wiki/User:EarwigBot .. _earwigbot-plugins: https://github.com/earwig/earwigbot-plugins diff --git a/docs/toolset.rst b/docs/toolset.rst index 7e85a50..1459dc8 100644 --- a/docs/toolset.rst +++ b/docs/toolset.rst @@ -104,8 +104,8 @@ and the following methods: - :py:meth:`get_user(username) `: returns a :py:class:`~earwigbot.wiki.User` object for the given username -Pages (and Categories) -~~~~~~~~~~~~~~~~~~~~~~ +Pages and categories +~~~~~~~~~~~~~~~~~~~~ Create :py:class:`earwigbot.wiki.Page` objects with :py:meth:`site.get_page(title) `, From 8d0a50970e1f797ab1d1ae9f37e9fc2f742aa9cf Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 5 May 2012 14:40:43 -0400 Subject: [PATCH 12/12] More reference fixes --- docs/tips.rst | 4 +- docs/toolset.rst | 216 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 114 insertions(+), 106 deletions(-) diff --git a/docs/tips.rst b/docs/tips.rst index 6836b7c..4f4052e 100644 --- a/docs/tips.rst +++ b/docs/tips.rst @@ -37,8 +37,8 @@ Tips .. rubric:: Footnotes .. [1] In reality, all this does is call :py:meth:`bot.commands.load() - ` and - :py:meth:`bot.tasks.load() `! + ` and + :py:meth:`bot.tasks.load() `! .. _logging: http://docs.python.org/library/logging.html .. _!git plugin: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/git.py diff --git a/docs/toolset.rst b/docs/toolset.rst index 1459dc8..cae1beb 100644 --- a/docs/toolset.rst +++ b/docs/toolset.rst @@ -6,12 +6,12 @@ EarwigBot's answer to the `Pywikipedia framework`_ is the Wiki Toolset :py:attr:`bot.wiki `. :py:attr:`bot.wiki ` provides three methods for the -management of Sites - :py:meth:`~earwigbot.wiki.SiteDB.get_site`, -:py:meth:`~earwigbot.wiki.SiteDB.add_site`, and -:py:meth:`~earwigbot.wiki.SiteDB.remove_site`. Sites are objects that simply -represent a MediaWiki site. A single instance of EarwigBot (i.e. a single -*working directory*) is expected to relate to a single site or group of sites -using the same login info (like all WMF wikis with CentralAuth). +management of Sites - :py:meth:`~earwigbot.wiki.sitesdb.SitesDB.get_site`, +:py:meth:`~earwigbot.wiki.sitesdb.SitesDB.add_site`, and +:py:meth:`~earwigbot.wiki.sitesdb.SitesDB.remove_site`. Sites are objects that +simply represent a MediaWiki site. A single instance of EarwigBot (i.e. a +single *working directory*) is expected to relate to a single site or group of +sites using the same login info (like all WMF wikis with `CentralAuth`_). Load your default site (the one that you picked during setup) with ``site = bot.wiki.get_site()``. @@ -28,16 +28,16 @@ load a site with ``site = bot.wiki.get_site(name)``, where ``name`` might be ``site = bot.wiki.get_site(project="wikipedia", lang="en")``). Recall that not giving any arguments to ``get_site()`` will return the default site. -:py:meth:`~earwigbot.wiki.SiteDB.add_site` is used to add new sites to the sites -database. It may be called with similar arguments as -:py:meth:`~earwigbot.wiki.SiteDB.get_site`, but the difference is important. -:py:meth:`~earwigbot.wiki.SiteDB.get_site` only needs enough information to -identify the site in its database, which is usually just its name; the database -stores all other necessary connection info. With -:py:meth:`~earwigbot.wiki.SiteDB.add_site`, you need to provide enough connection -info so the toolset can successfully access the site's API/SQL databases and -store that information for later. That might not be much; for WMF wikis, you -can usually use code like this:: +:py:meth:`~earwigbot.wiki.sitesdb.SitesDB.add_site` is used to add new sites to +the sites database. It may be called with similar arguments as +:py:meth:`~earwigbot.wiki.sitesdb.SitesDB.get_site`, but the difference is +important. :py:meth:`~earwigbot.wiki.sitesdb.SitesDB.get_site` only needs +enough information to identify the site in its database, which is usually just +its name; the database stores all other necessary connection info. With +:py:meth:`~earwigbot.wiki.sitesdb.SitesDB.add_site`, you need to provide enough +connection info so the toolset can successfully access the site's API/SQL +databases and store that information for later. That might not be much; for WMF +wikis, you can usually use code like this:: project, lang = "wikipedia", "es" try: @@ -61,90 +61,95 @@ something more like:: sql = {host: "sql.mysite.net", db: db_name} site = bot.wiki.add_site(base_url=base_url, script_path="/s", sql=sql) -:py:meth:`~earwigbot.wiki.SiteDB.remove_site` does the opposite of -:py:meth:`~earwigbot.wiki.SiteDB.add_site`: give it a site's name or a -project/lang pair like :py:meth:`~earwigbot.wiki.SiteDB.get_site` takes, and -it'll remove that site from the sites database. +:py:meth:`~earwigbot.wiki.sitesdb.SitesDB.remove_site` does the opposite of +:py:meth:`~earwigbot.wiki.sitesdb.SitesDB.add_site`: give it a site's name or a +project/lang pair like :py:meth:`~earwigbot.wiki.sitesdb.SitesDB.get_site` +takes, and it'll remove that site from the sites database. Sites ~~~~~ -:py:class:`earwigbot.wiki.Site` objects provide the following attributes: +:py:class:`earwigbot.wiki.Site ` objects provide the +following attributes: -- :py:attr:`~earwigbot.wiki.Site.name`: the site's name (or "wikiid"), like - ``"enwiki"`` -- :py:attr:`~earwigbot.wiki.Site.project`: the site's project name, like +- :py:attr:`~earwigbot.wiki.site.Site.name`: the site's name (or "wikiid"), + like ``"enwiki"`` +- :py:attr:`~earwigbot.wiki.site.Site.project`: the site's project name, like ``"wikipedia"`` -- :py:attr:`~earwigbot.wiki.Site.lang`: the site's language code, like ``"en"`` -- :py:attr:`~earwigbot.wiki.Site.domain`: the site's web domain, like +- :py:attr:`~earwigbot.wiki.site.Site.lang`: the site's language code, like + ``"en"`` +- :py:attr:`~earwigbot.wiki.site.Site.domain`: the site's web domain, like ``"en.wikipedia.org"`` and the following methods: -- :py:meth:`api_query(**kwargs) `: does an API - query with the given keyword arguments as params -- :py:meth:`sql_query(query, params=(), ...) `: - does an SQL query and yields its results (as a generator) -- :py:meth:`~earwigbot.wiki.Site.get_replag`: returns the estimated database - replication lag (if we have the site's SQL connection info) +- :py:meth:`api_query(**kwargs) `: does an + API query with the given keyword arguments as params +- :py:meth:`sql_query(query, params=(), ...) + `: does an SQL query and yields its + results (as a generator) +- :py:meth:`~earwigbot.wiki.site.Site.get_replag`: returns the estimated + database replication lag (if we have the site's SQL connection info) - :py:meth:`namespace_id_to_name(id, all=False) - `: given a namespace ID, returns - the primary associated namespace name (or a list of all names when ``all`` is - ``True``) + `: given a namespace ID, + returns the primary associated namespace name (or a list of all names when + ``all`` is ``True``) - :py:meth:`namespace_name_to_id(name) - `: given a namespace name, returns - the associated namespace ID + `: given a namespace name, + returns the associated namespace ID - :py:meth:`get_page(title, follow_redirects=False) - `: returns a ``Page`` object for the given - title (or a :py:class:`~earwigbot.wiki.Category` object if the page's - namespace is "``Category:``") + `: returns a ``Page`` object for the given + title (or a :py:class:`~earwigbot.wiki.category.Category` object if the + page's namespace is "``Category:``") - :py:meth:`get_category(catname, follow_redirects=False) - `: returns a ``Category`` object for the - given title (sans namespace) -- :py:meth:`get_user(username) `: returns a - :py:class:`~earwigbot.wiki.User` object for the given username + `: returns a ``Category`` object for + the given title (sans namespace) +- :py:meth:`get_user(username) `: returns a + :py:class:`~earwigbot.wiki.user.User` object for the given username Pages and categories ~~~~~~~~~~~~~~~~~~~~ -Create :py:class:`earwigbot.wiki.Page` objects with -:py:meth:`site.get_page(title) `, -:py:meth:`page.toggle_talk() `, -:py:meth:`user.get_userpage() `, or -:py:meth:`user.get_talkpage() `. They provide -the following attributes: - -- :py:attr:`~earwigbot.wiki.Page.title`: the page's title, or pagename -- :py:attr:`~earwigbot.wiki.Page.exists`: whether the page exists -- :py:attr:`~earwigbot.wiki.Page.pageid`: an integer ID representing the page -- :py:attr:`~earwigbot.wiki.Page.url`: the page's URL -- :py:attr:`~earwigbot.wiki.Page.namespace`: the page's namespace as an integer -- :py:attr:`~earwigbot.wiki.Page.protection`: the page's current protection - status -- :py:attr:`~earwigbot.wiki.Page.is_talkpage`: ``True`` if the page is a +Create :py:class:`earwigbot.wiki.Page ` objects with +:py:meth:`site.get_page(title) `, +:py:meth:`page.toggle_talk() `, +:py:meth:`user.get_userpage() `, or +:py:meth:`user.get_talkpage() `. They +provide the following attributes: + +- :py:attr:`~earwigbot.wiki.page.Page.title`: the page's title, or pagename +- :py:attr:`~earwigbot.wiki.page.Page.exists`: whether the page exists +- :py:attr:`~earwigbot.wiki.page.Page.pageid`: an integer ID representing the + page +- :py:attr:`~earwigbot.wiki.page.Page.url`: the page's URL +- :py:attr:`~earwigbot.wiki.page.Page.namespace`: the page's namespace as an + integer +- :py:attr:`~earwigbot.wiki.page.Page.protection`: the page's current + protection status +- :py:attr:`~earwigbot.wiki.page.Page.is_talkpage`: ``True`` if the page is a talkpage, else ``False`` -- :py:attr:`~earwigbot.wiki.Page.is_redirect`: ``True`` if the page is a +- :py:attr:`~earwigbot.wiki.page.Page.is_redirect`: ``True`` if the page is a redirect, else ``False`` and the following methods: -- :py:meth:`~earwigbot.wiki.Page.reload`: forcibly reload the page's attributes - (emphasis on *reload* - this is only necessary if there is reason to believe - they have changed) -- :py:meth:`toggle_talk(...) `: returns a +- :py:meth:`~earwigbot.wiki.page.Page.reload`: forcibly reload the page's + attributes (emphasis on *reload* - this is only necessary if there is reason + to believe they have changed) +- :py:meth:`toggle_talk(...) `: returns a content page's talk page, or vice versa -- :py:meth:`~earwigbot.wiki.Page.get`: returns page content -- :py:meth:`~earwigbot.wiki.Page.get_redirect_target`: if the page is a +- :py:meth:`~earwigbot.wiki.page.Page.get`: returns page content +- :py:meth:`~earwigbot.wiki.page.Page.get_redirect_target`: if the page is a redirect, returns its destination -- :py:meth:`~earwigbot.wiki.Page.get_creator`: returns a - :py:class:`~earwigbot.wiki.User` object representing the first user to edit - the page +- :py:meth:`~earwigbot.wiki.page.Page.get_creator`: returns a + :py:class:`~earwigbot.wiki.user.User` object representing the first user to + edit the page - :py:meth:`edit(text, summary, minor=False, bot=True, force=False) - `: replaces the page's content with ``text`` or - creates a new page + `: replaces the page's content with ``text`` + or creates a new page - :py:meth:`add_section(text, title, minor=False, bot=True, force=False) - `: adds a new section named ``title`` at the - bottom of the page + `: adds a new section named ``title`` + at the bottom of the page - :py:meth:`copyvio_check(...) `: checks the page for copyright violations @@ -153,51 +158,53 @@ and the following methods: :py:meth:`~earwigbot.wiki.copyvios.CopyvioMixin.copyvio_check`, but against a specific URL -Additionally, :py:class:`~earwigbot.wiki.Category` objects (created with -:py:meth:`site.get_category(name) ` or -:py:meth:`site.get_page(title) ` where ``title`` -is in the ``Category:`` namespace) provide the following additional method: +Additionally, :py:class:`~earwigbot.wiki.category.Category` objects (created +with :py:meth:`site.get_category(name) ` +or :py:meth:`site.get_page(title) ` where +``title`` is in the ``Category:`` namespace) provide the following additional +method: - :py:meth:`get_members(use_sql=False, limit=None) - `: returns a list of page titles in the - category (limit is ``50`` by default if using the API) + `: returns a list of page + titles in the category (limit is ``50`` by default if using the API) Users ~~~~~ -Create :py:class:`earwigbot.wiki.User` objects with -:py:meth:`site.get_user(name) ` or -:py:meth:`page.get_creator() `. They provide -the following attributes: +Create :py:class:`earwigbot.wiki.User ` objects with +:py:meth:`site.get_user(name) ` or +:py:meth:`page.get_creator() `. They +provide the following attributes: -- :py:attr:`~earwigbot.wiki.User.name`: the user's username -- :py:attr:`~earwigbot.wiki.User.exists`: ``True`` if the user exists, or +- :py:attr:`~earwigbot.wiki.user.User.name`: the user's username +- :py:attr:`~earwigbot.wiki.user.User.exists`: ``True`` if the user exists, or ``False`` if they do not -- :py:attr:`~earwigbot.wiki.User.userid`: an integer ID representing the user -- :py:attr:`~earwigbot.wiki.User.blockinfo`: information about any current +- :py:attr:`~earwigbot.wiki.user.User.userid`: an integer ID representing the + user +- :py:attr:`~earwigbot.wiki.user.User.blockinfo`: information about any current blocks on the user (``False`` if no block, or a dict of ``{"by": blocking_user, "reason": block_reason, "expiry": block_expire_time}``) -- :py:attr:`~earwigbot.wiki.User.groups`: a list of the user's groups -- :py:attr:`~earwigbot.wiki.User.rights`: a list of the user's rights -- :py:attr:`~earwigbot.wiki.User.editcount`: the number of edits made by the - user -- :py:attr:`~earwigbot.wiki.User.registration`: the time the user registered as - a :py:obj:`time.struct_time` -- :py:attr:`~earwigbot.wiki.User.emailable`: ``True`` if you can email the +- :py:attr:`~earwigbot.wiki.user.User.groups`: a list of the user's groups +- :py:attr:`~earwigbot.wiki.user.User.rights`: a list of the user's rights +- :py:attr:`~earwigbot.wiki.user.User.editcount`: the number of edits made by + the user +- :py:attr:`~earwigbot.wiki.user.User.registration`: the time the user + registered as a :py:obj:`time.struct_time` +- :py:attr:`~earwigbot.wiki.user.User.emailable`: ``True`` if you can email the user, ``False`` if you cannot -- :py:attr:`~earwigbot.wiki.User.gender`: the user's gender (``"male"``, +- :py:attr:`~earwigbot.wiki.user.User.gender`: the user's gender (``"male"``, ``"female"``, or ``"unknown"``) and the following methods: -- :py:meth:`~earwigbot.wiki.User.reload`: forcibly reload the user's attributes - (emphasis on *reload* - this is only necessary if there is reason to believe - they have changed) -- :py:meth:`~earwigbot.wiki.User.get_userpage`: returns a - :py:class:`~earwigbot.wiki.Page` object representing the user's userpage -- :py:meth:`~earwigbot.wiki.User.get_talkpage`: returns a - :py:class:`~earwigbot.wiki.Page` object representing the user's talkpage +- :py:meth:`~earwigbot.wiki.user.User.reload`: forcibly reload the user's + attributes (emphasis on *reload* - this is only necessary if there is reason + to believe they have changed) +- :py:meth:`~earwigbot.wiki.user.User.get_userpage`: returns a + :py:class:`~earwigbot.wiki.page.Page` object representing the user's userpage +- :py:meth:`~earwigbot.wiki.user.User.get_talkpage`: returns a + :py:class:`~earwigbot.wiki.page.Page` object representing the user's talkpage Additional features ~~~~~~~~~~~~~~~~~~~ @@ -205,8 +212,9 @@ Additional features Not all aspects of the toolset are covered here. Explore `its code and docstrings`_ to learn how to use it in a more hands-on fashion. For reference, :py:attr:`bot.wiki ` is an instance of -:py:class:`earwigbot.wiki.SitesDB` tied to the :file:`sites.db` file in the -bot's working directory. +:py:class:`earwigbot.wiki.SitesDB ` tied to the +:file:`sites.db` file in the bot's working directory. .. _Pywikipedia framework: http://pywikipediabot.sourceforge.net/ +.. _CentralAuth: http://www.mediawiki.org/wiki/Extension:CentralAuth .. _its code and docstrings: https://github.com/earwig/earwigbot/tree/develop/earwigbot/wiki