~timo-jyrinki/ubuntu/trusty/pitivi/backport_utopic_fixes

« back to all changes in this revision

Viewing changes to pitivi/utils/system.py

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2014-03-29 15:22:50 UTC
  • mto: (3.1.23 experimental)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20140329152250-flg9onx416bqf3e3
Tags: upstream-0.93
Import upstream version 0.93

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Lesser General Public License for more details.
16
16
#
17
17
# You should have received a copy of the GNU Lesser General Public
18
 
# License along with this program; if not, write to the
19
 
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
 
# Boston, MA 02111-1307, USA.
21
 
 
22
 
 
 
18
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
 
 
21
import datetime
 
22
import multiprocessing
23
23
import os
 
24
import resource
24
25
 
 
26
from pitivi.check import NOTIFY_SOFT_DEPENDENCY
25
27
from pitivi.configure import APPNAME
26
28
from pitivi.utils.loggable import Loggable
27
29
from pitivi.utils.signal import Signallable
38
40
 
39
41
    def __init__(self):
40
42
        Loggable.__init__(self)
41
 
        self.log("new object" + str(self))
 
43
        self.log("new object " + str(self))
42
44
        self._reset()
43
45
 
44
46
    def _reset(self):
47
49
 
48
50
    #generic functions
49
51
    def _inhibit(self, list_, key):
50
 
        if key is None or (not isinstance(key, str)):
51
 
            assert False
52
 
 
 
52
        assert key is not None
 
53
        assert isinstance(key, str)
53
54
        if not key in list_:
54
55
            list_.append(key)
55
56
            self.log("emitting 'update-power-inhibition'")
62
63
                self.log("emitting 'update-power-inhibition'")
63
64
                self.emit('update-power-inhibition')
64
65
        else:
65
 
            if not isinstance(key, str):
66
 
                assert False
67
 
 
 
66
            assert isinstance(key, str)
68
67
            if key in list_:
69
68
                list_.remove(key)
70
69
                self.log("emitting 'update-power-inhibition'")
71
70
                self.emit('update-power-inhibition')
72
71
 
73
 
    def _listToString(self, list_):
74
 
        keys = ""
75
 
        for key in list_:
76
 
            if keys != "":
77
 
                keys += ", "
78
 
            keys += key
79
 
 
80
 
        return keys
81
 
 
82
72
    def _isInhibited(self, list_, key=None):
83
73
        if key is None:
84
 
            if len(list_) > 0:
 
74
            if list_:
85
75
                return True
86
76
        elif key in list_:
87
77
            return True
114
104
 
115
105
    def getScreensaverInhibitors(self):
116
106
        """returns a comma seperated string of screensaver inhibitor keys"""
117
 
        return self._listToString(self._screensaver_keys)
 
107
        return ", ".join(self._screensaver_keys)
118
108
 
119
109
    def screensaverIsBlockable(self):
120
110
        return False
145
135
 
146
136
    def getSleepInhibitors(self):
147
137
        """returns a comma seperated string of sleep inhibitor keys"""
148
 
        return self._listToString(self._sleep_keys)
 
138
        return ", ".join(self._sleep_keys)
149
139
 
150
140
    def sleepIsBlockable(self):
151
141
        return False
154
144
    def uninhibitAll(self):
155
145
        self._reset()
156
146
        self.emit('update-power-inhibition')
157
 
        pass
158
147
 
159
 
    def desktopMessage(self, title, message, icon=None):
 
148
    def desktopMessage(self, title, message, unused_icon=None):
160
149
        """send a message to the desktop to be displayed to the user
161
150
        @arg title: C{str} the title of the message
162
151
        @arg message: C{str} the body of the message
163
 
        @arg icon: C{GdkPixbuf.Pixbuf} icon to be shown with the message
 
152
        @arg icon: C{str} icon to be shown with the message
164
153
        """
165
 
        self.debug("desktopMessage(): %s, %s" % title % message)
166
 
        pass
 
154
        self.debug("desktopMessage(): %s, %s", title, message)
 
155
        return None
167
156
 
168
157
    def getUniqueFilename(self, string):
169
158
        """Get a filename which can only be obtained from the specified string.
179
168
 
180
169
    def __init__(self):
181
170
        System.__init__(self)
182
 
        # FIXME Notifications disabled for the time being
183
 
        # Notify.init(APPNAME)
 
171
        if NOTIFY_SOFT_DEPENDENCY:
 
172
            from gi.repository import Notify
 
173
            Notify.init(APPNAME)
184
174
 
185
 
    def desktopMessage(self, title, message, icon=None):
 
175
    def desktopMessage(self, title, message, icon="pitivi"):
186
176
        #call super method for consistent logging
187
 
        System.desktopMessage(title, message, icon)
 
177
        System.desktopMessage(self, title, message, icon)
188
178
 
189
 
        # FIXME Notifications disabled for the time being
190
 
        #notification = Notify.Notification(title, message)
191
 
        #if icon is not None and isinstance(icon, GdkPixbuf.Pixbuf):
192
 
            #notification.set_icon_from_pixbuf(icon)
193
 
        #notification.show()
 
179
        if NOTIFY_SOFT_DEPENDENCY:
 
180
            from gi.repository import Notify
 
181
            notification = Notify.Notification.new(title, message, icon=icon)
 
182
            try:
 
183
                notification.show()
 
184
            except RuntimeError, e:
 
185
                # This can happen if the system is not properly configured.
 
186
                # See for example https://bugzilla.gnome.org/show_bug.cgi?id=719627.
 
187
                self.error("desktopMessage: Failed displaying notification: %s", e.message)
 
188
                return None
 
189
            return notification
 
190
        return None
194
191
 
195
192
 
196
193
#org.gnome.SessionManager flags
275
272
if os.name == 'posix':
276
273
    if 'GNOME_DESKTOP_SESSION_ID' in os.environ:
277
274
        try:
278
 
            # FIXME Disable notifications for the time being as it causes
279
 
            # various errors and the implementation is not done yet
280
 
            #from gi.repository import Notify
281
275
            import dbus
282
276
            system_ = GnomeSystem
283
277
        except:
284
278
            pass
285
279
 
286
280
    if system_ is None:
287
 
        try:
288
 
            # FIXME Disable notifications for the time being as it causes
289
 
            # various errors and the implementation is not done yet
290
 
            # from gi.repository import Notify
291
 
            system_ = FreedesktopOrgSystem
292
 
        except:
293
 
            pass
 
281
        system_ = FreedesktopOrgSystem
294
282
elif os.name == 'nt':
295
283
    pass
296
284
elif os.name == 'mac':
306
294
        system = System()
307
295
 
308
296
    return system
 
297
 
 
298
 
 
299
class CPUUsageTracker(object):
 
300
    def __init__(self):
 
301
        self.reset()
 
302
 
 
303
    def usage(self):
 
304
        delta_time = (datetime.datetime.now() - self.last_moment).total_seconds()
 
305
        delta_usage = resource.getrusage(resource.RUSAGE_SELF).ru_utime - self.last_usage.ru_utime
 
306
        usage = float(delta_usage) / delta_time * 100
 
307
        return usage / multiprocessing.cpu_count()
 
308
 
 
309
    def reset(self):
 
310
        self.last_moment = datetime.datetime.now()
 
311
        self.last_usage = resource.getrusage(resource.RUSAGE_SELF)