~yacinechaouche/+junk/BZR

« back to all changes in this revision

Viewing changes to CODE/FB/PASSWDMNGR/clipboard.py

  • Committer: yacinechaouche at yahoo
  • Date: 2015-01-14 22:23:03 UTC
  • Revision ID: yacinechaouche@yahoo.com-20150114222303-6gbtqqxii717vyka
Ajout de CODE et PROD. Il faudra ensuite ajouter ce qu'il y avait dan TMP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#The following is a python module made by Albert Sweigart.
 
2
 
 
3
import platform, os
 
4
 
 
5
def winGetClipboard():
 
6
    ctypes.windll.user32.OpenClipboard(0)
 
7
    pcontents = ctypes.windll.user32.GetClipboardData(1) # 1 is CF_TEXT
 
8
    data = ctypes.c_char_p(pcontents).value
 
9
    #ctypes.windll.kernel32.GlobalUnlock(pcontents)
 
10
    ctypes.windll.user32.CloseClipboard()
 
11
    return data
 
12
 
 
13
def winSetClipboard(text):
 
14
    GMEM_DDESHARE = 0x2000
 
15
    ctypes.windll.user32.OpenClipboard(0)
 
16
    ctypes.windll.user32.EmptyClipboard()
 
17
    try:
 
18
        # works on Python 2 (bytes() only takes one argument)
 
19
        hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
 
20
    except TypeError:
 
21
        # works on Python 3 (bytes() requires an encoding)
 
22
        hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, 'ascii'))+1)
 
23
    pchData = ctypes.windll.kernel32.GlobalLock(hCd)
 
24
    try:
 
25
        # works on Python 2 (bytes() only takes one argument)
 
26
        ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
 
27
    except TypeError:
 
28
        # works on Python 3 (bytes() requires an encoding)
 
29
        ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text, 'ascii'))
 
30
    ctypes.windll.kernel32.GlobalUnlock(hCd)
 
31
    ctypes.windll.user32.SetClipboardData(1,hCd)
 
32
    ctypes.windll.user32.CloseClipboard()
 
33
 
 
34
def macSetClipboard(text):
 
35
    outf = os.popen('pbcopy', 'w')
 
36
    outf.write(text)
 
37
    outf.close()
 
38
 
 
39
def macGetClipboard():
 
40
    outf = os.popen('pbpaste', 'r')
 
41
    content = outf.read()
 
42
    outf.close()
 
43
    return content
 
44
 
 
45
def gtkGetClipboard():
 
46
    return gtk.Clipboard().wait_for_text()
 
47
 
 
48
def gtkSetClipboard(text):
 
49
    cb = gtk.Clipboard()
 
50
    cb.set_text(text)
 
51
    cb.store()
 
52
 
 
53
def qtGetClipboard():
 
54
    return str(cb.text())
 
55
 
 
56
def qtSetClipboard(text):
 
57
    cb.setText(text)
 
58
 
 
59
def xclipSetClipboard(text):
 
60
    outf = os.popen('xclip -selection c', 'w')
 
61
    outf.write(text)
 
62
    outf.close()
 
63
 
 
64
def xclipGetClipboard():
 
65
    outf = os.popen('xclip -selection c -o', 'r')
 
66
    content = outf.read()
 
67
    outf.close()
 
68
    return content
 
69
 
 
70
def xselSetClipboard(text):
 
71
    outf = os.popen('xsel -i', 'w')
 
72
    outf.write(text)
 
73
    outf.close()
 
74
 
 
75
def xselGetClipboard():
 
76
    outf = os.popen('xsel -o', 'r')
 
77
    content = outf.read()
 
78
    outf.close()
 
79
    return content
 
80
 
 
81
 
 
82
if os.name == 'nt' or platform.system() == 'Windows':
 
83
    import ctypes
 
84
    getcb = winGetClipboard
 
85
    setcb = winSetClipboard
 
86
elif os.name == 'mac' or platform.system() == 'Darwin':
 
87
    getcb = macGetClipboard
 
88
    setcb = macSetClipboard
 
89
elif os.name == 'posix' or platform.system() == 'Linux':
 
90
    xclipExists = os.system('which xclip') == 0
 
91
    if xclipExists:
 
92
        getcb = xclipGetClipboard
 
93
        setcb = xclipSetClipboard
 
94
    else:
 
95
        xselExists = os.system('which xsel') == 0
 
96
        if xselExists:
 
97
            getcb = xselGetClipboard
 
98
            setcb = xselSetClipboard
 
99
        try: 
 
100
            import gtk
 
101
            getcb = gtkGetClipboard
 
102
            setcb = gtkSetClipboard
 
103
        except:
 
104
            try:
 
105
                import PyQt4.QtCore
 
106
                import PyQt4.QtGui
 
107
                app = QApplication([])
 
108
                cb = PyQt4.QtGui.QApplication.clipboard()
 
109
                getcb = qtGetClipboard
 
110
                setcb = qtSetClipboard
 
111
            except:
 
112
                raise Exception('Pyperclip requires the gtk or PyQt4 module installed, or the xclip command.')
 
113
copy = setcb
 
114
paste = getcb
 
115
 
 
116
#End of Module. The following is code that was personally written by me.