Skip to content

Commit

Permalink
Merge pull request #75 from kba/refactor-cli
Browse files Browse the repository at this point in the history
Refactor CLI
  • Loading branch information
kba authored May 8, 2018
2 parents 1a21dcf + a733e80 commit 33f7c80
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 161 deletions.
19 changes: 19 additions & 0 deletions ocrd/cli/__init__.py
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)
21 changes: 21 additions & 0 deletions ocrd/cli/generate_swagger.py
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))
41 changes: 41 additions & 0 deletions ocrd/cli/process.py
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)
150 changes: 0 additions & 150 deletions ocrd/cli/run.py

This file was deleted.

30 changes: 30 additions & 0 deletions ocrd/cli/server.py
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)
18 changes: 18 additions & 0 deletions ocrd/cli/validate_ocrd_tool.py
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
Loading

0 comments on commit 33f7c80

Please sign in to comment.