From 72499b6fbb161e7d46d82f09439cef1f8faee3b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 31 May 2016 17:57:40 +0200 Subject: [PATCH] Added Resource.new(dict_or_resource) class method. This allows one to up-cast a Resource to a File, Project, Node, etc. --- pillarsdk/resource.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pillarsdk/resource.py b/pillarsdk/resource.py index c25f3d9..350a0d1 100644 --- a/pillarsdk/resource.py +++ b/pillarsdk/resource.py @@ -129,6 +129,18 @@ class Resource(object): self[key] = val return self + @classmethod + def new(cls, dict_or_resource): + """None-safe constructor.""" + + if dict_or_resource is None: + return None + + if isinstance(dict_or_resource, Resource): + dict_or_resource = dict_or_resource.to_dict() + + return cls(dict_or_resource) + class Find(Resource):