refactor: 对配置处理器和配置结构进行重构
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
def epath(dct, path: str = '', default=None, parents=False):
|
||||
if not path:
|
||||
return dct
|
||||
|
||||
path = path.rstrip('/')
|
||||
target = dct
|
||||
|
||||
for i in path.split('/'):
|
||||
# 处理字典键
|
||||
if isinstance(target, dict) and i in target:
|
||||
target = target[i]
|
||||
# 处理列表索引
|
||||
elif i.startswith('[') and i.endswith(']') and isinstance(target, (list, tuple)):
|
||||
idx = int(i[1:-1])
|
||||
if 0 <= idx < len(target):
|
||||
target = target[idx]
|
||||
elif parents:
|
||||
while len(target) <= idx:
|
||||
target.append(None)
|
||||
target[idx] = {}
|
||||
target = target[idx]
|
||||
else:
|
||||
return default
|
||||
elif parents:
|
||||
target[i] = {}
|
||||
target = target[i]
|
||||
else:
|
||||
return default
|
||||
|
||||
return target
|
||||
Reference in New Issue
Block a user