[fmtools/f15/master] Improved tkradio.py

Paulo Roma Cavalcanti roma at fedoraproject.org
Sat Mar 12 12:14:32 UTC 2011


commit b646910ec5db8345ff4856098f86c384c33397c9
Author: Paulo Roma Cavalcanti <promac at gmail.com>
Date:   Sat Mar 12 09:14:01 2011 -0300

    Improved tkradio.py

 fmtools.spec |   10 +++++-
 sources      |    2 +-
 tkradio.py   |   87 ++++++++++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 76 insertions(+), 23 deletions(-)
---
diff --git a/fmtools.spec b/fmtools.spec
index fe5dac4..a7dafb5 100644
--- a/fmtools.spec
+++ b/fmtools.spec
@@ -3,7 +3,7 @@
 Summary: Simple Video for Linux radio card programs
 Name:    fmtools
 Version: 2.0.1
-Release: 5%{?dist}
+Release: 7%{?dist}
 License: GPLv2+
 Group:   Applications/Multimedia
 URL:     http://benpfaff.org/fmtools
@@ -97,10 +97,16 @@ rm -rf %{buildroot}
 %{_datadir}/%{name}/radio.gif
 
 %changelog
+
+* Tue Mar 10 2011 Paulo Roma <roma at lcg.ufrj.br> 2.0.1-7
+- Restoring the saved state (persistency).
+
+* Sat Feb 26 2011 Paulo Roma <roma at lcg.ufrj.br> 2.0.1-6
+- Ported to python3.
+
 * Tue Feb 08 2011 Fedora Release Engineering <rel-eng at lists.fedoraproject.org> - 2.0.1-5
 - Rebuilt for https://fedoraproject.org/wiki/Fedora_15_Mass_Rebuild
 
-
 * Mon Dec 20 2010 Paulo Roma <roma at lcg.ufrj.br> 2.0.1-4
 - Condionally build with python-lirc.
 
diff --git a/sources b/sources
index 24c3990..db88804 100644
--- a/sources
+++ b/sources
@@ -1,2 +1,2 @@
-377d5015cc65d9c7265e8c3247217927  fmcontrol.tar.gz
+5236130b1a178b906fb47d76f3d10149  fmcontrol.tar.gz
 5b48f552180f18d46fe92124b2fcfca0  fmtools-2.0.1.tar.gz
diff --git a/tkradio.py b/tkradio.py
index a188cf7..4dfd572 100644
--- a/tkradio.py
+++ b/tkradio.py
@@ -4,17 +4,20 @@
 # Date: 23/12/2009
 # The radio is turned off on exit.
 
-import os, sys, string
+import os, sys, string, pickle
 import datetime, time
 from threading import Thread
 from subprocess import Popen, PIPE
 
 try:
-    from mtTkinter import *
+    from tkinter import *    # python3
 except ImportError:
-    from Tkinter import *
-    print ( "mtTkinter not found: http://tkinter.unpythonic.net/wiki/mtTkinter" )
-    print ( "Remote control will not work!!" )
+    try:
+        from mtTkinter import *
+    except ImportError:
+        from Tkinter import *
+        print ( "mtTkinter not found: http://tkinter.unpythonic.net/wiki/mtTkinter" )
+        print ( "Remote control will not work!!" )
 
 try:
     import pylirc
@@ -30,10 +33,10 @@ try:
         use_notify = True
     else:
         use_notify = False
-        print "pynotify module initialization failed"
+        print ( "pynotify module initialization failed" )
 except:
     use_notify = False
-    print "notify-python not found: http://www.galago-project.org/downloads.php"
+    print ( "notify-python not found: http://www.galago-project.org/downloads.php" )
 
 # These are stations in the Rio de Janeiro area. 
 # Customize for your own locale. They can be set
@@ -130,7 +133,7 @@ class IRRec(Thread):
                         quit()
                    else:
                         # Print all the configs...
-                        print "Command: %s, Repeat: %d" % (code["config"], code["repeat"])
+                        print ( "Command: %s, Repeat: %d" % (code["config"], code["repeat"]) )
                if (not blocking):
                    s = pylirc.nextcode(1)
                else:
@@ -282,7 +285,7 @@ def quit(msg=""):
     """Quit the radio."""
 
     if ( msg ):
-         print msg
+         print (msg)
     else:
          radio ("off")
     # kill all threads
@@ -455,10 +458,25 @@ def getpid(proc):
     aid = aid.replace('\n','')
     return str2num(aid)
 
+class radioState:
+      """Holds the state of the clock (used for persistency)."""
+
+      def __init__ ( self, intial_station ):
+          self.volume   = 100
+          self.loop     = "OFF"
+          self.mute     = False
+          self.station  = intial_station
+          self.pos      = ""
+
+      def __str__ (self):
+          return " Volume = %s\n Loop = %s\n Mute = %d\n Station = %s\n Pos = %s\n" % \
+                  ( self.volume, self.loop, self.mute, self.station, self.pos )
+
 def main (argv=None):
     """Main program."""
 
     global scale         # volume scale
+    global state         # toggle mute/umute
     global station       # variable for the station radio buttons
     global btmute        # variable for the text in the mute button
     global btm           # mute button
@@ -470,11 +488,23 @@ def main (argv=None):
     global loopvar       # variable for setting loopback on/off
     global lid           # loopback process id
 
+    def cleanup():
+        savedState.volume  = scale.get()
+        savedState.loop    = loopvar.get()
+        savedState.mute    = state
+        savedState.station = station.get()
+        savedState.pos     = mw.geometry()
+        pf = open(statfile,'wb')
+        pickle.dump ( savedState, pf )
+        pf.close()
+        # print ( savedState )
+        raise SystemExit
+
     if argv is None:
        argv = sys.argv
 
-    pyversion = string.split(sys.version)[0]
-    print "Python Version: ", pyversion
+    pyversion = str.split(sys.version)[0]
+    print ( "Python Version: %s" % pyversion )
 
     # check whether tkradio is already running
     stat = os.popen (PS + " aux | " + GREP + " -E \"python(" + pyversion[0:3] + ")? " + argv[0] + "\"").readline()
@@ -484,15 +514,27 @@ def main (argv=None):
          if ( cid != int(pid) ): 
               sys.exit ( "%s is already running: pid = %s" %(argv[0], pid) )
 
+    path = os.environ.get("HOME")
+    statfile = path + '/.tkradio'
+    if (sys.hexversion > 0x03000000):
+        statfile += '3'
+    if ( not os.path.exists (statfile) ):
+         savedState = radioState(radbut)
+    else:
+         pf = open(statfile,'rb')
+         savedState = pickle.load(pf)
+         pf.close()
+
     mw = Tk()
     # do not resize the radio
     mw.resizable(False,False)
 
     station = StringVar()
-    station.set ( radbut )
+    station.set (savedState.station)
 
     btmute = StringVar()
-    btmute.set ( "Off" )
+    state = not savedState.mute
+    btmute.set ( "OFF" )
 
     top = Frame(); top.pack()
     bbt = Frame(); bbt.pack()
@@ -518,7 +560,7 @@ def main (argv=None):
     scale = Scale(top, from_=0, to=100, orient=HORIZONTAL, command=on_move, bd=0,
                   sliderlength=10, width=5, showvalue=0)
     scale.pack(side='top')
-    scale.set(volume)
+    scale.set(savedState.volume)
 
     # the current radio frequency
     Button(bbt,text="<", command = previous).pack(side="left",anchor=E)
@@ -528,18 +570,19 @@ def main (argv=None):
     freq.insert(0,station.get())
     freq.pack(side="bottom")
 
-    recvar = StringVar()   # creates a checkbutton for the alarm state
+    recvar = StringVar()   # creates a checkbutton for the recording state
     loopvar = StringVar()  # creates a checkbutton for the loopback
     recvar.set ( "OFF" )
-    aid = getpid ( string.rsplit(REC,'/',1)[1] )
+    aid = getpid ( str.rsplit(REC,'/',1)[1] )
     if ( aid ): # is the loop back already on?
          loopvar.set ( "ON" )
          lid = aid
     else:
-         loopvar.set ( "OFF" )
+         loopvar.set ( savedState.loop )
+         loop ()
 
     # create quit and mute buttons
-    Button(top,text="Exit", command = quit).pack(side="right")
+    Button(top,text="Exit", command = cleanup).pack(side="right")
     btm=Button(top,text="Off", command = mute, textvariable = btmute)
     btm.pack(side="left")
     Checkbutton (top, text="Rec", variable=recvar, onvalue="ON", offvalue="OFF", command=rec).pack(side="top",anchor=W)
@@ -551,7 +594,7 @@ def main (argv=None):
 
     # turn the radio on
     setstation()
-    radio ("on")
+    mute()
 
     # set an icon for the window
     icon_img = PhotoImage(file=RGIF)
@@ -561,10 +604,14 @@ def main (argv=None):
     if ( use_lirc ):
          start_irrec()  
 
+    mw.protocol("WM_DELETE_WINDOW", cleanup)
+
+    if ( savedState.pos ): mw.geometry(savedState.pos)
+
     top.mainloop()
 
 try:
   if __name__=="__main__":
      sys.exit(main())
-except (KeyboardInterrupt,SystemExit),msg:
+except (KeyboardInterrupt,SystemExit) as msg:
      quit(msg)


More information about the scm-commits mailing list