summaryrefslogtreecommitdiff
path: root/morphlib
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-01-21 17:16:23 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-01-21 17:27:30 +0000
commitd1882582a3d0641a06f908c65de200e5b13ff5c2 (patch)
treecf80a101c822ddbf040f5f90b7169fd46e915d41 /morphlib
parent7172dca14d7306d79e93b97dcef3a006309f2e03 (diff)
parent053c0993bb62dd5e1a73d3367ea04c70874f3bd1 (diff)
downloadmorph-d1882582a3d0641a06f908c65de200e5b13ff5c2.tar.gz
Merge branch 'jjardon/python_compatibility_fixes' of ssh://git.baserock.org/baserock/baserock/morph
This includes the following fixups: - altering the bootstrap script to install ordereddict and simplejson. - Adding a comment to clarify that it is intentional to use simplejson if collections does not have OrderedDict - Amending the copyright years to include 2013
Diffstat (limited to 'morphlib')
-rw-r--r--morphlib/morph2.py34
-rw-r--r--morphlib/morph2_tests.py62
2 files changed, 41 insertions, 55 deletions
diff --git a/morphlib/morph2.py b/morphlib/morph2.py
index 73d55d1d..4fdf7ba4 100644
--- a/morphlib/morph2.py
+++ b/morphlib/morph2.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2012 Codethink Limited
+# Copyright (C) 2012-2013 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -14,16 +14,19 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-import sys
-have_python27 = sys.version_info >= (2,7)
-
-import collections
-if not hasattr(collections, 'OrderedDict'): # pragma: no cover
- collections.OrderedDict = dict
import copy
-import json
import re
+# It is intentional that if collections does not have OrderedDict that
+# simplejson is also used in preference to json, as OrderedDict became
+# a member of collections in the same release json got its object_pairs_hook
+try: # pragma: no cover
+ from collections import OrderedDict
+ import json
+except ImportError: # pragma: no cover
+ from ordereddict import OrderedDict
+ import simplejson as json
+
class Morphology(object):
@@ -58,19 +61,8 @@ class Morphology(object):
]
}
- if have_python27: # pragma: no cover
- @staticmethod
- def _order_keys(pairs):
- result = collections.OrderedDict()
- for k,v in pairs:
- result[k] = v
- return result
-
def __init__(self, text):
- if have_python27: # pragma: no cover
- self._dict = json.loads(text, object_pairs_hook=self._order_keys)
- else: # pragma: no cover
- self._dict = json.loads(text)
+ self._dict = json.loads(text, object_pairs_hook=OrderedDict)
self._set_defaults()
self._validate_children()
@@ -158,7 +150,7 @@ class Morphology(object):
# Recreate dict without the empty default values, with a few kind
# specific hacks to try and edit standard morphologies as
# non-destructively as possible
- as_dict = collections.OrderedDict()
+ as_dict = OrderedDict()
for key in self.keys():
if self['kind'] == 'stratum' and key == 'chunks':
value = copy.copy(self[key])
diff --git a/morphlib/morph2_tests.py b/morphlib/morph2_tests.py
index baa6f724..756873a0 100644
--- a/morphlib/morph2_tests.py
+++ b/morphlib/morph2_tests.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2012 Codethink Limited
+# Copyright (C) 2012-2013 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -14,9 +14,6 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-import sys
-have_python27 = sys.version_info >= (2,7)
-
import StringIO
import unittest
@@ -202,9 +199,8 @@ class MorphologyTests(unittest.TestCase):
Morphology,
text)
- if have_python27:
- def test_writing_preserves_field_order(self):
- text = '''{
+ def test_writing_preserves_field_order(self):
+ text = '''{
"kind": "system",
"disk-size": 1073741824,
"description": "Some text",
@@ -223,19 +219,18 @@ class MorphologyTests(unittest.TestCase):
}
]
}'''
- morphology = Morphology(text)
- output = StringIO.StringIO()
- morphology.write_to_file(output)
+ morphology = Morphology(text)
+ output = StringIO.StringIO()
+ morphology.write_to_file(output)
- text_lines = text.splitlines()
- output_lines = output.getvalue().splitlines()
+ text_lines = text.splitlines()
+ output_lines = output.getvalue().splitlines()
- # Verify that input and output are equal.
- self.assertEqual(text_lines, output_lines)
+ # Verify that input and output are equal.
+ self.assertEqual(text_lines, output_lines)
- if have_python27:
- def test_writing_stratum_morphology_preserves_chunk_order(self):
- text = '''{
+ def test_writing_stratum_morphology_preserves_chunk_order(self):
+ text = '''{
"kind": "stratum",
"chunks": [
{
@@ -252,30 +247,29 @@ class MorphologyTests(unittest.TestCase):
}
]
}'''
- morphology = Morphology(text)
- output = StringIO.StringIO()
- morphology.write_to_file(output)
+ morphology = Morphology(text)
+ output = StringIO.StringIO()
+ morphology.write_to_file(output)
- text_lines = text.splitlines()
- output_lines = output.getvalue().splitlines()
+ text_lines = text.splitlines()
+ output_lines = output.getvalue().splitlines()
- # Verify that input and output are equal.
- self.assertEqual(text_lines, output_lines)
+ # Verify that input and output are equal.
+ self.assertEqual(text_lines, output_lines)
- if have_python27:
- def test_writing_preserves_disk_size(self):
- text = '''{
+ def test_writing_preserves_disk_size(self):
+ text = '''{
"kind": "system",
"disk-size": "1g",
"arch": "x86_64",
"system-kind": "syslinux-disk"
}'''
- morphology = Morphology(text)
- output = StringIO.StringIO()
- morphology.write_to_file(output)
+ morphology = Morphology(text)
+ output = StringIO.StringIO()
+ morphology.write_to_file(output)
- text_lines = text.splitlines()
- output_lines = output.getvalue().splitlines()
+ text_lines = text.splitlines()
+ output_lines = output.getvalue().splitlines()
- # Verify that in- and output are the same.
- self.assertEqual(text_lines, output_lines)
+ # Verify that in- and output are the same.
+ self.assertEqual(text_lines, output_lines)