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 disable and enable commands #26

Merged
merged 4 commits into from Sep 23, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions dispass/commands/disable.py
@@ -0,0 +1,66 @@
'''Subcommand module 'disable'; contains only a single class `Command`'''

# Copyright (c) 2011, 2012, 2013 Benjamin Althues <benjamin@babab.nl>
#
# 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.common import CommandBase
from dispass.dispass import settings
from dispass.filehandler import Filehandler


class Command(CommandBase):
'''Disable a label without throwing it away'''

useagestr = (
'usage: dispass disable <label>'
)

description = (
'Disable a label without throwing it away'
)

optionList = (
('help', ('h', False, 'show this help information')),
('dry-run', ('n', False, 'do not actually update label in labelfile')),
('silent', ('s', False, 'do not print success message')),
)

def run(self):
'''Parse the arguments and disable using `Filehandler.disable`.'''

if self.parentFlags['file']:
lf = Filehandler(settings, file_location=self.parentFlags['file'])
else:
lf = Filehandler(settings)

if not len(self.args) == 1 or self.flags['help']:
print self.usage
return

if not lf.file_found:
# File not found? No possible label to disable.
return 1

labelname = self.args[0]

if lf.disable(labelname):
if not self.flags['silent']:
print("Label '{name}' disabled".format(name=labelname))
else:
if not self.flags['silent']:
print("Label '{name}' could not be disabled"
.format(name=labelname))

if not self.flags['dry-run']:
lf.save()
66 changes: 66 additions & 0 deletions dispass/commands/enable.py
@@ -0,0 +1,66 @@
'''Subcommand module 'enable'; contains only a single class `Command`'''

# Copyright (c) 2011, 2012, 2013 Benjamin Althues <benjamin@babab.nl>
#
# 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.common import CommandBase
from dispass.dispass import settings
from dispass.filehandler import Filehandler


class Command(CommandBase):
'''Enable a label'''

useagestr = (
'usage: dispass enable <label>'
)

description = (
'Enable a label'
)

optionList = (
('help', ('h', False, 'show this help information')),
('dry-run', ('n', False, 'do not actually update label in labelfile')),
('silent', ('s', False, 'do not print success message')),
)

def run(self):
'''Parse the arguments and enable using `Filehandler.disable`.'''

if self.parentFlags['file']:
lf = Filehandler(settings, file_location=self.parentFlags['file'])
else:
lf = Filehandler(settings)

if not len(self.args) == 1 or self.flags['help']:
print self.usage
return

if not lf.file_found:
# File not found? No possible label to enable.
return 1

labelname = self.args[0]

if lf.disable(labelname, False):
if not self.flags['silent']:
print("Label '{name}' enabled".format(name=labelname))
else:
if not self.flags['silent']:
print("Label '{name}' could not be enabled"
.format(name=labelname))

if not self.flags['dry-run']:
lf.save()
7 changes: 5 additions & 2 deletions dispass/commands/list.py
Expand Up @@ -30,9 +30,11 @@ class Command(CommandBase):
'Column 1-50: labelname 50 chars wide\n'
'Column 52-54: length 3 chars wide\n'
'Column 56-70: hash algo 15 chars wide\n'
'Column 72-74: sequence number 3 chars wide'
'Column 72-74: sequence number 3 chars wide\n'
'Column 76-77: disabled 1 char wide'
)
optionList = (
('all', ('a', False, 'include disabled labels')),
('help', ('h', False, 'show this help information')),
('names-only', ('n', False, 'only print names of the labels')),
('script', ('', False, 'output in fixed columns')),
Expand All @@ -54,4 +56,5 @@ def run(self):
return 1

lf.printLabels(fixed_columns=self.flags['script'],
labels_only=self.flags['names-only'])
labels_only=self.flags['names-only'],
all_=self.flags['all'])
5 changes: 5 additions & 0 deletions dispass/dispass.py
Expand Up @@ -54,6 +54,9 @@ class Settings(object):
sequence_number = 1
'''Int. Default sequence number'''

disabled = False
'''Bool. Default disabled state'''

settings = Settings()


Expand All @@ -65,6 +68,8 @@ class DispassCommand(CommandBase):
description = (
'Commands:\n'
' add add a new label to labelfile\n'
' disable disable a label without throwing it away\n'
' enable enable a label'
' generate generate passphrases for one or more labels\n'
' gui start the graphical version of DisPass\n'
' help show this help information\n'
Expand Down
73 changes: 49 additions & 24 deletions dispass/filehandler.py
Expand Up @@ -34,7 +34,7 @@ class Filehandler:
'''String of labelfile location, set on init'''

labelfile = []
'''List of [(labelname, length, algorithm, seqno), ... ]'''
'''List of [(labelname, length, algorithm, seqno, disabled), ... ]'''

longest_label = None
'''Int. Length of the longest labelname of `labelfile`. Set on refresh()'''
Expand Down Expand Up @@ -66,7 +66,7 @@ def getDefaultFileLocation(self):
return home_file

def parse(self):
'''Create dictionary {algorithm: (label, (length, seqno))}'''
'''Create dictionary {algorithm: (label, (length, seqno, disabled))}'''

file_stripped = []
self.labelfile = []
Expand Down Expand Up @@ -103,6 +103,7 @@ def parse(self):
length = self.settings.passphrase_length
seqno = self.settings.sequence_number
algo = self.settings.algorithm
disabled = self.settings.disabled

for arg in line:
if 'length=' in arg:
Expand All @@ -114,8 +115,10 @@ def parse(self):
algo = arg.strip('algo=')
elif 'seqno=' in arg:
seqno = arg.strip('seqno=')
elif 'disabled=' in arg:
disabled = arg.lstrip('disabled=') == 'True'

self.labelfile.append((labelname, length, algo, seqno))
self.labelfile.append((labelname, length, algo, seqno, disabled))

return self

Expand All @@ -124,20 +127,23 @@ def find(self, labelname):
if labelname == label[0]:
return label

def add(self, labelname, length=None, algo=None, seqno=None):
def add(self, labelname, length=None, algo=None, seqno=None,
disabled=False):
'''Add label to `labelfile`'''

length = length if length else self.settings.passphrase_length
algo = algo if algo else self.settings.algorithm
seqno = seqno if seqno else self.settings.sequence_number
disabled = disabled if disabled else self.settings.disabled

if self.find(labelname):
return False

self.labelfile.append((labelname, length, algo, seqno))
self.labelfile.append((labelname, length, algo, seqno, disabled))
return True

def update(self, labelname, length=None, algo=None, seqno=None):
def update(self, labelname, length=None, algo=None, seqno=None,
disabled=None):
'''Update label in `labelfile`'''

label = self.find(labelname)
Expand All @@ -147,7 +153,8 @@ def update(self, labelname, length=None, algo=None, seqno=None):

params = {'length': length if length else label[1],
'algo': algo if algo else label[2],
'seqno': seqno if seqno else label[3]}
'seqno': seqno if seqno else label[3],
'disabled': disabled if disabled is not None else label[4]}

return self.remove(labelname) and self.add(labelname, **params)

Expand All @@ -161,6 +168,16 @@ def increment(self, labelname):

return self.update(labelname, seqno=int(label[3]) + 1)

def disable(self, labelname, disabled=True):
'''Disable or enable a label'''

label = self.find(labelname)

if not label:
return False

return self.update(labelname, disabled=disabled)

def remove(self, labelname):
'''Remove label from `labelfile`'''

Expand Down Expand Up @@ -194,12 +211,14 @@ def save(self):
datetime=datetime.datetime.now()))
for label in self.labelfile:
if label[2] == 'dispass1':
options = ('length={length} algo={algo}'
.format(length=label[1], algo=label[2]))
options = ('length={length} algo={algo} disabled={disabled}'
.format(length=label[1], algo=label[2],
disabled=label[4]))
else:
options = ('length={length} algo={algo} seqno={seqno}'
options = (('length={length} algo={algo} seqno={seqno} '
'disabled={disabled}')
.format(length=label[1], algo=label[2],
seqno=label[3]))
seqno=label[3], disabled=label[4]))
labelfile += ('{label:{divlen}} {options}\n'
.format(label=label[0], options=options,
divlen=self.longest_label))
Expand All @@ -219,11 +238,12 @@ def labeltup(self, label):
- `label`: The labelname

:Returns:
- A tuple with 4 values ``(label, length, algo, seqno))``:
- A tuple with 5 values ``(label, length, algo, seqno, disabled))``:
- `label`: Label to use for passprase generation
- `length`: Length to use for passprase generation
- `algo`: Algorithm to use for passprase generation
- `seqno`: Sequence number to use for passprase generation
- `disabled`: Whether or not the passphrase is disabled

'''

Expand All @@ -232,7 +252,7 @@ def labeltup(self, label):
return labeltup
return False

def printLabels(self, fixed_columns=False, labels_only=False):
def printLabels(self, fixed_columns=False, labels_only=False, all_=False):
'''Print a formatted table of labelfile contents

:Parameters:
Expand All @@ -250,6 +270,7 @@ def printLabels(self, fixed_columns=False, labels_only=False):
* Column 52-54: length (3 chars wide)
* Column 56-70: hash algo (15 chars wide)
* Column 72-74: sequence number (3 chars wide)
* Column 76-77: disabled (1 char wide)

If fixed columns is false an ascii table is printed with a variable
width depending on the length of the longest label.
Expand All @@ -258,28 +279,32 @@ def printLabels(self, fixed_columns=False, labels_only=False):
self.refresh()
if labels_only:
for label in self.labelfile:
print(label[0])
if all_ or not label[4]:
print(label[0])
return

if fixed_columns:
for label in self.labelfile:
print('{:50} {:3} {:15} {:3}'
.format(label[0][:50], str(label[1])[:3],
label[2][:15], str(label[3])))
if all_ or not label[4]:
print('{:50} {:3} {:15} {:3} {}'
.format(label[0][:50], str(label[1])[:3],
label[2][:15], str(label[3]),
'y' if label[4] else 'n'))
else:
divlen = self.longest_label
if not divlen:
return
print('+-{spacer:{fill}}-+--------+----------+--------+\n'
'| {title:{fill}} | Length | Algo | Number | \n'
'+-{spacer:{fill}}-+--------+----------+--------+'
print('+-{spacer:{fill}}-+--------+----------+--------+---+\n'
'| {title:{fill}} | Length | Algo | Number | X |\n'
'+-{spacer:{fill}}-+--------+----------+--------+---+'
.format(spacer='-' * divlen, title='Label', fill=divlen))

for label in self.labelfile:
print('| {:{fill}} | {:3} | {:8} | {:3>} |'
.format(label[0], label[1], label[2], int(label[3]),
fill=divlen))
print('+-{:{fill}}-+--------+----------+--------+'
if all_ or not label[4]:
print('| {:{fill}} | {:3} | {:8} | {:3>} | {} |'
.format(label[0], label[1], label[2], int(label[3]),
'y' if label[4] else 'n', fill=divlen))
print('+-{:{fill}}-+--------+----------+--------+---+'
.format('-' * divlen, fill=divlen))

def promptForCreation(self, silent=False):
Expand Down