add function to sanitise object_id and verify no //

Signed-off-by: Nico Schottelius <nico@brief.schottelius.org>
This commit is contained in:
Nico Schottelius 2012-02-12 00:05:21 +01:00
parent 6dcbfc3b88
commit 15fb097464

View file

@ -105,12 +105,17 @@ class Object(object):
raise IllegalObjectIdError(object_id, 'object_id may not start with /') raise IllegalObjectIdError(object_id, 'object_id may not start with /')
if OBJECT_MARKER in object_id.split(os.sep): if OBJECT_MARKER in object_id.split(os.sep):
raise IllegalObjectIdError(object_id, 'object_id may not contain \'%s\'' % OBJECT_MARKER) raise IllegalObjectIdError(object_id, 'object_id may not contain \'%s\'' % OBJECT_MARKER)
if '//' in object_id:
raise IllegalObjectIdError(object_id, 'object_id may not contain //')
def __init__(self, cdist_type, base_path, object_id=None): def __init__(self, cdist_type, base_path, object_id=None):
self.validate_object_id(object_id)
self.type = cdist_type # instance of Type self.type = cdist_type # instance of Type
self.base_path = base_path self.base_path = base_path
self.object_id = object_id self.object_id = object_id
self.sanitise_object_id()
self.validate_object_id(object_id)
self.name = self.join_name(self.type.name, self.object_id) self.name = self.join_name(self.type.name, self.object_id)
self.path = os.path.join(self.type.path, self.object_id, OBJECT_MARKER) self.path = os.path.join(self.type.path, self.object_id, OBJECT_MARKER)
self.absolute_path = os.path.join(self.base_path, self.path) self.absolute_path = os.path.join(self.base_path, self.path)
@ -128,7 +133,6 @@ class Object(object):
def __hash__(self): def __hash__(self):
return hash(self.name) return hash(self.name)
def __lt__(self, other): def __lt__(self, other):
return isinstance(other, self.__class__) and self.name < other.name return isinstance(other, self.__class__) and self.name < other.name
@ -146,6 +150,21 @@ class Object(object):
type_name, object_id = self.split_name(object_name) type_name, object_id = self.split_name(object_name)
return self.__class__(self.type.__class__(type_path, type_name), base_path, object_id=object_id) return self.__class__(self.type.__class__(type_path, type_name), base_path, object_id=object_id)
def sanitise_object_id(self):
"""
Remove leading and trailing slash (one only)
"""
print(self.__repr__())
# Remove leading slash
if self.object_id[0] == '/':
self.object_id = self.object_id[1:]
# Remove trailing slash
if self.object_id[-1] == '/':
self.object_id = self.object_id[:-1]
# FIXME: still needed? # FIXME: still needed?
@property @property
def explorer_path(self): def explorer_path(self):