summaryrefslogtreecommitdiff
path: root/efl/ecore/efl.ecore_file_download.pxi
blob: 3fd7d6a59acf22fede0c8d4861aead853dc2b1db (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright (C) 2007-2013 various contributors (see AUTHORS)
#
# This file is part of Python-EFL.
#
# Python-EFL is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# Python-EFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this Python-EFL.  If not, see <http://www.gnu.org/licenses/>.


cdef void _completion_cb(void *data, const_char_ptr file, int status) with gil:
    obj = <FileDownload>data
    try:
        obj._exec_completion(file, status)
    except Exception, e:
        traceback.print_exc()

cdef int _progress_cb(void *data, const_char_ptr file, long int dltotal,
                    long int dlnow, long int ultotal, long int ulnow) with gil:
    obj = <FileDownload>data
    try:
        return obj._exec_progress(file, dltotal, dlnow, ultotal, ulnow)
    except Exception, e:
        traceback.print_exc()


cdef class FileDownload(object):
    """ Download the given url to destination.

    You must provide the full url, including 'http://', 'ftp://' or 'file://'.
    If ``dst`` already exist it will not be overwritten and the function will fail.
    Ecore must be compiled with CURL to download using http and ftp protocols.
    The ``status`` param in the ``completion_cb`` will be 0 if the download
    end successfully or 1 in case of failure.
    The ``progress_cb`` must return 0 to continue normally or 1 to abort
    the download, note that no *completion_cb* will be called if you abort in
    this way.
    The constructor will raise a SystemError exception if the download don't
    start, this can happen if you don't have CURL compiled, if the destination
    exists or if the destination path don't exist or isn't writable.

    The callback signatures are::

        completion_cb(file, status, *args, **kargs)
        progress_cb(file, dltotal, dlnow, uptotal, upnow, *args, **kargs): int

    You can use the download feature as a class instance or you can use
    the legacy API.

    Instance example::
    
        from efl.ecore import FileDownload
        dl = FileDownload("http://your_url", "/path/to/destination", None, None)
        dl.abort()

    Legacy example::

        from efl import ecore
        dl = ecore.file_download("http://your_url",
                                 "/path/to/destination", None, None)
        ecore.file_download_abort(dl)

    :param url: The complete url to download
    :param dst: Where to download the file
    :param completion_cb: A callback called on download complete
    :param progress_cb: A callback called during the download operation

    """
    def __init__(self, url, dst, completion_cb, progress_cb, *args, **kargs):
        cdef Ecore_File_Download_Job *job

        if completion_cb is not None and not callable(completion_cb):
            raise TypeError("Parameter 'completion_cb' must be callable, or None")

        if progress_cb is not None and not callable(progress_cb):
            raise TypeError("Parameter 'progress_cb' must be callable, or None")

        self.completion_cb = completion_cb
        self.progress_cb = progress_cb
        self.args = args
        self.kargs = kargs

        if not ecore_file_download(_cfruni(url), _cfruni(dst),
                                   _completion_cb, _progress_cb,
                                   <void *>self, &job):
            raise SystemError("could not download '%s' to %s" % (url, dst))
            
        self.job = job
        Py_INCREF(self)

    def __str__(self):
        return "%s(completion_cb=%s, progress_cb=%s args=%s, kargs=%s)" % \
               (self.__class__.__name__, self.completion_cb,
                self.progress_cb, self.args, self.kargs)

    def __repr__(self):
        return ("%s(%#x, completion_cb=%s, progress_cb=%s, args=%s, kargs=%s, "
                "Ecore_File_Download_Job=%#x, refcount=%d)") % \
               (self.__class__.__name__, <unsigned long><void *>self,
                self.completion_cb, self.progress_cb, self.args, self.kargs,
                <unsigned long>self.job, PY_REFCOUNT(self))

    def __dealloc__(self):
        if self.job != NULL:
            ecore_file_download_abort(self.job)
            self.job = NULL
        self.completion_cb = None
        self.progress_cb = None
        self.args = None
        self.kargs = None

    cdef object _exec_completion(self, const_char_ptr file, int status):
        if self.completion_cb:
            self.completion_cb(file, status, *self.args, **self.kargs)

    cdef object _exec_progress(self, const_char_ptr file, long int dltotal,
                            long int dlnow, long int ultotal, long int ulnow):
        if self.progress_cb:
            return self.progress_cb(file, dltotal, dlnow, ultotal, ulnow,
                                    *self.args, **self.kargs)
        return 0

    def abort(self):
        """Abort the download and free internal resources."""
        if self.job != NULL:
            ecore_file_download_abort(self.job)
            self.job = NULL
            Py_DECREF(self)


def file_download(url, dst, completion_cb, progress_cb, *args, **kargs):
    """
    efl.ecore.FileDownload} factory, for C-api compatibility.

    :param url: The complete url to download
    :param dst: Where to download the file
    :param completion_cb: The function called when the download end
                          Expected signature::
                            completion_cb(file, status, *args, **kargs)
    :param progress_cb: The function to called while the download progress
                        advance. Ecpected signature::
                            progress_cb(file, dltotal, dlnow, uptotal, upnow, *args, **kargs): int

    :return: a new FileDownload instance
    :rtype: `efl.ecore.Download`
    """
    return FileDownload(url, dst, completion_cb, progress_cb, *args, **kargs)

def file_download_abort(instance):
    """
    C-api compatibility
    Abort the given download an free internal resources
    """
    instance.abort()

def file_download_abort_all():
    """
    This will abort all the download currently in progrss, use with caution.
    """
    ecore_file_download_abort_all()

def file_download_protocol_available(protocol):
    """
    Check if the given download protocol is available, available protocols
    are: "http://", "ftp://" and "file://". Note that ecore can be
    compiled without CURL support and thus http and ftp could not be available

    :param protocol: The protocol to check, ex: "http://"
    :type protocol: str
    :return: True if the protocol is supported
    :rtype: bool
    """
    return bool(ecore_file_download_protocol_available(protocol))