summaryrefslogtreecommitdiffstats
path: root/src/python/orcus/tools/file_processor.py
blob: 472fea33594cf8dee1a6ecb381047a92af72f342 (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
########################################################################
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
########################################################################

import argparse
import os
import os.path
import sys
import string
import pathlib
import enum
import re
import multiprocessing as mp
import importlib.util

import orcus


class _Config:
    ext_good = "orcus-pf.good"
    ext_bad = "orcus-pf.bad"
    ext_out = "orcus-pf.out"
    prefix_skip = ".orcus-pf.skip."


config = _Config()


def is_special_file(filename):
    if filename.find(config.prefix_skip) >= 0:
        return True

    return filename.endswith(config.ext_out) or filename.endswith(config.ext_good) or filename.endswith(config.ext_bad)


def skips_by_rule(filename, skip_rules):
    for rule in skip_rules:
        if rule.search(filename):
            return True
    return False


def sanitize_string(s):
    """Replace non-printable characters with \\x[value]."""

    buf = list()
    for c in s:
        if c in string.printable:
            buf.append(c)
        else:
            buf.append(f"\\x{ord(c):02X}")

    return "".join(buf)


class LoadStatus(enum.Enum):
    SUCCESS = 0
    FAILURE = 1
    SKIPPED = 2


def load_doc(bytes):

    buf = list()

    try:
        format_type = orcus.detect_format(bytes)
    except Exception as e:
        buf.append(str(e))
        status = LoadStatus.SKIPPED
        return None, status, buf

    buf.append(f"* format type: {format_type}")
    buf.append(f"* size: {len(bytes)} bytes")

    doc = None

    try:
        loader = orcus.get_document_loader_module(format_type)
        if loader is None:
            buf.append(f"unhandled format type: {format_type}")
            status = LoadStatus.SKIPPED
            return doc, status, buf

        status = LoadStatus.SUCCESS
        doc = loader.read(bytes, error_policy="skip")
        return doc, status, buf

    except Exception as e:
        buf.append(f"{e.__class__.__name__}: {e}")
        status = LoadStatus.FAILURE
        return None, status, buf


def print_results(inpath):
    outpath = f"{inpath}.{config.ext_out}"
    with open(outpath, "r") as f:
        print()
        for line in f.readlines():
            print(f"  {line.strip()}")
        print()


def remove_result_files(rootdir):
    for root, dir, files in os.walk(rootdir):
        for filename in files:
            if is_special_file(filename):
                filepath = os.path.join(root, filename)
                os.remove(filepath)


def show_result_stats(rootdir):
    counts = dict(good=0, bad=0, skipped=0, unprocessed=0)
    for root, dir, files in os.walk(rootdir):
        for filename in files:
            if is_special_file(filename):
                continue

            inpath = os.path.join(root, filename)
            out_filepath = f"{inpath}.{config.ext_out}"
            good_filepath = f"{inpath}.{config.ext_good}"
            bad_filepath = f"{inpath}.{config.ext_bad}"
            if os.path.isfile(good_filepath):
                counts["good"] += 1
            elif os.path.isfile(bad_filepath):
                counts["bad"] += 1
            elif os.path.isfile(out_filepath):
                counts["skipped"] += 1
            else:
                counts["unprocessed"] += 1

    print("* result counts")
    for cat in ("good", "bad", "skipped", "unprocessed"):
        print(f"  * {cat}: {counts[cat]}")

    total = counts["good"] + counts["bad"]
    if total:
        print("* ratios")
        print(f"  * good: {counts['good']/total*100:.1f}%")
        print(f"  * bad: {counts['bad']/total*100:.1f}%")


def show_results(rootdir, good, bad):
    for root, dir, files in os.walk(rootdir):
        for filename in files:
            if is_special_file(filename):
                continue
            inpath = os.path.join(root, filename)
            good_filepath = f"{inpath}.{config.ext_good}"
            bad_filepath = f"{inpath}.{config.ext_bad}"

            if os.path.isfile(good_filepath) and good:
                print(sanitize_string(inpath), flush=True)
                print_results(inpath)
            elif os.path.isfile(bad_filepath) and bad:
                print(sanitize_string(inpath), flush=True)
                print_results(inpath)
            else:
                continue


def load_module_from_filepath(filepath):
    if not os.path.isfile(filepath):
        raise RuntimeError(f"{filepath} is not a valid file.")

    mod_name = os.path.splitext(os.path.basename(filepath))[0]
    mod_name = mod_name.replace('-', '_')
    spec = importlib.util.spec_from_file_location(mod_name, filepath)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod


def process_filepath(i, inpath, outpath, processor_path):
    mod = load_module_from_filepath(processor_path) if processor_path else None
    term_buf = list()  # terminal output buffer
    term_buf.append(f"{i} {sanitize_string(inpath)}")

    good_filepath = f"{inpath}.{config.ext_good}"
    bad_filepath = f"{inpath}.{config.ext_bad}"

    if os.path.isfile(good_filepath) or os.path.isfile(bad_filepath):
        term_buf.append("already processed. skipping...")
        return "\n".join(term_buf)

    success = False
    with open(inpath, 'rb') as f:
        bytes = f.read()

    buf = list()  # non-terminal output buffer
    doc, status, output = load_doc(bytes)
    buf.extend(output)
    if doc and mod:
        buf.extend(mod.process_document(inpath, doc))

    with open(outpath, "w") as f:
        f.write("\n".join(buf))

    term_buf.extend(buf)

    if status == LoadStatus.SUCCESS:
        pathlib.Path(good_filepath).touch()
    elif status == LoadStatus.FAILURE:
        pathlib.Path(bad_filepath).touch()

    return "\n".join(term_buf)


def _create_argparser():
    parser = argparse.ArgumentParser(
        description="""This script allows you to process a collection of spreadsheet documents.""")
    parser.add_argument(
        "--skip-file", type=argparse.FileType("r"),
        help="Optional text file containing a set of regular expressions (one per line). Files that match one of these rules will be skipped.")
    parser.add_argument("--processes", type=int, default=1, help="Number of worker processes to use.")
    parser.add_argument("-p", "--processor", type=str, help="Python module file containing callback functions.")
    parser.add_argument(
        "--remove-results", action="store_true", default=False,
        help="Remove all cached results files from the directory tree.")
    parser.add_argument(
        "--results", action="store_true", default=False,
        help="Display the results of the processed files.")
    parser.add_argument(
        "--good", action="store_true", default=False,
        help="Display the results of the successfully processed files.")
    parser.add_argument(
        "--bad", action="store_true", default=False,
        help="Display the results of the unsuccessfully processed files.")
    parser.add_argument(
        "--stats", action="store_true", default=False,
        help="Display statistics of the results.  Use it with --results.")
    parser.add_argument(
        "rootdir", metavar="ROOT-DIR",
        help="Root directory below which to recursively find and process test files.")
    return parser


def main():
    parser = _create_argparser()
    args = parser.parse_args()

    if args.remove_results:
        remove_result_files(args.rootdir)
        return

    if args.results:
        if args.stats:
            show_result_stats(args.rootdir)
            return

        show_results(args.rootdir, args.good, args.bad)
        return

    skip_rules = list()

    if args.skip_file:
        for line in args.skip_file.readlines():
            line = line.strip()
            if not line:
                continue
            rule = re.compile(line)
            skip_rules.append(rule)

    # build a list of files to process.
    filepaths = list()
    for root, dir, files in os.walk(args.rootdir):
        for filename in files:
            if is_special_file(filename):
                continue

            inpath = os.path.join(root, filename)
            outpath = f"{inpath}.{config.ext_out}"
            if skips_by_rule(inpath, skip_rules):
                pathlib.Path(outpath).touch()
                continue

            filepaths.append((inpath, outpath))

    with mp.Pool(processes=args.processes) as pool:
        futures = list()
        for i, (inpath, outpath) in enumerate(filepaths):
            future = pool.apply_async(process_filepath, (i, inpath, outpath, args.processor))
            futures.append(future)

        for future in futures:
            output = future.get()
            print(output)


if __name__ == "__main__":
    main()