A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

98 řádky
3.8 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 core import config
  10. from wiki.tools.exceptions import ConfigError, SiteNotFoundError
  11. from wiki.tools.site import Site
  12. __all__ = ["get_site"]
  13. def _get_site_object_from_dict(name, d):
  14. """Return a Site object based on the contents of a dict, probably acquired
  15. through our config file, and a separate name."""
  16. project = d["project"]
  17. lang = d["lang"]
  18. try:
  19. api = d["apiURL"]
  20. except KeyError:
  21. api = None
  22. try:
  23. sql_server = d["sqlServer"]
  24. except KeyError:
  25. sql_server = None
  26. try:
  27. sql_db = d["sqlDB"]
  28. except KeyError:
  29. sql_db = None
  30. return Site(name, project, lang, api, (sql_server, sql_db))
  31. def get_site(name=None, project=None, lang=None):
  32. """Returns a Site instance based on information from our config file.
  33. With no arguments, returns the default site as specified by our config
  34. file. This is default = config.wiki["defaultSite"];
  35. config.wiki["sites"][default].
  36. With `name` specified, returns the site specified by
  37. config.wiki["sites"][name].
  38. With `project` and `lang` specified, returns the site specified by the
  39. member of config.wiki["sites"], `s`, for which s["project"] == project and
  40. s["lang"] == lang.
  41. Specifying a project without a lang or a lang without a project will raise
  42. TypeError. If all three args are specified, `name` will be first tried,
  43. then `project` and `lang`. If, with any number of args, a site cannot be
  44. found in the config, SiteNotFoundError is raised.
  45. """
  46. if config._config is None:
  47. e = "Config file has not been loaded: use config.verify_config() and then config.parse_config() to do so."
  48. raise ConfigError(e)
  49. if (project is None and lang is not None) or (project is not None and lang is None):
  50. e = "Keyword arguments 'lang' and 'project' must be specified together."
  51. raise TypeError(e)
  52. if name is None and project is None: # no args given (project is None implies lang is None)
  53. try: # ...so use the default site
  54. default = config.wiki["defaultSite"]
  55. except KeyError:
  56. e = "Default site is not specified in config."
  57. raise SiteNotFoundError(e)
  58. try:
  59. site = config.wiki["sites"][default]
  60. except KeyError:
  61. e = "Default site specified by config is not in the config's sites list."
  62. raise SiteNotFoundError(e)
  63. return _get_site_object_from_dict(default, site)
  64. if name is not None: # name arg given, but don't look at others yet
  65. try:
  66. site = config.wiki["sites"][name]
  67. except KeyError:
  68. if project is None: # implies lang is None, i.e., only name was given
  69. e = "Site '{0}' not found in config.".format(name)
  70. raise SiteNotFoundError(e)
  71. for sitename, site in config.wiki["sites"].items():
  72. if site["project"] == project and site["lang"] == lang:
  73. return _get_site_object_from_dict(sitename, site)
  74. e = "Neither site '{0}' nor site '{1}:{2}' found in config.".format(name, project, lang)
  75. raise SiteNotFoundError(e)
  76. else:
  77. return _get_site_object_from_dict(name, site)
  78. for sitename, site in config.wiki["sites"].items(): # implied lang and proj are not None
  79. if site["project"] == project and site["lang"] == lang:
  80. return _get_site_object_from_dict(sitename, site)
  81. e = "Site '{0}:{1}' not found in config.".format(project, lang)
  82. raise SiteNotFoundError(e)