Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-66-Convert-custom-parser-fields-into-sqla-based-fields 64a393ece -> c0fdc03f0
wip Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/c0fdc03f Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/c0fdc03f Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/c0fdc03f Branch: refs/heads/ARIA-66-Convert-custom-parser-fields-into-sqla-based-fields Commit: c0fdc03f0e4a2c10b5f82523c78e04b031146785 Parents: 64a393e Author: mxmrlv <mxm...@gmail.com> Authored: Mon Jan 30 17:05:14 2017 +0200 Committer: mxmrlv <mxm...@gmail.com> Committed: Mon Jan 30 17:08:45 2017 +0200 ---------------------------------------------------------------------- aria/storage/type.py | 123 +++++++++++++++++++++-------------------- tests/storage/__init__.py | 19 ++++--- 2 files changed, 75 insertions(+), 67 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c0fdc03f/aria/storage/type.py ---------------------------------------------------------------------- diff --git a/aria/storage/type.py b/aria/storage/type.py index 1b28f33..56c2425 100644 --- a/aria/storage/type.py +++ b/aria/storage/type.py @@ -12,6 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import json from collections import namedtuple from types import NoneType @@ -21,10 +22,9 @@ from sqlalchemy import ( VARCHAR, event ) - from sqlalchemy.ext import mutable -from . import exceptions +from aria.storage import exceptions class _MutableType(TypeDecorator): @@ -95,26 +95,22 @@ class _StrictDictMixin(object): self._assert_strict_value(value) super(_StrictDictMixin, self).setdefault(key, value) - def update(self, *a, **kw): - if isinstance(a, dict): - for k, v in a.items(): - self._assert_strict_key(k) - self._assert_strict_value(v) - for k, v in kw.items(): + def update(self, *args, **kwargs): + for k, v in kwargs.items(): self._assert_strict_key(k) self._assert_strict_value(v) - super(_StrictDictMixin, self).update(*a, **kw) + super(_StrictDictMixin, self).update(*args, **kwargs) @classmethod def _assert_strict_key(cls, key): - if not isinstance(key, cls._key_cls): + if not isinstance(key, (cls._key_cls, NoneType)): raise exceptions.StorageError("Key type was set strictly to {0}, but was {1}".format( cls._key_cls, type(key) )) @classmethod def _assert_strict_value(cls, value): - if not isinstance(value, cls._value_cls): + if not isinstance(value, (cls._value_cls, NoneType)): raise exceptions.StorageError("Value type was set strictly to {0}, but was {1}".format( cls._value_cls, type(value) )) @@ -159,21 +155,21 @@ class _StrictListMixin(object): self._assert_item(value) super(_StrictListMixin, self).__setitem__(index, value) - def append(self, x): - self._assert_item(x) - super(_StrictListMixin, self).append(x) + def append(self, item): + self._assert_item(item) + super(_StrictListMixin, self).append(item) - def extend(self, x): - self._assert_item(x) - super(_StrictListMixin, self).extend(x) + def extend(self, item): + self._assert_item(item) + super(_StrictListMixin, self).extend(item) - def insert(self, i, x): - self._assert_item(x) - super(_StrictListMixin, self).insert(i, x) + def insert(self, index, item): + self._assert_item(item) + super(_StrictListMixin, self).insert(index, item) @classmethod def _assert_item(cls, item): - if not isinstance(item, cls._item_cls): + if not isinstance(item, (cls._item_cls, NoneType)): raise exceptions.StorageError("Key type was set strictly to {0}, but was {1}".format( cls._item_cls, type(item) )) @@ -197,43 +193,52 @@ class _MutableList(mutable.MutableList): except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -_strict_dict_id = namedtuple('strict_dict_id', 'key_cls, value_cls') -_strict_map = {} - - -def StrictDict(key_cls, value_cls): - strict_dict_map_key = _strict_dict_id(key_cls=key_cls, value_cls=value_cls) - if strict_dict_map_key not in _strict_map: - strict_dict_cls = type( - 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), - (Dict, ), - {} - ) - type( - 'StrictMutableDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), - (_StrictDictMixin, _MutableDict), - {'_key_cls': key_cls, '_value_cls': value_cls} - ).associate_with(strict_dict_cls) - _strict_map[strict_dict_map_key] = strict_dict_cls - - return _strict_map[strict_dict_map_key] - - -def StrictList(item_cls): - if item_cls not in _strict_map: - strict_list_cls = type( - 'StrictList_{0}'.format(item_cls.__name__), - (List, ), - {} - ) - type( - 'StrictMutableList_{0}'.format(item_cls.__name__), - (_StrictListMixin, _MutableList), - {'_item_cls': item_cls} - ).associate_with(strict_list_cls) - _strict_map[item_cls] = strict_list_cls - - return _strict_map[item_cls] +StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') + + +class _StrictDict(object): + _strict_map = {} + + def __call__(self, key_cls=NoneType, value_cls=NoneType, *args, **kwargs): + strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) + if strict_dict_map_key not in self._strict_map: + strict_dict_cls = type( + 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), + (Dict, ), + {} + ) + type( + 'StrictMutableDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), + (_StrictDictMixin, _MutableDict), + {'_key_cls': key_cls, '_value_cls': value_cls} + ).associate_with(strict_dict_cls) + self._strict_map[strict_dict_map_key] = strict_dict_cls + + return self._strict_map[strict_dict_map_key] + +StrictDict = _StrictDict() + + +class _StrictList(object): + _strict_map = {} + + def __call__(self, item_cls): + if item_cls not in self._strict_map: + strict_list_cls = type( + 'StrictList_{0}'.format(item_cls.__name__), + (List, ), + {} + ) + type( + 'StrictMutableList_{0}'.format(item_cls.__name__), + (_StrictListMixin, _MutableList), + {'_item_cls': item_cls} + ).associate_with(strict_list_cls) + self._strict_map[item_cls] = strict_list_cls + + return self._strict_map[item_cls] + +StrictList = _StrictList() def _mutable_association_listener(mapper, cls): for prop in mapper.column_attrs: @@ -267,4 +272,4 @@ def remove_mutable_association_listener(): if event.contains(*_LISTENER_ARGS): event.remove(*_LISTENER_ARGS) -_register_mutable_association_listener() +_register_mutable_association_listener() \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c0fdc03f/tests/storage/__init__.py ---------------------------------------------------------------------- diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py index c772acb..3b3715e 100644 --- a/tests/storage/__init__.py +++ b/tests/storage/__init__.py @@ -17,18 +17,21 @@ import platform from tempfile import mkdtemp from shutil import rmtree -from sqlalchemy import Column, Text, Integer +from sqlalchemy import ( + create_engine, + orm, + Column, + Text, + Integer, + pool +) + from aria.storage import ( model, structure, type as aria_type, ) -from sqlalchemy import ( - create_engine, - orm) -from sqlalchemy.orm import scoped_session -from sqlalchemy.pool import StaticPool class MockModel(model.DeclarativeBase, structure.ModelMixin): #pylint: disable=abstract-method @@ -68,11 +71,11 @@ def get_sqlite_api_kwargs(base_dir=None, filename='db.sqlite'): else: uri = 'sqlite:///:memory:' engine_kwargs = dict(connect_args={'check_same_thread': False}, - poolclass=StaticPool) + poolclass=pool.StaticPool) engine = create_engine(uri, **engine_kwargs) session_factory = orm.sessionmaker(bind=engine) - session = scoped_session(session_factory=session_factory) if base_dir else session_factory() + session = orm.scoped_session(session_factory=session_factory) if base_dir else session_factory() model.DeclarativeBase.metadata.create_all(bind=engine) return dict(engine=engine, session=session)