def is_snapshotting_enabled(self) -> bool: """ Decides whether or not snapshotting is enabled by reading environment variable 'IS_SNAPSHOTTING_ENABLED'. Snapshotting is not enabled by default. """ return strtobool(self.env.get(self.IS_SNAPSHOTTING_ENABLED, "no"))
def construct_repository(self) -> Repository: """ Constructs a :class:`Repository` for use by the application. """ cache_maxsize_envvar = self.env.get(self.AGGREGATE_CACHE_MAXSIZE) if cache_maxsize_envvar: cache_maxsize = int(cache_maxsize_envvar) else: cache_maxsize = None return Repository( event_store=self.events, snapshot_store=self.snapshots, cache_maxsize=cache_maxsize, fastforward=strtobool(self.env.get(self.AGGREGATE_CACHE_FASTFORWARD, "y")), )
def test_raises_type_error(self): for x in (None, True, False, 1, 2, 3): with self.assertRaises(TypeError): strtobool(cast(str, x))
def test_raises_value_error(self): for s in ("", "a", "b", "c"): with self.assertRaises(ValueError): strtobool(s)
def test_false_values(self): for s in ("n", "no", "f", "false", "off", "0"): self.assertFalse(strtobool(s), s)
def test_true_values(self): for s in ("y", "yes", "t", "true", "on", "1"): self.assertTrue(strtobool(s), s)
def env_create_table(self) -> bool: default = "yes" return bool(strtobool(self.getenv(self.CREATE_TABLE) or default))
def __init__(self, application_name: str, env: Mapping): super().__init__(application_name, env) dbname = self.getenv(self.POSTGRES_DBNAME) if dbname is None: raise EnvironmentError("Postgres database name not found " "in environment with key " f"'{self.POSTGRES_DBNAME}'") host = self.getenv(self.POSTGRES_HOST) if host is None: raise EnvironmentError("Postgres host not found " "in environment with key " f"'{self.POSTGRES_HOST}'") port = self.getenv(self.POSTGRES_PORT) or "5432" user = self.getenv(self.POSTGRES_USER) if user is None: raise EnvironmentError("Postgres user not found " "in environment with key " f"'{self.POSTGRES_USER}'") password = self.getenv(self.POSTGRES_PASSWORD) if password is None: raise EnvironmentError("Postgres password not found " "in environment with key " f"'{self.POSTGRES_PASSWORD}'") conn_max_age: Optional[float] conn_max_age_str = self.getenv(self.POSTGRES_CONN_MAX_AGE) if conn_max_age_str is None: conn_max_age = None elif conn_max_age_str == "": conn_max_age = None else: try: conn_max_age = float(conn_max_age_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_CONN_MAX_AGE}' is invalid. " f"If set, a float or empty string is expected: " f"'{conn_max_age_str}'") pre_ping = strtobool(self.getenv(self.POSTGRES_PRE_PING) or "no") lock_timeout_str = self.getenv(self.POSTGRES_LOCK_TIMEOUT) or "0" try: lock_timeout = float(lock_timeout_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_LOCK_TIMEOUT}' is invalid. " f"If set, a float or empty string is expected: " f"'{lock_timeout_str}'") idle_in_transaction_session_timeout_str = (self.getenv( self.POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT) or "0") try: idle_in_transaction_session_timeout = float( idle_in_transaction_session_timeout_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT}' is invalid. " f"If set, a float or empty string is expected: " f"'{idle_in_transaction_session_timeout_str}'") self.datastore = PostgresDatastore( dbname=dbname, host=host, port=port, user=user, password=password, conn_max_age=conn_max_age, pre_ping=pre_ping, lock_timeout=lock_timeout, idle_in_transaction_session_timeout= idle_in_transaction_session_timeout, )
def env_create_table(self) -> bool: return strtobool(self.env.get(self.CREATE_TABLE) or "yes")
def __init__(self, env: Environment): super().__init__(env) dbname = self.env.get(self.POSTGRES_DBNAME) if dbname is None: raise EnvironmentError("Postgres database name not found " "in environment with key " f"'{self.POSTGRES_DBNAME}'") host = self.env.get(self.POSTGRES_HOST) if host is None: raise EnvironmentError("Postgres host not found " "in environment with key " f"'{self.POSTGRES_HOST}'") port = self.env.get(self.POSTGRES_PORT) or "5432" user = self.env.get(self.POSTGRES_USER) if user is None: raise EnvironmentError("Postgres user not found " "in environment with key " f"'{self.POSTGRES_USER}'") password = self.env.get(self.POSTGRES_PASSWORD) if password is None: raise EnvironmentError("Postgres password not found " "in environment with key " f"'{self.POSTGRES_PASSWORD}'") connect_timeout: Optional[int] connect_timeout_str = self.env.get(self.POSTGRES_CONNECT_TIMEOUT) if connect_timeout_str is None: connect_timeout = 5 elif connect_timeout_str == "": connect_timeout = 5 else: try: connect_timeout = int(connect_timeout_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_CONNECT_TIMEOUT}' is invalid. " f"If set, an integer or empty string is expected: " f"'{connect_timeout_str}'") idle_in_transaction_session_timeout_str = (self.env.get( self.POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT) or "5") try: idle_in_transaction_session_timeout = int( idle_in_transaction_session_timeout_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT}' is invalid. " f"If set, an integer or empty string is expected: " f"'{idle_in_transaction_session_timeout_str}'") pool_size: Optional[int] pool_size_str = self.env.get(self.POSTGRES_POOL_SIZE) if pool_size_str is None: pool_size = 5 elif pool_size_str == "": pool_size = 5 else: try: pool_size = int(pool_size_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_POOL_SIZE}' is invalid. " f"If set, an integer or empty string is expected: " f"'{pool_size_str}'") pool_max_overflow: Optional[int] pool_max_overflow_str = self.env.get(self.POSTGRES_POOL_MAX_OVERFLOW) if pool_max_overflow_str is None: pool_max_overflow = 10 elif pool_max_overflow_str == "": pool_max_overflow = 10 else: try: pool_max_overflow = int(pool_max_overflow_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_POOL_MAX_OVERFLOW}' is invalid. " f"If set, an integer or empty string is expected: " f"'{pool_max_overflow_str}'") pool_timeout: Optional[float] pool_timeout_str = self.env.get(self.POSTGRES_POOL_TIMEOUT) if pool_timeout_str is None: pool_timeout = 30 elif pool_timeout_str == "": pool_timeout = 30 else: try: pool_timeout = float(pool_timeout_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_POOL_TIMEOUT}' is invalid. " f"If set, a float or empty string is expected: " f"'{pool_timeout_str}'") conn_max_age: Optional[float] conn_max_age_str = self.env.get(self.POSTGRES_CONN_MAX_AGE) if conn_max_age_str is None: conn_max_age = None elif conn_max_age_str == "": conn_max_age = None else: try: conn_max_age = float(conn_max_age_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_CONN_MAX_AGE}' is invalid. " f"If set, a float or empty string is expected: " f"'{conn_max_age_str}'") pre_ping = strtobool(self.env.get(self.POSTGRES_PRE_PING) or "no") lock_timeout_str = self.env.get(self.POSTGRES_LOCK_TIMEOUT) or "0" try: lock_timeout = int(lock_timeout_str) except ValueError: raise EnvironmentError( f"Postgres environment value for key " f"'{self.POSTGRES_LOCK_TIMEOUT}' is invalid. " f"If set, an integer or empty string is expected: " f"'{lock_timeout_str}'") schema = self.env.get(self.POSTGRES_SCHEMA) or "" self.datastore = PostgresDatastore( dbname=dbname, host=host, port=port, user=user, password=password, connect_timeout=connect_timeout, idle_in_transaction_session_timeout= idle_in_transaction_session_timeout, pool_size=pool_size, max_overflow=pool_max_overflow, pool_timeout=pool_timeout, conn_max_age=conn_max_age, pre_ping=pre_ping, lock_timeout=lock_timeout, schema=schema, )