1
0

update_database.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. """
  3. Database management for Carcassonne score keeping system.
  4. Copyright 2018 George C. Privon
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. """
  16. import sys
  17. import argparse
  18. import re
  19. import sqlite3
  20. def parseArgs():
  21. """
  22. Command line arguments
  23. """
  24. parser = argparse.ArgumentParser(description="Update the Carcassonne \
  25. scoring database.")
  26. parser.add_argument('-n', '--newplayer', type=str, default=None,
  27. help='Add a new player.')
  28. parser.add_argument('-e', '--enableexpansion', action='store_true',
  29. default=False,
  30. help='Enable an expansion.')
  31. parser.add_argument('-d', '--disableexpansion', action='store_true',
  32. default=False,
  33. help='Disable an expansion.')
  34. return parser.parse_args()
  35. def main():
  36. """
  37. main routine
  38. """
  39. args = parseArgs()
  40. conn = sqlite3.connect('CarcassonneScore.db')
  41. cur = conn.cursor()
  42. VALID = False
  43. if args.newplayer:
  44. sys.stdout.write("Adding new player: " + args.newplayer)
  45. sys.stdout.write(" to the database.\n")
  46. while not VALID:
  47. ans = input("Is this correct (y/n)? ")
  48. if re.match('y', ans, re.IGNORECASE):
  49. cur.execute('INSERT INTO players (name) VALUES ("' + \
  50. args.newplayer + '")')
  51. VALID = True
  52. elif re.match('n', ans, re.IGNORECASE):
  53. VALID = True
  54. conn.commit()
  55. conn.close()
  56. if __name__ == "__main__":
  57. main()