Exemple #1
0
 def test_init(self):
     e = nameddict()
     self.assertEqual(len(e), 0)
     e = nameddict([('foo', 'bar'), ('pika', 'lol')])
     self.assertTrue(len(e), e)
     self.assertEqual(e['foo'], 'bar')
     self.assertEqual(e.foo, 'bar')
Exemple #2
0
 def test_add_key(self):
     e = nameddict()
     e['foo'] = 'bar'
     self.assertEqual(e.foo, 'bar')
     e = nameddict()
     e.foo = 'bar'
     self.assertEqual(e['foo'], 'bar')
 def test_init(self):
     e = nameddict()
     self.assertEqual(len(e), 0)
     e = nameddict([('foo', 'bar'), ('pika', 'lol')])
     self.assertTrue(len(e), e)
     self.assertEqual(e['foo'], 'bar')
     self.assertEqual(e.foo, 'bar')
 def test_add_key(self):
     e = nameddict()
     e['foo'] = 'bar'
     self.assertEqual(e.foo, 'bar')
     e = nameddict()
     e.foo = 'bar'
     self.assertEqual(e['foo'], 'bar')
Exemple #5
0
 def test_uppercase_keys(self):
     e = nameddict({'FOO': 'bar'})
     self.assertFalse('foo' in e)
     with self.assertRaises(AttributeError):
         e.foo
     self.assertEqual(e['FOO'], 'bar')
     self.assertEqual(e.FOO, 'bar')
 def test_del_key(self):
     e = nameddict([('foo', 'bar')])
     self.assertEqual(e.foo, 'bar')
     del e['foo']
     self.assertEqual(len(e), 0)
     with self.assertRaises(AttributeError):
         e.foo
 def test_uppercase_keys(self):
     e = nameddict({'FOO': 'bar'})
     self.assertFalse('foo' in e)
     with self.assertRaises(AttributeError):
         e.foo
     self.assertEqual(e['FOO'], 'bar')
     self.assertEqual(e.FOO, 'bar')
Exemple #8
0
 def test_del_key(self):
     e = nameddict([('foo', 'bar')])
     self.assertEqual(e.foo, 'bar')
     del e['foo']
     self.assertEqual(len(e), 0)
     with self.assertRaises(AttributeError):
         e.foo
Exemple #9
0
 def test_combined(self):
     proxy = contextobj(nameddict({'foo': 'bar'}))
     proxy['john'] = 'doe'
     self.assertEqual(proxy.john, 'doe')
     with proxy:
         proxy.clear()
         proxy['kikoo'] = 'plop'
         self.assertEqual(proxy.kikoo, 'plop')
 def test_combined(self):
     proxy = contextobj(nameddict({'foo': 'bar'}))
     proxy['john'] = 'doe'
     self.assertEqual(proxy.john, 'doe')
     with proxy:
         proxy.clear()
         proxy['kikoo'] = 'plop'
         self.assertEqual(proxy.kikoo, 'plop')
Exemple #11
0
    def test_nested_dict(self):
        data = {
            'foo': {
                'bar': {
                    'pika': 'value'
                }
            },
        }
        e = nameddict(data)
        self.assertEqual(e.foo, {'bar': {'pika': 'value'}})
        self.assertEqual(e.foo.bar, {'pika': 'value'})
        self.assertEqual(e.foo.bar.pika, 'value')

        e['pika'] = {'key': 'value'}
        self.assertEqual(e.pika, {'key': 'value'})
        self.assertEqual(e.pika.key, 'value')

        e = nameddict()
        e.foo = {'key': 'value'}
        self.assertEqual(e.foo.key, 'value')
 def test_start_from_empty_config(self):
     p = contextobj(nameddict())
     p['foo'] = 'bar'
     with p:
         self.assertEqual(p['foo'], 'bar')
         p['foo'] = 'foobar'
         with p:
             print p.keys()
             p.pop('foo')
         self.assertEqual(p['foo'], 'foobar')
     self.assertEqual(p['foo'], 'bar')
Exemple #13
0
 def test_start_from_empty_config(self):
     p = contextobj(nameddict())
     p['foo'] = 'bar'
     with p:
         self.assertEqual(p['foo'], 'bar')
         p['foo'] = 'foobar'
         with p:
             print p.keys()
             p.pop('foo')
         self.assertEqual(p['foo'], 'foobar')
     self.assertEqual(p['foo'], 'bar')
    def test_nested_dict(self):
        data = {
            'foo': {
                'bar': {
                    'pika': 'value'
                }
            },
        }
        e = nameddict(data)
        self.assertEqual(e.foo, {'bar': {'pika': 'value'}})
        self.assertEqual(e.foo.bar, {'pika': 'value'})
        self.assertEqual(e.foo.bar.pika, 'value')

        e['pika'] = {
            'key': 'value'
        }
        self.assertEqual(e.pika, {'key': 'value'})
        self.assertEqual(e.pika.key, 'value')

        e = nameddict()
        e.foo = {'key': 'value'}
        self.assertEqual(e.foo.key, 'value')
def token_info(token, refresh=True, refresh_cb=None, session=None):
    """
    :param OAuthToken token

    :param bool refresh:
      whether to attempt to refresh the OAuth token if it expired.
      default is `True`.

    :param refresh_cb:
      If specified, a callable object which is given the new token
      in parameter if it has been refreshed.

    :param requests.Session session:
      Optional `requests` session to use.

    :return:
      token information. see
      https://developers.google.com/identity/protocols/OAuth2UserAgent#tokeninfo-validation
      - `scope`: this field is not a space-delimited set of scopes
         but a real Python `set`.
      - `token`: additional field that provides the `OAuthToken`
      - `refreshed`: boolean that will tell if the token has been refreshed
    :rtype: nameddict
    """
    session = session or HTTP_SESSION
    params = dict(access_token=token.access_token)
    resp = session.get(TOKEN_INFO_URL, params=params)
    if resp.status_code != 200:
        if refresh:
            token = refresh_token(token, session=session)
            if refresh_cb is not None:
                try:
                    refresh_cb(token)
                except Exception:
                    LOGGER.exception('OAuth token refresh callback failed')
            info = token_info(token, refresh=False, session=session)
            info.update(refreshed=True)
            return info
        raise OAuthTokenRefreshRequiredError()
    info = __coerce_token_info(resp.json())
    info.update(token=token, refreshed=False)
    return nameddict(info)
Exemple #16
0
def token_info(token, refresh=True, refresh_cb=None, session=None):
    """
    :param OAuthToken token

    :param bool refresh:
      whether to attempt to refresh the OAuth token if it expired.
      default is `True`.

    :param refresh_cb:
      If specified, a callable object which is given the new token
      in parameter if it has been refreshed.

    :param requests.Session session:
      Optional `requests` session to use.

    :return:
      token information. see
      https://developers.google.com/identity/protocols/OAuth2UserAgent#tokeninfo-validation
      - `scope`: this field is not a space-delimited set of scopes
         but a real Python `set`.
      - `token`: additional field that provides the `OAuthToken`
      - `refreshed`: boolean that will tell if the token has been refreshed
    :rtype: nameddict
    """
    session = session or HTTP_SESSION
    params = dict(access_token=token.access_token)
    resp = session.get(TOKEN_INFO_URL, params=params)
    if resp.status_code != 200:
        if refresh:
            token = refresh_token(token, session=session)
            if refresh_cb is not None:
                try:
                    refresh_cb(token)
                except Exception:
                    LOGGER.exception('OAuth token refresh callback failed')
            info = token_info(token, refresh=False, session=session)
            info.update(refreshed=True)
            return info
        raise OAuthTokenRefreshRequiredError()
    info = __coerce_token_info(resp.json())
    info.update(token=token, refreshed=False)
    return nameddict(info)
 def test_nested_assignment(self):
     """ nested assignment is not supported"""
     e = nameddict()
     with self.assertRaises(AttributeError):
         e.foo.bar = 'pika'
Exemple #18
0
 def test_nested_assignment(self):
     """ nested assignment is not supported"""
     e = nameddict()
     with self.assertRaises(AttributeError):
         e.foo.bar = 'pika'