def locate_blocks(self, codestr):
        """
        Returns all code blocks between `{` and `}` and a proper key
        that can be multilined as long as it's joined by `,` or enclosed in
        `(` and `)`.
        Returns the "lose" code that's not part of the block as a third item.
        """
        def _strip_selprop(selprop, lineno):
            _lineno, _sep, selprop = selprop.partition(SEPARATOR)
            if _sep == SEPARATOR:
                lineno = _lineno.strip()
                lineno = int(lineno) if lineno else 0
            else:
                selprop = _lineno
            selprop = _nl_num_re.sub('\n', selprop)
            selprop = selprop.strip()
            return selprop, lineno

        def _strip(selprop):
            selprop, _ = _strip_selprop(selprop, 0)
            return selprop

        lineno = 0

        par = 0
        instr = None
        depth = 0
        skip = False
        thin = None
        i = init = safe = lose = 0
        start = end = None

        for m in _blocks_re.finditer(codestr):
            _s = m.start(0)
            _e = m.end(0)
            i = _e - 1
            if _s == _e:
                break
            if instr is not None:
                if codestr[i] == instr:
                    instr = None
            elif codestr[i] in ('"', "'"):
                instr = codestr[i]
            elif codestr[i] == '(':
                par += 1
                thin = None
                safe = i + 1
            elif codestr[i] == ')':
                par -= 1
            elif not par and not instr:
                if codestr[i] == '{':
                    if depth == 0:
                        if i > 0 and codestr[i - 1] == '#':
                            skip = True
                        else:
                            start = i
                            if thin is not None and _strip(codestr[thin:i - 1]):
                                init = thin
                            if lose < init:
                                losestr = codestr[lose:init]
                                for _property in losestr.split(';'):
                                    _property, lineno = _strip_selprop(
                                        _property, lineno)
                                    if _property:
                                        yield lineno, _property, None
                                lose = init
                            thin = None
                    depth += 1
                elif codestr[i] == '}':
                    if depth > 0:
                        depth -= 1
                        if depth == 0:
                            if not skip:
                                end = i
                                _selectors, lineno = _strip_selprop(
                                    codestr[init:start], lineno)
                                _codestr = codestr[start + 1:end].strip()
                                if _selectors:
                                    yield lineno, _selectors, _codestr
                                init = safe = lose = end + 1
                                thin = None
                            skip = False
                elif depth == 0:
                    if codestr[i] == ';':
                        init = safe = i + 1
                        thin = None
                    elif codestr[i] == ',':
                        if thin is not None and _strip(codestr[thin:i - 1]):
                            init = thin
                        thin = None
                        safe = i + 1
                    elif codestr[i] == '\n':
                        if thin is None and _strip(codestr[safe:i - 1]):
                            thin = i + 1
                        elif thin is not None and _strip(codestr[thin:i - 1]):
                            init = i + 1
                            thin = None
        if depth > 0:
            if not skip:
                _selectors, lineno = _strip_selprop(
                    codestr[init:start], lineno)
                _codestr = codestr[start + 1:].strip()
                if _selectors:
                    yield lineno, _selectors, _codestr
                if par:
                    raise Exception("Missing closing parenthesis somewhere in block: '%s'" %
                                    _selectors)
                elif instr:
                    raise Exception("Missing closing string somewhere in block: '%s'" %
                                    _selectors)
                else:
                    raise Exception("Block never closed: '%s'" % _selectors)
        losestr = codestr[lose:]
        for _property in losestr.split(';'):
            _property, lineno = _strip_selprop(_property, lineno)
            if _property:
                yield lineno, _property, None
def locate_blocks ( self , codestr ) : def _strip_selprop ( selprop , lineno ) : _lineno , _sep , selprop = selprop . partition ( SEPARATOR ) if _sep == SEPARATOR : lineno = _lineno . strip ( ) lineno = int ( lineno ) if lineno else 0 else : selprop = _lineno selprop = _nl_num_re . sub ( '\n' , selprop ) selprop = selprop . strip ( ) return selprop , lineno def _strip ( selprop ) : selprop , _ = _strip_selprop ( selprop , 0 ) return selprop lineno = 0 par = 0 instr = None depth = 0 skip = False thin = None i = init = safe = lose = 0 start = end = None for m in _blocks_re . finditer ( codestr ) : _s = m . start ( 0 ) _e = m . end ( 0 ) i = _e - 1 if _s == _e : break if instr is not None : if codestr [ i ] == instr : instr = None elif codestr [ i ] in ( '"' , "'" ) : instr = codestr [ i ] elif codestr [ i ] == '(' : par += 1 thin = None safe = i + 1 elif codestr [ i ] == ')' : par -= 1 elif not par and not instr : if codestr [ i ] == '{' : if depth == 0 : if i > 0 and codestr [ i - 1 ] == '#' : skip = True else : start = i if thin is not None and _strip ( codestr [ thin : i - 1 ] ) : init = thin if lose < init : losestr = codestr [ lose : init ] for _property in losestr . split ( ';' ) : _property , lineno = _strip_selprop ( _property , lineno ) if _property : yield lineno , _property , None lose = init thin = None depth += 1 elif codestr [ i ] == '}' : if depth > 0 : depth -= 1 if depth == 0 : if not skip : end = i _selectors , lineno = _strip_selprop ( codestr [ init : start ] , lineno ) _codestr = codestr [ start + 1 : end ] . strip ( ) if _selectors : yield lineno , _selectors , _codestr init = safe = lose = end + 1 thin = None skip = False elif depth == 0 : if codestr [ i ] == ';' : init = safe = i + 1 thin = None elif codestr [ i ] == ',' : if thin is not None and _strip ( codestr [ thin : i - 1 ] ) : init = thin thin = None safe = i + 1 elif codestr [ i ] == '\n' : if thin is None and _strip ( codestr [ safe : i - 1 ] ) : thin = i + 1 elif thin is not None and _strip ( codestr [ thin : i - 1 ] ) : init = i + 1 thin = None if depth > 0 : if not skip : _selectors , lineno = _strip_selprop ( codestr [ init : start ] , lineno ) _codestr = codestr [ start + 1 : ] . strip ( ) if _selectors : yield lineno , _selectors , _codestr if par : raise Exception ( "Missing closing parenthesis somewhere in block: '%s'" % _selectors ) elif instr : raise Exception ( "Missing closing string somewhere in block: '%s'" % _selectors ) else : raise Exception ( "Block never closed: '%s'" % _selectors ) losestr = codestr [ lose : ] for _property in losestr . split ( ';' ) : _property , lineno = _strip_selprop ( _property , lineno ) if _property : yield lineno , _property , None
Returns all code blocks between `{` and `}` and a proper key
        that can be multilined as long as it's joined by `,` or enclosed in
        `(` and `)`.
        Returns the "lose" code that's not part of the block as a third item.