[英]Bash script to Python no idea how
I want to convert this script to Python 3.5:我想将此脚本转换为 Python 3.5:
#!/bin/bash
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) source /home/user/bin/TV-Off.sh;;
*"boolean false"*) source /home/user/bin/Super-on.sh;;
esac
done
Is it even possible?有可能吗?
I found this converting script online but I cant get it to work.我在网上找到了这个转换脚本,但我无法让它工作。 I receive Segmentation fault (core dumped)
.我收到Segmentation fault (core dumped)
。
Update更新
I think i may have done it except I cant seem to loop it without high processor usage from the loop I commented out.我想我可能已经做到了,除了我似乎无法在没有我注释掉的循环的高处理器使用率的情况下循环它。 I also attempted to copy the bash script with no luck, but I feel I'm missing something simple.我还尝试复制 bash 脚本,但没有成功,但我觉得我遗漏了一些简单的东西。
#!/usr/bin/python3.7
import dbus
#import time
count = 0
#while True: <---- First loop try high cpu
session_bus = dbus.SessionBus()
gnome_screensaver = 'org.gnome.ScreenSaver'
object_path = '/{0}'.format(gnome_screensaver.replace('.', '/'))
get_object = session_bus.get_object(gnome_screensaver, object_path)
get_interface = dbus.Interface(get_object, gnome_screensaver)
status = bool(get_interface.GetActive())
# print(status) <---- First loop try high cpu
# if status == True: <---- First loop try high cpu
# print("do something") <---- First loop try high cpu
# if status == False: <---- First loop try high cpu
# print("stopping") <---- First loop try high cpu
# count += 1 <---- First loop try high cpu
# time.sleep(10) <---- First loop try high cpu
# if count >= 500: <---- First loop try high cpu
# break <---- First loop try high cpu
x = status
#while x: <- Second loop try didnt work no output as screen goes black
if status == True:
print("do something")
if status == False:
print("stopping")
So after a bit more searching I've found what I needed on another post So I changed it a bit.因此,经过更多搜索后,我在另一篇文章中找到了我需要的内容,所以我对其进行了一些更改。
#!/usr/bin/python3.7
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
#from Newtest import tvon, tvoff
import dbus
import logging
#def sccreen():
logging.basicConfig()
logger = logging.getLogger(__name__)
dbus_loop = DBusGMainLoop(set_as_default=True)
def message_callback(bus, message):
if message.get_interface() == "org.gnome.ScreenSaver":
if message.get_member() == "ActiveChanged":
screensaver_enabled = bool(message.get_args_list()[0])
# screensaver_enabled = bool(message.get_args_list()[0] and true)
logger.info("Screen saver changed. Active: %s", screensaver_enabled)
if screensaver_enabled == True:
global tvonn
print("tv off")
tvonn = False
# tvoff()
if screensaver_enabled == False:
print("tv on")
tvonn = True
# tvon()
# print("oooooo")
# print(screensaver_enabled)
session = dbus.SessionBus(mainloop=dbus_loop)
session.add_match_string_non_blocking("interface='org.gnome.ScreenSaver'")
session.add_message_filter(message_callback)
logger.info("Starting up.")
loop = GLib.MainLoop()
loop.run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.