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

CLI dispatch mechanism #108

Merged
merged 22 commits into from
Sep 23, 2016
Merged

CLI dispatch mechanism #108

merged 22 commits into from
Sep 23, 2016

Conversation

mailund
Copy link
Member

@mailund mailund commented Sep 14, 2016

Design for a CLI interface based on installing subcommands and callbacks.

For each subcommand you write a class with two hooks: one for setting sub-command arguments and the other to be called if the sub-command is invoked.

I haven't tested the _split_target_list function in StatusCommand, 'cause I don't know how to muck up a test of target status checking right now.
@@ -23,7 +23,7 @@ lint:
flake8

coverage:
coverage report
coverage report --omit gwf/cli/__main__.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be specified in the configuration file .coveragerc instead.

@@ -1,117 +1,212 @@
from __future__ import absolute_import
from __future__ import print_function
from __future__ import absolute_import, print_function
Copy link
Collaborator

Choose a reason for hiding this comment

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

We only support Python 3.4+, so we don't need imports from __future__.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, but that is actually your commit and not mine ;)

@@ -0,0 +1 @@
from __future__ import absolute_import, print_function
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unnecessary import. See comment above.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@@ -0,0 +1,26 @@
from __future__ import absolute_import, print_function
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 this file should be called entrypoint.py to make it clear that this is the entry point of the program.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed.

import argparse


class SubCommand(object):
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 just Command is better than SubCommand. Otherwise RunCommand should be called RunSubCommand, but that's just ugly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Python 3 has new-style classes by default, so there's no reason to inherit from object explicitly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed the name.

Copy link
Collaborator

@dansondergaard dansondergaard left a comment

Choose a reason for hiding this comment

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

Great!

I don't know why there's a lot of changes to the SLURM backend included in this PR. Is it possible to remove again, maybe my merging master into the branch again?

Some general comments:

  • Order imports according to PEP8 (this is done automatically if you use Atom with the atom-beautify plugin).
  • Remove spacing around =. The above mentioned plugin takes care of that too.
  • Don't import from __future__.

from __future__ import absolute_import, print_function
from ..utils import cache, import_object
from ..core import PreparedWorkflow
import argparse
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer if the file was just called parsing.py to be consistent with PEP8 recommendations about not using underscores in module names. It's also prettier.

Order imports alphabetically with stdlib imports first, then third-party imports, then package imports.

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed the name of the file and order of imporots.

@property
@cache
def workflow(cls):
if cls._workflow is None: # pragma: no cover
Copy link
Collaborator

Choose a reason for hiding this comment

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

As you write, no reason to check this. We'll make sure to always set the workflow.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed the check

def handle(self, arguments):
"Callback when this sub-command is invoked."

class ArgumentDispatching(object):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't inherit from object.

I think this should just be called Dispatcher.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why do you have a problem with inheriting from object? in 2.7 it is the way to use the new class system and it doesn't look like it is doing any damage in 3.5.

Copy link
Collaborator

Choose a reason for hiding this comment

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

True, it's just unnecessary :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok. Will remove it then


def __init__(self):
self.parser = argparse.ArgumentParser(
description="Grid WorkFlow",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe use the same description as in the docs: "A flexible, pragmatic workflow tool."

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.


# Set global options here. Options for sub-commands will be set by the
# sub-commands we dispatch to.
self.parser.add_argument("-w", "--workflow",
Copy link
Collaborator

Choose a reason for hiding this comment

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

For backward compatibility I think the flag should be -f and --file. No reason to confuse users unnecessarily.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok :(

self.subparsers = self.parser.add_subparsers(title = "commands", description = "Subcommands to execute")

def install_subcommand(self, name, description, handler):
"""Install a sub-command with "name" and with functionality implemented
Copy link
Collaborator

Choose a reason for hiding this comment

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

Weird wrapping?

Copy link
Member Author

Choose a reason for hiding this comment

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

Weird how?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, my mistake. That was my editor wrapping it :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

default = "workflow.py")

# Prepare for sub-commands
self.subparsers = self.parser.add_subparsers(title = "commands", description = "Subcommands to execute")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wrap this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

from math import ceil


__doc__ = """
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doc string should just go to the top of the module (then it will be automatically assigned to __doc__).

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

@@ -13,6 +13,16 @@ def __init__(self, target):

super(TargetExistsError, self).__init__(message)

class TargetDoesNotExistsError(GWFError):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Grammar: Exists -> Exist.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

test_suite='tests',
install_requires=[
"colorama",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Specify a minimum version with >=major.minor.

Copy link
Member Author

Choose a reason for hiding this comment

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

Bah. I don't know which minimal version to use.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The most recent one right now: 0.3.7 ;)

mailund and others added 9 commits September 21, 2016 16:31
# Conflicts:
#	setup.py
The unit tests are failing now but it is in the code that prepares the workflow, so I think the problem is with the code I merged in from the master branch which I haven't checked how is changed. @dansondergaard, can you see why there is a key error in a dictionary lookup in the workflow preparation now?
@dansondergaard dansondergaard merged commit c7cce2c into master Sep 23, 2016
@dansondergaard dansondergaard deleted the new-cli branch September 23, 2016 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants