def print_model(model): printable_model = dict(model, type=model['type'].__name__) print fmt % printable_model if dump: from hwp5.binmodel import model_to_json print model_to_json(model, sort_keys=True, indent=2) def print_log(fmt, *args): print fmt % args from hwp5.bintype import log_events list(log_events(model['binevents'], print_log))
def print_models_json(models): from hwp5.binmodel import model_to_json from hwp5.utils import generate_json_array jsonobjects = (model_to_json(model, sort_keys=True, indent=2) for model in models) for s in generate_json_array(jsonobjects): sys.stdout.write(s)
def test_model_to_json_with_unparsed(self): model = dict(type=RecordModel, content=[], payload='\x00\x01\x02\x03', unparsed='\xff\xfe\xfd\xfc') json_string = model_to_json(model) jsonobject = json.loads(json_string) self.assertEquals(['ff fe fd fc'], jsonobject['unparsed'])
def test_model_to_json(self): from hwp5.binmodel import model_to_json model = self.hwp5file.docinfo.model(0) json = model_to_json(model) simplejson = importjson() jsonobject = simplejson.loads(json) self.assertEquals('DocumentProperties', jsonobject['type'])
def test_model_to_json_with_unparsed(self): model = dict(type=RecordModel, content=[], payload=b'\x00\x01\x02\x03', unparsed=b'\xff\xfe\xfd\xfc') json_string = model_to_json(model) jsonobject = json.loads(json_string) self.assertEqual(['ff fe fd fc'], jsonobject['unparsed'])
def test_model_to_json_with_controlchar(self): model = self.hwp5file.bodytext.section(0).model(1) json_string = model_to_json(model) jsonobject = json.loads(json_string) self.assertEqual('ParaText', jsonobject['type']) self.assertEqual([[0, 8], dict(code=2, param='\x00' * 8, chid='secd')], jsonobject['content']['chunks'][0])
def test_model_to_json_with_controlchar(self): model = self.hwp5file.bodytext.section(0).model(1) json_string = model_to_json(model) jsonobject = json.loads(json_string) self.assertEqual('ParaText', jsonobject['type']) self.assertEqual( [[0, 8], dict(code=2, param='\x00' * 8, chid='secd')], jsonobject['content']['chunks'][0])
def test_model_to_json_with_unparsed(self): from hwp5.binmodel import model_to_json model = dict(type=RecordModel, content=[], payload='\x00\x01\x02\x03', unparsed='\xff\xfe\xfd\xfc') json = model_to_json(model) simplejson = importjson() jsonobject = simplejson.loads(json) self.assertEquals(['ff fe fd fc'], jsonobject['unparsed'])
def test_model_to_json_with_controlchar(self): from hwp5.binmodel import model_to_json model = self.hwp5file.bodytext.section(0).model(1) json = model_to_json(model) simplejson = importjson() jsonobject = simplejson.loads(json) self.assertEquals('ParaText', jsonobject['type']) self.assertEquals( [[0, 8], dict(code=2, param='\x00' * 8, chid='secd')], jsonobject['content']['chunks'][0])
def test_model_to_json_with_controlchar(self): from hwp5.binmodel import model_to_json model = self.hwp5file.bodytext.section(0).model(1) json = model_to_json(model) simplejson = importjson() jsonobject = simplejson.loads(json) self.assertEquals('ParaText', jsonobject['type']) self.assertEquals([[0, 8], dict(code=2, param='\x00' * 8, chid='secd')], jsonobject['content']['chunks'][0])
def main(args): filenames = args['<hwp5files>'] from hwp5.dataio import ParseError from hwp5.binmodel import Hwp5File conditions = [] if args['--model']: def with_model_name(model): return args['--model'] == model['type'].__name__ conditions.append(with_model_name) if args['--tag']: tag = args['--tag'] try: tag = int(tag) except ValueError: pass else: from hwp5.tagids import tagnames tag = tagnames[tag] def with_tag(model): return model['tagname'] == tag conditions.append(with_tag) if args['--incomplete']: def with_incomplete(model): return 'unparsed' in model conditions.append(with_incomplete) def flat_models(hwp5file, **kwargs): for model in hwp5file.docinfo.models(**kwargs): model['stream'] = 'DocInfo' yield model for section in hwp5file.bodytext: for model in hwp5file.bodytext[section].models(**kwargs): model['stream'] = 'BodyText/'+section yield model for filename in filenames: try: hwp5file = Hwp5File(filename) def with_filename(models): for model in models: model['filename'] = filename yield model models = flat_models(hwp5file) models = with_filename(models) for model in models: if all(condition(model) for condition in conditions): print '%s:%s(%s): %s' % (model['filename'], model['stream'], model['seqno'], model['type'].__name__) if args['--dump']: from hwp5.binmodel import model_to_json print model_to_json(model, sort_keys=True, indent=2) def print_log(fmt, *args): print fmt % args from hwp5.bintype import log_events list(log_events(model['binevents'], print_log)) except ParseError, e: from hwp5.proc import logger logger.error('---- On processing %s:', filename) e.print_to_logger(logger)
def test_model_to_json_should_not_modify_input(self): from hwp5.binmodel import model_to_json model = self.hwp5file.docinfo.model(0) model_to_json(model, indent=2, sort_keys=True) self.assertFalse(isinstance(model['type'], basestring))
def test_model_to_json(self): model = self.hwp5file.docinfo.model(0) json_string = model_to_json(model) jsonobject = json.loads(json_string) self.assertEqual('DocumentProperties', jsonobject['type'])
def test_model_to_json_should_not_modify_input(self): model = self.hwp5file.docinfo.model(0) model_to_json(model, indent=2, sort_keys=True) self.assertFalse(isinstance(model['type'], basestring))