|
1
|
|
|
# pylint: disable=missing-module-docstring,invalid-name |
|
2
|
|
|
from os.path import join, basename |
|
3
|
|
|
from pkg_resources import resource_string |
|
4
|
|
|
|
|
5
|
|
|
import click |
|
6
|
|
|
|
|
7
|
|
|
from ocrd import Processor |
|
8
|
|
|
from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor |
|
9
|
|
|
from ocrd_utils import ( |
|
10
|
|
|
getLogger, |
|
11
|
|
|
assert_file_grp_cardinality, |
|
12
|
|
|
make_file_id, |
|
13
|
|
|
MIME_TO_EXT, |
|
14
|
|
|
parse_json_string_with_comments |
|
15
|
|
|
) |
|
16
|
|
|
|
|
17
|
|
|
OCRD_TOOL = parse_json_string_with_comments(resource_string(__name__, 'dummy/ocrd-tool.json').decode('utf8')) |
|
18
|
|
|
|
|
19
|
|
|
LOG = getLogger('ocrd.dummy') |
|
20
|
|
|
|
|
21
|
|
|
class DummyProcessor(Processor): |
|
22
|
|
|
""" |
|
23
|
|
|
Bare-bones processor that copies mets:file from input group to output group. |
|
24
|
|
|
""" |
|
25
|
|
|
|
|
26
|
|
|
def process(self): |
|
27
|
|
|
assert_file_grp_cardinality(self.input_file_grp, 1) |
|
28
|
|
|
assert_file_grp_cardinality(self.output_file_grp, 1) |
|
29
|
|
|
for input_file in self.input_files: |
|
30
|
|
|
input_file = self.workspace.download_file(input_file) |
|
31
|
|
|
file_id = make_file_id(input_file, self.output_file_grp) |
|
32
|
|
|
ext = MIME_TO_EXT.get(input_file.mimetype, '') |
|
33
|
|
|
local_filename = join(self.output_file_grp, file_id + ext) |
|
34
|
|
|
LOG.info("cp %s %s # %s -> %s", input_file.url, local_filename, input_file.ID, file_id) |
|
35
|
|
|
with open(input_file.local_filename, 'rb') as f: |
|
36
|
|
|
content = f.read() |
|
37
|
|
|
self.workspace.add_file( |
|
38
|
|
|
ID=file_id, |
|
39
|
|
|
file_grp=self.output_file_grp, |
|
40
|
|
|
pageId=input_file.pageId, |
|
41
|
|
|
mimetype=input_file.mimetype, |
|
42
|
|
|
local_filename=local_filename, |
|
43
|
|
|
content=content) |
|
44
|
|
|
|
|
45
|
|
|
def __init__(self, *args, **kwargs): |
|
46
|
|
|
kwargs['ocrd_tool'] = OCRD_TOOL |
|
47
|
|
|
kwargs['version'] = '0.0.1' |
|
48
|
|
|
super(DummyProcessor, self).__init__(*args, **kwargs) |
|
49
|
|
|
|
|
50
|
|
|
@click.command() |
|
51
|
|
|
@ocrd_cli_options |
|
52
|
|
|
def cli(*args, **kwargs): |
|
53
|
|
|
return ocrd_cli_wrap_processor(DummyProcessor, *args, **kwargs) |
|
54
|
|
|
|