def _parse_from_args()

in src/google/appengine/ext/ndb/key.py [0:0]


  def _parse_from_args(pairs=None, flat=None, app=None, namespace=None,
                       parent=None):
    if flat:
      if pairs is not None:
        raise TypeError('Key() cannot accept both flat and pairs arguments.')
      if len(flat) % 2:
        raise ValueError('Key() must have an even number of positional '
                         'arguments.')
      pairs = [(flat[i], flat[i + 1]) for i in range(0, len(flat), 2)]
    else:
      pairs = list(pairs)
    if not pairs:
      raise TypeError('Key must consist of at least one pair.')
    for i, (kind, id) in enumerate(pairs):
      if isinstance(id, six.text_type):
        id = six.ensure_binary(id)
      elif id is None:
        if i + 1 < len(pairs):
          raise datastore_errors.BadArgumentError(
              'Incomplete Key entry must be last')
      else:
        if not isinstance(id, six.integer_types + (six.binary_type,)):
          raise TypeError('Key id must be a string or a number; received %r' %
                          id)
      if isinstance(kind, type):
        kind = kind._get_kind()
      if isinstance(kind, six.text_type):
        kind = six.ensure_binary(kind)
      if not isinstance(kind, six.binary_type):
        raise TypeError(
            'Key kind must be a string or Model class; received %r' % kind)
      if not id:
        id = None
      pairs[i] = (kind, id)
    if parent is not None:
      if not isinstance(parent, Key):
        raise datastore_errors.BadValueError(
            'Expected Key instance, got %r' % parent)
      if not parent.id():
        raise datastore_errors.BadArgumentError(
            'Parent cannot have incomplete key')
      pairs[:0] = parent.__pairs
      if app:
        if app != parent.app():
          raise ValueError('Cannot specify a different app %r '
                           'than the parent app %r' %
                           (app, parent.app()))
      else:
        app = parent.__app
      if namespace is not None:
        if namespace != parent.namespace():
          raise ValueError('Cannot specify a different namespace %r '
                           'than the parent namespace %r' %
                           (namespace, parent.namespace()))
      else:
        namespace = parent.__namespace
    if not app:
      app = _DefaultAppId()
    if namespace is None:
      namespace = namespace_manager.get_namespace()



    app = six.ensure_binary(app)
    namespace = six.ensure_binary(namespace)

    return tuple(pairs), app, namespace