File size: 4,060 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
# -*- 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/>.
#
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from argparse import ArgumentParser
import gettext
import logging
import os
import sys

from . import __version__
from .cli import init_logger
from .dataio import ParseError
from .errors import InvalidHwp5FileError


PY3 = sys.version_info.major == 3
logger = logging.getLogger(__name__)

locale_dir = os.path.join(os.path.dirname(__file__), 'locale')
locale_dir = os.path.abspath(locale_dir)
t = gettext.translation('hwp5proc', locale_dir, fallback=True)
_ = t.gettext


program = 'hwp5proc (pyhwp) {version}'.format(version=__version__)

copyright = 'Copyright (C) 2010-2023 mete0r <https://github.com/mete0r>'

license = _('''License AGPLv3+: GNU Affero GPL version 3 or any later
<http://gnu.org/licenses/agpl.txt>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.''')

disclosure = _('''Disclosure: This program has been developed in accordance with a public
document named "HWP Binary Specification 1.1" published by Hancom Inc.
<http://www.hancom.co.kr>.''')  # noqa

version = '''{program}
{copyright}
{license}
{disclosure}'''.format(
    program=program,
    copyright=copyright,
    license=license,
    disclosure=disclosure,
)


def main():
    argparser = main_argparser()
    args = argparser.parse_args()
    init_logger(args)

    try:
        subcommand_fn = args.func
    except AttributeError:
        argparser.print_help()
        raise SystemExit(1)

    try:
        return subcommand_fn(args)
    except InvalidHwp5FileError as e:
        logger.error('%s', e)
        raise SystemExit(1)
    except ParseError as e:
        e.print_to_logger(logger)
        raise SystemExit(1)


def main_argparser():
    from .proc.version import version_argparser
    from .proc.header import header_argparser
    from .proc.summaryinfo import summaryinfo_argparser
    from .proc.ls import ls_argparser
    from .proc.cat import cat_argparser
    from .proc.unpack import unpack_argparser
    from .proc.records import records_argparser
    from .proc.models import models_argparser
    from .proc.find import find_argparser
    from .proc.xml import xml_argparser
    from .proc.rawunz import rawunz_argparser
    from .proc.diststream import diststream_argparser
    parser = ArgumentParser(
        prog='hwp5proc',
        description=_('Do various operations on HWPv5 files.'),
    )
    parser.add_argument(
        '--loglevel',
        help=_('Set log level.'),
    )
    parser.add_argument(
        '--logfile',
        help=_('Set log file.'),
    )
    subcommands = parser.add_subparsers(
        title=_('subcommands'),
        description=_('valid subcommands'),
    )
    version_argparser(subcommands, _)
    header_argparser(subcommands, _)
    summaryinfo_argparser(subcommands, _)
    ls_argparser(subcommands, _)
    cat_argparser(subcommands, _)
    unpack_argparser(subcommands, _)
    records_argparser(subcommands, _)
    models_argparser(subcommands, _)
    find_argparser(subcommands, _)
    xml_argparser(subcommands, _)
    rawunz_argparser(subcommands, _)
    diststream_argparser(subcommands, _)
    return parser