File: uclipget.py

package info (click to toggle)
far2l 2.7.0~beta%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,304 kB
  • sloc: cpp: 263,566; ansic: 53,886; python: 7,048; sh: 1,516; perl: 410; javascript: 279; xml: 145; makefile: 31
file content (63 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import logging

import far2lc
from far2l.plugin import PluginBase

log = logging.getLogger(__name__)

class Plugin(PluginBase):
    label = "Python Clip GET"
    openFrom = ["PLUGINSMENU", 'FILEPANEL']

    def CopyFiles(self, data):
        pwd = self.panel.GetPanelDir()
        data = data.decode('utf8')
        log.debug(f'copyfiles: {data}')
        prefix = 'file://'
        for uri in data.split('\n'):
            if uri[:len(prefix)] != prefix:
                continue
            fqname = uri[len(prefix):]
            fname = fqname.split('/')[-1]
            dqname = os.path.join(pwd, fname)
            log.debug(f'CopyFile: {fqname} -> {dqname}')
            with open(dqname, 'wb') as fo:
                with open(fqname, 'rb') as fi:
                    while True:
                        rec = fi.read(4096)
                        if not rec:
                            break
                        fo.write(rec)

    def OpenPlugin(self, OpenFrom):
        winport = self.ffi.cast("struct WINPORTDECL *", far2lc.WINPORT())
        clipurifmt = winport.RegisterClipboardFormat("text/uri-list")
        if not clipurifmt:
            log.error('uclipset.ClipboardRegisterFormat.1')
            return
        clipgnofmt = winport.RegisterClipboardFormat("x-special/gnome-copied-files")
        if not clipgnofmt:
            log.error('uclipset.ClipboardRegisterFormat.2')
            return
        if not winport.OpenClipboard(self.ffi.NULL):
            log.error('uclipset.OpenClipboard')
            return
        try:
            data = winport.GetClipboardData(clipurifmt)
            if data is not None:
                nb = winport.ClipboardSize(data)
                result = self.ffi.buffer(self.ffi.cast("PSTR", data), nb)
                self.CopyFiles(bytes(result))
            else:
                data = winport.GetClipboardData(clipgnofmt)
                if data is not None:
                    nb = winport.ClipboardSize(data)
                    result = self.ffi.buffer(self.ffi.cast("PSTR", data), nb)
                    self.CopyFiles(bytes(result))
        except:
            log.exception('uclipset.GetClipboardData')
        finally:
            if not winport.CloseClipboard():
                log.error('uclipset.CloseClipboard')
                return