add utilitly method to split an object name into its type name and object_id
Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
parent
70ee0f1681
commit
afba5e9280
1 changed files with 20 additions and 12 deletions
|
@ -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 __foo/bar>.object_from_name('__other/object') -> <Object __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 __foo/bar>.object_from_name('__other/object') -> <Object __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
|
||||
|
|
Loading…
Reference in a new issue