2023-06-15 08:50:08 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
2023-07-14 07:56:59 +00:00
|
|
|
def get_obj_list_element(obj, attr: str):
|
2023-06-21 06:30:06 +00:00
|
|
|
r"""
|
|
|
|
Get the element of the list in the object
|
2023-07-14 07:56:59 +00:00
|
|
|
|
|
|
|
If the attr is a normal attribute, return the attribute of the object.
|
|
|
|
If the attr is a index type, return the element of the index in the list, like `layers[0]`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (Object): The object to get
|
|
|
|
attr (str): The suffix of the attribute to get
|
|
|
|
|
2023-06-21 06:30:06 +00:00
|
|
|
"""
|
2023-06-15 08:50:08 +00:00
|
|
|
re_pattern = r'\[\d+\]'
|
|
|
|
prog = re.compile(re_pattern)
|
2023-07-14 07:56:59 +00:00
|
|
|
result = prog.search(attr)
|
2023-06-15 08:50:08 +00:00
|
|
|
if result:
|
|
|
|
matched_brackets = result.group()
|
|
|
|
matched_index = matched_brackets.replace('[', '')
|
|
|
|
matched_index = matched_index.replace(']', '')
|
2023-07-14 07:56:59 +00:00
|
|
|
attr_ = attr.replace(matched_brackets, '')
|
|
|
|
container_obj = getattr(obj, attr_)
|
2023-06-15 08:50:08 +00:00
|
|
|
obj = container_obj[int(matched_index)]
|
|
|
|
else:
|
2023-07-14 07:56:59 +00:00
|
|
|
obj = getattr(obj, attr)
|
2023-06-15 08:50:08 +00:00
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2023-07-14 07:56:59 +00:00
|
|
|
def set_obj_list_element(obj, attr: str, value):
|
|
|
|
r"""
|
|
|
|
Set the element to value of a list object
|
|
|
|
|
|
|
|
It used like set_obj_list_element(obj, 'lyaers[0]', new_layer), it will set obj.layers[0] to value
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (object): The object to set
|
|
|
|
attr (str): the string including a list index like `layers[0]`
|
|
|
|
"""
|
|
|
|
re_pattern = r'\[\d+\]'
|
|
|
|
prog = re.compile(re_pattern)
|
|
|
|
result = prog.search(attr)
|
|
|
|
if result:
|
|
|
|
matched_brackets = result.group()
|
|
|
|
matched_index = matched_brackets.replace('[', '')
|
|
|
|
matched_index = matched_index.replace(']', '')
|
|
|
|
attr_ = attr.replace(matched_brackets, '')
|
|
|
|
container_obj = getattr(obj, attr_)
|
|
|
|
container_obj[int(matched_index)] = value
|
|
|
|
else:
|
|
|
|
setattr(obj, attr, value)
|
|
|
|
|
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
def hasattr_(obj, attr: str):
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Check whether the object has the multi sublevel attr
|
|
|
|
|
|
|
|
Args:
|
2023-05-24 02:26:46 +00:00
|
|
|
obj (object): The object to check
|
|
|
|
attr (str): The multi level attr to check
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
attrs = attr.split('.')
|
|
|
|
for a in attrs:
|
|
|
|
try:
|
2023-06-15 08:50:08 +00:00
|
|
|
obj = get_obj_list_element(obj, a)
|
2023-05-22 07:02:17 +00:00
|
|
|
except AttributeError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-05-24 02:26:46 +00:00
|
|
|
|
|
|
|
def setattr_(obj, attr: str, value, ignore: bool = False):
|
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Set the object's multi sublevel attr to value, if ignore, ignore when it doesn't exist
|
|
|
|
|
|
|
|
Args:
|
2023-05-24 02:26:46 +00:00
|
|
|
obj (object): The object to set
|
|
|
|
attr (str): The multi level attr to set
|
|
|
|
value (Any): The value to set
|
|
|
|
ignore (bool): Whether to ignore when the attr doesn't exist
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
attrs = attr.split('.')
|
|
|
|
for a in attrs[:-1]:
|
|
|
|
try:
|
2023-06-15 08:50:08 +00:00
|
|
|
obj = get_obj_list_element(obj, a)
|
2023-05-22 07:02:17 +00:00
|
|
|
except AttributeError:
|
|
|
|
if ignore:
|
2023-05-24 02:26:46 +00:00
|
|
|
return
|
2023-06-30 08:16:44 +00:00
|
|
|
raise AttributeError(f"Object {obj.__class__.__name__} has no attribute {attr}")
|
2023-07-14 07:56:59 +00:00
|
|
|
set_obj_list_element(obj, attrs[-1], value)
|
2023-05-22 07:02:17 +00:00
|
|
|
|
2023-05-24 02:26:46 +00:00
|
|
|
|
2023-06-12 08:52:18 +00:00
|
|
|
def getattr_(obj, attr: str, ignore: bool = False):
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Get the object's multi sublevel attr
|
2023-05-24 02:26:46 +00:00
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
Args:
|
2023-05-24 02:26:46 +00:00
|
|
|
obj (object): The object to set
|
|
|
|
attr (str): The multi level attr to set
|
|
|
|
ignore (bool): Whether to ignore when the attr doesn't exist
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
attrs = attr.split('.')
|
|
|
|
for a in attrs:
|
|
|
|
try:
|
2023-06-15 08:50:08 +00:00
|
|
|
obj = get_obj_list_element(obj, a)
|
2023-05-22 07:02:17 +00:00
|
|
|
except AttributeError:
|
|
|
|
if ignore:
|
|
|
|
return None
|
2023-06-30 08:16:44 +00:00
|
|
|
raise AttributeError(f"Object {obj.__class__.__name__} has no attribute {attr}")
|
2023-05-24 02:26:46 +00:00
|
|
|
return obj
|