-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle actions manually instead of trying to automate them
- Loading branch information
1 parent
1a58c6c
commit b9fc359
Showing
13 changed files
with
74 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,18 @@ | ||
from typing import Dict, Any | ||
from .. import PycrittyError | ||
from ..io import log | ||
from abc import ABCMeta, abstractmethod | ||
|
||
|
||
class Command: | ||
"""Objects representing a CLI command or subcommand""" | ||
|
||
requires_args = False | ||
class Command(metaclass=ABCMeta): | ||
"""Objects representing a CLI command""" | ||
|
||
@abstractmethod | ||
def execute(self, actions: Dict[str, Any]): | ||
"""Execute the actions associted to this command. | ||
By default it will try to map each action to a method | ||
of this object and execute it with the given arguments. | ||
>>> # Example: | ||
>>> Command c = SomeSubclassCommand() | ||
>>> c.execute({'do_this', [1, 2, 3], 'then_do_this', 'foo'}) | ||
>>> # Would result in: | ||
>>> c.do_this([1, 2, 3]) | ||
>>> c.then_do_this('foo') | ||
This makes it easier to 'reference' the code that has | ||
to run from the CLI, but it cannot be used if the method | ||
requires more than one argument, unless using tuples | ||
or passing arguments manually. | ||
""" | ||
|
||
errors = 0 | ||
for method, args in actions.items(): | ||
call = getattr(self, method) | ||
try: | ||
if args is None: | ||
call() | ||
else: | ||
call(args) | ||
except PycrittyError as e: | ||
log.err(e) | ||
errors += 1 | ||
|
||
self.apply() | ||
|
||
if errors > 0: | ||
raise PycrittyError(f'\n{errors} error(s) found') | ||
|
||
def apply(self): | ||
"""Some commands might need to dump data to config files or run | ||
code after all actions have been performed. | ||
"""Actions are key value pairs parsed from sys.argv with argparse. | ||
For example, the command that sets configs might receive this: | ||
>>> {'set_theme': 'onedark', 'set_font': 'UbuntuMono'} | ||
The command that installs configs might receive something like this: | ||
>>> {'url': 'https://example.io./conf.yml', 'override': True, 'name': 'Foo'} | ||
These key value pairs have to be interpreted as arguments or callable | ||
functions, depending on the command. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters