Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite CLI to use click instad of typer #270

Merged
merged 5 commits into from
Apr 26, 2024

Conversation

WAKayser
Copy link
Contributor

As discussed in #261 typer changed the scope of the project and started pulling in other dependencies.

This pull request changes the dependency used from typer to click.
The resulting code is a bit more verbose, but the general way of working with it is largely similar.
I found that there were two features missing in click.

First is no support for enums as choices. But this is easy to work around by supplying a list of choices and in the function itself convert to an enum.
Second feature missing was the option to show the environment variables for each option. I have added a simple monkey patch to the option function to extend the help text.

Current state is functional, all option types work and environment variables in general work.. But some more testing is wise.

Copy link
Member

@gi0baro gi0baro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good in general.

I would really love to re-use the existing enums though, just to avoid PITAs in the future if those enums gets changed.

A simple function like this should be enough:

def enum_to_click_choice(v):
    return click.Choice([member.value for member in v])

so we can write

type=enum_to_click_choice(Interfaces)

@gi0baro gi0baro linked an issue Apr 15, 2024 that may be closed by this pull request
…ition of choices. Also added a formatter for default value to automatically return disabled, enabled or the enum string value
granian/cli.py Outdated Show resolved Hide resolved
@WAKayser
Copy link
Contributor Author

I have added a simple EnumType to create the click.Choice from the enum. This is based on: pallets/click#605 (comment)

Finally I have also added a pretty printer to print the defaults a bit neater. This allows to turn all booleans into "disabled" or "enabled" without duplicate configurations.

Finally, I have also finished testing. This was primarily done by adding the following snippet to the start of the cli function. Then check the diff between the version from this PR and what is currently on pypi.

    print(f'{app=}, {host=}, {port=}, {interface=}, {http=}, {websockets=}, {workers=}, {threads=}, {blocking_threads=}')
    print(f'{loop=}, {loop_opt=}, {backlog=}, {http1_buffer_size=}, {http1_keep_alive=}, {http1_pipeline_flush}')
    print(f'{http2_adaptive_window=}, {http2_initial_connection_window_size=}, {http2_initial_stream_window_size}')
    print(f'{http2_keep_alive_interval=}, {http2_keep_alive_timeout=}, {http2_max_concurrent_streams=}')
    print(f'{http2_max_frame_size=}, {http2_max_headers_size=}, {http2_max_send_buffer_size=}, {log_enabled=}')
    print(f'{log_level=}, {log_config=}, {ssl_keyfile=}, {ssl_certificate=}, {url_path_prefix=}')
    print(f'{respawn_failed_workers=}, {reload=}, {process_name=}')
    return

This returned the same when using all the defaults and with all values set via either the command options or via environment variables.

@WAKayser WAKayser changed the title WIP: Rewrite CLI to use click instad of typer Rewrite CLI to use click instad of typer Apr 15, 2024
@WAKayser WAKayser marked this pull request as ready for review April 15, 2024 18:47
@gi0baro gi0baro added this to the 1.3 milestone Apr 15, 2024
@gi0baro gi0baro merged commit 42ca343 into emmett-framework:master Apr 26, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Changed dependency structure of typer.
2 participants