File size: 4,818 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
# -*- 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
import io
import os.path
import sys


PY3 = sys.version_info.major == 3
if PY3:
    basestring = str


def is_storage(item):
    return hasattr(item, '__iter__') and hasattr(item, '__getitem__')


def is_stream(item):
    return hasattr(item, 'open') and callable(item.open)


class ItemWrapper(object):
    def __init__(self, wrapped):
        self.wrapped = wrapped

    def __getattr__(self, name):
        return getattr(self.wrapped, name)


class StorageWrapper(ItemWrapper):
    def __iter__(self):
        return iter(self.wrapped)

    def __getitem__(self, name):
        return self.wrapped[name]


class ItemConversionStorage(StorageWrapper):

    def __getitem__(self, name):
        item = self.wrapped[name]
        # 기반 스토리지에서 찾은 아이템에 대해, conversion()한다.
        conversion = self.resolve_conversion_for(name)
        if conversion:
            return conversion(item)
        return item

    def resolve_conversion_for(self, name):
        ''' return a conversion function for the specified storage item '''
        pass


class ExtraItemStorage(StorageWrapper):

    def __iter__(self):
        for name in self.wrapped:
            yield name

            item = self.wrapped[name]
            if hasattr(item, 'other_formats'):
                other_formats = item.other_formats()
                if other_formats:
                    for ext in other_formats:
                        yield name + ext

    def __getitem__(self, name):
        try:
            item = self.wrapped[name]
            if is_storage(item):
                item = ExtraItemStorage(item)
            return item
        except KeyError:
            # 기반 스토리지에는 없으므로, other_formats() 중에서 찾아본다.
            for root in self.wrapped:
                item = self.wrapped[root]
                if hasattr(item, 'other_formats'):
                    other_formats = item.other_formats()
                    if other_formats:
                        for ext, func in other_formats.items():
                            if root + ext == name:
                                return Open2Stream(func)
            raise


class Open2Stream(object):

    def __init__(self, open):
        self.open = open


def iter_storage_leafs(stg, basepath=''):
    ''' iterate every leaf nodes in the storage

        stg: an instance of Storage
    '''
    for name in stg:
        path = basepath + name
        item = stg[name]
        if is_storage(item):
            for x in iter_storage_leafs(item, path + '/'):
                yield x
        else:
            yield path


def unpack(stg, outbase):
    ''' unpack a storage into outbase directory

        stg: an instance of Storage
        outbase: path to a directory in filesystem (should not end with '/')
    '''
    for name in stg:
        outpath = os.path.join(outbase, name)
        item = stg[name]
        if is_storage(item):
            if not os.path.exists(outpath):
                os.mkdir(outpath)
            unpack(item, outpath)
        else:
            f = item.open()
            try:
                outpath = outpath.replace('\x05', '_05')
                with io.open(outpath, 'wb') as outfile:
                    outfile.write(f.read())
            finally:
                f.close()


def open_storage_item(stg, path):
    if isinstance(path, basestring):
        path_segments = path.split('/')
    else:
        path_segments = path

    item = stg
    for name in path_segments:
        item = item[name]
    return item


def printstorage(stg, basepath=''):
    names = list(stg)
    names.sort()
    for name in names:
        path = basepath + name
        item = stg[name]
        if is_storage(item):
            printstorage(item, path + '/')
        elif is_stream(item):
            print(path.encode('unicode_escape').decode('utf-8'))