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.

140 lines
5.1 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. try:
  31. project = d["project"]
  32. except KeyError:
  33. project = None
  34. try:
  35. lang = d["lang"]
  36. except KeyError:
  37. lang = None
  38. try:
  39. base_url = d["baseURL"]
  40. except KeyError:
  41. base_url = None
  42. try:
  43. article_path = d["articlePath"]
  44. except KeyError:
  45. article_path = None
  46. try:
  47. script_path = d["scriptPath"]
  48. except KeyError:
  49. script_path = None
  50. try:
  51. sql_server = d["sqlServer"]
  52. except KeyError:
  53. sql_server = None
  54. try:
  55. sql_db = d["sqlDB"]
  56. except KeyError:
  57. sql_db = None
  58. try:
  59. namespaces = d["namespaces"]
  60. except KeyError:
  61. namespaces = None
  62. return Site(name=name, project=project, lang=lang, base_url=base_url,
  63. article_path=article_path, script_path=script_path,
  64. sql=(sql_server, sql_db), namespaces=namespaces)
  65. def get_site(name=None, project=None, lang=None):
  66. """Returns a Site instance based on information from our config file.
  67. With no arguments, returns the default site as specified by our config
  68. file. This is default = config.wiki["defaultSite"];
  69. config.wiki["sites"][default].
  70. With `name` specified, returns the site specified by
  71. config.wiki["sites"][name].
  72. With `project` and `lang` specified, returns the site specified by the
  73. member of config.wiki["sites"], `s`, for which s["project"] == project and
  74. s["lang"] == lang.
  75. Specifying a project without a lang or a lang without a project will raise
  76. TypeError. If all three args are specified, `name` will be first tried,
  77. then `project` and `lang`. If, with any number of args, a site cannot be
  78. found in the config, SiteNotFoundError is raised.
  79. """
  80. # check if config has been loaded, and load it if it hasn't
  81. if not config.is_config_loaded():
  82. _load_config()
  83. # someone specified a project without a lang (or a lang without a project)!
  84. if (project is None and lang is not None) or (project is not None and lang is None):
  85. e = "Keyword arguments 'lang' and 'project' must be specified together."
  86. raise TypeError(e)
  87. # no args given, so return our default site (project is None implies lang
  88. # is None, so we don't need to add that in)
  89. if name is None and project is None:
  90. try: # ...so use the default site
  91. default = config.wiki["defaultSite"]
  92. except KeyError:
  93. e = "Default site is not specified in config."
  94. raise SiteNotFoundError(e)
  95. try:
  96. site = config.wiki["sites"][default]
  97. except KeyError:
  98. e = "Default site specified by config is not in the config's sites list."
  99. raise SiteNotFoundError(e)
  100. return _get_site_object_from_dict(default, site)
  101. # name arg given, but don't look at others unless `name` isn't found
  102. if name is not None:
  103. try:
  104. site = config.wiki["sites"][name]
  105. except KeyError:
  106. if project is None: # implies lang is None, i.e., only name was given
  107. e = "Site '{0}' not found in config.".format(name)
  108. raise SiteNotFoundError(e)
  109. for sitename, site in config.wiki["sites"].items():
  110. if site["project"] == project and site["lang"] == lang:
  111. return _get_site_object_from_dict(sitename, site)
  112. e = "Neither site '{0}' nor site '{1}:{2}' found in config.".format(name, project, lang)
  113. raise SiteNotFoundError(e)
  114. else:
  115. return _get_site_object_from_dict(name, site)
  116. # if we end up here, then project and lang are both not None
  117. for sitename, site in config.wiki["sites"].items():
  118. if site["project"] == project and site["lang"] == lang:
  119. return _get_site_object_from_dict(sitename, site)
  120. e = "Site '{0}:{1}' not found in config.".format(project, lang)
  121. raise SiteNotFoundError(e)