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
CLI as wrapper to core functionality #73
Changes from 2 commits
3ee383a
d900ff4
252fc8f
6e69ab1
66a7d8f
54a959e
ea87f18
23cedbc
5571e7c
2941fdb
58ba1ac
84727d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,8 @@ | ||
import click | ||
import os | ||
from pathlib import Path | ||
|
||
import xerox | ||
from .deon import ExtensionException, FormatException, create | ||
|
||
from .parser import Checklist | ||
from .formats import FORMATS, EXTENSIONS | ||
|
||
DEFAULT_CHECKLIST = Path(__file__).parent / 'assets' / 'checklist.yml' | ||
|
||
CHECKLIST_FILE = Path(os.environ.get('ETHICS_CHECKLIST', DEFAULT_CHECKLIST)) | ||
from .formats import EXTENSIONS | ||
|
||
|
||
@click.command('deon') | ||
|
@@ -28,41 +21,20 @@ | |
Default is False , which will append \ | ||
to existing file.') | ||
def main(checklist, format, output, clipboard, overwrite): | ||
# load checklist | ||
cl_path = Path(checklist) if checklist else DEFAULT_CHECKLIST | ||
cl = Checklist.read(cl_path) | ||
|
||
output = Path(output) if output else None | ||
|
||
# output extension is given priority if differing format is included | ||
if output: | ||
# get format by file extension | ||
ext = output.suffix.lower() | ||
if ext in EXTENSIONS.keys(): | ||
output_format = EXTENSIONS[ext] | ||
else: | ||
with click.get_current_context() as ctx: | ||
msg = 'Output requires a file name with a supported extension.\n' | ||
try: | ||
result = create(checklist, format, output, clipboard, overwrite) | ||
except ExtensionException: | ||
with click.get_current_context() as ctx: | ||
msg = 'Output requires a file name with a supported extension.\n\n' | ||
raise click.ClickException(msg + ctx.get_help()) | ||
elif format: | ||
if format in FORMATS: | ||
output_format = format | ||
else: | ||
with click.get_current_context() as ctx: | ||
msg = "File format is not supported.\n" | ||
except FormatException: | ||
with click.get_current_context() as ctx: | ||
msg = "File format is not supported.\n\n" | ||
raise click.ClickException(msg + ctx.get_help()) | ||
else: | ||
output_format = 'markdown' | ||
|
||
template = FORMATS[output_format](cl) | ||
|
||
# write output or print to stdout | ||
if output: | ||
template.write(output, overwrite=overwrite) | ||
elif clipboard: | ||
xerox.copy(str(template.render())) | ||
else: | ||
click.echo(template.render()) | ||
# write output or print to stdout | ||
if result: | ||
click.echo(result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this I didn't understand exactly what you mean. Perhaps you could rephrase it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like:
|
||
|
||
|
||
if __name__ == '__main__': | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to add something like |
||
from pathlib import Path | ||
|
||
import xerox | ||
|
||
from .formats import EXTENSIONS, FORMATS | ||
from .parser import Checklist | ||
|
||
DEFAULT_CHECKLIST = Path(__file__).parent / 'assets' / 'checklist.yml' | ||
|
||
CHECKLIST_FILE = Path(os.environ.get('ETHICS_CHECKLIST', DEFAULT_CHECKLIST)) | ||
|
||
|
||
class ExtensionException(Exception): | ||
pass | ||
|
||
|
||
class FormatException(Exception): | ||
pass | ||
|
||
|
||
def create(checklist, format, output, clipboard, overwrite): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add tests that directly hit this entry point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggests to re purpose the previous tests that were testing the CLI and expand them, and then simplify the CLI testing. While at the topic of testing, should I take the task to activate code coverage report within the test runner? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that approach works fine. I'll add codecov to CI so we can get those reports per PR. For some reason, CI stopped running on this repo, and we're working on fixing that. |
||
# load checklist | ||
cl_path = Path(checklist) if checklist else DEFAULT_CHECKLIST | ||
cl = Checklist.read(cl_path) | ||
|
||
output = Path(output) if output else None | ||
|
||
# output extension is given priority if differing format is included | ||
if output: | ||
# get format by file extension | ||
ext = output.suffix.lower() | ||
if ext in EXTENSIONS.keys(): | ||
output_format = EXTENSIONS[ext] | ||
else: | ||
raise ExtensionException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we pass a message that includes the |
||
elif format: | ||
if format in FORMATS: | ||
output_format = format | ||
else: | ||
raise FormatException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we pass a message that includes the |
||
else: | ||
output_format = 'markdown' | ||
|
||
template = FORMATS[output_format](cl) | ||
|
||
# write output or print to stdout | ||
if output: | ||
template.write(output, overwrite=overwrite) | ||
elif clipboard: | ||
xerox.copy(str(template.render())) | ||
else: | ||
return template.render() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're in here, could you rename
format
tooutput_format
so we don't clash with built-in the python function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, looks useful for both
cli.py
anddeon.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, thought about that while refactoring but didn't want to take that liberty.