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ů.

76 řádky
3.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 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(name=None, project=None, lang=None):
  14. """Returns a Site instance based on information from our config file.
  15. With no arguments, returns the default site as specified by our config
  16. file. This is default = config.wiki["defaultSite"];
  17. config.wiki["sites"][default].
  18. With `name` specified, returns the site specified by
  19. config.wiki["sites"][name].
  20. With `project` and `lang` specified, returns the site specified by the
  21. member of config.wiki["sites"], `s`, for which s["project"] == project and
  22. s["lang"] == lang.
  23. Specifying a project without a lang or a lang without a project will raise
  24. TypeError. If all three args are specified, `name` will be first tried,
  25. then `project` and `lang`. If, with any number of args, a site cannot be
  26. found in the config, SiteNotFoundError is raised.
  27. """
  28. if config._config is None:
  29. e = "Config file has not been loaded: use config.verify_config() and then config.parse_config() to do so."
  30. raise ConfigError(e)
  31. if (project is None and lang is not None) or (project is not None and lang is None):
  32. e = "Keyword arguments 'lang' and 'project' must be specified together."
  33. raise TypeError(e)
  34. if name is None and project is None: # no args given (project is None implies lang is None)
  35. try: # ...so use the default site
  36. default = config.wiki["defaultSite"]
  37. except KeyError:
  38. e = "Default site is not specified in config."
  39. raise SiteNotFoundError(e)
  40. try:
  41. return config.wiki["sites"][default]
  42. except KeyError:
  43. e = "Default site specified by config is not in the config's sites list."
  44. raise SiteNotFoundError(e)
  45. if name is not None: # name arg given, but don't look at others yet
  46. try:
  47. return config.wiki["sites"][name]
  48. except KeyError:
  49. if project is None: # implies lang is None, i.e., only name was given
  50. e = "Site '{0}' not found in config.".format(name)
  51. raise SiteNotFoundError(e)
  52. for site in config.wiki["sites"].values():
  53. if site["project"] == project and site["lang"] == lang:
  54. return site
  55. e = "Neither site '{0}' nor site '{1}:{2}' found in config.".format(name, project, lang)
  56. raise SiteNotFoundError(e)
  57. for site in config.wiki["sites"].values(): # implied lang and proj are not None
  58. if site["project"] == project and site["lang"] == lang:
  59. return site
  60. e = "Site '{0}:{1}' not found in config.".format(project, lang)
  61. raise SiteNotFoundError(e)