From 4240ca597238491182d6eae15e1eed468b7c8c62 Mon Sep 17 00:00:00 2001 From: tengqm Date: Tue, 6 Jan 2015 09:45:09 +0800 Subject: [PATCH] Move to use MutableDict from new SQLAlchemy versions This was legacy code where MutableDict has to be reinvented. This restriction is no longer valid now. --- senlin/db/sqlalchemy/mutable.py | 62 --------------------------------- senlin/db/sqlalchemy/types.py | 17 ++------- 2 files changed, 3 insertions(+), 76 deletions(-) delete mode 100644 senlin/db/sqlalchemy/mutable.py diff --git a/senlin/db/sqlalchemy/mutable.py b/senlin/db/sqlalchemy/mutable.py deleted file mode 100644 index 3b12184e1..000000000 --- a/senlin/db/sqlalchemy/mutable.py +++ /dev/null @@ -1,62 +0,0 @@ -# Licensed 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. -# -# The MIT License -# -# ext/mutable.py -# Copyright (C) 2005-2013 the SQLAlchemy authors -# and contributors -# -# This module is part of SQLAlchemy and is released under -# the MIT License: http://www.opensource.org/lic enses/mit-license.php -""" -Submitted on behalf of a third-party: sqlalchemy -""" -from sqlalchemy.ext.mutable import Mutable - - -class MutableDict(Mutable, dict): - """A dictionary type that implements :class:`.Mutable`. - - .. versionadded:: 0.8 - - """ - - def __setitem__(self, key, value): - """Detect dictionary set events and emit change events.""" - dict.__setitem__(self, key, value) - self.changed() - - def __delitem__(self, key): - """Detect dictionary del events and emit change events.""" - dict.__delitem__(self, key) - self.changed() - - def clear(self): - dict.clear(self) - self.changed() - - @classmethod - def coerce(cls, key, value): - """Convert plain dictionary to MutableDict.""" - if not isinstance(value, MutableDict): - if isinstance(value, dict): - return MutableDict(value) - return Mutable.coerce(key, value) - else: - return value - - def __getstate__(self): - return dict(self) - - def __setstate__(self, state): - self.update(state) diff --git a/senlin/db/sqlalchemy/types.py b/senlin/db/sqlalchemy/types.py index 95ebd280e..e118c2be6 100644 --- a/senlin/db/sqlalchemy/types.py +++ b/senlin/db/sqlalchemy/types.py @@ -14,9 +14,9 @@ import json from sqlalchemy.dialects import mysql +from sqlalchemy.ext import mutable from sqlalchemy import types - dumps = json.dumps loads = json.loads @@ -32,7 +32,6 @@ class LongText(types.TypeDecorator): class Json(LongText): - def process_bind_param(self, value, dialect): return dumps(value) @@ -42,15 +41,5 @@ class Json(LongText): return loads(value) -def associate_with(sqltype): - # TODO(leizhang) When we removed sqlalchemy 0.7 dependence - # we can import MutableDict directly and remove ./mutable.py - try: - from sqlalchemy.ext import mutable - mutable.MutableDict.associate_with(Json) - except ImportError: - from senlin.db.sqlalchemy import mutable - mutable.MutableDict.associate_with(Json) - -associate_with(LongText) -associate_with(Json) +mutable.MutableDict.associate_with(LongText) +mutable.MutableDict.associate_with(Json)