File size: 6,402 Bytes
d94b56e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | # -*- coding: utf-8 -*-
#
# pyhwp : hwp file format parser in python
# Copyright (C) 2010-2023 mete0r <https://github.com/mete0r>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
''' Find record models with specified predicates.
Usage::
hwp5proc find [--model=<model-name> | --tag=<hwptag>]
[--incomplete] [--dump] [--format=<format>]
[--loglevel=<loglevel>] [--logfile=<logfile>]
(--from-stdin | <hwp5files>...)
hwp5proc find --help
Options::
-h --help Show this screen
--loglevel=<level> Set log level.
--logfile=<file> Set log file.
--from-stdin get filenames fro stdin
--model=<model-name> filter with record model name
--tag=<hwptag> filter with record HWPTAG
--incomplete filter with incompletely parsed content
--format=<format> record output format
%(filename)s %(stream)s %(seqno)s %(type)s
--dump dump record
<hwp5files>... HWPv5 files (*.hwp)
Example: Find paragraphs::
$ hwp5proc find --model=Paragraph samples/*.hwp
$ hwp5proc find --tag=HWPTAG_PARA_TEXT samples/*.hwp
$ hwp5proc find --tag=66 samples/*.hwp
Example: Find and dump records of ``HWPTAG_LIST_HEADER`` which is parsed
incompletely::
$ hwp5proc find --tag=HWPTAG_LIST_HEADER --incomplete --dump samples/*.hwp
'''
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from functools import partial
import logging
import itertools
import sys
from ..binmodel import Hwp5File
from ..binmodel import model_to_json
from ..bintype import log_events
from ..dataio import ParseError
from ..tagids import tagnames
PY2 = sys.version_info.major == 2
if PY2:
ifilter = itertools.ifilter
imap = itertools.imap
else:
ifilter = filter
imap = map
logger = logging.getLogger(__name__)
def main(args):
filenames = filenames_from_args(args)
conditions = list(conditions_from_args(args))
filter_conditions = partial(
ifilter, lambda m: all(condition(m) for condition in conditions)
)
print_model = printer_from_args(args)
for filename in filenames:
try:
models = hwp5file_models(filename)
models = filter_conditions(models)
for model in models:
print_model(model)
except ParseError as e:
logger.error('---- On processing %s:', filename)
e.print_to_logger(logger)
def find_argparser(subparsers, _):
parser = subparsers.add_parser(
'find',
help=_(
'Find record models with specified predicates.'
),
description=_(
'Find record models with specified predicates.'
),
)
parser.add_argument(
'hwp5files',
nargs='*',
metavar='<hwp5files>',
help=_('.hwp files to analyze'),
)
parser.add_argument(
'--from-stdin',
action='store_true',
help=_('get filenames from stdin'),
)
filter_group = parser.add_mutually_exclusive_group()
filter_group.add_argument(
'--model',
metavar='<model-name>',
help=_(
'filter with record model name'
),
)
filter_group.add_argument(
'--tag',
metavar='<hwptag>',
help=_(
'filter with record HWPTAG'
),
)
parser.add_argument(
'--incomplete',
action='store_true',
help=_('filter with incompletely parsed content'),
)
parser.add_argument(
'--format',
metavar='<format>',
help=_(
'record output format'
),
)
parser.add_argument(
'--dump',
action='store_true',
help=_('dump record'),
)
parser.set_defaults(func=main)
return parser
def filenames_from_args(args):
if args.from_stdin:
return filenames_from_stdin(args)
return args.hwp5files
def filenames_from_stdin(args):
return imap(lambda line: line[:-1], sys.stdin)
def conditions_from_args(args):
if args.model:
def with_model_name(model):
return args.model == model['type'].__name__
yield with_model_name
if args.tag:
tag = args.tag
try:
tag = int(tag)
except ValueError:
pass
else:
tag = tagnames[tag]
def with_tag(model):
return model['tagname'] == tag
yield with_tag
if args.incomplete:
def with_incomplete(model):
return 'unparsed' in model
yield with_incomplete
def hwp5file_models(filename):
hwp5file = Hwp5File(filename)
for model in flat_models(hwp5file):
model['filename'] = filename
yield model
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
def printer_from_args(args):
if args.format:
fmt = args.format
else:
fmt = '%(filename)s %(stream)s %(seqno)s %(tagname)s %(type)s'
dump = args.dump
def print_model(model):
printable_model = dict(model, type=model['type'].__name__)
print(fmt % printable_model)
if dump:
print(model_to_json(model, sort_keys=True, indent=2))
def print_log(fmt, *args):
print(fmt % args)
list(log_events(model['binevents'], print_log))
return print_model
|