-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from kba/refactor-cli
Refactor CLI
- Loading branch information
Showing
10 changed files
with
294 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import click | ||
|
||
from ocrd.cli.validate_ocrd_tool import validate_ocrd_tool_cli | ||
from ocrd.cli.workspace import workspace_cli | ||
from ocrd.cli.generate_swagger import generate_swagger_cli | ||
from ocrd.cli.process import process_cli | ||
from ocrd.cli.server import server_cli | ||
|
||
@click.group() | ||
def cli(): | ||
""" | ||
CLI to OCR-D | ||
""" | ||
|
||
cli.add_command(validate_ocrd_tool_cli) | ||
cli.add_command(workspace_cli) | ||
cli.add_command(generate_swagger_cli) | ||
cli.add_command(process_cli) | ||
cli.add_command(server_cli) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import json | ||
import yaml | ||
|
||
import click | ||
|
||
from ocrd import OcrdSwagger | ||
|
||
# ---------------------------------------------------------------------- | ||
# ocrd generate-swagger | ||
# ---------------------------------------------------------------------- | ||
|
||
@click.command('generate-swagger', help="Generate Swagger schema from ocrd-tool.json files") | ||
@click.option('-S', '--swagger-template', help="Swagger template to add operations to. Use builtin if not specified.") | ||
@click.option('-T', '--ocrd-tool', multiple=True, help="ocrd-tool.json file to generate from. Repeatable") | ||
@click.option('-f', '--format', help="Format to generate, JSON or YAML", type=click.Choice(['JSON', 'YAML']), default='JSON') | ||
def generate_swagger_cli(swagger_template, ocrd_tool, **kwargs): | ||
swagger = OcrdSwagger.from_ocrd_tools(swagger_template, *ocrd_tool) | ||
if kwargs['format'] == 'YAML': | ||
print(yaml.dump(swagger)) | ||
else: | ||
print(json.dumps(swagger, indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import json | ||
import codecs | ||
|
||
import click | ||
|
||
from ocrd import run_cli, Resolver | ||
from ocrd.decorators import ocrd_cli_options | ||
|
||
# ---------------------------------------------------------------------- | ||
# ocrd process | ||
# ---------------------------------------------------------------------- | ||
|
||
@click.command('process') | ||
@ocrd_cli_options | ||
@click.option('-T', '--ocrd-tool', multiple=True) | ||
@click.argument('steps', nargs=-1) | ||
def process_cli(mets_url, **kwargs): | ||
""" | ||
Execute OCR-D processors for a METS file directly. | ||
""" | ||
resolver = Resolver(cache_enabled=True) | ||
workspace = resolver.workspace_from_url(mets_url) | ||
|
||
cmds = [] | ||
for ocrd_tool_file in kwargs['ocrd_tool']: | ||
with codecs.open(ocrd_tool_file, encoding='utf-8') as f: | ||
obj = json.loads(f.read()) | ||
for tool in obj['tools']: | ||
cmds.append(tool['binary']) | ||
|
||
for cmd in kwargs['steps']: | ||
if cmd not in cmds: | ||
raise Exception("Tool not registered: '%s'" % cmd) | ||
|
||
for cmd in kwargs['steps']: | ||
run_cli(cmd, mets_url, resolver, workspace) | ||
|
||
workspace.reload_mets() | ||
|
||
# print('\n'.join(k + '=' + str(kwargs[k]) for k in kwargs)) | ||
print(workspace) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import click | ||
|
||
from ocrd.webservice.processor import create as create_processor_ws | ||
from ocrd.webservice.repository import create as create_repository_ws | ||
|
||
# ---------------------------------------------------------------------- | ||
# ocrd server | ||
# ---------------------------------------------------------------------- | ||
|
||
@click.group('server') | ||
def server_cli(): | ||
""" | ||
Start OCR-D web services | ||
""" | ||
|
||
@server_cli.command('process') | ||
@click.option('-p', '--port', help="Port to run processor webservice on", default=5010) | ||
def _start_processor(port): | ||
""" | ||
Start a server exposing the processors as webservices | ||
""" | ||
create_processor_ws().run(port=port) | ||
|
||
@server_cli.command('repository') | ||
@click.option('-p', '--port', help="Port to run repository webservice on", default=5000) | ||
def _start_repository(port): | ||
""" | ||
Start a minimal repository. | ||
""" | ||
create_repository_ws().run(port=port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import codecs | ||
|
||
import click | ||
|
||
from ocrd import OcrdToolValidator | ||
|
||
# ---------------------------------------------------------------------- | ||
# ocrd validate-ocrd-tool | ||
# ---------------------------------------------------------------------- | ||
|
||
@click.command('validate-ocrd-tool', help='Validate an ocrd-tool.json') | ||
@click.argument('json_file', "ocrd-tool.json to validate") | ||
def validate_ocrd_tool_cli(json_file): | ||
with codecs.open(json_file, encoding='utf-8') as f: | ||
report = OcrdToolValidator.validate_json(f.read()) | ||
print(report.to_xml()) | ||
if not report.is_valid: | ||
return 128 |
Oops, something went wrong.