def powersave(self): log('Setting power state to powersave') if self.__uses_pstate(): self.__cpufreq('performance') self.__pstate(0, 0) else: self.__cpufreq('powersave') S.run('sudo pm-powersave true')
def __on_switch(self): try: cmd = self.__conf.get('OnSwitch', 'command') except: # No `OnSwitch` section. cmd = None if cmd is not None: log(cmd) S.run(cmd, shell=True)
def __pstate(self, min_perf, max_perf): cmd = 'echo {min} > {dir}min_perf_pct && ' \ 'echo {max} > {dir}max_perf_pct' cmd = cmd.format( min = min_perf, max = max_perf, dir = Power.PSTATE_DIR, ) S.run('sudo sh -c "{}"'.format(cmd))
def __on_switch(self): try: cmd = self.__conf.get('OnSwitch', 'command') except: # No `OnSwitch` section. cmd = None if cmd is not None: log(cmd) S.run(cmd, shell = True)
def is_using_external_display(self): user = S.get_xuser() cmd = 'env DISPLAY=:0 XAUTHORITY=/home/{}/.Xauthority xrandr --current' out = S.get_output(cmd.format(user)) lines = out.splitlines()[1:] for line in lines: if line.startswith(' '): continue m = re.findall('([A-Z]+)-?([0-9]+) ([a-z]+) ', line)[0] if m[2] == 'connected' and not m[0].startswith('LVDS'): return True return False
def open_file(self, file, background): mime = self.__get_mime(file) if mime is None: return if mime not in self.__handlers: stderr("Don't know how to handle '{}' mime type.", mime) return app = self.__handlers[mime] if background: S.run("nohup {0} '{1}' &> /dev/null &".format(app, file), shell=True) else: S.run("{0} '{1}'".format(app, file), shell=True)
def switch(self, mode): log('Switching to {} display mode', mode) if mode not in self.__modes: log('{} display mode is not defined', mode) return # Commands that will be run. cmds = [] # Command to turn requested displays on. on = ['xrandr'] # Command to turn all other displays off. off = ['xrandr'] # On and off commands are separate because most laptop video cards are # limited to only two displays at a time. Turning displays off and on # with one xrandr command will fail. d = self.__displays.copy() for display in self.__conf.options(mode): d.remove(display) pos = self.__conf.get(mode, display) on.append('--output') on.append(display) on.append('--auto') if pos == 'main': on.append('--primary') else: on.append('--' + pos) if display in self.__properties: prop = self.__conf.get('Properties', display) cmds.append(' '.join([ 'xrandr --output', display, '--set', '"' + prop.split(':')[0] + '"', '"' + prop.split(':')[1] + '"' ])) for display in d: off.append('--output') off.append(display) off.append('--off') cmds.append(' '.join(off)) cmds.append(' '.join(on)) cmds.append('xrandr --dpi 96') for cmd in cmds: log(cmd) S.run(cmd) self.__on_switch()
def __get_first(self): files = S.get_output('ls -1').split('\n') if len(files) == 0: stderr('No file to open was found') return None else: return files[0]
def open_file(self, file, background): mime = self.__get_mime(file) if mime is None: return if mime not in self.__handlers: stderr("Don't know how to handle '{}' mime type.", mime) return app = self.__handlers[mime] if background: S.run( "nohup {0} '{1}' &> /dev/null &".format(app, file), shell = True ) else: S.run( "{0} '{1}'".format(app, file), shell = True )
def __get_mime(self, path): # os.path.realpath will resolve symbolic links try: mime = S.get_output("file -b --mime-type '{0}'".format( os.path.realpath(path))).strip() except: stderr("Couldn't get mime type of '{}'", path) return None return mime
def __get_mime(self, path): # os.path.realpath will resolve symbolic links try: mime = S.get_output("file -b --mime-type '{0}'".format( os.path.realpath(path) )).strip() except: stderr("Couldn't get mime type of '{}'", path) return None return mime
def performance(self): log('Setting power state to performance') self.__cpufreq('performance') if self.__uses_pstate(): self.__pstate(0, 100) S.run('sudo pm-powersave false')
def __cpufreq(self, governor): S.run('sudo cpupower frequency-set -g {0}'.format(governor))
def lock(self): log('Locking screen') cmd = 'sudo -u {} xscreensaver-command -lock' S.run(cmd.format(S.get_xuser())) # Wait for screensaver to appear. time.sleep(5)
def __get_output(self, cmd): if self.__user is not None: cmd = 'sudo -u {} {}'.format(self.__user, cmd) return S.get_output(cmd)