diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8552857 --- /dev/null +++ b/LICENSE @@ -0,0 +1,31 @@ +Copyright (c) 2016 by Armin Ronacher. + +Some rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * The names of the contributors may not be used to endorse or + promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/phpserialize.py b/phpserialize.py index 2d21041..1b4c53c 100644 --- a/phpserialize.py +++ b/phpserialize.py @@ -285,7 +285,7 @@ class WP_User extends WP_UserBase { xrange = range __author__ = 'Armin Ronacher ' -__version__ = '1.3' +__version__ = '1.4' __all__ = ('phpobject', 'convert_member_dict', 'dict_to_list', 'dict_to_tuple', 'load', 'loads', 'dump', 'dumps', 'serialize', 'unserialize') @@ -580,6 +580,35 @@ def dict_to_list(d): except KeyError: raise ValueError('dict is not a sequence') +def full_dict_to_list(d, array_hook=dict): + """Converts a full dict into a more Pythonic structure. In particular ensure that all list-like structures are handled as Python lists + Examples: + + d1 = {'a': 'b', 'c': {0: '1', 1: '2', 2: {'e': 7}, 3: {2: 8}}} + full_dict_to_list(d1) returns + {'a': 'b', 'c': ['1', '2', {'e': 7}, {2: 8}]} + + d2 = OrderedDict({'a': 'b', 'c': OrderedDict({0: '1', 1: '2', 2: OrderedDict({'e': 7}), 3: OrderedDict({2: 8})})}) + full_dict_to_list(d2, OrderedDict) returns + OrderedDict([('a', 'b'), ('c', ['1', '2', OrderedDict([('e', 7)]), OrderedDict([(2, 8)])])]) + """ + if type(d) is array_hook: + keys = list(d.keys()) + + if keys == list(range(0, len(keys))): #Any array with keys 0..N is considered to be a list + return [full_dict_to_list(val) for val in d.values()] + elif len(keys) == 1: + key = keys[0] + return {key : full_dict_to_list(d[key], array_hook)} + else: + tmp_dict = array_hook() + for key in keys: + tmp_dict.update( {key : full_dict_to_list(d[key], array_hook)} ) + return tmp_dict + else: + return d + + def dict_to_tuple(d): """Converts an ordered dict into a tuple.""" diff --git a/tests.py b/tests.py index e065f92..a7352d3 100644 --- a/tests.py +++ b/tests.py @@ -99,6 +99,19 @@ def test_basic_object_hook(self): self.assertEqual(user.username, 'admin') self.assertEqual(user.__name__, 'WP_User') + def test_full_dict_to_list(self): + #with normal dict + d1 = {'a': 'b', 'c': {0: '1', 1: '2', 2: {'e': 7}, 3: {2: 8}}} + d1_cleaned = {'a': 'b', 'c': ['1', '2', {'e': 7}, {2: 8}]} + self.assertEqual(phpserialize.full_dict_to_list(d1), d1_cleaned) + + #with OrderedDict as array_hook + from collections import OrderedDict + d2 = OrderedDict({'a': 'b', 'c': OrderedDict({0: '1', 1: '2', 2: OrderedDict({'e': 7}), 3: OrderedDict({2: 8})})}) + d2_cleaned = OrderedDict([('a', 'b'), ('c', ['1', '2', OrderedDict([('e', 7)]), OrderedDict([(2, 8)])])]) + self.assertEqual(phpserialize.full_dict_to_list(d2, OrderedDict), d2_cleaned) + + def test_session(self): data = b'foo|a:1:{s:1:"a";s:1:"b";}bar|a:1:{s:1:"c";s:1:"d";}' session = phpserialize.loads(data)