expand the more-to-the-left/outer bracket, return a list of ToExpand's

def expand_one (self):
        "expand the more-to-the-left/outer bracket, return a list of ToExpand's"
        data= self.indexes.pop (0)

        left_cb, right_cb= data
        prefix= self.text[:left_cb]
        # the body includes the {}'s
        body= self.text[left_cb:right_cb+1]
        postfix= self.text[right_cb+1:]
        # print ("pre:%r b:%r post:%r" % (prefix, body, postfix))

        # will hold the expansion of the body
        expanded= []
        # do not count the opening bracket
        last= 1
        split_here= False
        comma_found= False
        # index for the position in the original string of the beginning of
        # the current body starts
        # we need it because the indexes are based on the positions on the
        # original string
        base= left_cb+1

        for i, c in enumerate (body):
            if c==',' and body[i-1]!='\\':
                split_here= True
                # check if this comma belongs to a inner group
                for l, r in self.indexes:
                    # TODO: show that this cannot actually happen
                    if l>right_cb:
                        # stop searching, this pair is beyond us
                        break

                    orig_i= base+i
                    # print (s, l, orig_i, r)

                    if l<orig_i and orig_i<r:
                        # print 'not splitting, comma @%d(%d) between {%d,%d}' % (orig_i, i, l, r)
                        split_here= False
                        # stop searching too
                        break

                if split_here:
                    comma_found= True
                    dst= body[last:i]
                    te= ToExpand (prefix+dst+postfix)
                    expanded.append (te)
                    # print ('split!', expanded)
                    last= i+1 # point to the next char, not the comma itself

        # only add the last element if a comma was found
        if comma_found:
            # do not count the closing bracket
            dst= body[last:-1]
            te= ToExpand (prefix+dst+postfix)
            expanded.append (te)
            # print ('append', expanded)
        else:
            # otherwise, just leave untouched
            # (except for the index we already removed at the beginning of this method)
            expanded.append (self)

        return expanded