[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user asfgit closed the pull request at: https://github.com/apache/incubator-ariatosca/pull/62 --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user dankilman commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98648305 --- Diff: aria/storage/type.py --- @@ -175,72 +172,105 @@ class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." - try: -if not isinstance(value, cls): -if isinstance(value, list): -return cls(value) - -return mutable.Mutable.coerce(key, value) -else: -return value - +return mutable.MutableList.coerce(key, value) except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') +StrictDictID = namedtuple('StrictDictID', 'key_cls, value_cls') +StrictValue = namedtuple('StrictValue', 'type_cls, listener_cls') class _StrictDict(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} -def __call__(self, key_cls=None, value_cls=None, *args, **kwargs): +def __call__(self, key_cls=None, value_cls=None): strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) if strict_dict_map_key not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_dict_cls = type( 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), (Dict, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, +# while setting the necessary _key_cls and _value_cls as class attributes. +listener_cls = 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 +) +listener_cls.associate_with(strict_dict_cls) +self._strict_map[strict_dict_map_key] = StrictValue(type_cls=strict_dict_cls, + listener_cls=listener_cls) -return self._strict_map[strict_dict_map_key] +return self._strict_map[strict_dict_map_key].type_cls StrictDict = _StrictDict() class _StrictList(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} def __call__(self, item_cls=None): + if item_cls not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_list_cls = type( 'StrictList_{0}'.format(item_cls.__name__), (List, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, +# while setting the necessary _item_cls as class attribute. +listener_cls = 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 +) +self._strict_map[item_cls] = StrictValue(type_cls=strict_list_cls, + listener_cls=listener_cls) -return self._strict_map[item_cls] +return self._strict_map[item_cls].type_cls StrictList = _StrictList() + def _mutable_association_listener(mapper, cls): +
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98646660 --- Diff: aria/storage/type.py --- @@ -175,72 +172,105 @@ class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." - try: -if not isinstance(value, cls): -if isinstance(value, list): -return cls(value) - -return mutable.Mutable.coerce(key, value) -else: -return value - +return mutable.MutableList.coerce(key, value) except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') +StrictDictID = namedtuple('StrictDictID', 'key_cls, value_cls') +StrictValue = namedtuple('StrictValue', 'type_cls, listener_cls') class _StrictDict(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} -def __call__(self, key_cls=None, value_cls=None, *args, **kwargs): +def __call__(self, key_cls=None, value_cls=None): strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) if strict_dict_map_key not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_dict_cls = type( 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), (Dict, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, +# while setting the necessary _key_cls and _value_cls as class attributes. +listener_cls = 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 +) +listener_cls.associate_with(strict_dict_cls) +self._strict_map[strict_dict_map_key] = StrictValue(type_cls=strict_dict_cls, + listener_cls=listener_cls) -return self._strict_map[strict_dict_map_key] +return self._strict_map[strict_dict_map_key].type_cls StrictDict = _StrictDict() class _StrictList(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} def __call__(self, item_cls=None): + if item_cls not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_list_cls = type( 'StrictList_{0}'.format(item_cls.__name__), (List, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, +# while setting the necessary _item_cls as class attribute. +listener_cls = 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 +) +self._strict_map[item_cls] = StrictValue(type_cls=strict_list_cls, + listener_cls=listener_cls) -return self._strict_map[item_cls] +return self._strict_map[item_cls].type_cls StrictList = _StrictList() + def _mutable_association_listener(mapper, cls): +
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user dankilman commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98640124 --- Diff: aria/storage/type.py --- @@ -175,72 +172,105 @@ class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." - try: -if not isinstance(value, cls): -if isinstance(value, list): -return cls(value) - -return mutable.Mutable.coerce(key, value) -else: -return value - +return mutable.MutableList.coerce(key, value) except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') +StrictDictID = namedtuple('StrictDictID', 'key_cls, value_cls') +StrictValue = namedtuple('StrictValue', 'type_cls, listener_cls') class _StrictDict(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} -def __call__(self, key_cls=None, value_cls=None, *args, **kwargs): +def __call__(self, key_cls=None, value_cls=None): strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) if strict_dict_map_key not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_dict_cls = type( 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), (Dict, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, +# while setting the necessary _key_cls and _value_cls as class attributes. +listener_cls = 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 +) +listener_cls.associate_with(strict_dict_cls) +self._strict_map[strict_dict_map_key] = StrictValue(type_cls=strict_dict_cls, + listener_cls=listener_cls) -return self._strict_map[strict_dict_map_key] +return self._strict_map[strict_dict_map_key].type_cls StrictDict = _StrictDict() class _StrictList(object): +""" +This entire class functions as a factory for strict dicts and their listeners. --- End diff -- wrong comment: not dict, list --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user dankilman commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98640012 --- Diff: aria/storage/type.py --- @@ -175,72 +172,105 @@ class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." - try: -if not isinstance(value, cls): -if isinstance(value, list): -return cls(value) - -return mutable.Mutable.coerce(key, value) -else: -return value - +return mutable.MutableList.coerce(key, value) except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') +StrictDictID = namedtuple('StrictDictID', 'key_cls, value_cls') +StrictValue = namedtuple('StrictValue', 'type_cls, listener_cls') class _StrictDict(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} -def __call__(self, key_cls=None, value_cls=None, *args, **kwargs): +def __call__(self, key_cls=None, value_cls=None): strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) if strict_dict_map_key not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_dict_cls = type( 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), (Dict, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, --- End diff -- wrong comment: not list, dict --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user dankilman commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98641330 --- Diff: aria/storage/type.py --- @@ -175,72 +172,105 @@ class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." - try: -if not isinstance(value, cls): -if isinstance(value, list): -return cls(value) - -return mutable.Mutable.coerce(key, value) -else: -return value - +return mutable.MutableList.coerce(key, value) except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') +StrictDictID = namedtuple('StrictDictID', 'key_cls, value_cls') +StrictValue = namedtuple('StrictValue', 'type_cls, listener_cls') class _StrictDict(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} -def __call__(self, key_cls=None, value_cls=None, *args, **kwargs): +def __call__(self, key_cls=None, value_cls=None): strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) if strict_dict_map_key not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_dict_cls = type( 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), (Dict, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, +# while setting the necessary _key_cls and _value_cls as class attributes. +listener_cls = 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 +) +listener_cls.associate_with(strict_dict_cls) +self._strict_map[strict_dict_map_key] = StrictValue(type_cls=strict_dict_cls, + listener_cls=listener_cls) -return self._strict_map[strict_dict_map_key] +return self._strict_map[strict_dict_map_key].type_cls StrictDict = _StrictDict() class _StrictList(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} def __call__(self, item_cls=None): + if item_cls not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_list_cls = type( 'StrictList_{0}'.format(item_cls.__name__), (List, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, +# while setting the necessary _item_cls as class attribute. +listener_cls = 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 +) +self._strict_map[item_cls] = StrictValue(type_cls=strict_list_cls, + listener_cls=listener_cls) -return self._strict_map[item_cls] +return self._strict_map[item_cls].type_cls StrictList = _StrictList() + def _mutable_association_listener(mapper, cls): +
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user dankilman commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98640329 --- Diff: aria/storage/type.py --- @@ -175,72 +172,105 @@ class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." - try: -if not isinstance(value, cls): -if isinstance(value, list): -return cls(value) - -return mutable.Mutable.coerce(key, value) -else: -return value - +return mutable.MutableList.coerce(key, value) except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') +StrictDictID = namedtuple('StrictDictID', 'key_cls, value_cls') +StrictValue = namedtuple('StrictValue', 'type_cls, listener_cls') class _StrictDict(object): +""" +This entire class functions as a factory for strict dicts and their listeners. +No type class, and no listener type class is created (and associated) more than once. If a +relevant type class exists it is returned. +""" _strict_map = {} -def __call__(self, key_cls=None, value_cls=None, *args, **kwargs): +def __call__(self, key_cls=None, value_cls=None): strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) if strict_dict_map_key not in self._strict_map: +# Creating the type class itself. this class would be returned (used by the sqlalchemy +# Column). strict_dict_cls = type( 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), (Dict, ), {} ) -type( +# Creating the type listening class and associating it with the newly created type. +# The new class inherits from both the _MutableList class and the _StrictListMixin, +# while setting the necessary _key_cls and _value_cls as class attributes. +listener_cls = 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 +) +listener_cls.associate_with(strict_dict_cls) --- End diff -- shouldn't you not be calling `associate_with` here? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98484829 --- Diff: aria/storage/type.py --- @@ -74,16 +125,115 @@ def coerce(cls, key, value): raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) +class _StrictListMixin(object): +ANY_TYPE = 'any_type' + +_item_cls = ANY_TYPE + +@classmethod +def coerce(cls, key, value): +"Convert plain dictionaries to MutableDict." +try: +if not isinstance(value, cls): +if isinstance(value, list): +for item in value: +cls._assert_item(item) +return cls(value) +return mutable.MutableList.coerce(key, value) +else: +return value +except ValueError as e: +raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) + +def __setitem__(self, index, value): +"""Detect list set events and emit change events.""" +self._assert_item(value) +super(_StrictListMixin, self).__setitem__(index, value) + +def append(self, item): +self._assert_item(item) +super(_StrictListMixin, self).append(item) + +def extend(self, item): +self._assert_item(item) +super(_StrictListMixin, self).extend(item) + +def insert(self, index, item): +self._assert_item(item) +super(_StrictListMixin, self).insert(index, item) + +@classmethod +def _assert_item(cls, item): +if cls._item_cls is not None and not isinstance(item, cls._item_cls): +raise exceptions.StorageError("Key type was set strictly to {0}, but was {1}".format( +cls._item_cls, type(item) +)) + + class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." + try: -return mutable.MutableList.coerce(key, value) +if not isinstance(value, cls): --- End diff -- try to remove --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98481960 --- Diff: aria/storage/type.py --- @@ -12,17 +12,18 @@ # 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 sqlalchemy import ( TypeDecorator, VARCHAR, event ) - from sqlalchemy.ext import mutable -from . import exceptions --- End diff -- fix --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98488233 --- Diff: aria/storage/type.py --- @@ -74,16 +125,115 @@ def coerce(cls, key, value): raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) +class _StrictListMixin(object): +ANY_TYPE = 'any_type' + +_item_cls = ANY_TYPE + +@classmethod +def coerce(cls, key, value): +"Convert plain dictionaries to MutableDict." +try: +if not isinstance(value, cls): +if isinstance(value, list): +for item in value: +cls._assert_item(item) +return cls(value) +return mutable.MutableList.coerce(key, value) +else: +return value +except ValueError as e: +raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) + +def __setitem__(self, index, value): +"""Detect list set events and emit change events.""" +self._assert_item(value) +super(_StrictListMixin, self).__setitem__(index, value) + +def append(self, item): +self._assert_item(item) +super(_StrictListMixin, self).append(item) + +def extend(self, item): +self._assert_item(item) +super(_StrictListMixin, self).extend(item) + +def insert(self, index, item): +self._assert_item(item) +super(_StrictListMixin, self).insert(index, item) + +@classmethod +def _assert_item(cls, item): +if cls._item_cls is not None and not isinstance(item, cls._item_cls): +raise exceptions.StorageError("Key type was set strictly to {0}, but was {1}".format( +cls._item_cls, type(item) +)) + + class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." + try: -return mutable.MutableList.coerce(key, value) +if not isinstance(value, cls): +if isinstance(value, list): +return cls(value) + +return mutable.Mutable.coerce(key, value) +else: +return value + except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) +StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') + + +class _StrictDict(object): +_strict_map = {} + +def __call__(self, key_cls=None, value_cls=None, *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=None): --- End diff -- doc --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98482667 --- Diff: aria/storage/type.py --- @@ -74,16 +125,115 @@ def coerce(cls, key, value): raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) +class _StrictListMixin(object): +ANY_TYPE = 'any_type' + +_item_cls = ANY_TYPE + +@classmethod +def coerce(cls, key, value): +"Convert plain dictionaries to MutableDict." +try: +if not isinstance(value, cls): +if isinstance(value, list): +for item in value: +cls._assert_item(item) +return cls(value) +return mutable.MutableList.coerce(key, value) +else: +return value +except ValueError as e: +raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) + +def __setitem__(self, index, value): +"""Detect list set events and emit change events.""" +self._assert_item(value) +super(_StrictListMixin, self).__setitem__(index, value) + +def append(self, item): +self._assert_item(item) +super(_StrictListMixin, self).append(item) + +def extend(self, item): +self._assert_item(item) +super(_StrictListMixin, self).extend(item) + +def insert(self, index, item): +self._assert_item(item) +super(_StrictListMixin, self).insert(index, item) + +@classmethod +def _assert_item(cls, item): +if cls._item_cls is not None and not isinstance(item, cls._item_cls): +raise exceptions.StorageError("Key type was set strictly to {0}, but was {1}".format( +cls._item_cls, type(item) +)) + + class _MutableList(mutable.MutableList): @classmethod def coerce(cls, key, value): "Convert plain dictionaries to MutableDict." + try: -return mutable.MutableList.coerce(key, value) +if not isinstance(value, cls): +if isinstance(value, list): +return cls(value) + +return mutable.Mutable.coerce(key, value) +else: +return value + except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) +StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') + + +class _StrictDict(object): +_strict_map = {} + +def __call__(self, key_cls=None, value_cls=None, *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=None): +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) --- End diff -- integrate with _register_mutable_association_listener --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/62#discussion_r98486834 --- Diff: tests/storage/test_structures.py --- @@ -0,0 +1,277 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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 pytest + +import sqlalchemy + +from aria.storage import ( +ModelStorage, +sql_mapi, +model, +type, +exceptions +) + +from ..storage import get_sqlite_api_kwargs, release_sqlite_storage, structure +from . import MockModel +from ..mock import ( +models, +operations, +context as mock_context +) + + +@pytest.fixture +def storage(): +base_storage = ModelStorage(sql_mapi.SQLAlchemyModelAPI, api_kwargs=get_sqlite_api_kwargs()) +base_storage.register(MockModel) +yield base_storage +release_sqlite_storage(base_storage) + + +@pytest.fixture(scope='module', autouse=True) +def module_cleanup(): +model.DeclarativeBase.metadata.remove(MockModel.__table__) #pylint: disable=no-member + + +@pytest.fixture +def context(): +return mock_context.simple(get_sqlite_api_kwargs()) + + +def test_inner_dict_update(storage): +inner_dict = {'inner_value': 1} + +mock_model = MockModel(model_dict={'inner_dict': inner_dict, 'value': 0}) +storage.mock_model.put(mock_model) + +storage_mm = storage.mock_model.get(mock_model.id) +assert storage_mm == mock_model + +storage_mm.model_dict['inner_dict']['inner_value'] = 2 +storage_mm.model_dict['value'] = -1 +storage.mock_model.update(storage_mm) +storage_mm = storage.mock_model.get(storage_mm.id) + +assert storage_mm.model_dict['inner_dict']['inner_value'] == 2 +assert storage_mm.model_dict['value'] == -1 + + +def test_inner_list_update(storage): +mock_model = MockModel(model_list=[0, [1]]) +storage.mock_model.put(mock_model) + +storage_mm = storage.mock_model.get(mock_model.id) +assert storage_mm == mock_model + +storage_mm.model_list[1][0] = 'new_inner_value' +storage_mm.model_list[0] = 'new_value' +storage.mock_model.update(storage_mm) +storage_mm = storage.mock_model.get(storage_mm.id) + +assert storage_mm.model_list[1][0] == 'new_inner_value' +assert storage_mm.model_list[0] == 'new_value' + + +def test_model_to_dict(context): +deployment = context.deployment +deployment_dict = deployment.to_dict() + +expected_keys = [ +'created_at', +'description', +'inputs', +'groups', +'permalink', +'policy_triggers', +'policy_types', +'outputs', +'scaling_groups', +'updated_at', +'workflows', +'blueprint_name', +] + +for expected_key in expected_keys: +assert expected_key in deployment_dict + +assert 'blueprint_fk' not in deployment_dict + + +def test_relationship_model_ordering(context): +deployment = context.model.deployment.get_by_name(models.DEPLOYMENT_NAME) +source_node = context.model.node.get_by_name(models.DEPENDENT_NODE_NAME) +source_node_instance = context.model.node_instance.get_by_name( +models.DEPENDENT_NODE_INSTANCE_NAME) +target_node = context.model.node.get_by_name(models.DEPENDENCY_NODE_NAME) +target_node_instance = context.model.node_instance.get_by_name( +models.DEPENDENCY_NODE_INSTANCE_NAME) +new_node = model.Node( +name='new_node', +type='test_node_type', +type_hierarchy=[], +number_of_instances=1, +planned_number_of_instances=1, +deploy_number_of_instances=1, +properties={}, +operations=dict((key, {}) for key in operations.NODE_OPERATIONS), +min_number_of_instances=1, +
[GitHub] incubator-ariatosca pull request #62: ARIA-66-Convert-custom-parser-fields-i...
GitHub user mxmrlv opened a pull request: https://github.com/apache/incubator-ariatosca/pull/62 ARIA-66-Convert-custom-parser-fields-into-sqla-based-fields You can merge this pull request into a Git repository by running: $ git pull https://github.com/apache/incubator-ariatosca ARIA-66-Convert-custom-parser-fields-into-sqla-based-fields Alternatively you can review and apply these changes as the patch at: https://github.com/apache/incubator-ariatosca/pull/62.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #62 commit f86f433f70946ba64aa5c139ca8efd688ab1d61b Author: mxmrlvDate: 2017-01-15T17:07:29Z added support for strict lists and dictionaries --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---