1
0

update_database.py 2.3 KB

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