Created
February 12, 2016 08:03
-
-
Save lubomir/721dd62b48c2bcf84443 to your computer and use it in GitHub Desktop.
Context manager instead of wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/pdc/apps/common/hacks.py b/pdc/apps/common/hacks.py | |
index ca55552..c9ce891 100644 | |
--- a/pdc/apps/common/hacks.py | |
+++ b/pdc/apps/common/hacks.py | |
@@ -5,6 +5,7 @@ | |
# http://opensource.org/licenses/MIT | |
# | |
import re | |
+import contextlib | |
from django.db import connection | |
from django.conf import settings | |
@@ -14,12 +15,13 @@ from rest_framework import serializers | |
from pkg_resources import parse_version | |
-def deserialize_wrapper(func, data): | |
[email protected] | |
+def productmd_wrapper(): | |
""" | |
Convert generic productmd exceptions into validation errors. | |
""" | |
try: | |
- func(data) | |
+ yield | |
except KeyError as e: | |
raise serializers.ValidationError( | |
{'detail': 'Error parsing productmd metadata.', | |
diff --git a/pdc/apps/compose/lib.py b/pdc/apps/compose/lib.py | |
index b63900c..321be52 100644 | |
--- a/pdc/apps/compose/lib.py | |
+++ b/pdc/apps/compose/lib.py | |
@@ -91,10 +91,12 @@ def compose__import_rpms(request, release_id, composeinfo, rpm_manifest): | |
release_obj = release_models.Release.objects.get(release_id=release_id) | |
ci = productmd.composeinfo.ComposeInfo() | |
- common_hacks.deserialize_wrapper(ci.deserialize, composeinfo) | |
+ with common_hacks.productmd_wrapper(): | |
+ ci.deserialize(composeinfo) | |
rm = productmd.rpms.Rpms() | |
- common_hacks.deserialize_wrapper(rm.deserialize, rpm_manifest) | |
+ with common_hacks.productmd_wrapper(): | |
+ rm.deserialize(rpm_manifest) | |
_maybe_raise_inconsistency_error(ci, rm, 'rpms') | |
@@ -181,10 +183,12 @@ def compose__import_images(request, release_id, composeinfo, image_manifest): | |
release_obj = release_models.Release.objects.get(release_id=release_id) | |
ci = productmd.composeinfo.ComposeInfo() | |
- common_hacks.deserialize_wrapper(ci.deserialize, composeinfo) | |
+ with common_hacks.productmd_wrapper(): | |
+ ci.deserialize(composeinfo) | |
im = productmd.images.Images() | |
- common_hacks.deserialize_wrapper(im.deserialize, image_manifest) | |
+ with common_hacks.productmd_wrapper(): | |
+ im.deserialize(image_manifest) | |
_maybe_raise_inconsistency_error(ci, im, 'images') | |
diff --git a/pdc/apps/release/lib.py b/pdc/apps/release/lib.py | |
index 1af5088..e8fd1e0 100644 | |
--- a/pdc/apps/release/lib.py | |
+++ b/pdc/apps/release/lib.py | |
@@ -86,7 +86,8 @@ def release__import_from_composeinfo(request, composeinfo_json): | |
Import release including variants and architectures from composeinfo json. | |
""" | |
ci = productmd.composeinfo.ComposeInfo() | |
- common_hacks.deserialize_wrapper(ci.deserialize, composeinfo_json) | |
+ with common_hacks.productmd_wrapper(): | |
+ ci.deserialize(composeinfo_json) | |
if ci.release.is_layered: | |
base_product_obj, _ = _logged_get_or_create( | |
-- | |
2.5.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment