A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

114 рядки
4.2 KiB

  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. """
  24. usage: :command:`earwigbot [-h] [-v] [-d] [-q] [-t NAME] [PATH]`
  25. This is EarwigBot's command-line utility, enabling you to easily start the bot
  26. or run specific tasks.
  27. .. glossary::
  28. ``PATH``
  29. path to the bot's working directory, which will be created if it doesn't
  30. exist; current directory assumed if not specified
  31. ``-h``, ``--help``
  32. show this help message and exit
  33. ``-v``, ``--version``
  34. show program's version number and exit
  35. ``-d``, ``--debug``
  36. print all logs, including ``DEBUG``-level messages
  37. ``-q``, ``--quiet``
  38. don't print any logs except warnings and errors
  39. ``-t NAME``, ``--task NAME``
  40. given the name of a task, the bot will run it instead of the main bot and
  41. then exit
  42. """
  43. from argparse import ArgumentParser
  44. import logging
  45. from os import path
  46. from time import sleep
  47. from earwigbot import __version__
  48. from earwigbot.bot import Bot
  49. __all__ = ["main"]
  50. def main():
  51. """Main entry point for the command-line utility."""
  52. version = "EarwigBot v{0}".format(__version__)
  53. desc = """This is EarwigBot's command-line utility, enabling you to easily
  54. start the bot or run specific tasks."""
  55. parser = ArgumentParser(description=desc)
  56. parser.add_argument("path", nargs="?", metavar="PATH", default=path.curdir,
  57. help="""path to the bot's working directory, which will
  58. be created if it doesn't exist; current
  59. directory assumed if not specified""")
  60. parser.add_argument("-v", "--version", action="version", version=version)
  61. parser.add_argument("-d", "--debug", action="store_true",
  62. help="print all logs, including DEBUG-level messages")
  63. parser.add_argument("-q", "--quiet", action="store_true",
  64. help="don't print any logs except warnings and errors")
  65. parser.add_argument("-t", "--task", metavar="NAME",
  66. help="""given the name of a task, the bot will run it
  67. instead of the main bot and then exit""")
  68. args = parser.parse_args()
  69. level = logging.INFO
  70. if args.debug and args.quiet:
  71. parser.print_usage()
  72. print "earwigbot: error: cannot show debug messages and be quiet at the same time"
  73. return
  74. if args.debug:
  75. level = logging.DEBUG
  76. elif args.quiet:
  77. level = logging.WARNING
  78. print version
  79. print
  80. bot = Bot(path.abspath(args.path), level=level)
  81. if args.task:
  82. thread = bot.tasks.start(args.task)
  83. if not thread:
  84. return
  85. try:
  86. while thread.is_alive(): # Keep it alive; it's a daemon
  87. sleep(1)
  88. except KeyboardInterrupt:
  89. pass
  90. finally:
  91. if thread.is_alive():
  92. bot.tasks.logger.warn("The task is will be killed")
  93. else:
  94. try:
  95. bot.run()
  96. except KeyboardInterrupt:
  97. pass
  98. finally:
  99. if bot._keep_looping: # Indicates bot hasn't already been stopped
  100. bot.stop()
  101. if __name__ == "__main__":
  102. main()