Beispiel #1
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)))

        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]))

        # {
        _leading = ustr('')
        if position & C._AS_ELEMENT_:
            _leading += self.config.indent_char * indent_cnt
        elif position & C._AS_VALUE_:
            _leading += ustr('')

        name = get_name(o)
        label = get_type(o)

        ret = _leading
        if (self.config.instance_repr_enable and 
                is_class_instance(self.ctx.obj) and
                has_custom_repr(self.ctx.obj)):
            ret = self.ctn.write(ret + ustr(repr(self.ctx.obj)))
        else:
            ret += '%s(%s):' % (label, name) + '\n'

            # body
            ele_ctnr = self.get_elements()
            props_cnt = len(ele_ctnr)

            if props_cnt == 0:
                # right strip ':\n'
                ret = ret[:-2]

            ret = self.ctn.write(ret)

            for idx, key, val in ele_ctnr:
                # '忽略掉 以__开头的成员、自引用成员、函数成员'
                ctx = self.ctx.clone()
                ctx.obj = (key, val)
                ctx.parent = self
                ctx.position = C._AS_CLASS_ELEMENT_
                ctx.indent_cnt = self.ctx.indent_cnt + 1
                ret += str(PairBlock(self.config, ctx))

        # }
        ret += self.ctn.write(tail + block_ending)
        return ret
Beispiel #2
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)))

        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]))

        # {
        _leading = ustr('')
        if position & C._AS_ELEMENT_:
            _leading += self.config.indent_char * indent_cnt
        elif position & C._AS_VALUE_:
            _leading += ustr('')

        name = get_name(o)
        label = get_type(o)

        ret = _leading
        if (self.config.instance_repr_enable
                and is_class_instance(self.ctx.obj)
                and has_custom_repr(self.ctx.obj)):
            ret = self.ctn.write(ret + ustr(repr(self.ctx.obj)))
        else:
            ret += '%s(%s):' % (label, name) + '\n'

            # body
            ele_ctnr = self.get_elements()
            props_cnt = len(ele_ctnr)

            if props_cnt == 0:
                # right strip ':\n'
                ret = ret[:-2]

            ret = self.ctn.write(ret)

            for idx, key, val in ele_ctnr:
                # '忽略掉 以__开头的成员、自引用成员、函数成员'
                ctx = self.ctx.clone()
                ctx.obj = (key, val)
                ctx.parent = self
                ctx.position = C._AS_CLASS_ELEMENT_
                ctx.indent_cnt = self.ctx.indent_cnt + 1
                ret += str(PairBlock(self.config, ctx))

        # }
        ret += self.ctn.write(tail + block_ending)
        return ret
Beispiel #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
Beispiel #4
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
Beispiel #5
0
    def __call__(self, ctx, typ):
        ctx.obj = ustr(repr(ctx.obj))
        shrink_inner_string(ctx, 
                            self.config.string_break_method,
                            self.config.string_break_width,
                            None)

        return ctx.obj
Beispiel #6
0
    def get_key(self, position, name):
        if position & C._AS_CLASS_ELEMENT_:
            # class method name or attribute name no need to add u or b prefix
            key = ustr(name)
        else:
            key = repr_block(name, self.config)

        return key
Beispiel #7
0
    def get_key(self, position, name):
        if position & C._AS_CLASS_ELEMENT_:
            # class method name or attribute name no need to add u or b prefix
            key = ustr(name)
        else:
            key = repr_block(name, self.config)

        return key
Beispiel #8
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.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 k, v in self.get_elements():
            if dict_key_filter(o, k, v):
                continue
            ctx = self.ctx.clone()
            ctx.obj = (k, v)
            ctx.parent = self
            ctx.position = C._AS_DICT_ELEMENT_
            ctx.indent_cnt = self.ctx.indent_cnt + 1
            ret += str(PairBlock(self.config, ctx))

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

        return ret
Beispiel #9
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
Beispiel #10
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
Beispiel #11
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
Beispiel #12
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.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 k, v in self.get_elements():
            if dict_key_filter(o, k, v):
                continue
            ctx = self.ctx.clone()
            ctx.obj = (k, v)
            ctx.parent = self
            ctx.position = C._AS_DICT_ELEMENT_
            ctx.indent_cnt = self.ctx.indent_cnt + 1
            ret += str(PairBlock(self.config, ctx))

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

        return ret
Beispiel #13
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
Beispiel #14
0
 def __call__(self, ctx, typ):
     return ustr(repr(ctx.obj))
Beispiel #15
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
Beispiel #16
0
    def __call__(self, ctx, typ):
        ctx.obj = ustr(repr(ctx.obj))
        shrink_inner_string(ctx, self.config.string_break_method,
                            self.config.string_break_width, None)

        return ctx.obj
Beispiel #17
0
 def __call__(self, ctx, typ):
     return ustr(repr(ctx.obj))
Beispiel #18
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