class EnumType(SchemaType, TypeDecorator): def __init__(self, enum, name): self.enum = enum self.name = name members = (member.value for member in enum) kwargs = {'name': name} self.impl = SAEnum(*members, **kwargs) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return EnumType(self.enum, self.name) def process_bind_param(self, enum_instance, dialect): if enum_instance is None: return None return enum_instance.value def process_result_value(self, value, dialect): if value is None: return None return self.enum(value)
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum self.impl = Enum( *enum.values(), name="ck%s" % re.sub( '([A-Z])', lambda m:"_" + m.group(1).lower(), enum.__name__) ) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip())
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum, **kwargs): super(DeclEnumType, self).__init__(**kwargs) self.enum = enum self.impl = Enum( *enum.values(), name="ck%s" % re.sub( '([A-Z])', lambda m:"_" + m.group(1).lower(), enum.__name__) ) def _set_table(self, column, table): self.impl.name = "ck_%s_%s_%s" % ( '_'.join(table.schema.split('.')), table.name, self.impl.name[3:]) self.impl._set_table(column, table) def copy(self, **kw): return DeclEnumType(self.enum, **kw) def process_bind_param(self, value, dialect): if value is None: return None if isinstance(value, EnumSymbol): return value.value elif isinstance(value, (str, unicode)): # Should not happen, but mask the error for now. return value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip())
class Enum(SchemaType, TypeDecorator): def __init__(self, enum): name = re.sub('([A-Z])', lambda m:"_" + m.group(1).lower(), enum.__name__) self.enum = enum self.impl = SaEnum(*enum.values(), name="ck%s" % name) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return Enum(self.enum) def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip()) def create(self, bind=None, checkfirst=False): super(Enum, self).create(bind, checkfirst) t = self.dialect_impl(bind.dialect) if t.impl.__class__ is not self.__class__ and isinstance(t, SchemaType): t.impl.create(bind=bind, checkfirst=checkfirst)
class DeclEnumType(SchemaType, TypeDecorator): """DeclEnum augmented so that it can persist to the database.""" def __init__(self, enum): self.enum = enum self.impl = Enum(*enum.names(), name="ck%s" % re.sub('([A-Z])', lambda m: '_' + m.group(1).lower(), enum.__name__)) def drop(self, bind=None, checkfirst=False): return "DROP {} IF EXISTSZZ".format("ck%s" % re.sub( '([A-Z])', lambda m: '_' + m.group(1).lower(), self.enum.__name__)) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if isinstance(value, EnumSymbol): value = value.name return value def process_result_value(self, value, dialect): if value is not None: return getattr(self.enum, value.strip())
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum # convert CamelCase to underscore_separated name = re.sub('.([A-Z])', lambda m: '_' + m.group(1).lower(), enum.__name__).lower() self.impl = Enum(*enum.values(), name=name) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None if isinstance(value, basestring): return value if value.cls_ != self.enum: raise TypeError('Cannot use %r as bind parameter for column ' 'of type %r' % (value, self.enum)) return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip())
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum self.impl = Enum(*enum.values(), name=to_underscore(enum.__name__)) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return value if isinstance(value, str): return value return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip()) python_type = str
class DeclarativeEnumType(SchemaType, TypeDecorator): """Column type usable in table column definitions.""" def __init__(self, enum): self.enum = enum constraint = 'ck{0}'.format(re.sub('([A-Z])', lambda m: '_' + m.group(1).lower(), enum.__name__)) self.impl = Enum(*enum.values(), name=constraint) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclarativeEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: # pragma: no cover return None return value.value def process_result_value(self, value, dialect): if value is None: # pragma: no cover return None return self.enum.from_string(value.strip())
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum self.impl = Enum(*enum.values(), name="ck%s" % re.sub('([A-Z])', lambda m: "_" + m.group(1).lower(), enum.__name__)) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip()) def create(self, bind=None, checkfirst=False): """Issue CREATE ddl for this type, if applicable.""" super(DeclEnumType, self).create(bind, checkfirst) t = self.dialect_impl(bind.dialect) if t.impl.__class__ is not self.__class__ and isinstance( t, SchemaType): t.impl.create(bind=bind, checkfirst=checkfirst)
class DeclarativeEnumType(SchemaType, TypeDecorator): """Column type usable in table column definitions.""" def __init__(self, enum, name=None): self.enum = enum enum_args = enum.__enum_args__.copy() if name is not None: enum_args['name'] = name elif 'name' not in enum_args: enum_args['name'] = 'ck_' + camelcase_to_underscore(enum.__name__) self.impl = Enum(*enum.values(), **enum_args) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclarativeEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: # pragma: no cover return None return value.value def process_result_value(self, value, dialect): if value is None: # pragma: no cover return None return self.enum.from_string(value.strip())
class DeclEnumType(SchemaType, TypeDecorator): """ DeclEnumType supports object instantiation in two different ways: Passing in an enum: This is to be used in the application code. It will pull enum values straight from the DeclEnum object. A helper for this is available in DeclEnum.db_type() Passing in a tuple with enum values: In migrations the enum value list needs to be fix. It should not be pulled in from the application code, otherwise later modifications of enum values could result in those values being added in an earlier migration when re-running migrations from the beginning. Therefore DeclEnum(enum_values=('one', 'two'), enum_name='MyEnum') should be used. """ def __init__(self, enum=None, enum_values=None, enum_name=None): self.enum = enum self.enum_values = enum_values self.enum_name = enum_name if enum: self.enum_values=enum.values() self.enum_name=enum.__name__ self.impl = Enum( *self.enum_values, name="ck%s" % re.sub( '([A-Z])', lambda m: "_" + m.group(1).lower(), self.enum_name) ) def create(self, bind=None, checkfirst=False): """Issue CREATE ddl for this type, if applicable.""" super(DeclEnumType, self).create(bind, checkfirst) t = self.dialect_impl(bind.dialect) if t.impl.__class__ is not self.__class__ and isinstance(t, SchemaType): t.impl.create(bind=bind, checkfirst=checkfirst) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): if self.enum: return DeclEnumType(self.enum) else: return DeclEnumType(enum_name=self.enum_name, enum_values=self.enum_values) def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip())
class EnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum self.impl = SAEnum( *[sym.value for sym in enum], name="enum%s" % re.sub( '([A-Z])', lambda m: "_" + m.group(1).lower(), enum.__name__)) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return EnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None if isinstance(value, str): return value return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum(value.strip())
class DeclarativeEnumType(SchemaType, TypeDecorator): """Column type usable in table column definitions.""" def __init__(self, enum, name=None): self.enum = enum enum_args = enum.__enum_args__.copy() if name is not None: enum_args['name'] = name elif 'name' not in enum_args: enum_args['name'] = 'ck_' + camelcase_to_underscore(enum.__name__) self.impl = Enum(*enum.values(), **enum_args) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclarativeEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: # pragma: no cover return None return value.value def process_result_value(self, value, dialect): if value is None: # pragma: no cover return None return self.enum.from_string(value.strip())
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum, **kwargs): super(DeclEnumType, self).__init__(**kwargs) self.enum = enum self.impl = Enum(*list(enum.values()), name="ck%s" % re.sub('([A-Z])', lambda m: "_" + m.group(1).lower(), enum.__name__)) def _set_table(self, column, table): self.impl.name = "ck_%s_%s_%s" % ('_'.join( table.schema.split('.')), table.name, self.impl.name[3:]) self.impl._set_table(column, table) def copy(self, **kw): return DeclEnumType(self.enum, **kw) def process_bind_param(self, value, dialect): if value is None: return None if isinstance(value, EnumSymbol): return value.value elif isinstance(value, string_types): # Should not happen, but mask the error for now. return value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip())
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum self.impl = Enum( *enum.values(), name="ck%s" % re.sub( '([A-Z])', lambda m: "_" + m.group(1).lower(), enum.__name__) ) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip())
class EnumType(SchemaType, TypeDecorator): # pylint: disable=W0223 def __init__(self, enum, *args, **kwargs): self.enum = enum self.impl = SQLAEnum( *enum.values(), name="ck%s" % re.sub( "([A-Z])", lambda m: "_" + m.group(1).lower(), enum.__name__)) super(EnumType, self).__init__(*args, **kwargs) def _set_table(self, table, column): # pylint: disable=W0212 self.impl._set_table(table, column) def copy(self): return EnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip())
class EnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum self.impl = SAEnum( *[sym.value for sym in enum], name="enum%s" % re.sub( '([A-Z])', lambda m: "_" + m.group(1).lower(), enum.__name__) ) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return EnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None if isinstance(value, str): return value return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum(value.strip())
class EnumType(SchemaType, TypeDecorator): def __init__(self, enum, name): self.enum = enum self.name = name members = (member.value for member in enum) kwargs = {'name': name} self.impl = SAEnum(*members, **kwargs) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return EnumType(self.enum, self.name) def process_bind_param(self, enum_instance, dialect): if enum_instance is None: return None return enum_instance.value def process_result_value(self, value, dialect): if value is None: return None return self.enum(value)
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum # convert CamelCase to underscore_separated name = re.sub('.([A-Z])', lambda m: '_' + m.group(1).lower(), enum.__name__).lower() self.impl = Enum(*enum.values(), name=name) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None if isinstance(value, basestring): return value if value.cls_ != self.enum: raise TypeError('Cannot use %r as bind parameter for column ' 'of type %r' % (value, self.enum)) return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip())
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum, **kwargs): self.enum = enum self.impl = Enum(*enum.values(), name="ck%s" % re.sub('([A-Z])', lambda m: "_" + m.group(1).lower(), enum.__name__), **kwargs) super(DeclEnumType, self).__init__(**kwargs) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip()) def register_with_psycopg(self, conn): import psycopg2.extensions my_enum_array_oid = conn.execute(text( "SELECT typarray FROM pg_catalog.pg_type " "WHERE typname = :tn"), tn=self.impl.name).scalar() psycopg2.extensions.register_type( psycopg2.extensions.new_array_type( (my_enum_array_oid, ), self.impl.name + '[]', psycopg2.STRING))
class ASEnumType(SchemaType, TypeDecorator): def __init__(self, enum, regex = None): self.enum = enum self.impl = Enum( *enum.values(), name="ck%s" % re.sub( '([A-Z])', lambda m:"_" + m.group(1).lower(), enum.__name__) ) self.regex = regex def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return ASEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None if self.__checkValue(value): return value.value return None def process_result_value(self, value, dialect): if value is None: return None if self.__checkValue(self.enum.from_string(value.strip())): return self.enum.from_string(value.strip()) return None def __checkValue(self, value): if self.regex is not None: if (re.match(self.regex, value)): return value if value in self.enum: return True return False;
class DeclEnumType(SchemaType, TypeDecorator): def __init__(self, enum): self.enum = enum self.impl = Enum( *enum.values(), name="ck%s" % re.sub( '([A-Z])', lambda m: "_" + m.group(1).lower(), enum.__name__) ) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: return None return value.value def process_result_value(self, value, dialect): if value is None: return None return self.enum.from_string(value.strip()) def create(self, bind=None, checkfirst=False): """Issue CREATE ddl for this type, if applicable.""" super(DeclEnumType, self).create(bind, checkfirst) t = self.dialect_impl(bind.dialect) if t.impl.__class__ is not self.__class__ and isinstance(t, SchemaType): t.impl.create(bind=bind, checkfirst=checkfirst) def drop(self, bind=None, checkfirst=False): """Issue DROP ddl for this type, if applicable.""" super(DeclEnumType, self).drop(bind, checkfirst) t = self.dialect_impl(bind.dialect) if t.impl.__class__ is not self.__class__ and isinstance(t, SchemaType): t.impl.drop(bind=bind, checkfirst=checkfirst)
class DeclEnumType(SchemaType, TypeDecorator): """DeclEnum augmented so that it can persist to the database.""" def __init__(self, enum): import re self.enum = enum self.impl = Enum(*enum.names(), name="ck%s" % re.sub( '([A-Z])', lambda m: '_' + m.group(1).lower(), enum.__name__)) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self, **kw): return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): if isinstance(value, EnumSymbol): value = value.name return value def process_result_value(self, value, dialect): if value is not None: return getattr(self.enum, value.strip())
class PythonEnum(TypeDecorator, SchemaType): impl = Enum def __init__(self, enum_class, **kw): if hasattr(enum_class, "__db_name") and 'name' not in kw: kw['name'] = getattr(enum_class, "__db_name") if hasattr(enum_class, "__db_schema") and 'schema' not in kw: kw['schema'] = getattr(enum_class, "__db_schema") if 'name' not in kw: kw['name'] = "ck%s" % re.sub('([A-Z])', lambda m: '_' + m.group(1).lower(), enum_class.__name__) self.impl = Enum(*(m.name for m in enum_class), **kw) # super().__init__(*(m.name for m in enum_class), **kw) self._enum_class = enum_class def process_bind_param(self, value, dialect): if isinstance(value, self._enum_class): return value.name return value def process_result_value(self, value, dialect): if value is not None: return self._enum_class[value] @property def python_type(self): return self._enum_class def copy(self): return PythonEnum(self._enum_class) def _set_table(self, table, column): self.impl._set_table(table, column) def __repr__(self): return repr(self.impl)
class DeclarativeEnumType(SchemaType, TypeDecorator): """Column type usable in table column definitions.""" def __init__(self, enum): self.enum = enum constraint = 'ck{0}'.format( re.sub('([A-Z])', lambda m: '_' + m.group(1).lower(), enum.__name__)) self.impl = Enum(*enum.values(), name=constraint) def _set_table(self, table, column): self.impl._set_table(table, column) def copy(self): return DeclarativeEnumType(self.enum) def process_bind_param(self, value, dialect): if value is None: # pragma: no cover return None return value.value def process_result_value(self, value, dialect): if value is None: # pragma: no cover return None return self.enum.from_string(value.strip())
class DeclEnumType(SchemaType, TypeDecorator): """A database column type for an enum.""" def __init__(self, enum): """ Initialize with the given enum. Args: enum (.EnumMeta): The enum metaclass. """ self.enum = enum self.impl = Enum(*enum.values(), name="ck%s" % re.sub("([A-Z])", lambda m: "_" + m.group(1).lower(), enum.__name__)) def _set_table(self, table, column): """ Set the table for this object. Args: table (sqlalchemy.sql.schema.Table): The table that uses this Enum. column (sqlalchemy.sql.schema.Column): The column that uses this Enum. """ self.impl._set_table(table, column) def copy(self): """ Return a copy of self. Returns: DeclEnumType: A copy of self. """ return DeclEnumType(self.enum) def process_bind_param(self, value, dialect): """ Return the value of the enum. Args: value (.enum.EnumSymbol): The enum symbol we are resolving the value of. dialect (sqlalchemy.engine.default.DefaultDialect): Unused. Returns: basestring: The EnumSymbol's value. """ if value is None: return None return value.value def process_result_value(self, value, dialect): """ Return the enum that matches the given string. Args: value (str): The name of an enum. dialect (sqlalchemy.engine.default.DefaultDialect): Unused. Returns: EnumSymbol or None: The enum that matches value, or ``None`` if ``value`` is ``None``. """ if value is None: return None return self.enum.from_string(value.strip()) def create(self, bind=None, checkfirst=False): """Issue CREATE ddl for this type, if applicable.""" super(DeclEnumType, self).create(bind, checkfirst) t = self.dialect_impl(bind.dialect) if t.impl.__class__ is not self.__class__ and isinstance( t, SchemaType): t.impl.create(bind=bind, checkfirst=checkfirst) def drop(self, bind=None, checkfirst=False): """Issue DROP ddl for this type, if applicable.""" super(DeclEnumType, self).drop(bind, checkfirst) t = self.dialect_impl(bind.dialect) if t.impl.__class__ is not self.__class__ and isinstance( t, SchemaType): t.impl.drop(bind=bind, checkfirst=checkfirst)