A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 regels
4.5 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. EarwigBot's Wiki Toolset: Misc Functions
  4. This module, a component of the wiki.tools package, contains miscellaneous
  5. functions that are not methods of any class, like get_site().
  6. There's no need to import this module explicitly. All functions here are
  7. automatically available from wiki.tools.
  8. """
  9. from getpass import getpass
  10. from core import config
  11. from wiki.tools.exceptions import SiteNotFoundError
  12. from wiki.tools.site import Site
  13. __all__ = ["get_site"]
  14. def _load_config():
  15. """Called by a config-requiring function, such as get_site(), when config
  16. has not been loaded. This will usually happen only if we're running code
  17. directly from Python's interpreter and not the bot itself, because
  18. earwigbot.py or core/main.py will already call these functions.
  19. """
  20. is_encrypted = config.verify_config()
  21. if is_encrypted: # passwords in the config file are encrypted
  22. key = getpass("Enter key to unencrypt bot passwords: ")
  23. config.parse_config(key)
  24. else:
  25. config.parse_config(None)
  26. def _get_site_object_from_dict(name, d):
  27. """Return a Site object based on the contents of a dict, probably acquired
  28. through our config file, and a separate name.
  29. """
  30. project = d["project"]
  31. lang = d["lang"]
  32. try:
  33. api = d["apiURL"]
  34. except KeyError:
  35. api = None
  36. try:
  37. sql_server = d["sqlServer"]
  38. except KeyError:
  39. sql_server = None
  40. try:
  41. sql_db = d["sqlDB"]
  42. except KeyError:
  43. sql_db = None
  44. return Site(name, project, lang, api, (sql_server, sql_db))
  45. def get_site(name=None, project=None, lang=None):
  46. """Returns a Site instance based on information from our config file.
  47. With no arguments, returns the default site as specified by our config
  48. file. This is default = config.wiki["defaultSite"];
  49. config.wiki["sites"][default].
  50. With `name` specified, returns the site specified by
  51. config.wiki["sites"][name].
  52. With `project` and `lang` specified, returns the site specified by the
  53. member of config.wiki["sites"], `s`, for which s["project"] == project and
  54. s["lang"] == lang.
  55. Specifying a project without a lang or a lang without a project will raise
  56. TypeError. If all three args are specified, `name` will be first tried,
  57. then `project` and `lang`. If, with any number of args, a site cannot be
  58. found in the config, SiteNotFoundError is raised.
  59. """
  60. # check if config has been loaded, and load it if it hasn't
  61. if not config.is_config_loaded():
  62. _load_config()
  63. # someone specified a project without a lang (or a lang without a project)!
  64. if (project is None and lang is not None) or (project is not None and lang is None):
  65. e = "Keyword arguments 'lang' and 'project' must be specified together."
  66. raise TypeError(e)
  67. # no args given, so return our default site (project is None implies lang
  68. # is None, so we don't need to add that in)
  69. if name is None and project is None:
  70. try: # ...so use the default site
  71. default = config.wiki["defaultSite"]
  72. except KeyError:
  73. e = "Default site is not specified in config."
  74. raise SiteNotFoundError(e)
  75. try:
  76. site = config.wiki["sites"][default]
  77. except KeyError:
  78. e = "Default site specified by config is not in the config's sites list."
  79. raise SiteNotFoundError(e)
  80. return _get_site_object_from_dict(default, site)
  81. # name arg given, but don't look at others unless `name` isn't found
  82. if name is not None:
  83. try:
  84. site = config.wiki["sites"][name]
  85. except KeyError:
  86. if project is None: # implies lang is None, i.e., only name was given
  87. e = "Site '{0}' not found in config.".format(name)
  88. raise SiteNotFoundError(e)
  89. for sitename, site in config.wiki["sites"].items():
  90. if site["project"] == project and site["lang"] == lang:
  91. return _get_site_object_from_dict(sitename, site)
  92. e = "Neither site '{0}' nor site '{1}:{2}' found in config.".format(name, project, lang)
  93. raise SiteNotFoundError(e)
  94. else:
  95. return _get_site_object_from_dict(name, site)
  96. # if we end up here, then project and lang are both not None
  97. for sitename, site in config.wiki["sites"].items():
  98. if site["project"] == project and site["lang"] == lang:
  99. return _get_site_object_from_dict(sitename, site)
  100. e = "Site '{0}:{1}' not found in config.".format(project, lang)
  101. raise SiteNotFoundError(e)