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.

182 lines
6.9 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 cookielib import LWPCookieJar, LoadError
  10. import errno
  11. from getpass import getpass
  12. from os import chmod, path
  13. import stat
  14. from core import config
  15. from wiki.tools.exceptions import SiteNotFoundError
  16. from wiki.tools.site import Site
  17. __all__ = ["get_site"]
  18. _cookiejar = None
  19. def _load_config():
  20. """Called by a config-requiring function, such as get_site(), when config
  21. has not been loaded. This will usually happen only if we're running code
  22. directly from Python's interpreter and not the bot itself, because
  23. earwigbot.py or core/main.py will already call these functions.
  24. """
  25. is_encrypted = config.verify_config()
  26. if is_encrypted: # passwords in the config file are encrypted
  27. key = getpass("Enter key to unencrypt bot passwords: ")
  28. config.parse_config(key)
  29. else:
  30. config.parse_config(None)
  31. def _get_cookiejar():
  32. """Returns a LWPCookieJar object loaded from our .cookies file. The same
  33. one is returned every time.
  34. The .cookies file is located in the project root, same directory as
  35. config.json and earwigbot.py. If it doesn't exist, we will create the file
  36. and set it to be readable and writeable only by us. If it exists but the
  37. information inside is bogus, we will ignore it.
  38. This is normally called by _get_site_object_from_dict() (in turn called by
  39. get_site()), and the cookiejar is passed to our Site's constructor, used
  40. when it makes API queries. This way, we can easily preserve cookies between
  41. sites (e.g., for CentralAuth), making logins easier.
  42. """
  43. global _cookiejar
  44. if _cookiejar is not None:
  45. return _cookiejar
  46. cookie_file = path.join(config.root_dir, ".cookies")
  47. _cookiejar = LWPCookieJar(cookie_file)
  48. try:
  49. _cookiejar.load()
  50. except LoadError:
  51. # file contains bad data, so ignore it completely
  52. pass
  53. except IOError as e:
  54. if e.errno == errno.ENOENT: # "No such file or directory"
  55. # create the file and restrict reading/writing only to the owner,
  56. # so others can't peak at our cookies
  57. open(cookie_file, "w").close()
  58. chmod(cookie_file, stat.S_IRUSR|stat.S_IWUSR)
  59. else:
  60. raise
  61. return _cookiejar
  62. def _get_site_object_from_dict(name, d):
  63. """Return a Site object based on the contents of a dict, probably acquired
  64. through our config file, and a separate name.
  65. """
  66. project = d.get("project")
  67. lang = d.get("lang")
  68. base_url = d.get("baseURL")
  69. article_path = d.get("articlePath")
  70. script_path = d.get("scriptPath")
  71. sql = (d.get("sqlServer"), d.get("sqlDB"))
  72. namespaces = d.get("namespaces")
  73. login = (config.wiki.get("username"), config.wiki.get("password"))
  74. cookiejar = _get_cookiejar()
  75. return Site(name=name, project=project, lang=lang, base_url=base_url,
  76. article_path=article_path, script_path=script_path, sql=sql,
  77. namespaces=namespaces, login=login, cookiejar=cookiejar)
  78. def get_site(name=None, project=None, lang=None):
  79. """Returns a Site instance based on information from our config file.
  80. With no arguments, returns the default site as specified by our config
  81. file. This is default = config.wiki["defaultSite"];
  82. config.wiki["sites"][default].
  83. With `name` specified, returns the site specified by
  84. config.wiki["sites"][name].
  85. With `project` and `lang` specified, returns the site specified by the
  86. member of config.wiki["sites"], `s`, for which s["project"] == project and
  87. s["lang"] == lang.
  88. We will attempt to login to the site automatically
  89. using config.wiki["username"] and config.wiki["password"] if both are
  90. defined.
  91. Specifying a project without a lang or a lang without a project will raise
  92. TypeError. If all three args are specified, `name` will be first tried,
  93. then `project` and `lang`. If, with any number of args, a site cannot be
  94. found in the config, SiteNotFoundError is raised.
  95. """
  96. # check if config has been loaded, and load it if it hasn't
  97. if not config.is_config_loaded():
  98. _load_config()
  99. # someone specified a project without a lang (or a lang without a project)!
  100. if (project is None and lang is not None) or (project is not None and
  101. lang is None):
  102. e = "Keyword arguments 'lang' and 'project' must be specified together."
  103. raise TypeError(e)
  104. # no args given, so return our default site (project is None implies lang
  105. # is None, so we don't need to add that in)
  106. if name is None and project is None:
  107. try:
  108. default = config.wiki["defaultSite"]
  109. except KeyError:
  110. e = "Default site is not specified in config."
  111. raise SiteNotFoundError(e)
  112. try:
  113. site = config.wiki["sites"][default]
  114. except KeyError:
  115. e = "Default site specified by config is not in the config's sites list."
  116. raise SiteNotFoundError(e)
  117. return _get_site_object_from_dict(default, site)
  118. # name arg given, but don't look at others unless `name` isn't found
  119. if name is not None:
  120. try:
  121. site = config.wiki["sites"][name]
  122. except KeyError:
  123. if project is None: # implies lang is None, so only name was given
  124. e = "Site '{0}' not found in config.".format(name)
  125. raise SiteNotFoundError(e)
  126. for sitename, site in config.wiki["sites"].items():
  127. if site["project"] == project and site["lang"] == lang:
  128. return _get_site_object_from_dict(sitename, site)
  129. e = "Neither site '{0}' nor site '{1}:{2}' found in config."
  130. e.format(name, project, lang)
  131. raise SiteNotFoundError(e)
  132. else:
  133. return _get_site_object_from_dict(name, site)
  134. # if we end up here, then project and lang are both not None
  135. for sitename, site in config.wiki["sites"].items():
  136. if site["project"] == project and site["lang"] == lang:
  137. return _get_site_object_from_dict(sitename, site)
  138. e = "Site '{0}:{1}' not found in config.".format(project, lang)
  139. raise SiteNotFoundError(e)
  140. def add_site():
  141. """STUB: config editing is required first.
  142. Returns True if the site was added successfully or False if the site was
  143. already in our config. Raises ConfigError if saving the updated file failed
  144. for some reason."""
  145. pass
  146. def del_site(name):
  147. """STUB: config editing is required first.
  148. Returns True if the site was removed successfully or False if the site was
  149. not in our config originally. Raises ConfigError if saving the updated file
  150. failed for some reason."""
  151. pass