From afba5e92804e537b372b8548aca0d619aa66488b Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 2 Nov 2011 07:52:08 +0100 Subject: [PATCH] add utilitly method to split an object name into its type name and object_id Signed-off-by: Steven Armstrong --- lib/cdist/core/object.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/cdist/core/object.py b/lib/cdist/core/object.py index 29dc16e9..b62417b7 100644 --- a/lib/cdist/core/object.py +++ b/lib/cdist/core/object.py @@ -60,9 +60,7 @@ class Object(object): def list_objects(cls, object_base_path, type_base_path): """Return a list of object instances""" for object_name in cls.list_object_names(object_base_path): - type_name = object_name.split(os.sep)[0] - # FIXME: allow object without object_id? e.g. for singleton - object_id = os.sep.join(object_name.split(os.sep)[1:]) + type_name, object_id = cls.split_name(object_name) yield cls(cdist.core.Type(type_base_path, type_name), object_base_path, object_id=object_id) @classmethod @@ -77,20 +75,17 @@ 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. + @staticmethod + def split_name(object_name): + """split_name('__type_name/the/object_id') -> ('__type_name', 'the/object_id') - Mainly intended to create objects when resolving requirements. - - e.g: - .object_from_name('__other/object') -> + Split the given object name into it's type and object_id parts. """ - type_path = self.type.base_path - object_path = self.base_path type_name = object_name.split(os.sep)[0] + # FIXME: allow object without object_id? e.g. for singleton 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) + return type_name, object_id def __init__(self, cdist_type, base_path, object_id=None): if object_id: @@ -118,6 +113,19 @@ class Object(object): def __lt__(self, other): return isinstance(other, self.__class__) and self.name < other.name + 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_id = self.split_name(object_name) + return self.__class__(self.type.__class__(type_path, type_name), object_path, object_id=object_id) # FIXME: still needed? @property