def get_credential_storage_custom_key(
    filename, key_dict, warn_on_readonly=True):
  """Get a Storage instance for a credential using a dictionary as a key.

  Allows you to provide a dictionary as a custom key that will be used for
  credential storage and retrieval.

  Args:
    filename: The JSON file storing a set of credentials
    key_dict: A dictionary to use as the key for storing this credential. There
      is no ordering of the keys in the dictionary. Logically equivalent
      dictionaries will produce equivalent storage keys.
    warn_on_readonly: if True, log a warning if the store is readonly

  Returns:
    An object derived from client.Storage for getting/setting the
    credential.
  """
  filename = os.path.expanduser(filename)
  _multistores_lock.acquire()
  try:
    multistore = _multistores.setdefault(
        filename, _MultiStore(filename, warn_on_readonly=warn_on_readonly))
  finally:
    _multistores_lock.release()
  key = util.dict_to_tuple_key(key_dict)
  return multistore._get_storage(key)
Example #2
0
def get_credential_storage_custom_key(filename,
                                      key_dict,
                                      warn_on_readonly=True):
    """Get a Storage instance for a credential using a dictionary as a key.

  Allows you to provide a dictionary as a custom key that will be used for
  credential storage and retrieval.

  Args:
    filename: The JSON file storing a set of credentials
    key_dict: A dictionary to use as the key for storing this credential. There
      is no ordering of the keys in the dictionary. Logically equivalent
      dictionaries will produce equivalent storage keys.
    warn_on_readonly: if True, log a warning if the store is readonly

  Returns:
    An object derived from client.Storage for getting/setting the
    credential.
  """
    filename = os.path.expanduser(filename)
    _multistores_lock.acquire()
    try:
        multistore = _multistores.setdefault(
            filename, _MultiStore(filename, warn_on_readonly=warn_on_readonly))
    finally:
        _multistores_lock.release()
    key = util.dict_to_tuple_key(key_dict)
    return multistore._get_storage(key)
Example #3
0
  def test_key_conversions(self):
    d = {'somekey': 'some value', 'another': 'something else', 'onemore': 'foo'}
    tuple_key = util.dict_to_tuple_key(d)

    # the resulting key should be naturally sorted
    self.assertEqual(
        (('another', 'something else'),
         ('onemore', 'foo'),
         ('somekey', 'some value')),
        tuple_key)

    # check we get the original dictionary back
    self.assertEqual(d, dict(tuple_key))
  def test_key_conversions(self):
    d = {'somekey': 'some value', 'another': 'something else', 'onemore': 'foo'}
    tuple_key = util.dict_to_tuple_key(d)

    # the resulting key should be naturally sorted
    self.assertEqual(
        (('another', 'something else'),
         ('onemore', 'foo'),
         ('somekey', 'some value')),
        tuple_key)

    # check we get the original dictionary back
    self.assertEqual(d, dict(tuple_key))
    def _decode_credential_from_json(self, cred_entry):
        """Load a credential from our JSON serialization.

        Args:
          cred_entry: A dict entry from the data member of our format

        Returns:
          (key, cred) where the key is the key tuple and the cred is the
            OAuth2Credential object.
        """
        raw_key = cred_entry['key']
        key = util.dict_to_tuple_key(raw_key)
        credential = None
        credential = Credentials.new_from_json(simplejson.dumps(cred_entry['credential']))
        return (key, credential)
Example #6
0
  def _decode_credential_from_json(self, cred_entry):
    """Load a credential from our JSON serialization.

    Args:
      cred_entry: A dict entry from the data member of our format

    Returns:
      (key, cred) where the key is the key tuple and the cred is the
        OAuth2Credential object.
    """
    raw_key = cred_entry['key']
    key = util.dict_to_tuple_key(raw_key)
    credential = None
    credential = Credentials.new_from_json(json.dumps(cred_entry['credential']))
    return (key, credential)