def env(key, default='', type=None): "Extract an environment variable for use in configuration" # First check an internal cache, so we can `pop` multiple times # without actually losing the value. try: rv = env._cache[key] except KeyError: if 'SENTRY_RUNNING_UWSGI' in os.environ: # We do this so when the process forks off into uwsgi # we want to actually be popping off values. This is so that # at runtime, the variables aren't actually available. fn = os.environ.pop else: fn = os.environ.__getitem__ try: rv = fn(key) env._cache[key] = rv except KeyError: rv = default if type is None: type = type_from_value(default) return type(rv)
def register( self, key, default=None, type=None, flags=DEFAULT_FLAGS, ttl=DEFAULT_KEY_TTL, grace=DEFAULT_KEY_GRACE ): assert key not in self.registry, 'Option already registered: %r' % key if len(key) > 64: raise ValueError('Option key has max length of 64 characters') # If our default is a callable, execute it to # see what value is returns, so we can use that to derive the type if not callable(default): default_value = default def default(): return default_value else: default_value = default() # Guess type based on the default value if type is None: # the default value would be equivilent to '' if no type / default # is specified and we assume six.text_type for safety if default_value is None: default_value = u'' def default(): return default_value type = type_from_value(default_value) # We disallow None as a value for options since this is ambiguous and doesn't # really make sense as config options. There should be a sensible default # value instead that matches the type expected, rather than relying on None. if type is NoneType: raise TypeError('Options must not be None') # Make sure the type is correct at registration time if default_value is not None and not type.test(default_value): raise TypeError('got %r, expected %r' % (_type(default), type)) # If we don't have a default, but we have a type, pull the default # value from the type if default_value is None: default = type default_value = default() # Boolean values need to be set to ALLOW_EMPTY becaues otherwise, "False" # would be treated as a not valid value if default_value is True or default_value is False: flags |= FLAG_ALLOW_EMPTY settings.SENTRY_DEFAULT_OPTIONS[key] = default_value self.registry[key] = self.store.make_key(key, default, type, flags, ttl, grace)
def env(key, default='', type=None): """ Extract an environment variable for use in configuration :param key: The environment variable to be extracted. :param default: The value to be returned if `key` is not found. :param type: The type of the returned object (defaults to the type of `default`). :return: The environment variable if it exists, else `default`. """ # First check an internal cache, so we can `pop` multiple times # without actually losing the value. try: rv = env._cache[key] except KeyError: if 'SENTRY_RUNNING_UWSGI' in os.environ: # We do this so when the process forks off into uwsgi # we want to actually be popping off values. This is so that # at runtime, the variables aren't actually available. fn = os.environ.pop else: fn = os.environ.__getitem__ try: rv = fn(key) env._cache[key] = rv except KeyError: rv = default if type is None: type = type_from_value(default) return type(rv)
def env(key, default='', type=None): "Extract an environment variable for use in configuration" if 'SENTRY_RUNNING_UWSGI' in os.environ: # We do this so when the process forks off into uwsgi # we want to actually be popping off values. This is so that # at runtime, the variables aren't actually available. fn = os.environ.pop else: fn = os.environ.get if type is None: type = type_from_value(default) return type(fn(key, default))
def register(self, key, default=None, type=None, flags=DEFAULT_FLAGS, ttl=DEFAULT_KEY_TTL, grace=DEFAULT_KEY_GRACE): assert key not in self.registry, 'Option already registered: %r' % key # If our default is a callable, execute it to # see what value is returns, so we can use that to derive the type if not callable(default): default_value = default default = lambda: default_value else: default_value = default() # Guess type based on the default value if type is None: # the default value would be equivilent to '' if no type / default # is specified and we assume unicode for safety if default_value is None: default_value = u'' default = lambda: default_value type = type_from_value(default_value) # We disallow None as a value for options since this is ambiguous and doesn't # really make sense as config options. There should be a sensible default # value instead that matches the type expected, rather than relying on None. if type is NoneType: raise TypeError('Options must not be NoneType') # Make sure the type is correct at registration time if default_value is not None and not type.test(default_value): raise TypeError('got %r, expected %r' % (_type(default), type)) # If we don't have a default, but we have a type, pull the default # value from the type if default_value is None: default = type self.registry[key] = self.store.make_key(key, default, type, flags, ttl, grace)