# HG changeset patch # User Thinker K.F. Li # Date 1308147360 -28800 # Node ID 0766bd54c9d3ec37f3f52e84e98f25998a316ca7 # Parent ee8aeb299f106fd4b1f1afbcc4bade9869338a0c Formally parse path into parts in structpath.py diff -r ee8aeb299f10 -r 0766bd54c9d3 paraspace/structpath.py --- a/paraspace/structpath.py Wed Jun 15 13:30:11 2011 +0800 +++ b/paraspace/structpath.py Wed Jun 15 22:16:00 2011 +0800 @@ -24,6 +24,10 @@ # objs = find_objs_path(ctx, '/car/wheels') # \endcode # +import re + +_reo_nest_chars = re.compile('[\'"\\[\\]/\\\\]') + class context(object): all_classes = None class_instances = None @@ -44,9 +48,83 @@ pass +STATE_START = 0 +STATE_PRED = 1 +STATE_STR_SQ = 2 +STATE_STR_DQ = 3 +STATE_BKSP = 4 + def _path_split(path): - parts = [p.strip() - for p in path.split('/')] + stk = [] + idx = 0 + left = 0 + state = STATE_START + parts = [] + while True: + mo = _reo_nest_chars.search(path, idx) + + if mo == None: + parts.append(path[left:]) + break + + ch = mo.group() + idx = mo.end() + if state == STATE_START: + if ch == '/': + parts.append(path[left:idx - 1]) + left = idx + elif ch == '[': + stk.append(state) + state = STATE_PRED + elif ch == '\'': + stk.append(state) + state = STATE_STR_SQ + elif ch == '"': + stk.append(state) + state = STATE_STR_DQ + pass + pass + elif state == STATE_STR_SQ: + if ch == '\'': + state = stk.pop() + pass + elif ch == '\\': + stk.append(state) + state = STATE_BKSP + bksp_idx = idx + pass + pass + elif state == STATE_STR_DQ: + if ch == '"': + state = stk.pop() + elif ch == '\\': + stk.append(state) + state = STATE_BKSP + bksp_idx = idx + pass + pass + elif state == STATE_PRED: + if ch == '[': + stk.append(state) + state = STATE_PRED + elif ch == '\'': + stk.append(state) + state = STATE_STR_SQ + elif ch == '"': + stk.append(state) + state = STATE_STR_DQ + elif ch == ']': + state = stk.pop() + pass + pass + elif state == STATE_BKSP: + if idx != (bksp_idx + 1): + idx = mo.start() + pass + state = stk.pop() + pass + pass + return parts