Skip to content

Commit

Permalink
fix language parameters in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
David Caron committed Mar 12, 2020
1 parent 70c8a3d commit 993aff5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 74 deletions.
42 changes: 15 additions & 27 deletions birdy/cli/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
autoescape=True,
)

# Path to local commands folder (non wps process related)
_local_command_folder = os.path.join(os.path.dirname(__file__), 'commands')

# Adds commands from commands folder (non wps process related)
_local_commands = ['language']


class BirdyCLI(click.MultiCommand):
"""BirdyCLI is an implementation of :class:`click.MultiCommand`. It
Expand All @@ -36,9 +30,16 @@ def __init__(self, name=None, url=None, caps_xml=None, desc_xml=None, **attrs):
self.verify = get_ssl_verify()
self.caps_xml = caps_xml
self.desc_xml = desc_xml
self.wps = WebProcessingService(self.url, verify=self.verify, skip_caps=True)
self._wps = None
self.commands = OrderedDict()

@property
def wps(self):
if self._wps is None:
language = self.context_settings['obj'].get('language')
self._wps = WebProcessingService(self.url, verify=self.verify, skip_caps=True, language=language)
return self._wps

def _update_commands(self):
if not self.commands:
try:
Expand All @@ -47,7 +48,6 @@ def _update_commands(self):
raise ConnectionError('SSL verfication of server certificate failed. Set WPS_SSL_VERIFY=false.')
except Exception:
raise ConnectionError("Web Processing Service not available.")

for process in self.wps.processes:
self.commands[process.identifier] = dict(
name=process.identifier,
Expand All @@ -56,30 +56,18 @@ def _update_commands(self):
help=BirdyCLI.format_command_help(process),
options=[])

for cmd in _local_commands:
self.commands[cmd] = dict()

def list_commands(self, ctx):
ctx.obj = True
self._update_commands()
return list(self.commands.keys())

def get_command(self, ctx, name):
if name in _local_commands:
ns = {}
fn = os.path.join(_local_command_folder, 'cmd_' + name + '.py')
with open(fn) as f:
code = compile(f.read(), fn, 'exec')
eval(code, ns, ns)
return ns['cli']
else:
self._update_commands()
cmd_templ = template_env.get_template('cmd.py.j2')
rendered_cmd = cmd_templ.render(self._get_command_info(name, ctx))
ns = {}
code = compile(rendered_cmd, filename='<string>', mode='exec')
eval(code, ns, ns)
return ns['cli']
self._update_commands()
cmd_templ = template_env.get_template('cmd.py.j2')
rendered_cmd = cmd_templ.render(self._get_command_info(name, ctx))
ns = {}
code = compile(rendered_cmd, filename='<string>', mode='exec')
eval(code, ns, ns)
return ns['cli']

def _get_command_info(self, name, ctx):
cmd = self.commands.get(name)
Expand Down
Empty file removed birdy/cli/commands/__init__.py
Empty file.
45 changes: 0 additions & 45 deletions birdy/cli/commands/cmd_language.py

This file was deleted.

25 changes: 24 additions & 1 deletion birdy/cli/run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import os

import click
from birdy.cli.base import BirdyCLI
from birdy.cli.misc import get_ssl_verify

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
from owslib.wps import WebProcessingService

CONTEXT_OBJ = dict(language=None)
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'], obj=CONTEXT_OBJ)
DEFAULT_URL = "http://localhost:5000/wps"


def _show_languages(ctx, param, value):
if not value or ctx.resilient_parsing:
return
url = os.environ.get('WPS_SERVICE') or DEFAULT_URL
wps = WebProcessingService(url, verify=get_ssl_verify())
click.echo(','.join(wps.languages.supported))
ctx.exit()


def _set_language(ctx, param, value):
if not value or ctx.resilient_parsing:
return
CONTEXT_OBJ['language'] = value


@click.command(cls=BirdyCLI, context_settings=CONTEXT_SETTINGS,
Expand All @@ -12,6 +33,8 @@
@click.option('--send', '-S', is_flag=True, help="Send client side certificate to WPS. Default: false")
@click.option("--sync", '-s', is_flag=True, help="Execute process in sync mode. Default: async mode.")
@click.option("--token", "-t", help="Token to access the WPS service.")
@click.option("--language", "-l", expose_value=False, is_eager=True, callback=_set_language, help="Set the accepted language to send to the WPS service.")
@click.option("--show-languages", "-L", expose_value=False, is_flag=True, is_eager=True, callback=_show_languages, help="Show a list of accepted languages for the WPS service.")
@click.pass_context
def cli(ctx, cert, send, sync, token):
"""
Expand Down
3 changes: 2 additions & 1 deletion birdy/templates/cmd.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def cli(ctx, **options):
if send and cert:
with open(cert, 'r') as fh:
headers = {'X-Ssl-Client-Cert': quote(fh.read())}
wps = WebProcessingService('{{ url }}', skip_caps=True, verify=verify, cert=cert, headers=headers)
language = ctx.obj.get('language')
wps = WebProcessingService('{{ url }}', skip_caps=True, verify=verify, cert=cert, headers=headers, language=language)
inputs = []
for opt in options.keys():
if not options[opt]:
Expand Down

0 comments on commit 993aff5

Please sign in to comment.