Esempio n. 1
0
    def __str__(self):
        info = self.buf.line_info(self.pos)
        template = "{}({}:{}) {} :\n{}\n{}^\n{}"
        text = info.text.rstrip()
        leading = re.sub(r'[^\t]', ' ', text)[:info.col]

        text = text.expandtabs()
        leading = leading.expandtabs()
        return template.format(info.filename, info.line + 1, info.col + 1,
                               self.message.rstrip(), text, leading,
                               '\n'.join(reversed(self.stack)))
    def __str__(self):
        info = self.buf.line_info(self.pos)
        template = "{}({}:{}) {} :\n{}\n{}^\n{}"
        text = info.text.rstrip()
        leading = re.sub(r'[^\t]', ' ', text)[:info.col]

        text = text.expandtabs()
        leading = leading.expandtabs()
        return template.format(info.filename,
                               info.line + 1, info.col + 1,
                               self.message.rstrip(),
                               text,
                               leading,
                               '\n'.join(reversed(self.stack))
                               )
    def _find_walker(self, node, prefix='walk_'):
        def pythonize_match(m):
            return '_' + m.group().lower()

        classid = id(node.__class__)

        if classid in self._walker_cache:
            return self._walker_cache[classid]

        classes = [node.__class__]
        while classes:
            cls = classes.pop(0)

            cammelcase_name = cls.__name__
            walker = getattr(self, prefix + cammelcase_name, None)
            if callable(walker):
                break

            # walk__pythonic_name with double underscore after walk
            pythonic_name = re.sub('[A-Z]+', pythonize_match, cls.__name__)
            if pythonic_name != cammelcase_name:
                walker = getattr(self, prefix + pythonic_name, None)
                if callable(walker):
                    break

            # walk_pythonic_name with single underscore after walk

            # pythonic_name = pythonic_name.lstrip('_')
            # if pythonic_name != cammelcase_name:
            #     walker = getattr(self, prefix + pythonic_name, None)
            #     if callable(walker):
            #         break

            for b in cls.__bases__:
                if b not in classes:
                    classes.append(b)
        else:
            walker = getattr(self, '_walk_default', None)
            if walker is None:
                walker = getattr(self, 'walk_default',
                                 None)  # backwars compatibility
            if not callable(walker):
                walker = None

        self._walker_cache[classid] = walker
        return walker
    def _find_walker(self, node, prefix='walk_'):
        def pythonize_match(m):
            return '_' + m.group().lower()

        classid = id(node.__class__)

        if classid in self._walker_cache:
            return self._walker_cache[classid]

        classes = [node.__class__]
        while classes:
            cls = classes.pop(0)

            cammelcase_name = cls.__name__
            walker = getattr(self, prefix + cammelcase_name, None)
            if callable(walker):
                break

            # walk__pythonic_name with double underscore after walk
            pythonic_name = re.sub('[A-Z]+', pythonize_match, cls.__name__)
            if pythonic_name != cammelcase_name:
                walker = getattr(self, prefix + pythonic_name, None)
                if callable(walker):
                    break

            # walk_pythonic_name with single underscore after walk

            # pythonic_name = pythonic_name.lstrip('_')
            # if pythonic_name != cammelcase_name:
            #     walker = getattr(self, prefix + pythonic_name, None)
            #     if callable(walker):
            #         break

            for b in cls.__bases__:
                if b not in classes:
                    classes.append(b)
        else:
            walker = getattr(self, '_walk_default', None)
            if walker is None:
                walker = getattr(self, 'walk_default', None)  # backwars compatibility
            if not callable(walker):
                walker = None

        self._walker_cache[classid] = walker
        return walker