Skip to content

Commit 01c8612

Browse files
committed
Refactored clipboard code to make it a tiny bit simpler
Also added a few unit tests related to running an empty statement and dealing with precmd hook success and failure.
1 parent ed54110 commit 01c8612

2 files changed

Lines changed: 49 additions & 21 deletions

File tree

cmd2.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -303,27 +303,25 @@ def new_func(instance, arg):
303303
return option_setup
304304

305305

306-
# Can we access the clipboard, always true on Windows and Mac, but only sometimes on Linux
307-
can_clip = True
308-
if sys.platform.startswith('linux'):
309-
try:
310-
pyperclip.paste()
311-
except pyperclip.exceptions.PyperclipException:
312-
can_clip = False
306+
# Can we access the clipboard? Should always be true on Windows and Mac, but only sometimes on Linux
307+
try:
308+
pyperclip.paste()
309+
except pyperclip.exceptions.PyperclipException:
310+
can_clip = False
311+
else:
312+
can_clip = True
313313

314314

315315
def get_paste_buffer():
316316
"""Get the contents of the clipboard / paste buffer.
317317
318318
:return: str - contents of the clipboard
319319
"""
320-
pb_unicode = pyperclip.paste()
320+
pb_str = pyperclip.paste()
321321

322-
if six.PY3:
323-
pb_str = pb_unicode
324-
else:
322+
if six.PY2:
325323
import unicodedata
326-
pb_str = unicodedata.normalize('NFKD', pb_unicode).encode('ascii', 'ignore')
324+
pb_str = unicodedata.normalize('NFKD', pb_str).encode('ascii', 'ignore')
327325

328326
return pb_str
329327

@@ -368,16 +366,15 @@ def replace_with_file_contents(fname):
368366
"""Action to perform when successfully matching parse element definition for inputFrom parser.
369367
370368
:param fname: str - filename
371-
:return: str - contents of file "fname" or contents of the clipboard if fname is None or an empty string
369+
:return: str - contents of file "fname"
372370
"""
373-
if fname:
374-
try:
375-
with open(os.path.expanduser(fname[0])) as source_file:
376-
result = source_file.read()
377-
except IOError:
378-
result = '< %s' % fname[0] # wasn't a file after all
379-
else:
380-
result = get_paste_buffer()
371+
try:
372+
with open(os.path.expanduser(fname[0])) as source_file:
373+
result = source_file.read()
374+
except IOError:
375+
result = '< %s' % fname[0] # wasn't a file after all
376+
377+
# TODO: IF pyparsing input parser logic gets fixed to support empty file, add support to get from paste buffer
381378
return result
382379

383380

tests/test_cmd2.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def test_ver():
2525
assert cmd2.__version__ == '0.7.4'
2626

2727

28+
def test_empty_statement(base_app):
29+
out = run_cmd(base_app, '')
30+
expected = normalize('')
31+
assert out == expected
32+
2833
def test_base_help(base_app):
2934
out = run_cmd(base_app, 'help')
3035
expected = normalize(BASE_HELP)
@@ -683,3 +688,29 @@ def test_cmdloop_without_rawinput():
683688
app.cmdloop()
684689
out = app.stdout.buffer
685690
assert out == expected
691+
692+
693+
class HookFailureApp(cmd2.Cmd):
694+
def __init__(self, *args, **kwargs):
695+
# Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x
696+
cmd2.Cmd.__init__(self, *args, **kwargs)
697+
698+
def postparsing_precmd(self, statement):
699+
"""Simulate precmd hook failure."""
700+
return True, statement
701+
702+
703+
@pytest.fixture
704+
def hook_failure():
705+
app = HookFailureApp()
706+
app.stdout = StdOut()
707+
return app
708+
709+
def test_precmd_hook_success(base_app):
710+
out = base_app.onecmd_plus_hooks('help')
711+
assert out is None
712+
713+
714+
def test_precmd_hook_failure(hook_failure):
715+
out = hook_failure.onecmd_plus_hooks('help')
716+
assert out == True

0 commit comments

Comments
 (0)