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):
        """遍历对象,判断对象内成员的类型,然后构造对应的 *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 #4
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