def __init__(cls, name, bases, attrs): super(MetaModel, cls).__init__(name, bases, attrs) if isinstance(attrs, dict): for attr_name, attr_obj in attrs.items(): if attr_name == META_CLASS_NAME: if not hasattr(attr_obj, REGION): setattr(attr_obj, REGION, get_settings_value('region')) if not hasattr(attr_obj, HOST): setattr(attr_obj, HOST, get_settings_value('host')) if hasattr(attr_obj, 'session_cls') or hasattr( attr_obj, 'request_timeout_seconds'): warnings.warn( "The `session_cls` and `request_timeout_second` options are no longer supported" ) if not hasattr(attr_obj, 'connect_timeout_seconds'): setattr(attr_obj, 'connect_timeout_seconds', get_settings_value('connect_timeout_seconds')) if not hasattr(attr_obj, 'read_timeout_seconds'): setattr(attr_obj, 'read_timeout_seconds', get_settings_value('read_timeout_seconds')) if not hasattr(attr_obj, 'base_backoff_ms'): setattr(attr_obj, 'base_backoff_ms', get_settings_value('base_backoff_ms')) if not hasattr(attr_obj, 'max_retry_attempts'): setattr(attr_obj, 'max_retry_attempts', get_settings_value('max_retry_attempts')) if not hasattr(attr_obj, 'max_pool_connections'): setattr(attr_obj, 'max_pool_connections', get_settings_value('max_pool_connections')) if not hasattr(attr_obj, 'extra_headers'): setattr(attr_obj, 'extra_headers', get_settings_value('extra_headers')) if not hasattr(attr_obj, 'aws_access_key_id'): setattr(attr_obj, 'aws_access_key_id', None) if not hasattr(attr_obj, 'aws_secret_access_key'): setattr(attr_obj, 'aws_secret_access_key', None) elif issubclass(attr_obj.__class__, (Index, )): attr_obj.Meta.model = cls if not hasattr(attr_obj.Meta, "index_name"): attr_obj.Meta.index_name = attr_name elif issubclass(attr_obj.__class__, (Attribute, )): if attr_obj.attr_name is None: attr_obj.attr_name = attr_name if META_CLASS_NAME not in attrs: setattr(cls, META_CLASS_NAME, DefaultMeta) # create a custom Model.DoesNotExist derived from pynamodb.exceptions.DoesNotExist, # so that "except Model.DoesNotExist:" would not catch other models' exceptions if 'DoesNotExist' not in attrs: exception_attrs = {'__module__': attrs.get('__module__')} if hasattr(cls, '__qualname__'): # On Python 3, Model.DoesNotExist exception_attrs['__qualname__'] = '{}.{}'.format( cls.__qualname__, 'DoesNotExist') cls.DoesNotExist = type('DoesNotExist', (DoesNotExist, ), exception_attrs)
def __init__(self, region=None, host=None, read_timeout_seconds=None, connect_timeout_seconds=None, max_retry_attempts=None, base_backoff_ms=None, max_pool_connections=None, extra_headers=None): self._tables = {} self.host = host self._local = local() self._client = None if region: self.region = region else: self.region = get_settings_value('region') if connect_timeout_seconds is not None: self._connect_timeout_seconds = connect_timeout_seconds else: self._connect_timeout_seconds = get_settings_value('connect_timeout_seconds') if read_timeout_seconds is not None: self._read_timeout_seconds = read_timeout_seconds else: self._read_timeout_seconds = get_settings_value('read_timeout_seconds') if max_retry_attempts is not None: self._max_retry_attempts_exception = max_retry_attempts else: self._max_retry_attempts_exception = get_settings_value('max_retry_attempts') if base_backoff_ms is not None: self._base_backoff_ms = base_backoff_ms else: self._base_backoff_ms = get_settings_value('base_backoff_ms') if max_pool_connections is not None: self._max_pool_connections = max_pool_connections else: self._max_pool_connections = get_settings_value('max_pool_connections') if extra_headers is not None: self._extra_headers = extra_headers else: self._extra_headers = get_settings_value('extra_headers')
def __init__(self, name, bases, namespace, discriminator=None) -> None: super().__init__(name, bases, namespace, discriminator) cls = cast(Type['Model'], self) for attr_name, attribute in cls.get_attributes().items(): if attribute.is_hash_key: if cls._hash_keyname and cls._hash_keyname != attr_name: raise ValueError( f"{cls.__name__} has more than one hash key: {cls._hash_keyname}, {attr_name}" ) cls._hash_keyname = attr_name if attribute.is_range_key: if cls._range_keyname and cls._range_keyname != attr_name: raise ValueError( f"{cls.__name__} has more than one range key: {cls._range_keyname}, {attr_name}" ) cls._range_keyname = attr_name if isinstance(attribute, VersionAttribute): if cls._version_attribute_name and cls._version_attribute_name != attr_name: raise ValueError( "The model has more than one Version attribute: {}, {}" .format(cls._version_attribute_name, attr_name)) cls._version_attribute_name = attr_name ttl_attr_names = [ name for name, attr in cls.get_attributes().items() if isinstance(attr, TTLAttribute) ] if len(ttl_attr_names) > 1: raise ValueError("{} has more than one TTL attribute: {}".format( cls.__name__, ", ".join(ttl_attr_names))) if isinstance(namespace, dict): for attr_name, attr_obj in namespace.items(): if attr_name == META_CLASS_NAME: if not hasattr(attr_obj, REGION): setattr(attr_obj, REGION, get_settings_value('region')) if not hasattr(attr_obj, HOST): setattr(attr_obj, HOST, get_settings_value('host')) if hasattr(attr_obj, 'session_cls') or hasattr( attr_obj, 'request_timeout_seconds'): warnings.warn( "The `session_cls` and `request_timeout_second` options are no longer supported" ) if not hasattr(attr_obj, 'connect_timeout_seconds'): setattr(attr_obj, 'connect_timeout_seconds', get_settings_value('connect_timeout_seconds')) if not hasattr(attr_obj, 'read_timeout_seconds'): setattr(attr_obj, 'read_timeout_seconds', get_settings_value('read_timeout_seconds')) if not hasattr(attr_obj, 'base_backoff_ms'): setattr(attr_obj, 'base_backoff_ms', get_settings_value('base_backoff_ms')) if not hasattr(attr_obj, 'max_retry_attempts'): setattr(attr_obj, 'max_retry_attempts', get_settings_value('max_retry_attempts')) if not hasattr(attr_obj, 'max_pool_connections'): setattr(attr_obj, 'max_pool_connections', get_settings_value('max_pool_connections')) if not hasattr(attr_obj, 'extra_headers'): setattr(attr_obj, 'extra_headers', get_settings_value('extra_headers')) if not hasattr(attr_obj, 'aws_access_key_id'): setattr(attr_obj, 'aws_access_key_id', None) if not hasattr(attr_obj, 'aws_secret_access_key'): setattr(attr_obj, 'aws_secret_access_key', None) if not hasattr(attr_obj, 'aws_session_token'): setattr(attr_obj, 'aws_session_token', None) elif isinstance(attr_obj, Index): attr_obj.Meta.model = cls if not hasattr(attr_obj.Meta, "index_name"): attr_obj.Meta.index_name = attr_name # create a custom Model.DoesNotExist derived from pynamodb.exceptions.DoesNotExist, # so that "except Model.DoesNotExist:" would not catch other models' exceptions if 'DoesNotExist' not in namespace: exception_attrs = { '__module__': namespace.get('__module__'), '__qualname__': f'{cls.__qualname__}.{"DoesNotExist"}', } cls.DoesNotExist = type('DoesNotExist', (DoesNotExist, ), exception_attrs)
def __init__(cls, name, bases, attrs): super(MetaModel, cls).__init__(name, bases, attrs) for attr_name, attribute in cls.get_attributes().items(): if attribute.is_hash_key: cls._hash_keyname = attr_name if attribute.is_range_key: cls._range_keyname = attr_name if isinstance(attribute, VersionAttribute): if cls._version_attribute_name: raise ValueError( "The model has more than one Version attribute: {}, {}" .format(cls._version_attribute_name, attr_name)) cls._version_attribute_name = attr_name if isinstance(attrs, dict): for attr_name, attr_obj in attrs.items(): if attr_name == META_CLASS_NAME: if not hasattr(attr_obj, REGION): setattr(attr_obj, REGION, get_settings_value('region')) if not hasattr(attr_obj, HOST): setattr(attr_obj, HOST, get_settings_value('host')) if hasattr(attr_obj, 'session_cls') or hasattr( attr_obj, 'request_timeout_seconds'): warnings.warn( "The `session_cls` and `request_timeout_second` options are no longer supported" ) if not hasattr(attr_obj, 'connect_timeout_seconds'): setattr(attr_obj, 'connect_timeout_seconds', get_settings_value('connect_timeout_seconds')) if not hasattr(attr_obj, 'read_timeout_seconds'): setattr(attr_obj, 'read_timeout_seconds', get_settings_value('read_timeout_seconds')) if not hasattr(attr_obj, 'base_backoff_ms'): setattr(attr_obj, 'base_backoff_ms', get_settings_value('base_backoff_ms')) if not hasattr(attr_obj, 'max_retry_attempts'): setattr(attr_obj, 'max_retry_attempts', get_settings_value('max_retry_attempts')) if not hasattr(attr_obj, 'max_pool_connections'): setattr(attr_obj, 'max_pool_connections', get_settings_value('max_pool_connections')) if not hasattr(attr_obj, 'extra_headers'): setattr(attr_obj, 'extra_headers', get_settings_value('extra_headers')) if not hasattr(attr_obj, 'aws_access_key_id'): setattr(attr_obj, 'aws_access_key_id', None) if not hasattr(attr_obj, 'aws_secret_access_key'): setattr(attr_obj, 'aws_secret_access_key', None) if not hasattr(attr_obj, 'aws_session_token'): setattr(attr_obj, 'aws_session_token', None) elif isinstance(attr_obj, Index): attr_obj.Meta.model = cls if not hasattr(attr_obj.Meta, "index_name"): attr_obj.Meta.index_name = attr_name elif isinstance(attr_obj, Attribute): if attr_obj.attr_name is None: attr_obj.attr_name = attr_name ttl_attr_names = [ name for name, attr_obj in attrs.items() if isinstance(attr_obj, TTLAttribute) ] if len(ttl_attr_names) > 1: raise ValueError( "The model has more than one TTL attribute: {}".format( ", ".join(ttl_attr_names))) if META_CLASS_NAME not in attrs: setattr(cls, META_CLASS_NAME, DefaultMeta) # create a custom Model.DoesNotExist derived from pynamodb.exceptions.DoesNotExist, # so that "except Model.DoesNotExist:" would not catch other models' exceptions if 'DoesNotExist' not in attrs: exception_attrs = {'__module__': attrs.get('__module__')} if hasattr(cls, '__qualname__'): # On Python 3, Model.DoesNotExist exception_attrs['__qualname__'] = '{}.{}'.format( cls.__qualname__, 'DoesNotExist') cls.DoesNotExist = type('DoesNotExist', (DoesNotExist, ), exception_attrs)
def resolv_environment(): """ AWS configuration parameters such as AWS credentials can de defined by using environ variables or configuration file. PyNAMoDb settings should be defined in the configuration file """ attrs = {} app = current_app._get_current_object() aws_config = app.config.get('AWS', {}) pynamo_config = app.config.get('PYNAMODB', {}) if 'AWS_REGION' in aws_config: attrs['AWS_REGION'] = aws_config['AWS_REGION'] elif 'AWS_REGION' in os.environ: attrs['AWS_REGION'] = os.environ['AWS_REGION'] else: attrs['AWS_REGION'] = get_settings_value(REGION) if 'AWS_ACCESS_KEY_ID' in aws_config: attrs['AWS_ACCESS_KEY_ID'] = aws_config['AWS_ACCESS_KEY_ID'] elif 'AWS_ACCESS_KEY_ID' in os.environ: attrs['AWS_ACCESS_KEY_ID'] = os.environ['AWS_ACCESS_KEY_ID'] else: raise ConfigurationError('AWS_ACCESS_KEY_ID undefined') if 'AWS_SECRET_ACCESS_KEY' in aws_config: attrs['AWS_SECRET_ACCESS_KEY'] = aws_config['AWS_SECRET_ACCESS_KEY'] elif 'AWS_ACCESS_KEY_ID' in os.environ: attrs['AWS_SECRET_ACCESS_KEY'] = os.environ['AWS_SECRET_ACCESS_KEY'] else: raise ConfigurationError('AWS_SECRET_ACCESS_KEY undefined') if HOST in pynamo_config: attrs[HOST] = pynamo_config[HOST] else: attrs[HOST] = get_settings_value('host') if 'request_timeout_seconds' in pynamo_config: attrs['request_timeout_seconds'] =\ pynamo_config['request_timeout_seconds'] else: attrs['request_timeout_seconds'] =\ get_settings_value('request_timeout_seconds') if 'base_backoff_ms' in pynamo_config: attrs['base_backoff_ms'] = pynamo_config['base_backoff_ms'] else: attrs['base_backoff_ms'] = get_settings_value('base_backoff_ms') if 'max_retry_attempts' in pynamo_config: attrs['max_retry_attempts'] = pynamo_config['max_retry_attempts'] else: attrs['max_retry_attempts'] = get_settings_value('max_retry_attempts') if 'session_cls' in pynamo_config: attrs['session_cls'] = pynamo_config['session_cls'] else: attrs['session_cls'] = get_settings_value('session_cls') return attrs