示例#1
0
class ReformatContentHandler(YajlContentHandler):
    '''
    Content handler to reformat a json file using yajl_gen
    '''
    def __init__(self, beautify=True, indent_string=b"  ", stream=False):
        self.out = sys.stdout
        self.beautify = beautify
        self.indent_string = indent_string
        self.stream = stream

    def parse_start(self):
        self.g = YajlGen(
            beautify=self.beautify,
            indent_string=self.indent_string,
        )

    def parse_buf(self):
        self.out.write(self.g.yajl_gen_get_buf().decode('utf-8'))

    def parse_complete(self):
        if not stream:
            # not necessary, gc will do this @ python shutdown
            del self.g

    def check_and_return(self, func, *args, **kwargs):
        try:
            func(*args, **kwargs)
        except YajlGenException as e:
            if self.stream and e.value == 'yajl_gen_generation_complete':
                self.g.yajl_gen_reset(b'\n')
                func(*args, **kwargs)
            else:
                raise

    def yajl_null(self, ctx):
        self.check_and_return(self.g.yajl_gen_null, )

    def yajl_boolean(self, ctx, boolVal):
        self.check_and_return(self.g.yajl_gen_bool, boolVal)

    def yajl_number(self, ctx, stringNum):
        self.check_and_return(self.g.yajl_gen_number, stringNum)

    def yajl_string(self, ctx, stringVal):
        self.check_and_return(self.g.yajl_gen_string, stringVal)

    def yajl_start_map(self, ctx):
        self.check_and_return(self.g.yajl_gen_map_open, )

    def yajl_map_key(self, ctx, stringVal):
        self.check_and_return(self.g.yajl_gen_string, stringVal)

    def yajl_end_map(self, ctx):
        self.check_and_return(self.g.yajl_gen_map_close, )

    def yajl_start_array(self, ctx):
        self.check_and_return(self.g.yajl_gen_array_open, )

    def yajl_end_array(self, ctx):
        self.check_and_return(self.g.yajl_gen_array_close, )
示例#2
0
    def __init__(self, output, error_level):
        #        try:
        #            self.output = open(output, "w")
        #        except IOError:
        #            print('cannot open file')
        #       sys.exit(1)
        self.output = output
        self.filetype = 0
        try:
            output.write()
        except AttributeError:
            self.output = open(output, 'w')
            self.filetype = 1

        if error_level == 1 or error_level == 0:
            self.error_level = error_level
        else:
            self.error_level = 2

        self.pretty = 0
        self.writer = YajlGen(beautify=False)
        self.curr = "initial"

        self.bugID = 1
        self.metricID = 1

        self.start = 0
        self.metricSummaries = {}
        self.bugSummaries = {}
示例#3
0
 def parse_start(self):
     self.g = YajlGen(
         beautify=self.beautify,
         indent_string=self.indent_string,
     )
示例#4
0
class ReformatContentHandler(YajlContentHandler):
    '''
    Content handler to reformat a json file using yajl_gen
    '''
    def __init__(self, beautify=True, indent_string="  ", stream=False):
        self.out = sys.stdout
        self.beautify = beautify
        self.indent_string = indent_string
        self.stream = stream
    def parse_start(self):
        self.g = YajlGen(
            beautify=self.beautify,
            indent_string=self.indent_string,
        )
    def parse_buf(self):
        self.out.write(self.g.yajl_gen_get_buf())
    def parse_complete(self):
        if not stream:
            # not necessary, gc will do this @ python shutdown
            del self.g
    def check_and_return(self, func, *args, **kwargs):
        try:
            func(*args, **kwargs)
        except YajlGenException as e:
            if self.stream and e.value == 'yajl_gen_generation_complete':
                self.g.yajl_gen_reset('\n')
                func(*args, **kwargs)
            else:
                raise
    def yajl_null(self, ctx):
        self.check_and_return(
            self.g.yajl_gen_null,
        )
    def yajl_boolean(self, ctx, boolVal):
        self.check_and_return(
            self.g.yajl_gen_bool,
            boolVal
        )
    def yajl_number(self, ctx, stringNum):
        self.check_and_return(
            self.g.yajl_gen_number,
            stringNum
        )
    def yajl_string(self, ctx, stringVal):
        self.check_and_return(
            self.g.yajl_gen_string,
            stringVal
        )
    def yajl_start_map(self, ctx):
        self.check_and_return(
            self.g.yajl_gen_map_open,
        )
    def yajl_map_key(self, ctx, stringVal):
        self.check_and_return(
            self.g.yajl_gen_string,
            stringVal
        )
    def yajl_end_map(self, ctx):
        self.check_and_return(
            self.g.yajl_gen_map_close,
        )
    def yajl_start_array(self, ctx):
        self.check_and_return(
            self.g.yajl_gen_array_open,
        )
    def yajl_end_array(self, ctx):
        self.check_and_return(
            self.g.yajl_gen_array_close,
        )
示例#5
0
 def parse_start(self):
     self.g = YajlGen(
         beautify=self.beautify,
         indent_string=self.indent_string,
     )
示例#6
0
 def setPrettyPrint(self, pretty_enable):
     self.pretty = pretty_enable
     if self.pretty:
         self.writer = YajlGen(beautify=True)
     else:
         self.writer = YajlGen(beautify=False)