aboutsummaryrefslogtreecommitdiff
path: root/lmlib.py
blob: 67b207fd309bf694a42062e6544ba2829db16fed (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
# File: lmlib.py
# License: CC-BY-SA 4.0
# Author: bgstack15
# Startdate: 2019-06-12
# Title: Python libs for logout-manager
# Purpose: Store the common elements for operating a logout-manager
# History:
# Usage:
#    In a logout-manager-gtk.py program
# Reference:
#    platform info https://stackoverflow.com/questions/110362/how-can-i-find-the-current-os-in-python/10091465#10091465
# Improve:
#    detect if el7, use icon_category = "apps". This should be an internal variable (not listed in .conf)
# Documentation:

import configparser, platform

logout_manager_version="2019-06-14a"

class Actions:

   @staticmethod
   def hibernate(config, event=None):
      #print("need to run the /sys/power/state trick, if available")
      print(config.get_hibernate_command())

   @staticmethod
   def lock(config, event=None):
      #print("please lock the screen.")
      print(config.get_lock_command())

   @staticmethod
   def logout(config, event=None):
      #print("please log out of current session!")
      print(config.get_logout_command())

   @staticmethod
   def reboot(config, event=None):
      #print("please reboot.")
      print(config.get_reboot_command())

   @staticmethod
   def shutdown(config, event=None):
      #print("please shut yourself down!")
      print(config.get_shutdown_command())

class Config:
   def __init__(self):
      # load defaults which can be overwritten
      self.hibernate_command = ""
      self.lock_command = ""
      self.logout_command = ""
      self.reboot_command = ""
      self.shutdown_command = ""
      self.hibernate_icon = "system-hibernate"
      self.hibernate_fallback_icon = "system-hibernate"
      self.lock_icon = "system-lock-screen"
      self.lock_fallback_icon = "system-lock-screen"
      self.logout_icon = "system-log-out"
      self.logout_fallback_icon = "system-log-out"
      self.reboot_icon = "system-reboot"
      self.reboot_fallback_icon = "system-reboot"
      self.shutdown_icon = "system-shutdown"
      self.shutdown_fallback_icon = "system-shutdown"
      self.icon_size = 24
      self.icon_theme = "default"
      self.icon_category = "actions"
      self.can_hibernate = False
      self.application_icon=("system-log-out")

   def set_hibernate_command(self,hibernate_command):
      self.hibernate_command = hibernate_command

   def set_lock_command(self,lock_command):
      self.lock_command = lock_command

   def set_logout_command(self,logout_command):
      self.logout_command = logout_command

   def set_reboot_command(self,reboot_command):
      self.reboot_command = reboot_command

   def set_shutdown_command(self,shutdown_command):
      self.shutdown_command = shutdown_command

   def set_hibernate_icon(self,hibernate_icon):
      self.hibernate_icon = hibernate_icon

   def set_lock_icon(self,lock_icon):
      self.lock_icon = lock_icon

   def set_logout_icon(self,logout_icon):
      self.logout_icon = logout_icon

   def set_reboot_icon(self,reboot_icon):
      self.reboot_icon = reboot_icon

   def set_shutdown_icon(self,shutdown_icon):
      self.shutdown_icon = shutdown_icon

   def set_icon_size(self,icon_size):
      self.icon_size = int(icon_size)

   def set_icon_theme(self,icon_theme):
      self.icon_theme = icon_theme

   def set_icon_category(self,icon_category):
      self.icon_category = icon_category

   def set_can_hibernate(self,can_hibernate):
      print("Setting can_hibernate:",can_hibernate)
      self.can_hibernate = bool(can_hibernate)

   def get_hibernate_command(self):
      return self.hibernate_command

   def get_lock_command(self):
      return self.lock_command

   def get_logout_command(self):
      return self.logout_command

   def get_reboot_command(self):
      return self.reboot_command

   def get_shutdown_command(self):
      return self.shutdown_command

   def get_hibernate_icon(self):
      return self.hibernate_icon

   def get_lock_icon(self):
      return self.lock_icon

   def get_logout_icon(self):
      return self.logout_icon

   def get_reboot_icon(self):
      return self.reboot_icon

   def get_shutdown_icon(self):
      return self.shutdown_icon

   def get_hibernate_fallback_icon(self):
      return self.hibernate_fallback_icon

   def get_lock_fallback_icon(self):
      return self.lock_fallback_icon

   def get_logout_fallback_icon(self):
      return self.logout_fallback_icon

   def get_reboot_fallback_icon(self):
      return self.reboot_fallback_icon

   def get_shutdown_fallback_icon(self):
      return self.shutdown_fallback_icon

   def get_icon_size(self):
      return self.icon_size

   def get_icon_theme(self):
      return self.icon_theme

   def get_icon_category(self):
      return self.icon_category

   def get_can_hibernate(self):
      return self.can_hibernate

def Initialize_config():
   # Read config
   config_in = configparser.ConfigParser()
   config_in.read('logout-manager.conf')
   config = Config()
   try:
      ci = config_in['logout-manager']
   except:
      # no definition
      print("Using default commands")

   try:
      ci_icons = config_in['icons']
   except:
      # no definition
      print("Using default icons")

   # load up our custom class, which stores the defaults in case we do not set them here
   if 'hibernate_command' in ci:
      config.set_hibernate_command(ci['hibernate_command'])
   if 'lock_command' in ci:
      config.set_lock_command(ci['lock_command'])
   if 'logout_command' in ci:
      config.set_logout_command(ci['logout_command'])
   if 'reboot_command' in ci:
      config.set_reboot_command(ci['reboot_command'])
   if 'shutdown_command' in ci:
      config.set_shutdown_command(ci['shutdown_command'])
   if 'hibernate' in ci_icons:
      config.set_hibernate_icon(ci_icons['hibernate'])
   if 'lock' in ci_icons:
      config.set_lock_icon(ci_icons['lock'])
   if 'logout' in ci_icons:
      config.set_logout_icon(ci_icons['logout'])
   if 'reboot' in ci_icons:
      config.set_reboot_icon(ci_icons['reboot'])
   if 'shutdown' in ci_icons:
      config.set_shutdown_icon(ci_icons['shutdown'])
   if 'size' in ci_icons:
      config.set_icon_size(ci_icons['size'])
   if 'theme' in ci_icons:
      config.set_icon_theme(ci_icons['theme'])
   # store the info about if hibernate is an option
   can_hibernate = False
   try:
      with open('/sys/power/state') as r:
         line = r.read()
         if 'disk' in line: can_hibernate = True
   except:
      pass
   config.set_can_hibernate(can_hibernate)

   # set icon category
   # written primarily for el7 which uses "app" for the system-reboot icons, etc.
   a = platform.dist()
   try:
      if a[0] == "redhat" and int(a[1].split(".")[0]) <= 7:
         config.set_icon_category("apps")
   except:
      pass

   # DEBUG, raw from conf file and system status
   print("Raw values:")
   for item in config_in.sections():
      print("["+item+"]")
      for key in config_in[item]:
         print(key+" = "+config_in[item][key])
   print("Can hibernate:",can_hibernate)

   # DEBUG, stored values
   print("Stored values:")
   print(config.get_hibernate_command())
   print(config.get_lock_command())
   print(config.get_logout_command())
   print(config.get_reboot_command())
   print(config.get_shutdown_command())
   print(config.get_hibernate_icon())
   print(config.get_lock_icon())
   print(config.get_logout_icon())
   print(config.get_reboot_icon())
   print(config.get_shutdown_icon())
   print(config.get_icon_size())
   print(config.get_icon_theme())
   print(config.get_icon_category())
   print("Can hibernate:",config.get_can_hibernate())

   return config
bgstack15