printrun-src/printrun/power/__init__.py

Wed, 20 Jan 2021 10:17:01 +0100

author
mdd
date
Wed, 20 Jan 2021 10:17:01 +0100
changeset 47
dcc64b767b64
parent 46
cce0af6351f0
permissions
-rw-r--r--

updated pronterface.py losing my own changes

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 import platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 if platform.system() == "Darwin":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 from .osx import inhibit_sleep_osx, deinhibit_sleep_osx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 inhibit_sleep = inhibit_sleep_osx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 deinhibit_sleep = deinhibit_sleep_osx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 elif platform.system() == "Windows":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 import ctypes
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 ES_CONTINUOUS = 0x80000000
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 ES_SYSTEM_REQUIRED = 0x00000001
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 def inhibit_sleep(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 mode = ES_CONTINUOUS | ES_SYSTEM_REQUIRED
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 ctypes.windll.kernel32.SetThreadExecutionState(ctypes.c_int(mode))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 def deinhibit_sleep():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 ctypes.windll.kernel32.SetThreadExecutionState(ctypes.c_int(ES_CONTINUOUS))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 import dbus
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 inhibit_sleep_handler = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 inhibit_sleep_token = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 bus = dbus.SessionBus()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 try:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
42 if os.environ.get('DESKTOP_SESSION') == "mate":
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
43 # Mate uses a special service
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
44 service_name = "org.mate.ScreenSaver"
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
45 object_path = "/org/mate/ScreenSaver"
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
46 else:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
47 # standard service name
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
48 service_name = "org.freedesktop.ScreenSaver"
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
49 object_path = "/org/freedesktop/ScreenSaver"
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
50 # GNOME and Mate use the right object path, try it first
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
51 proxy = bus.get_object(service_name, object_path)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 inhibit_sleep_handler = dbus.Interface(proxy, service_name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 # Do a test run
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 token = inhibit_sleep_handler.Inhibit("printrun", "test")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 inhibit_sleep_handler.UnInhibit(token)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 except dbus.DBusException:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 # KDE uses /ScreenSaver object path, let's try it as well
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 proxy = bus.get_object(service_name,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 "/ScreenSaver")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 inhibit_sleep_handler = dbus.Interface(proxy, service_name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 token = inhibit_sleep_handler.Inhibit("printrun", "test")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 inhibit_sleep_handler.UnInhibit(token)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 def inhibit_sleep(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 global inhibit_sleep_handler, inhibit_sleep_token
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 inhibit_sleep_token = inhibit_sleep_handler.Inhibit("printrun", reason)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 def deinhibit_sleep():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 global inhibit_sleep_handler, inhibit_sleep_token
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 if inhibit_sleep_handler is None or inhibit_sleep_token is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 inhibit_sleep_handler.UnInhibit(inhibit_sleep_token)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 inhibit_sleep_token = None
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
74 except Exception as e:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 logging.warning("Could not setup DBus for sleep inhibition: %s" % e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 def inhibit_sleep(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 def deinhibit_sleep():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 import psutil
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 def get_nice(nice, p = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 if not p: p = psutil.Process(os.getpid())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 if callable(p.nice):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 return p.nice()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 return p.nice
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 def set_nice(nice, p = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 if not p: p = psutil.Process(os.getpid())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 if callable(p.nice):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 p.nice(nice)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 p.nice = nice
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 if platform.system() != "Windows":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 import resource
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 if hasattr(psutil, "RLIMIT_NICE"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 nice_limit, _ = resource.getrlimit(psutil.RLIMIT_NICE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 high_priority_nice = 20 - nice_limit
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 high_priority_nice = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 # RLIMIT_NICE is not available (probably OSX), let's probe
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 # Try setting niceness to -20 .. -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 p = psutil.Process(os.getpid())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 orig_nice = get_nice(p)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 for i in range(-20, 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 set_nice(i, p)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 high_priority_nice = i
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 break
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
116 except psutil.AccessDenied as e:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 set_nice(orig_nice, p)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 def set_priority():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 if platform.system() == "Windows":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 set_nice(psutil.HIGH_PRIORITY_CLASS)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 if high_priority_nice < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 set_nice(high_priority_nice)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 def reset_priority():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 if platform.system() == "Windows":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 set_nice(psutil.NORMAL_PRIORITY_CLASS)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 if high_priority_nice < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 set_nice(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 def powerset_print_start(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 set_priority()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 inhibit_sleep(reason)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 def powerset_print_stop():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 reset_priority()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 deinhibit_sleep()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
141 except ImportError as e:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 logging.warning("psutil unavailable, could not import power utils:" + str(e))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 def powerset_print_start(reason):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 def powerset_print_stop():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 pass

mercurial