Ejemplo n.º 1
0
    def build_block(self):
        indent_cnt = self.ctx.indent_cnt
        name, val = self.ctx.obj
        position = self.ctx.position

        debug(self.config, C._DL_FUNC_, indent_cnt,
              ('key:{}, indent_cnt:{}, position:{:b}'.format(
                  name, indent_cnt, position)))
        ret = ustr('')

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending(val)
        debug(self.config, C._DL_STATEMENT, 
              indent_cnt, 'tail, block_ending: ' + str([tail, block_ending]))

        key = self.ctx.key_expr

        ret += self.ctn.write(self.config.indent_char * indent_cnt + key + ':')
        if is_extendable(val) and self.config.max_depth > indent_cnt:
            # still in of range, and can be expanded

            # value need to be dispalyed on new line
            # including: 
            #   class type & class instance
            #   function type
            if self.config.newline or (is_newline_obj(val) and
                                 position & C._AS_ELEMENT_):
                ret += self.ctn.write(ustr('\n'))
                indent_cnt = indent_cnt + 1
                position = C._AS_ELEMENT_
                debug(self.config, C._DL_STATEMENT, indent_cnt, 'make newline')
            # value will be dispalyed immediately after one space
            else:
                ret += self.ctn.write(ustr(" "))
                position = C._AS_VALUE_

            ctx = self.ctx.clone()
            ctx.obj = val
            ctx.parent = self
            ctx.position =  position
            ctx.indent_cnt = indent_cnt
            ret += str(Block(self.config, ctx))
            ret += self.ctn.write(tail + block_ending)
        else:
            ctx = self.ctx.clone()
            ctx.obj = val
            if self.config.max_depth <= indent_cnt:
                # reach max_depth, just show brief message
                rb = ReprBlock(self.config, ctx, 
                               handlers=[ReprStringHandlerInlineRepr(self.config), 
                                         ReprOthersHandlerInlineRepr(self.config)])
                ret += self.ctn.write(ustr(" " + str(rb) + tail + block_ending))
            else:
                rb = ReprBlock(self.config, ctx, 
                               handlers=[ReprStringHandlerMultiLiner(self.config), 
                                         ReprOthersHandler(self.config)])
                ret += self.ctn.write(ustr(" ") + str(rb) + ustr(tail + block_ending))

        return ret
Ejemplo n.º 2
0
    def build_block(self):
        indent_cnt = self.ctx.indent_cnt
        o = self.ctx.obj
        position = self.ctx.position
        ret = ustr('')

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending()
        debug(self.config, 
              C._DL_FUNC_, 
              indent_cnt, 'tail, block_ending: ' + str([tail, block_ending]))

        '所有元素显示在同一行'
        if self.config.list_in_line:
            _f = map(
                lambda e: (not (is_extendable(e) or 
                                too_long(self.config.indent_char, 
                                         indent_cnt, position, repr_block(e, self.config)))), 
                o)
            if all(_f):
                _o = map(lambda e: repr_block(e, self.config), o)
                if self.config.newline or position & C._AS_ELEMENT_:
                    ret += self.ctn.write(self.config.indent_char * indent_cnt)
                ret += self.ctn.write("[")
                for (i, e) in enumerate(_o):
                    if i:
                        ret += self.ctn.write(', ')
                    ret += self.ctn.write(e)
                ret +=  self.ctn.write("]" + tail + block_ending)
                return ret

        # [
        if self.config.newline or position & C._AS_ELEMENT_:
            ret += self.ctn.write(self.config.indent_char * indent_cnt + ustr('[') + ustr('\n'))
        else:
            ret += self.ctn.write(ustr('[') + ustr('\n'))

        # body
        for e in o:
            ctx = self.ctx.clone()
            ctx.obj = e
            ctx.parent = self
            ctx.indent_cnt = self.ctx.indent_cnt + 1
            ctx.position = C._AS_ELEMENT_ | C._AS_LIST_ELEMENT_
            ret += str(Block(self.config, ctx))

        # ]
        ret += self.ctn.write(self.config.indent_char * indent_cnt + ustr(']' + tail + block_ending))

        return ret
Ejemplo n.º 3
0
    def build_block(self):
        indent_cnt = self.ctx.indent_cnt
        o = self.ctx.obj
        position = self.ctx.position

        debug(self.config, C._DL_FUNC_, indent_cnt,
              ('obj:{} indent_cnt:{} position:{:b}'.format(
                  o, indent_cnt, position)))
        ret = ustr('')

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending()
        debug(self.config, C._DL_STATEMENT, indent_cnt,
              'tail, block_ending: ' + str([tail, block_ending]))

        if self.config.tuple_in_line:
            _f = map(lambda e: not is_extendable(e), o)
            if all(_f):
                _o = map(lambda e: repr_block(e, self.config), o)
                if self.config.newline or position & C._AS_ELEMENT_:
                    ret += self.ctn.write(self.config.indent_char * indent_cnt)
                ret += self.ctn.write(ustr("("))
                for (i, e) in enumerate(_o):
                    if i:
                        ret += self.ctn.write(', ')
                    ret += self.ctn.write(e)
                ret += self.ctn.write(')' + tail + block_ending)
                return ret

        # (
        if self.config.newline or position & C._AS_ELEMENT_:
            ret += self.ctn.write(self.config.indent_char * indent_cnt +
                                  ustr('(\n'))
        else:
            ret += self.ctn.write(ustr('(\n'))

        # body
        for e in self.get_elements():
            ctx = self.ctx.clone()
            ctx.obj = e
            ctx.parent = self
            ctx.position = C._AS_ELEMENT_ | C._AS_TUPLE_ELEMENT_
            ctx.indent_cnt += 1
            ret += str(Block(self.config, ctx))

        # )
        ret += self.ctn.write(self.config.indent_char * indent_cnt +
                              ustr(')' + tail + block_ending))

        return ret
Ejemplo n.º 4
0
    def build_block(self):
        indent_cnt = self.ctx.indent_cnt
        o = self.ctx.obj
        position = self.ctx.position
        ret = ustr('')

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending()
        debug(self.config, C._DL_FUNC_, indent_cnt,
              'tail, block_ending: ' + str([tail, block_ending]))

        '所有元素显示在同一行'
        if self.config.list_in_line:
            _f = map(
                lambda e: (not (is_extendable(e) or too_long(
                    self.config.indent_char, indent_cnt, position,
                    repr_block(e, self.config)))), o)
            if all(_f):
                _o = map(lambda e: repr_block(e, self.config), o)
                if self.config.newline or position & C._AS_ELEMENT_:
                    ret += self.ctn.write(self.config.indent_char * indent_cnt)
                ret += self.ctn.write("[")
                for (i, e) in enumerate(_o):
                    if i:
                        ret += self.ctn.write(', ')
                    ret += self.ctn.write(e)
                ret += self.ctn.write("]" + tail + block_ending)
                return ret

        # [
        if self.config.newline or position & C._AS_ELEMENT_:
            ret += self.ctn.write(self.config.indent_char * indent_cnt +
                                  ustr('[') + ustr('\n'))
        else:
            ret += self.ctn.write(ustr('[') + ustr('\n'))

        # body
        for e in o:
            ctx = self.ctx.clone()
            ctx.obj = e
            ctx.parent = self
            ctx.indent_cnt = self.ctx.indent_cnt + 1
            ctx.position = C._AS_ELEMENT_ | C._AS_LIST_ELEMENT_
            ret += str(Block(self.config, ctx))

        # ]
        ret += self.ctn.write(self.config.indent_char * indent_cnt +
                              ustr(']' + tail + block_ending))

        return ret
Ejemplo n.º 5
0
    def build_block(self):
        indent_cnt = self.ctx.indent_cnt
        o = self.ctx.obj
        position = self.ctx.position

        debug(self.config, C._DL_FUNC_, 
              indent_cnt,
              ('obj:{} indent_cnt:{} position:{:b}'.format(
                  o, indent_cnt, position)))
        ret = ustr('')

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending()
        debug(self.config, C._DL_STATEMENT, indent_cnt, 'tail, block_ending: ' + str([tail, block_ending]))

        if self.config.tuple_in_line:
            _f = map(lambda e: not is_extendable(e), o)
            if all(_f):
                _o = map(lambda e: repr_block(e, self.config), o)
                if self.config.newline or position & C._AS_ELEMENT_:
                    ret += self.ctn.write(self.config.indent_char * indent_cnt)
                ret += self.ctn.write(ustr("("))
                for (i, e) in enumerate(_o):
                    if i:
                        ret += self.ctn.write(', ')
                    ret += self.ctn.write(e)
                ret += self.ctn.write(')' + tail + block_ending)
                return ret

        # (
        if self.config.newline or position & C._AS_ELEMENT_:
            ret += self.ctn.write(self.config.indent_char * indent_cnt + ustr('(\n'))
        else:
            ret += self.ctn.write(ustr('(\n'))

        # body
        for e in self.get_elements():
            ctx = self.ctx.clone()
            ctx.obj = e
            ctx.parent = self
            ctx.position = C._AS_ELEMENT_ | C._AS_TUPLE_ELEMENT_
            ctx.indent_cnt += 1
            ret += str(Block(self.config, ctx))

        # )
        ret += self.ctn.write(self.config.indent_char * indent_cnt + ustr(')' + tail + block_ending))

        return ret
Ejemplo n.º 6
0
    def build_block(self):
        """遍历对象,判断对象内成员的类型,然后构造对应的 *Block"""

        indent_cnt = self.ctx.indent_cnt
        obj = self.ctx.obj
        position = self.ctx.position

        debug(self.config, C._DL_FUNC_, 
              indent_cnt,
              ('obj:{} indent_cnt:{} position:{:b}'.format(
                  obj, indent_cnt, position)))

        ret = ustr('')

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending()
        debug(self.config, C._DL_STATEMENT, 
              indent_cnt, 'tail, block_ending: ' + str([tail, block_ending]))

        # recursive check
        oid = id(obj)
        if oid in self.ctx.obj_ids:
            if self.config.newline or position & C._AS_ELEMENT_:
                ret = ustr(indent_cnt * self.config.indent_char)
            else:
                ret = ustr("")

            if is_base_type(obj):
                typename = '%s' % get_name(obj)
            else:
                typename = '%s(%s)' % (get_type(obj), get_name(obj))
            ret += ustr("<Recursion on %s with id=%d>" % (typename, oid))
            ret += tail + block_ending
            return self.ctn.write(ret)
        self.ctx.obj_ids = self.ctx.obj_ids.copy()
        self.ctx.obj_ids.add(oid)

        if self.config.max_depth <= indent_cnt:
            if self.config.newline or position & C._AS_ELEMENT_:
                ret = ustr(indent_cnt * self.config.indent_char)
            else:
                ret = ustr(" ")

            rb = ReprBlock(self.config, self.ctx, handlers=[
                ReprStringHandlerInlineRepr(self.config), 
                ReprOthersHandlerInlineRepr(self.config)])
            ret += str(rb) + tail + '\n'
            return self.ctn.write(ret)

        if isinstance(obj, dict):
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is dict')
            ret += str(DictBlock(self.config, self.ctx))
        elif isinstance(obj, list):
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is list')
            ret += str(ListBlock(self.config, self.ctx))
        elif isinstance(obj, tuple):
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is tuple')
            ret += str(TupleBlock(self.config, self.ctx))
        elif is_extendable(obj):
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is extendable')
            ret += str(ClassBlock(self.config, self.ctx))
        else:
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is simple type')
            rb = ReprBlock(self.config, self.ctx, handlers=[
                ReprStringHandlerMultiLiner(self.config), 
                ReprOthersHandler(self.config)])
            ret += self.ctn.write(indent_cnt * self.config.indent_char + str(rb) + ustr(tail + '\n'))

        return ret
Ejemplo n.º 7
0
    def build_block(self):
        """遍历对象,判断对象内成员的类型,然后构造对应的 *Block"""

        indent_cnt = self.ctx.indent_cnt
        obj = self.ctx.obj
        position = self.ctx.position

        debug(self.config, C._DL_FUNC_, indent_cnt,
              ('obj:{} indent_cnt:{} position:{:b}'.format(
                  obj, indent_cnt, position)))

        ret = ustr('')

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending()
        debug(self.config, C._DL_STATEMENT, indent_cnt,
              'tail, block_ending: ' + str([tail, block_ending]))

        # recursive check
        oid = id(obj)
        if oid in self.ctx.obj_ids:
            if self.config.newline or position & C._AS_ELEMENT_:
                ret = ustr(indent_cnt * self.config.indent_char)
            else:
                ret = ustr("")

            if is_base_type(obj):
                typename = '%s' % get_name(obj)
            else:
                typename = '%s(%s)' % (get_type(obj), get_name(obj))
            ret += ustr("<Recursion on %s with id=%d>" % (typename, oid))
            ret += tail + block_ending
            return self.ctn.write(ret)
        self.ctx.obj_ids = self.ctx.obj_ids.copy()
        self.ctx.obj_ids.add(oid)

        if self.config.max_depth <= indent_cnt:
            if self.config.newline or position & C._AS_ELEMENT_:
                ret = ustr(indent_cnt * self.config.indent_char)
            else:
                ret = ustr(" ")

            rb = ReprBlock(self.config,
                           self.ctx,
                           handlers=[
                               ReprStringHandlerInlineRepr(self.config),
                               ReprOthersHandlerInlineRepr(self.config)
                           ])
            ret += str(rb) + tail + '\n'
            return self.ctn.write(ret)

        if isinstance(obj, dict):
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is dict')
            ret += str(DictBlock(self.config, self.ctx))
        elif isinstance(obj, list):
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is list')
            ret += str(ListBlock(self.config, self.ctx))
        elif isinstance(obj, tuple):
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is tuple')
            ret += str(TupleBlock(self.config, self.ctx))
        elif is_extendable(obj):
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is extendable')
            ret += str(ClassBlock(self.config, self.ctx))
        else:
            debug(self.config, C._DL_STATEMENT, indent_cnt, 'is simple type')
            rb = ReprBlock(self.config,
                           self.ctx,
                           handlers=[
                               ReprStringHandlerMultiLiner(self.config),
                               ReprOthersHandler(self.config)
                           ])
            ret += self.ctn.write(indent_cnt * self.config.indent_char +
                                  str(rb) + ustr(tail + '\n'))

        return ret
Ejemplo n.º 8
0
    def build_block(self):
        indent_cnt = self.ctx.indent_cnt
        name, val = self.ctx.obj
        position = self.ctx.position

        debug(self.config, C._DL_FUNC_, indent_cnt,
              ('key:{}, indent_cnt:{}, position:{:b}'.format(
                  name, indent_cnt, position)))
        ret = ustr('')

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending(val)
        debug(self.config, C._DL_STATEMENT, indent_cnt,
              'tail, block_ending: ' + str([tail, block_ending]))

        key = self.ctx.key_expr

        ret += self.ctn.write(self.config.indent_char * indent_cnt + key + ':')
        if is_extendable(val) and self.config.max_depth > indent_cnt:
            # still in of range, and can be expanded

            # value need to be dispalyed on new line
            # including:
            #   class type & class instance
            #   function type
            if self.config.newline or (is_newline_obj(val)
                                       and position & C._AS_ELEMENT_):
                ret += self.ctn.write(ustr('\n'))
                indent_cnt = indent_cnt + 1
                position = C._AS_ELEMENT_
                debug(self.config, C._DL_STATEMENT, indent_cnt, 'make newline')
            # value will be dispalyed immediately after one space
            else:
                ret += self.ctn.write(ustr(" "))
                position = C._AS_VALUE_

            ctx = self.ctx.clone()
            ctx.obj = val
            ctx.parent = self
            ctx.position = position
            ctx.indent_cnt = indent_cnt
            ret += str(Block(self.config, ctx))
            ret += self.ctn.write(tail + block_ending)
        else:
            ctx = self.ctx.clone()
            ctx.obj = val
            if self.config.max_depth <= indent_cnt:
                # reach max_depth, just show brief message
                rb = ReprBlock(self.config,
                               ctx,
                               handlers=[
                                   ReprStringHandlerInlineRepr(self.config),
                                   ReprOthersHandlerInlineRepr(self.config)
                               ])
                ret += self.ctn.write(ustr(" " + str(rb) + tail +
                                           block_ending))
            else:
                rb = ReprBlock(self.config,
                               ctx,
                               handlers=[
                                   ReprStringHandlerMultiLiner(self.config),
                                   ReprOthersHandler(self.config)
                               ])
                ret += self.ctn.write(
                    ustr(" ") + str(rb) + ustr(tail + block_ending))

        return ret