-1

I can't exit from this if construct

#!/usr/bin/python2.7
import os
import re
import time
global state


#print status_on.group()
def main():
    get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
    output = os.popen(get_current_jack_status).read()
    status_on = re.search('values=on', output)
    if status_on:
        state = status_on
        os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
        if status_on == state:
            print "True"
            state = 1   #here i can't exit

        if status_on != state:
            print state
            os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")


while True:
    main()

I've tried pass but when I execute the script it says as output:

True
1
True
1

ecc. If I use "break" it crashes.

dreamwhite
  • 116
  • 8

2 Answers2

1

This should work. Move your global declaration to inside the main function, then this should initialize the status, and then check for status changes. This can probably be cleaned up a bit (namely the if statement), but it should work.

#!/usr/bin/python2.7
import os
import sys
import re
import time


#print status_on.group()
def main():
    global state
    get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
    output = os.popen(get_current_jack_status).read()
    status_on = re.search('values=on', output)
    if (status_on is None and state) or (state is None and status_on):
        state = status_on
        if status_on:
            print "a jack was plugged in"
            os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
        else:
            print "a jack was unplugged"
            os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")

get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
state = re.search('values=on', output)

while True:
        main()
deweyredman
  • 1,440
  • 1
  • 9
  • 12
  • Nope, what I am trying to do is to execute only one time the "notify-send ecc..." command when the status_on == state – dreamwhite Jan 04 '17 at 21:57
  • You realize that you're resetting state, and checking it again in the next if statement, right? – deweyredman Jan 04 '17 at 22:00
  • also, as someone mentioned above, you have main wrapped in a while True... – deweyredman Jan 04 '17 at 22:01
  • Check out my edits: i'd recommend using if and elif in stead of two ifs. That might be the root of your problem... – deweyredman Jan 04 '17 at 22:04
  • Yes I know that I've used while true 'cause I have to run this script in background. What I am trying to do is to send a notification when the jack is plugged in or unplugged. – dreamwhite Jan 04 '17 at 22:04
  • just try the if/ elif edits...that should do the trick then, I believe. – deweyredman Jan 04 '17 at 22:05
  • Actually, I think I see what you need to do...give me a second and I'll edit my post. – deweyredman Jan 04 '17 at 22:10
  • Check out my updated answer: 1) state is initialized to the current state. 2) whenever status_on is not state, reset it to state, and indicate the change that happened. – deweyredman Jan 04 '17 at 22:16
  • I've executed the script but when I added (for debugging) a "print 'true' after the "plugged in" case it prints infinite – dreamwhite Jan 04 '17 at 22:22
0

Then use a variable holding the previous state. And do it like:

if status_on == state and previousState!=1:
    print "True"
    state = 1