|
41 | 41 | import sys |
42 | 42 | from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Union |
43 | 43 |
|
44 | | -import pyperclip |
45 | | - |
46 | 44 | from . import constants |
47 | 45 | from . import utils |
48 | | - |
49 | | -from cmd2.parsing import StatementParser, Statement |
| 46 | +from .argparse_completer import AutoCompleter, ACArgumentParser |
| 47 | +from .clipboard import can_clip, get_paste_buffer, write_to_paste_buffer |
| 48 | +from .parsing import StatementParser, Statement |
50 | 49 |
|
51 | 50 | # Set up readline |
52 | 51 | from .rl_utils import rl_type, RlType |
53 | | -if rl_type == RlType.NONE: # pragma: no cover |
| 52 | +if rl_type == RlType.NONE: # pragma: no cover |
54 | 53 | rl_warning = "Readline features including tab completion have been disabled since no \n" \ |
55 | 54 | "supported version of readline was found. To resolve this, install \n" \ |
56 | 55 | "pyreadline on Windows or gnureadline on Mac.\n\n" |
|
79 | 78 | rl_basic_quote_characters = ctypes.c_char_p.in_dll(readline_lib, "rl_basic_quote_characters") |
80 | 79 | orig_rl_basic_quotes = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value |
81 | 80 |
|
82 | | -from .argparse_completer import AutoCompleter, ACArgumentParser |
83 | | - |
84 | | -# Newer versions of pyperclip are released as a single file, but older versions had a more complicated structure |
85 | | -try: |
86 | | - from pyperclip.exceptions import PyperclipException |
87 | | -except ImportError: # pragma: no cover |
88 | | - # noinspection PyUnresolvedReferences |
89 | | - from pyperclip import PyperclipException |
90 | | - |
91 | 81 | # Collection is a container that is sizable and iterable |
92 | 82 | # It was introduced in Python 3.6. We will try to import it, otherwise use our implementation |
93 | 83 | try: |
@@ -121,7 +111,7 @@ def __subclasshook__(cls, C): |
121 | 111 | try: |
122 | 112 | # noinspection PyUnresolvedReferences,PyPackageRequirements |
123 | 113 | from IPython import embed |
124 | | -except ImportError: # pragma: no cover |
| 114 | +except ImportError: # pragma: no cover |
125 | 115 | ipython_available = False |
126 | 116 |
|
127 | 117 | __version__ = '0.9.2a' |
@@ -271,48 +261,6 @@ def cmd_wrapper(instance, cmdline): |
271 | 261 | return arg_decorator |
272 | 262 |
|
273 | 263 |
|
274 | | -# Can we access the clipboard? Should always be true on Windows and Mac, but only sometimes on Linux |
275 | | -# noinspection PyUnresolvedReferences |
276 | | -try: |
277 | | - # Get the version of the pyperclip module as a float |
278 | | - pyperclip_ver = float('.'.join(pyperclip.__version__.split('.')[:2])) |
279 | | - |
280 | | - # The extraneous output bug in pyperclip on Linux using xclip was fixed in more recent versions of pyperclip |
281 | | - if sys.platform.startswith('linux') and pyperclip_ver < 1.6: |
282 | | - # Avoid extraneous output to stderr from xclip when clipboard is empty at cost of overwriting clipboard contents |
283 | | - pyperclip.copy('') |
284 | | - else: |
285 | | - # Try getting the contents of the clipboard |
286 | | - _ = pyperclip.paste() |
287 | | -except PyperclipException: |
288 | | - can_clip = False |
289 | | -else: |
290 | | - can_clip = True |
291 | | - |
292 | | - |
293 | | -def disable_clip() -> None: |
294 | | - """ Allows user of cmd2 to manually disable clipboard cut-and-paste functionality.""" |
295 | | - global can_clip |
296 | | - can_clip = False |
297 | | - |
298 | | - |
299 | | -def get_paste_buffer() -> str: |
300 | | - """Get the contents of the clipboard / paste buffer. |
301 | | -
|
302 | | - :return: contents of the clipboard |
303 | | - """ |
304 | | - pb_str = pyperclip.paste() |
305 | | - return pb_str |
306 | | - |
307 | | - |
308 | | -def write_to_paste_buffer(txt: str) -> None: |
309 | | - """Copy text to the clipboard / paste buffer. |
310 | | -
|
311 | | - :param txt: text to copy to the clipboard |
312 | | - """ |
313 | | - pyperclip.copy(txt) |
314 | | - |
315 | | - |
316 | 264 | class EmbeddedConsoleExit(SystemExit): |
317 | 265 | """Custom exception class for use with the py command.""" |
318 | 266 | pass |
@@ -546,6 +494,9 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h |
546 | 494 | self.pager = 'less -RXF' |
547 | 495 | self.pager_chop = 'less -SRXF' |
548 | 496 |
|
| 497 | + # This boolean flag determines whether or not the cmd2 application can interact with the clipboard |
| 498 | + self.can_clip = can_clip |
| 499 | + |
549 | 500 | # ----- Methods related to presenting output to the user ----- |
550 | 501 |
|
551 | 502 | @property |
@@ -1881,7 +1832,7 @@ def _redirect_output(self, statement: Statement) -> None: |
1881 | 1832 | raise ex |
1882 | 1833 | elif statement.output: |
1883 | 1834 | import tempfile |
1884 | | - if (not statement.output_to) and (not can_clip): |
| 1835 | + if (not statement.output_to) and (not self.can_clip): |
1885 | 1836 | raise EnvironmentError("Cannot redirect to paste buffer; install 'pyperclip' and re-run to enable") |
1886 | 1837 | self.kept_state = Statekeeper(self, ('stdout',)) |
1887 | 1838 | self.kept_sys = Statekeeper(sys, ('stdout',)) |
|
0 commit comments