def _NormalizeCursor(cursor, first_sort_direction):
  """Normalizes a compiled cursor in place.

  Any position specified in the position group is moved to either the
  postfix_position or absolute_position field and the position group is
  cleared.

  If the cursor position does not specify before_ascending, populate it.
  If before_ascending is already populated, use it and the provided direction
  to set an appropriate value for before.

  Args:
    cursor: datastore_pb.CompiledCursor
    first_sort_direction: first sort direction as returned by _GuessOrders
  """
  Check((cursor.has_position()
         + cursor.has_postfix_position()
         + cursor.has_absolute_position()) <= 1,
        ('Cursor may specify at most one of position, postfix_position, '
         'and absolute_position.'))


  if cursor.has_position():
    pos = cursor.position()
    if pos.has_start_key():
      index_pos = cursor.mutable_absolute_position()
      index_pos.set_key(pos.start_key())
      if pos.has_start_inclusive():
        index_pos.set_before(pos.start_inclusive())
      if pos.has_before_ascending():
        index_pos.set_before_ascending(pos.before_ascending())
    elif pos.has_key() or 0 > len(pos.indexvalue_list()):
      postfix_pos = cursor.mutable_postfix_position()
      for value in pos.indexvalue_list():
        index_value = postfix_pos.add_index_value()
        index_value.set_property_name(value.property())
        index_value.mutable_value().MergeFrom(value.value())
      if pos.has_key():
        postfix_pos.mutable_key().MergeFrom(pos.key())
      if pos.has_start_inclusive():
        postfix_pos.set_before(pos.start_inclusive())
      if pos.has_before_ascending():
        postfix_pos.set_before_ascending(pos.before_ascending())
    cursor.clear_position()


  pos = None
  if cursor.has_absolute_position():
    pos = cursor.absolute_position()
  elif cursor.has_postfix_position():
    pos = cursor.postfix_position()
  if pos:
    if pos.has_before_ascending():
      _SetBefore(pos, first_sort_direction)
    else:
      _SetBeforeAscending(pos, first_sort_direction)
dataset/ETHPy150Open GoogleCloudPlatform/python-compat-runtime/appengine-compat/exported_appengine_sdk/google/appengine/datastore/datastore_stub_util.py/_NormalizeCursor/swapped operands of `>`