annotate src/plugins/plugin_misc_file.py @ 1555:eb8aae35085b

plugin ip: local ip cache + DNS error detection
author Goffi <goffi@goffi.org>
date Mon, 02 Nov 2015 22:02:41 +0100
parents ebf97c1ac14a
children 1f7a34d499e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for file tansfer
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import os
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.internet import defer
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import uuid
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 PLUGIN_INFO = {
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 "name": "File Tansfer",
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 "import_name": "FILE",
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 "type": C.PLUG_TYPE_MISC,
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 "main": "FilePlugin",
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 "handler": "no",
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "description": _("""File Tansfer Management:
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 This plugin manage the various ways of sending a file, and choose the best one.""")
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 }
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
1553
ebf97c1ac14a fixed bad inheritance of SatFile
Goffi <goffi@goffi.org>
parents: 1525
diff changeset
40 class SatFile(object):
1525
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 """A file-like object to have high level files manipulation"""
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 # TODO: manage "with" statement
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def __init__(self, host, path, mode='r', uid=None, size=None, profile=C.PROF_KEY_NONE):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 """
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 @param host: %(doc_host)s
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @param path(str): path of the file to get
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 @param mode(str): same as for built-in "open" function
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @param uid(unicode, None): unique id identifing this progressing element
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 will be automaticaly generated if None
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 @param size(None, int): size of the file
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 """
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.host = host
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.uid = uid or unicode(uuid.uuid4())
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self._file = open(path, mode)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.size = None
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.profile = profile
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.eof = defer.Deferred()
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.host.registerProgressCb(self.uid, self.getProgress, profile)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.host.bridge.progressStarted(self.uid, self.profile)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.eof.addCallback(lambda ignore: self.host.bridge.progressFinished(self.uid, self.profile))
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.eof.addErrback(lambda failure: self.host.bridge.progressError(self.uid, unicode(failure), self.profile))
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def close(self):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self._file.close()
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.host.removeProgressCb(self.uid, self.profile)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def read(self, size=-1):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 read = self._file.read(size)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 if not read:
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.eof.callback(None)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return read
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def seek(self, offset, whence=os.SEEK_SET):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self._file.seek(offset, whence)
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def tell(self):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 return self._file.tell()
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def getProgress(self, progress_id, data, profile):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return {'position': self._file.tell(), 'size': self.size or 0}
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 class FilePlugin(object):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 File=SatFile
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def __init__(self, host):
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 log.info(_("plugin File initialization"))
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.host = host
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
49d33cb48207 plugin file: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91