update_database.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. cmds = parser.add_mutually_exclusive_group(required=True)
  27. cmds.add_argument('-n', '--newplayer', type=str, default=None,
  28. nargs='+',
  29. help='Add a new player.')
  30. cmds.add_argument('-e', '--enableexpansion', action='store_true',
  31. default=False,
  32. help='Enable an expansion.')
  33. cmds.add_argument('-d', '--disableexpansion', action='store_true',
  34. default=False,
  35. help='Disable an expansion.')
  36. return parser.parse_args()
  37. def main():
  38. """
  39. main routine
  40. """
  41. args = parseArgs()
  42. conn = sqlite3.connect('CarcassonneScore.db')
  43. cur = conn.cursor()
  44. VALID = False
  45. if args.newplayer:
  46. pname = ' '.join(args.newplayer)
  47. sys.stdout.write("Adding new player: " + pname)
  48. sys.stdout.write(" to the database.\n")
  49. while not VALID:
  50. ans = input("Is this correct (y/n)? ")
  51. if re.match('y', ans, re.IGNORECASE):
  52. cur.execute('INSERT INTO players (name) VALUES ("' + \
  53. pname + '")')
  54. sys.stdout.write(pname + ' added to the database.\n')
  55. VALID = True
  56. elif re.match('n', ans, re.IGNORECASE):
  57. sys.stdout.write('Canceling.\n')
  58. VALID = True
  59. conn.commit()
  60. conn.close()
  61. if __name__ == "__main__":
  62. main()