Exemplo n.º 1
0
    def test_copy_to_system(self):
        # Test with format_unicode.
        text1 = u"unicode text"
        c = Clipboard(contents={format_unicode: text1})
        c.copy_to_system(clear=True)
        self.assertEqual(Clipboard.get_system_text(), text1)

        # Test with format_text. Text string used deliberately here;
        # get_system_text() should returns those.
        text2 = u"text"
        c = Clipboard(contents={format_text: text2})
        c.copy_to_system(clear=True)
        self.assertEqual(Clipboard.get_system_text(), text2)

        # Test with text.
        text3 = u"testing"
        c = Clipboard(text=text3)
        c.copy_to_system(clear=True)
        self.assertEqual(Clipboard.get_system_text(), text3)

        # Test with an empty Clipboard instance.
        c = Clipboard()
        c.copy_to_system(clear=False)
        self.assertEqual(Clipboard.get_system_text(), text3)
        c.copy_to_system(clear=True)
        self.assertEqual(Clipboard.get_system_text(), u"")
Exemplo n.º 2
0
    def test_get_format(self):
        # Test with an empty Clipboard instance.
        c = Clipboard()
        self.assertRaises(ValueError, c.get_format, format_unicode)
        self.assertRaises(ValueError, c.get_format, format_text)
        self.assertRaises(ValueError, c.get_format, format_hdrop)

        # Test with one format.
        text1 = u"unicode text"
        c = Clipboard(contents={format_unicode: text1})
        self.assertEqual(c.get_format(format_unicode), text1)
        self.assertRaises(ValueError, c.get_format, format_text)
        self.assertRaises(ValueError, c.get_format, format_hdrop)

        # Test with two formats.
        text2 = b"text"
        c = Clipboard(contents={format_unicode: text1,
                                format_text: text2})
        self.assertEqual(c.get_format(format_unicode), text1)
        self.assertEqual(c.get_format(format_text), text2)
        self.assertRaises(ValueError, c.get_format, format_hdrop)

        # Test with format_hdrop.
        with NamedTemporaryFile() as f:
            c = Clipboard(contents={format_hdrop: f.name})
            self.assertRaises(ValueError, c.get_format, format_unicode)
            self.assertRaises(ValueError, c.get_format, format_text)
            self.assertEqual(c.get_format(format_hdrop), (f.name,))
Exemplo n.º 3
0
    def test_backwards_compatibility(self):
        # The multi-platform class should be backwards compatible with the
        # Windows-only Clipboard class, at least for the constructor.
        text = u"unicode text"
        c = Clipboard(contents={13: text})
        c.copy_to_system()
        self.assertEqual(Clipboard.get_system_text(), text)

        # Test with the CF_TEXT format (1)
        text = "text"
        c = Clipboard(contents={1: text})
        c.copy_to_system()
        self.assertEqual(Clipboard.get_system_text(), text)
Exemplo n.º 4
0
    def test_copy_from_system(self):
        text = "testing"
        Clipboard.set_system_text(text)
        c = Clipboard()

        # Test the method with clear=False (default)
        c.copy_from_system(clear=False)
        self.assertEqual(c.text, text)
        self.assertEqual(Clipboard.get_system_text(), text)

        # Test again with clear=True
        c = Clipboard()
        c.copy_from_system(clear=True)
        self.assertEqual(c.text, text)
        self.assertEqual(Clipboard.get_system_text(), "")
Exemplo n.º 5
0
def select_string(include_quotes):
    mouse("left/3").execute()
    clipboard = Clipboard()
    saved_text = clipboard.get_system_text()
    clipboard.set_system_text('')
    left_counter = 0
    while left_counter < 50:
        key("s-left, c-c/3").execute()
        left_counter += 1
        if clipboard.get_system_text().startswith("\""):
            break

    key("left").execute()
    move_right = left_counter
    if not include_quotes:
        move_right -= 1
        key("right").execute()
    key("s-right:%s" % move_right).execute()

    right_counter = 0
    while right_counter < 50:
        key("s-right, c-c/3").execute()
        right_counter += 1
        if clipboard.get_system_text().endswith("\""):
            break

    if not include_quotes:
        key("s-left").execute()

    clipboard.set_text(saved_text)
    clipboard.copy_to_system()
Exemplo n.º 6
0
def stoosh_keep_clipboard(nnavi500, nexus):
    if nnavi500 == 1:
        Key("c-c").execute()
    else:
        max_tries = 20
        cb = Clipboard(from_system=True)
        Key("c-c").execute()
        key = str(nnavi500)
        for i in range(0, max_tries):
            failure = False
            try:
                # time for keypress to execute
                time.sleep(
                    settings.SETTINGS["miscellaneous"]["keypress_wait"] /
                    1000.)
                nexus.clip[key] = Clipboard.get_system_text()
                utilities.save_json_file(
                    nexus.clip,
                    settings.SETTINGS["paths"]["SAVED_CLIPBOARD_PATH"])
            except Exception:
                failure = True
                utilities.simple_log()
            if not failure:
                break
        cb.copy_to_system()
Exemplo n.º 7
0
 def test_set_text(self):
     c = Clipboard()
     text = "test"
     c.set_text(text)
     self.assertTrue(c.has_text())
     self.assertEqual(c.get_text(), text)
     self.assertEqual(c.text, text)
Exemplo n.º 8
0
    def test_set_format(self):
        # Test with one format.
        text1 = u"unicode text"
        c = Clipboard()
        c.set_format(format_unicode, text1)
        self.assertTrue(c.has_format(format_unicode))
        self.assertEqual(c.get_format(format_unicode), text1)
        self.assertFalse(c.has_format(format_text))
        self.assertRaises(ValueError, c.get_format, format_text)

        # Test with two formats.
        text2 = b"text"
        c.set_format(format_text, text2)
        self.assertTrue(c.has_format(format_unicode))
        self.assertEqual(c.get_format(format_unicode), text1)
        self.assertTrue(c.has_format(format_text))
        self.assertEqual(c.get_format(format_text), text2)

        # Setting a format to None removes the format's content from the
        # instance.
        c.set_format(format_text, None)
        self.assertFalse(c.has_format(format_text))
        self.assertRaises(ValueError, c.get_format, format_text)
        c.set_format(format_unicode, None)
        self.assertFalse(c.has_format(format_unicode))
        self.assertRaises(ValueError, c.get_format, format_unicode)
Exemplo n.º 9
0
 def test_from_system_argument(self):
     # Test the optional from_system argument of Clipboard.__init__
     text = "something"
     Clipboard.set_system_text(text)
     c = Clipboard(from_system=True)
     self.assertEqual(c.text, text)
     self.assertTrue(c.has_text())
Exemplo n.º 10
0
    def test_get_format(self):
        # Test with an empty Clipboard instance.
        c = Clipboard()
        self.assertRaises(ValueError, c.get_format, format_unicode)
        self.assertRaises(ValueError, c.get_format, format_text)

        # Test with one format.
        text1 = u"unicode text"
        c = Clipboard(contents={format_unicode: text1})
        self.assertEqual(c.get_format(format_unicode), text1)
        self.assertRaises(ValueError, c.get_format, format_text)

        # Test with two formats.
        text2 = b"text"
        c = Clipboard(contents={format_unicode: text1, format_text: text2})
        self.assertEqual(c.get_format(format_unicode), text1)
        self.assertEqual(c.get_format(format_text), text2)
Exemplo n.º 11
0
def duple_keep_clipboard(nnavi50):
    cb = Clipboard(from_system=True)
    Key("escape, home, s-end, c-c, end").execute()
    time.sleep(settings.SETTINGS["miscellaneous"]["keypress_wait"] / 1000.)
    for i in range(0, nnavi50):
        Key("enter, c-v").execute()
        time.sleep(settings.SETTINGS["miscellaneous"]["keypress_wait"] / 1000.)
    cb.copy_to_system()
Exemplo n.º 12
0
    def test_has_format(self):
        # Test with an empty Clipboard instance.
        c = Clipboard()
        self.assertFalse(c.has_format(format_unicode))
        self.assertFalse(c.has_format(format_text))
        self.assertFalse(c.has_format(format_hdrop))

        # Test with one format.
        c = Clipboard(contents={format_unicode: u"unicode text"})
        self.assertTrue(c.has_format(format_unicode))
        self.assertFalse(c.has_format(format_text))

        # Test with two formats.
        c = Clipboard(contents={format_unicode: u"unicode text",
                                format_text: u"text"})
        self.assertTrue(c.has_format(format_unicode))
        self.assertTrue(c.has_format(format_text))
Exemplo n.º 13
0
def get_selected_text():
    clipboard = Clipboard()
    previous = clipboard.get_system_text()
    clipboard.set_system_text("")
    Key("c-c/3").execute()
    selected = clipboard.get_system_text()
    clipboard.set_text(previous)
    clipboard.copy_to_system()
    return selected
Exemplo n.º 14
0
    def test_flexible_string_types(self):
        # This is similar to the clipboard format conversion that Windows
        # performs when necessary. The Clipboard class should do this
        # regardless of platform/implementation.

        # Binary strings used with format_unicode are converted for us.
        c = Clipboard(contents={format_unicode: b"text"})
        self.assertEqual(c.get_format(format_unicode), u"text")
        c = Clipboard()
        c.set_format(format_unicode, b"text")
        self.assertEqual(c.get_format(format_unicode), u"text")

        # Text strings used with format_text (ANSI) are converted for us.
        c = Clipboard(contents={format_text: u"text"})
        self.assertEqual(c.get_format(format_text), b"text")
        c = Clipboard()
        c.set_format(format_text, u"text")
        self.assertEqual(c.get_format(format_text), b"text")
Exemplo n.º 15
0
def _select_and_cut_text(wordCount):
    """Selects wordCount number of words to the left of the cursor and cuts
    them out of the text. Returns the text from the system clip board.

    """
    clipboard = Clipboard()
    clipboard.set_system_text('')
    Key('cs-left/3:%s/10, c-x/10' % wordCount).execute()
    return clipboard.get_system_text()
Exemplo n.º 16
0
    def test_has_text(self):
        # Test with an empty Clipboard instance.
        c = Clipboard()
        self.assertFalse(c.has_text())

        # Test with format_unicode only.
        c = Clipboard(contents={format_unicode: u"unicode text"})
        self.assertTrue(c.has_text())

        # Test with both text formats.
        c = Clipboard(contents={
            format_unicode: u"unicode text",
            format_text: b"text"
        })
        self.assertTrue(c.has_text())

        # Test with format_text only.
        c = Clipboard(contents={format_text: b"text"})
        self.assertTrue(c.has_text())
Exemplo n.º 17
0
def paste_string(content):
    time.sleep(0.05)
    cb = Clipboard(from_system=True)
    try:
        Clipboard.set_system_text(str(content))
        Key("c-v").execute()
        time.sleep(0.05)
        cb.copy_to_system()
    except Exception:
        return False
    return True
Exemplo n.º 18
0
    def test_set_text(self):
        c = Clipboard()
        text = "test"
        c.set_text(text)
        self.assertTrue(c.has_text())
        self.assertEqual(c.get_text(), text)
        self.assertEqual(c.text, text)

        # Setting the text to None clears the stored text.
        c.set_text(None)
        self.assertFalse(c.has_text())
        self.assertIs(c.get_text(), None)
Exemplo n.º 19
0
def paste_string(content):
    cb = Clipboard(from_system=True)
    time.sleep(SETTINGS["keypress_wait"])
    try:
        Clipboard.set_system_text(str(content))
        time.sleep(SETTINGS["keypress_wait"])
        Key("c-v").execute()
        time.sleep(SETTINGS["keypress_wait"])
        cb.copy_to_system()
    except Exception:
        return False
    return True
Exemplo n.º 20
0
    def test_copy_from_system(self):
        text = "testing"
        Clipboard.set_system_text(text)
        c = Clipboard()

        # Test the method with clear=False (default).
        c.copy_from_system(clear=False)
        self.assertEqual(c.text, text)
        self.assertEqual(Clipboard.get_system_text(), text)

        # Test again with clear=True.
        c = Clipboard()
        c.copy_from_system(clear=True)
        self.assertEqual(c.text, text)
        self.assertEqual(Clipboard.get_system_text(), "")

        # Test formats=format_unicode.
        # Set the system clipboard before testing.
        text1 = u"unicode text"
        c1 = Clipboard(contents={format_unicode: text1})
        c1.copy_to_system()
        c2 = Clipboard()
        c2.copy_from_system(formats=format_unicode)
        self.assertTrue(c2.has_format(format_unicode))
        self.assertEqual(c2.get_format(format_unicode), text1)

        # Test formats=format_text.
        # Set the system clipboard before testing.
        text2 = b"text"
        c1 = Clipboard(contents={format_text: text2})
        c1.copy_to_system()
        c2 = Clipboard()
        c2.copy_from_system(formats=format_text)
        self.assertTrue(c2.has_format(format_text))
        self.assertEqual(c2.get_format(format_text), text2)

        # Test formats=(format_unicode, format_text).
        # Set the system clipboard before testing. Use the same string for
        # both formats so the test will work on all platforms.
        c1 = Clipboard(contents={
            format_text: b"text",
            format_unicode: u"text"
        })
        c1.copy_to_system()
        c2 = Clipboard()
        c2.copy_from_system(formats=(format_unicode, format_text))
        self.assertTrue(c2.has_format(format_unicode))
        self.assertEqual(c2.get_format(format_unicode), u"text")
        self.assertTrue(c2.has_format(format_text))
        self.assertEqual(c2.get_format(format_text), b"text")
Exemplo n.º 21
0
    def test_get_available_formats(self):
        # Test with an empty Clipboard instance.
        c = Clipboard()
        self.assertEqual(c.get_available_formats(), [])

        # Test with one format.
        c = Clipboard(contents={format_unicode: u"unicode text"})
        self.assertEqual(c.get_available_formats(), [format_unicode])

        # Test with two formats.
        c = Clipboard(contents={format_unicode: u"unicode text",
                                format_text: u"text"})
        self.assertEqual(c.get_available_formats(), [format_unicode,
                                                     format_text],
                         "format_unicode should precede format_text")

        # Test with text formats and one custom format. The preferred text
        # format should be first on the list.
        format_custom = 0
        c = Clipboard(contents={format_custom: 123,
                                format_unicode: u"unicode text"})
        self.assertEqual(c.get_available_formats(), [format_unicode,
                                                     format_custom],
                         "format_unicode should precede format_custom")
        c = Clipboard(contents={format_custom: 123,
                                format_text: b"text"})
        self.assertEqual(c.get_available_formats(), [format_text,
                                                     format_custom],
                         "format_text should precede format_custom")

        # Test with no text format and one custom format.
        c = Clipboard(contents={format_custom: 123})
        self.assertEqual(c.get_available_formats(), [format_custom])
Exemplo n.º 22
0
    def test_has_text(self):
        # Test with an empty Clipboard instance.
        c = Clipboard()
        self.assertFalse(c.has_text())

        # Test with format_unicode only.
        c = Clipboard(contents={format_unicode: u"unicode text"})
        self.assertTrue(c.has_text())

        # Test with both text formats.
        c = Clipboard(contents={format_unicode: u"unicode text",
                                format_text: b"text"})
        self.assertTrue(c.has_text())

        # Test with format_text only.
        c = Clipboard(contents={format_text: b"text"})
        self.assertTrue(c.has_text())

        # Test with format_hdrop only.
        with NamedTemporaryFile() as f:
            c = Clipboard(contents={format_hdrop: f.name})
            self.assertFalse(c.has_text())
Exemplo n.º 23
0
    def test_get_text(self):
        # Test with an empty Clipboard instance.
        c = Clipboard()
        self.assertIsNone(c.get_text())

        # Test with Unicode text.
        text1 = u"test"
        c.set_text(text1)
        self.assertEqual(c.get_text(), text1)

        # Test with text set to None.
        c.set_text(None)
        self.assertIsNone(c.get_text())

        # Test with binary text.
        text2 = b"test"
        c.set_text(text2)
        self.assertEqual(c.get_text(), text2)

        # Test with format_hdrop.
        with NamedTemporaryFile() as f:
            c = Clipboard(contents={format_hdrop: f.name})
            self.assertIsNone(c.get_text())
Exemplo n.º 24
0
def read_selected(same_is_okay=False):
    time.sleep(SETTINGS["keypress_wait"])
    cb = Clipboard(from_system=True)
    temporary = None
    prior_content = None
    try:
        prior_content = Clipboard.get_system_text()
        Clipboard.set_system_text("")
        Key("c-c").execute()
        time.sleep(SETTINGS["keypress_wait"])
        temporary = Clipboard.get_system_text()
        cb.copy_to_system()
    except Exception:
        return None
    if prior_content == temporary and not same_is_okay:
        return None
    return temporary
Exemplo n.º 25
0
    def test_get_text(self):
        # Test with an empty Clipboard instance.
        c = Clipboard()
        self.assertIsNone(c.get_text())

        # Test with Unicode text.
        text1 = u"test"
        c.set_text(text1)
        self.assertEqual(c.get_text(), text1)

        # Test with text set to None.
        c.set_text(None)
        self.assertIsNone(c.get_text())

        # Test with binary text.
        text2 = b"test"
        c.set_text(text2)
        self.assertEqual(c.get_text(), text2)
Exemplo n.º 26
0
    def test_non_strings(self):
        # Using non-string objects for text formats raises errors.
        self.assertRaises(TypeError, Clipboard, {format_text: 0})
        self.assertRaises(TypeError, Clipboard, {format_unicode: 0})
        self.assertRaises(TypeError, Clipboard, {format_text: object()})
        self.assertRaises(TypeError, Clipboard, {format_unicode: object()})
        self.assertRaises(TypeError, Clipboard, {format_text: None})
        self.assertRaises(TypeError, Clipboard, {format_unicode: None})

        c = Clipboard()
        self.assertRaises(TypeError, c.set_text, 0)
        self.assertRaises(TypeError, c.set_text, object())
        self.assertRaises(TypeError, c.set_format, format_text, 0)
        self.assertRaises(TypeError, c.set_format, format_text, object())
        self.assertRaises(TypeError, c.set_format, format_unicode, 0)
        self.assertRaises(TypeError, c.set_format, format_unicode, object())
        self.assertRaises(TypeError, c.set_system_text, 0)
        self.assertRaises(TypeError, c.set_system_text, object())
Exemplo n.º 27
0
def _select_and_cut_text(wordCount):
    """Selects wordCount number of words to the left of the cursor and cuts
    them out of the text. Returns the text from the system clip board.

    """
    clipboard = Clipboard()
    clipboard.set_system_text('')
    try:  # Try selecting n number of words.
        Key('ctrl:down, shift:down').execute()
        Key('left:%s' % wordCount).execute()
        Key('shift:up').execute()
    finally:
        # It is important to make sure that the buttons are released.
        # Otherwise you get stuck in an unpleasant situation.
        Key('shift:up, ctrl:up').execute()
    Pause("10").execute()
    Key('c-x').execute()  # Cut out the selected words.
    Pause("20").execute()
    return clipboard.get_system_text()
Exemplo n.º 28
0
def read_selected(same_is_okay=False):
    '''Returns a tuple:
    (0, "text from system") - indicates success
    (1, None) - indicates no change
    (2, None) - indicates clipboard error
    '''
    time.sleep(0.05)
    cb = Clipboard(from_system=True)
    temporary = None
    prior_content = None
    try:
        prior_content = Clipboard.get_system_text()
        Clipboard.set_system_text("")
        Key("c-c").execute()
        time.sleep(0.05)
        temporary = Clipboard.get_system_text()
        cb.copy_to_system()
    except Exception:
        return 2, None
    if prior_content == temporary and not same_is_okay:
        return 1, None
    return 0, temporary
Exemplo n.º 29
0
def drop_keep_clipboard(nnavi500, nexus):
    if nnavi500 == 1:
        Key("c-v").execute()
    else:
        max_tries = 20
        key = str(nnavi500)
        cb = Clipboard(from_system=True)
        for i in range(0, max_tries):
            failure = False
            try:
                if key in nexus.clip:
                    Clipboard.set_system_text(nexus.clip[key])
                    Key("c-v").execute()
                    time.sleep(
                        settings.SETTINGS["miscellaneous"]["keypress_wait"] /
                        1000.)
                else:
                    dragonfly.get_engine().speak("slot empty")
            except Exception:
                failure = True
            if not failure:
                break
        cb.copy_to_system()
Exemplo n.º 30
0
 def setUpClass(cls):
     # Save the current contents of the clipboard.
     cls.clipboard = Clipboard(from_system=True)