1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 from sqlalchemy
.ext
.mutable
import Mutable
19 from sqlalchemy
.types
import TypeDecorator
, Unicode
, TEXT
23 class PathTupleWithSlashes(TypeDecorator
):
24 "Represents a Tuple of strings as a slash separated string."
28 def process_bind_param(self
, value
, dialect
):
33 value
= '/'.join(value
)
36 def process_result_value(self
, value
, dialect
):
38 value
= tuple(value
.split('/'))
42 # The following two classes and only these two classes is in very
43 # large parts based on example code from sqlalchemy.
45 # The original copyright notice and license follows:
46 # Copyright (C) 2005-2011 the SQLAlchemy authors and contributors <see AUTHORS file>
48 # This module is part of SQLAlchemy and is released under
49 # the MIT License: http://www.opensource.org/licenses/mit-license.php
51 class JSONEncoded(TypeDecorator
):
52 "Represents an immutable structure as a json-encoded string."
56 def process_bind_param(self
, value
, dialect
):
58 value
= json
.dumps(value
)
61 def process_result_value(self
, value
, dialect
):
63 value
= json
.loads(value
)
67 class MutationDict(Mutable
, dict):
69 def coerce(cls
, key
, value
):
70 "Convert plain dictionaries to MutationDict."
72 if not isinstance(value
, MutationDict
):
73 if isinstance(value
, dict):
74 return MutationDict(value
)
76 # this call will raise ValueError
77 return Mutable
.coerce(key
, value
)
81 def __setitem__(self
, key
, value
):
82 "Detect dictionary set events and emit change events."
84 dict.__setitem
__(self
, key
, value
)
87 def __delitem__(self
, key
):
88 "Detect dictionary del events and emit change events."
90 dict.__delitem
__(self
, key
)