-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put labelfile handling for commands in decorators
Instead of defining the label file opening, and possibly saving, logic in each single command, put them in decorators so they can easily be added to each command. This lets us define a centralized point where loading and saving of label files occur. Also move the ‘dry-run’ flag to the write_labels decorator since that is currently the only place that uses it. This again takes responsibility away from each individual command. I’ve opted to have the decorators add parameters to the ‘run’ method since that seems fairly clear, and doesn’t start adding instance variables out of nowhere. Remove the option to create a label file when generating passphrases.
- Loading branch information
Showing
12 changed files
with
139 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .read_labels import read_labels | ||
from .write_labels import write_labels | ||
|
||
__all__ = [read_labels, write_labels] |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'''Decorator for commands that read labels''' | ||
|
||
# Copyright (c) 2012-2016 Tom Willemse <tom@ryuslash.org> | ||
# Copyright (c) 2011-2016 Benjamin Althues <benjamin@althu.es> | ||
# | ||
# Permission to use, copy, modify, and distribute this software for any | ||
# purpose with or without fee is hereby granted, provided that the above | ||
# copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
from dispass.dispass import settings | ||
from dispass.filehandler import Filehandler | ||
|
||
|
||
def _run_with_labelfile(func, optional): | ||
def run(inst, *args, **kwargs): | ||
if inst.parentFlags['file']: | ||
lf = Filehandler(settings, file_location=inst.parentFlags['file']) | ||
else: | ||
lf = Filehandler(settings) | ||
|
||
if not lf.file_found and not optional: | ||
print('error: could not load labelfile at "{loc}"' | ||
.format(loc=lf.file_location)) | ||
return 1 | ||
|
||
func(inst, lf, *args, **kwargs) | ||
|
||
return run | ||
|
||
|
||
def read_labels(optional=False): | ||
'''Decorate the `run` method of CLS. | ||
An instance of dispass.labelfile.Filehandler is added to the front | ||
of the argument list of the `run` method, after self. This file is | ||
ready for reading. | ||
''' | ||
def func(cls): | ||
cls.run = _run_with_labelfile(cls.run, optional) | ||
return cls | ||
|
||
return func |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'''Decorator for commands that write labels''' | ||
|
||
# Copyright (c) 2012-2016 Tom Willemse <tom@ryuslash.org> | ||
# Copyright (c) 2011-2016 Benjamin Althues <benjamin@althu.es> | ||
# | ||
# Permission to use, copy, modify, and distribute this software for any | ||
# purpose with or without fee is hereby granted, provided that the above | ||
# copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
from dispass.dispass import settings | ||
from dispass.filehandler import Filehandler | ||
|
||
|
||
def _run_with_labelfile(func): | ||
def run(inst, *args, **kwargs): | ||
if inst.parentFlags['file']: | ||
lf = Filehandler(settings, file_location=inst.parentFlags['file']) | ||
else: | ||
lf = Filehandler(settings) | ||
|
||
if not lf.file_found: | ||
if not lf.promptForCreation(silent=inst.flags['silent']): | ||
return 1 | ||
|
||
func(inst, lf, *args, **kwargs) | ||
|
||
if not inst.flags['dry-run']: | ||
lf.save() | ||
|
||
return run | ||
|
||
|
||
def write_labels(cls): | ||
'''Decorate the `run` method of CLS. | ||
An instance of dispass.labelfile.Filehandler is added to the front | ||
of the argument list of the `run` method, after self. This file is | ||
ready for writing. After the `run` method is finished, the label | ||
file is automatically saved. A `dry-run` flag is also added to the | ||
command indicating that the labelfile should not actually be | ||
saved. | ||
''' | ||
cls.run = _run_with_labelfile(cls.run) | ||
cls.optionList += ( | ||
('dry-run', ('n', False, 'do not actually add label to labelfile')), | ||
) | ||
|
||
return cls |
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
Oops, something went wrong.