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.

functions.py 7.3 KiB

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