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

Add proper CLI, supports upload, download and repl commands #30

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions download.py

This file was deleted.

3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
include_package_data=True,
python_requires=">=3.5",
install_requires=[
"Click>=7.0",
"prompt-toolkit>=2.0.10",
"pyserial>=3.4",
],
Expand All @@ -34,7 +35,7 @@
},
entry_points={
"console_scripts": [
"druid=druid.main:main",
"druid=druid.cli:cli",
],
},
)
62 changes: 62 additions & 0 deletions src/druid/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
""" Command-line interface for druid """

import sys
import time

import click

from druid import crowlib
from druid import repl as druid_repl

@click.group(invoke_without_command=True)
@click.pass_context
@click.version_option()
def cli(ctx):
""" Terminal interface for crow """
if ctx.invoked_subcommand is None:
ctx.invoke(repl)

@cli.command(short_help="Download a file from crow")
def download():
Copy link
Member Author

@simonvanderveldt simonvanderveldt Oct 10, 2019

Choose a reason for hiding this comment

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

Looking at the repl interaction (p) and the crow command (^^p) it might make more sense to call this print instead of upload?

Copy link
Collaborator

Choose a reason for hiding this comment

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

i think ^^p is 'download' right? the main usage of the download script is that you arrow it to a file druid download > myscript.lua. this style of behaviour makes it feel more like a 'download' than a 'print' to me. that said, you are correct that what druid does is just 'print'.

Copy link
Member Author

@simonvanderveldt simonvanderveldt Oct 11, 2019

Choose a reason for hiding this comment

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

Yeah, I was just thinking that for naming consistency it might make sense to call it print.

Also looking at your example for me it seems to support that print might be a better name in that the command itself just prints to stdout, it doesn't write to a file, which is what I'd normally associate with downloading.

On the other hand if you curl a file then it goes to stdout as well and I'd call that downloading as well.

Hmm 🤔
Naming is hard...

"""
Download a file from crow and print it to stdout
"""
try:
crow = crowlib.connect()
except ValueError as err:
click.echo(err)
sys.exit(1)

crow.write(bytes("^^p", "utf-8"))
click.echo(crow.read(1000000).decode())
crow.close()

def myprint(string):
click.echo(string)

@cli.command(short_help="Upload a file to crow")
@click.argument("filename", type=click.Path(exists=True))
def upload(filename):
"""
Upload a file to crow.
FILENAME is the path to the Lua file to upload
"""
try:
crow = crowlib.connect()
except ValueError as err:
click.echo(err)
sys.exit(1)

crowlib.upload(crow.write, myprint, filename)
click.echo(crow.read(1000000).decode())
click.echo("File uploaded")
time.sleep(0.5) # wait for new script to be ready
crow.write(bytes("^^p", "utf-8"))
click.echo(crow.read(1000000).decode())
simonvanderveldt marked this conversation as resolved.
Show resolved Hide resolved
crow.close()

@cli.command()
@click.argument("filename", type=click.Path(exists=True), required=False)
def repl(filename):
""" Start interactive terminal """
druid_repl.main(filename)
18 changes: 7 additions & 11 deletions src/druid/main.py → src/druid/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async def printer():
await asyncio.sleep(sleeptime)


def main():
def main(script=None):
logging.config.dictConfig({
'version': 1,
'formatters': {
Expand Down Expand Up @@ -210,17 +210,17 @@ def main():
})

global crow
loop = asyncio.get_event_loop()

try:
crow = crowlib.connect()
except ValueError as err:
print(err)
exit()
sys.exit(1)

# run script passed from command line
if len(sys.argv) == 2:
crowlib.execute(crow.write, myprint, sys.argv[1])
if script:
crowlib.execute(crow.write, myprint, script)

loop = asyncio.get_event_loop()

use_asyncio_event_loop()

Expand All @@ -231,8 +231,4 @@ def main():
loop.run_until_complete(background_task)

crow.close()
exit()


if __name__ == '__main__':
main()
sys.exit()
33 changes: 0 additions & 33 deletions upload.py

This file was deleted.