From b122b53d7338d507a13ad70b2dda097b9c80342a Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 14 Oct 2011 09:05:51 +0200 Subject: [PATCH] implement Object object_from_name Signed-off-by: Steven Armstrong --- lib/cdist/core/object.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/cdist/core/object.py b/lib/cdist/core/object.py index a170dd30..282e8be5 100644 --- a/lib/cdist/core/object.py +++ b/lib/cdist/core/object.py @@ -63,6 +63,21 @@ class Object(object): if DOT_CDIST in dirs: yield os.path.relpath(path, object_base_path) + def object_from_name(self, object_name): + """Convenience method for creating an object instance from an object name. + + Mainly intended to create objects when resolving requirements. + + e.g: + .object_from_name('__other/object') -> + + """ + type_path = self.type.base_path + object_path = self.base_path + type_name = object_name.split(os.sep)[0] + object_id = os.sep.join(object_name.split(os.sep)[1:]) + return self.__class__(self.type.__class__(type_path, type_name), object_path, object_id=object_id) + def __init__(self, cdist_type, base_path, object_id=None): self.type = cdist_type # instance of Type self.base_path = base_path