diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-06-30 20:54:02 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-06-30 20:54:02 -0400 |
commit | 01c8612f26a98ef145d1ce8636124a6a39ff9762 (patch) | |
tree | 85e39c1fec60dcbe46eb8db29558b95620cb132d /cmd2.py | |
parent | ed54110d6fa27833467f55051c47c0cea77fabee (diff) | |
download | cmd2-git-01c8612f26a98ef145d1ce8636124a6a39ff9762.tar.gz |
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.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-x | cmd2.py | 39 |
1 files changed, 18 insertions, 21 deletions
@@ -303,13 +303,13 @@ def options(option_list, arg_desc="arg"): return option_setup -# Can we access the clipboard, always true on Windows and Mac, but only sometimes on Linux -can_clip = True -if sys.platform.startswith('linux'): - try: - pyperclip.paste() - except pyperclip.exceptions.PyperclipException: - can_clip = False +# Can we access the clipboard? Should always be true on Windows and Mac, but only sometimes on Linux +try: + pyperclip.paste() +except pyperclip.exceptions.PyperclipException: + can_clip = False +else: + can_clip = True def get_paste_buffer(): @@ -317,13 +317,11 @@ def get_paste_buffer(): :return: str - contents of the clipboard """ - pb_unicode = pyperclip.paste() + pb_str = pyperclip.paste() - if six.PY3: - pb_str = pb_unicode - else: + if six.PY2: import unicodedata - pb_str = unicodedata.normalize('NFKD', pb_unicode).encode('ascii', 'ignore') + pb_str = unicodedata.normalize('NFKD', pb_str).encode('ascii', 'ignore') return pb_str @@ -368,16 +366,15 @@ def replace_with_file_contents(fname): """Action to perform when successfully matching parse element definition for inputFrom parser. :param fname: str - filename - :return: str - contents of file "fname" or contents of the clipboard if fname is None or an empty string + :return: str - contents of file "fname" """ - if fname: - try: - with open(os.path.expanduser(fname[0])) as source_file: - result = source_file.read() - except IOError: - result = '< %s' % fname[0] # wasn't a file after all - else: - result = get_paste_buffer() + try: + with open(os.path.expanduser(fname[0])) as source_file: + result = source_file.read() + except IOError: + result = '< %s' % fname[0] # wasn't a file after all + + # TODO: IF pyparsing input parser logic gets fixed to support empty file, add support to get from paste buffer return result |