PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app_inv_game_server/game_server/utils.py

http://app-inventor-for-android.googlecode.com/
Python | 159 lines | 120 code | 8 blank | 31 comment | 1 complexity | f9fdcd3fcc9b6ecac0f5fb808facfe85 MD5 | raw file
Possible License(s): Apache-2.0
  1. # Copyright 2010 Google Inc.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. """
  12. Utility functions for input validation and database model access.
  13. """
  14. __authors__ = ['"Bill Magnuson" <billmag@mit.edu>']
  15. import re
  16. from google.appengine.ext import db
  17. from google.appengine.ext.db import Key
  18. EMAIL_ADDRESS_REGEX = ("([0-9a-zA-Z]+[-._+&amp;])*[0-9a-zA-Z]+@"
  19. "([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}")
  20. def get_game_model(gid):
  21. """ Return a Game model for the given game id.
  22. Args:
  23. gid: The game id of the Game.
  24. Returns:
  25. The database model for the specified id or None if no such model
  26. exists.
  27. """
  28. game_key = Key.from_path('Game', gid)
  29. model = db.get(game_key)
  30. return model
  31. def get_instance_model(gid, iid):
  32. """ Return a GameInstance model for the given game and instance ids.
  33. Args:
  34. gid: The game id of the GameInstance.
  35. iid: The instance id of the GameInstance.
  36. Returns:
  37. The database model for the specified ids or None if the
  38. GameInstance doesn't exist..
  39. """
  40. instance_key = Key.from_path('Game', gid, 'GameInstance', iid)
  41. model = db.get(instance_key)
  42. return model
  43. def check_playerid(pid, instance = None):
  44. """ Return a valid player id.
  45. Args:
  46. pid: A string containing the email address of the player or the
  47. special identified 'leader'.
  48. instance: (optional) The instance from which to fetch the leader
  49. from when pid is 'leader'.
  50. Returns:
  51. Strips the supplied player id of superfluous characters and
  52. returns only the email address. Also does conversion of the
  53. special string 'leader' to the current leader of instance.
  54. Raises:
  55. ValueError if pid does not match an email address regular
  56. expression.
  57. """
  58. if instance and pid.lower() == 'leader':
  59. pid = instance.leader
  60. if pid is None or pid == "":
  61. raise ValueError('The player identifier is blank.')
  62. stripped_email = re.search(EMAIL_ADDRESS_REGEX, pid)
  63. if stripped_email is None:
  64. raise ValueError('%s is not a valid email address.' % pid)
  65. return stripped_email.group(0)
  66. def check_gameid(gid):
  67. """ Validate the game id to make sure it is not empty.
  68. Args:
  69. gid: The game id to check
  70. Returns:
  71. The game id.
  72. Raises:
  73. ValueError if the game id is the empty string or None.
  74. """
  75. if gid == "" or gid is None:
  76. raise ValueError('Bad Game Id: %s' % gid)
  77. return gid
  78. def check_instanceid(iid):
  79. """ Validate the instance id to make sure it is not empty.
  80. Args:
  81. iid: The instance id to check
  82. Returns:
  83. The instance id.
  84. Raises:
  85. ValueError if the instance id is the empty string or None.
  86. """
  87. if iid == "" or iid is None:
  88. raise ValueError('No instance specified for request.')
  89. return iid
  90. def get_boolean(value):
  91. """ Return a bool from value.
  92. Args:
  93. value: A string or bool representing a boolean.
  94. Returns:
  95. If value is a bool, value is returned without modification.
  96. If value is a string this will convert 'true' and 'false'
  97. (ignoring case) to their associated values.
  98. Raises:
  99. ValueError if value does not match one of the string tests and is
  100. not a bool.
  101. """
  102. if type(value) is not bool:
  103. value = value.lower()
  104. if value == 'true':
  105. value = True
  106. elif value == 'false':
  107. value = False
  108. else:
  109. raise ValueError("Boolean value was not valid")
  110. return value
  111. def get_game(model):
  112. """ Return a Game object.
  113. Args:
  114. model: A database model that is either a GameInstance or Game.
  115. Returns:
  116. Either returns model or its parent if either of them is a Game
  117. object.
  118. Raises:
  119. ValueError if either model or its parent is not a Game object.
  120. """
  121. if model.__class__.__name__ == 'GameInstance':
  122. model = model.parent()
  123. if model.__class__.__name__ == 'Game':
  124. return model
  125. raise ValueError('Invalid model passed to get_game')