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.

69 line
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import sqlite3 as sqlite
  23. from threading import Lock
  24. __all__ = ["PermissionsDB"]
  25. class PermissionsDB(object):
  26. def __init__(self, dbfile):
  27. self._dbfile = dbfile
  28. self._db_access_lock = Lock()
  29. self._data = {}
  30. def __repr__(self):
  31. """Return the canonical string representation of the PermissionsDB."""
  32. res = "PermissionsDB(dbfile={0!r})"
  33. return res.format(self._dbfile)
  34. def __str__(self):
  35. """Return a nice string representation of the PermissionsDB."""
  36. return "<PermissionsDB at {0}>".format(self._dbfile)
  37. def _create(self, conn):
  38. """Initialize the permissions database with its necessary tables."""
  39. query = """CREATE TABLE users (user_nick, user_ident, user_host,
  40. user_rank)"""
  41. conn.execute(query)
  42. def load(self):
  43. """Load permissions from an existing database, or create a new one."""
  44. query = "SELECT user_nick, user_ident, user_host, user_rank FROM users"
  45. self._data = {}
  46. with sqlite.connect(self._dbfile) as conn, self._db_access_lock:
  47. try:
  48. for nick, ident, host, rank in conn.execute(query):
  49. try:
  50. self._data[rank].append(_User(nick, ident, host))
  51. except KeyError:
  52. self._data[rank] = [_User(nick, ident, host)]
  53. except sqlite.OperationalError:
  54. self._create(conn)
  55. class _User(object):
  56. def __init__(self, nick, ident, host):
  57. self.nick = nick
  58. self.ident = ident
  59. self.host = host