Hi Tanja!

On Wednesday 30 March 2005 20:18, you wrote:
> I had a look at your diff file.
Thanks - the third time I posted it, but so far noone complained.. ;-)

> Would you mind to post also the following files:
> /src/helpers/nextrecord.py 
Why? It should not even be referenced?! No, it is not.
How do you know of it? ARGH! ARGL!! GRMBL!!!
How come everytime I post a patch, I am doing a mistake in the very last 
minute(*)?!?? This time I posted the *raw* patch, which I had carefully 
separated into several ones in sth. like an hour of work! :-(

> /src/plugins/shutdown.py
> freevo_config.py
> local_config.py
Why do you want to have these?  I hope you don't need them now that I send a 
clean patch, all right?  They should all be patchable from "current" CVS with 
tag "rel-1-5".

> I think a good solution would be an integration of your work and Georg's
> work. My idea is to have
> 1) a idletimer plugin, which decides when it is time for shutdown.
Why this way and not that which I described in my last postings?

> 2) a shutdown helper, who shedules the wakeup and shuts down the system
>    (this could also be called outside of freevo, instead of "halt" or
> "shutdown")
Should be trivial to implement on top of my patch.

> 3) an improved shutdown plugin, which warns if there are 
> recordings in the near future and gives the possibilty to stop the
> automatic shutdown (idletimer) 
Apart from the idletimer stuff, that's contained in my patch.

> 4) your rebootflag.py for the possibly needed reboot
>
> What do you think?

I am very unhappy that my careful work has been in vain so far, thanks for 
having a look at it anyways and hopefully you are more happy with the 
attached, clean version.

BTW: As you could see, I also translated the new strings from my patch into 
German, but that part of the diff file seemed so unclean that I removed it 
(before sending it anyways.. [EMAIL PROTECTED]"$##!), does anyone know a way to 
get better 
patches from .po(t) files?

Ciao,  /  /
      /--/
     /  / ANS

* Last time I introduced a single spurious tab in my patch the minute before 
sending, which of course makes python choke. ;-(
Index: freevo_config.py
===================================================================
RCS file: /cvsroot/freevo/freevo/freevo_config.py,v
retrieving revision 1.349.2.9
diff -u -3 -p -d -b -r1.349.2.9 freevo_config.py
--- freevo_config.py	9 Jan 2005 10:45:55 -0000	1.349.2.9
+++ freevo_config.py	28 Mar 2005 20:57:38 -0000
@@ -296,6 +296,24 @@ RESTART_SYS_CMD  = 'shutdown -r now'  # 
 ENABLE_SHUTDOWN_SYS = 0  # Performs a whole system shutdown at SHUTDOWN!
                          # For standalone boxes.
 
+# Warn before shutting down the system if the next recording is
+# scheduled for starting in less than this number of seconds:
+WARN_SHUTDOWN = 15 * 60
+
+# On Freevo shutdown, use nvram-wakeup to program the computer to
+# wakeup / boot up right before the next recording:
+USE_NVRAM_WAKEUP = 0
+
+# See SHUTDOWN_SYS_CMD for sudo comment.
+# If you change this, note that an exitcode of 1 means "reboot needed"
+# and should get through to Freevo
+NVRAM_WAKEUP_CMD = 'nvram-wakeup -s %d'
+
+# If using nvram-wakeup, time in seconds to startup before recording:
+BOOTTIME_PADDING = 150
+
+# Location of the reboot-flag used if nvram-wakeup returns 1:
+NVRAM_REBOOT_FLAG = '%s/reboot_flag' % FREEVO_CACHEDIR
 
 #
 # You can add more keybindings by adding them to the correct hash. 
Index: local_conf.py.example
===================================================================
RCS file: /cvsroot/freevo/freevo/local_conf.py.example,v
retrieving revision 1.37.2.4
diff -u -3 -p -d -b -r1.37.2.4 local_conf.py.example
--- local_conf.py.example	9 Jan 2005 10:45:55 -0000	1.37.2.4
+++ local_conf.py.example	28 Mar 2005 20:57:39 -0000
@@ -781,12 +781,25 @@ IMAGEVIEWER_BLEND_MODE = -1
 # TV_RECORD_SERVER_IP = 'localhost'
 # TV_RECORD_SERVER_PORT = 18001
 
-# start every recording X minutes before scheduled,
+# Start every recording X minutes before scheduled,
 # and stop X minutes after scheduled - default to zero minutes.
 # This must be a value in seconds although at the moment only has
 # the percision of one minute.
 # TV_RECORD_PADDING = 0 * 60
 
+# Warn before shutting down the system if the next recording is
+# scheduled for starting in less than this number of seconds:
+# WARN_SHUTDOWN = 15 * 60 # quarter hour
+# (This has no extra effect if CONFIRM_SHUTDOWN is set, since then
+#  the shutdown is already confirmed.)
+
+# On Freevo shutdown, use nvram-wakeup to program the computer to
+# wakeup / boot up right before the next recording:
+# USE_NVRAM_WAKEUP    = 1
+
+# If using nvram-wakeup, time in seconds to startup before recording:
+# BOOTTIME_PADDING    = 2 * 60
+
 # VCR_AUDIO = (':adevice=%s' % AUDIO_DEVICE +
 #              ':audiorate=32000' +         # 44100 for better sound
 #              ':forceaudio:forcechan=1:' + # Forced mono for bug in my driver
Index: src/plugins/shutdown.py
===================================================================
RCS file: /cvsroot/freevo/freevo/src/plugins/shutdown.py,v
retrieving revision 1.7
diff -u -3 -p -d -b -r1.7 shutdown.py
--- src/plugins/shutdown.py	10 Jul 2004 12:33:40 -0000	1.7
+++ src/plugins/shutdown.py	28 Mar 2005 20:57:40 -0000
@@ -50,6 +50,9 @@ from gui import ConfirmBox
 from item import Item
 from plugin import MainMenuPlugin
 
+if config.WARN_SHUTDOWN or config.USE_NVRAM_WAKEUP:
+    import tv.record_client as record_client
+
 
 def shutdown(menuw=None, argshutdown=None, argrestart=None, exit=False):
     """
@@ -126,7 +129,6 @@ def shutdown(menuw=None, argshutdown=Non
         time.sleep(1)
         
 
-
 class ShutdownItem(Item):
     """
     Item for shutdown
@@ -146,7 +148,7 @@ class ShutdownItem(Item):
                           (self.confirm_system_restart, _('Restart system') ) ]
         else:
             items = [ (self.shutdown_freevo, _('Shutdown Freevo') ),
-                          (self.shutdown_system, _('Shutdown system') ),
+                      (self.check_shutdown_system, _('Shutdown system') ),
                           (self.shutdown_system_restart, _('Restart system') ) ]
         if config.ENABLE_SHUTDOWN_SYS:
             items = [ items[1], items[0], items[2] ]
@@ -154,12 +156,46 @@ class ShutdownItem(Item):
         return items
 
 
+    def next_scheduled_recording(self):
+        """
+        return starting time of next scheduled recording (or None)
+        """
+        (server_available, msg) = record_client.connectionTest()
+        if not server_available:
+            return None
+        (result, recordings) = record_client.getScheduledRecordings()
+        if result:
+            progs = recordings.getProgramList().values()
+            if len(progs):
+                progs.sort(lambda a, b: cmp(a.start, b.start))
+                return progs[0].start
+        return None
+
+
+    def next_recording_message(self, format, start_time = None):
+        if start_time == None:
+            start_time = self.next_scheduled_recording()
+        if start_time == None:
+            return ""
+        rec_distance = start_time - time.time() - config.TV_RECORD_PADDING
+        if rec_distance < 0:
+            result = _('A recording is in progress.')
+        elif rec_distance < 60*60:
+            result = _('The next scheduled recording begins in %d minutes.') % int(rec_distance/60)
+        elif rec_distance < 60*60*10:
+            result = _('The next scheduled recording begins at %s.') % time.strftime(config.TV_TIMEFORMAT, time.localtime(start_time))
+        else:
+            result = _('The next recording is scheduled for %s.') % time.strftime(config.TV_DATETIMEFORMAT, time.localtime(start_time))
+        return format % result
+
+
     def confirm_freevo(self, arg=None, menuw=None):
         """
         Pops up a ConfirmBox.
         """
         self.menuw = menuw
-        what = _('Do you really want to shut down Freevo?')
+        what = _('Do you really want to shut down Freevo?') \
+               + self.next_recording_message(" %s")
         ConfirmBox(text=what, handler=self.shutdown_freevo, default_choice=1).show()
         
         
@@ -168,39 +204,95 @@ class ShutdownItem(Item):
         Pops up a ConfirmBox.
         """
         self.menuw = menuw
-        what = _('Do you really want to shut down the system?')
+        what = _('Do you really want to shut down the system?') \
+               + self.next_recording_message(" %s")
         ConfirmBox(text=what, handler=self.shutdown_system, default_choice=1).show()
 
+
     def confirm_system_restart(self, arg=None, menuw=None):
         """
         Pops up a ConfirmBox.
         """
         self.menuw = menuw
-        what = _('Do you really want to restart the system?')
+        what = _('Do you really want to restart the system?') \
+               + self.next_recording_message(" %s")
         ConfirmBox(text=what, handler=self.shutdown_system_restart, default_choice=1).show()
 
 
+    def check_shutdown_system(self, arg=None, menuw=None):
+        """
+        Shutdown the complete system if the next recording is not too far away
+        """
+        if config.WARN_SHUTDOWN:
+            start_time = self.next_scheduled_recording()
+            if start_time != None:
+                if start_time - config.TV_RECORD_PADDING - time.time() \
+                       < config.WARN_SHUTDOWN:
+                    what = self.next_recording_message("%s ", start_time) + \
+                           _('Do you really want to shut down the system?')
+                    ConfirmBox(text=what, handler=self.shutdown_system, default_choice=1).show()
+                    return
+        self.shutdown_system(arg, menuw)
+
+
     def shutdown_freevo(self, arg=None, menuw=None):
         """
-        shutdown freevo, don't shutdown the system
+        Shutdown freevo, don't shutdown the system
         """
         shutdown(menuw=menuw, argshutdown=False, argrestart=False)
 
         
     def shutdown_system(self, arg=None, menuw=None):
         """
-        shutdown the complete system
+        Shutdown the complete system, use nvram-wakeup to schedule
+        boot-up before next recording (if configured to do so).
+        nvram-wakup may signal that an additional reboot is needed
+        (with exitcode 1), in this case a flag file is created and the
+        system is rebooted.
+        """
+        doShutdown = True
+        if config.USE_NVRAM_WAKEUP:
+            start_time = self.next_scheduled_recording()
+            if start_time != None:
+                wakeupTime = start_time \
+                             - config.TV_RECORD_PADDING \
+                             - config.BOOTTIME_PADDING
+                _debug_("calling nvram-wakeup with %d" % wakeupTime)
+                ec = os.system(config.NVRAM_WAKEUP_CMD % (wakeupTime,))
+                _debug_(".. exitcode was %d" % ec)
+                if ec == 256:
+                    doShutdown = False
+                    file(config.NVRAM_REBOOT_FLAG, "w").close()
+                elif ec > 0:
+                    ConfirmBox(text=_('Could not program computer to boot up before next recording. Shutdown anyway?'),
+                               handler=self.shutdown_system_anyway, default_choice=1).show()
+                    return
+        shutdown(menuw=menuw, argshutdown=doShutdown, argrestart=not doShutdown)
+
+
+    def shutdown_system_anyway(self, arg=None, menuw=None):
+        """
+        Shutdown freevo, don't shutdown the system
         """
         shutdown(menuw=menuw, argshutdown=True, argrestart=False)
 
+
     def shutdown_system_restart(self, arg=None, menuw=None):
         """
-        restart the complete system
+        Restart the complete system
         """
         shutdown(menuw=menuw, argshutdown=False, argrestart=True)
 
         
         
+# According to
+# http://mail.python.org/pipermail/python-list/2004-September/241553.html
+# a) Python has no uptime() implementation and
+# b) calling "uptime" is not more portable than parsing /proc/uptime.
+# Since the latter is much easier, I will do so.. ;-)
+def uptime():
+    uptime, idletime = [float(f) for f in file("/proc/uptime").read().split()]
+    return uptime
 
 
 #
@@ -212,7 +304,8 @@ class PluginInterface(MainMenuPlugin):
     Plugin to shutdown Freevo from the main menu
     """
 
+    def __init__(self, *args):
+        MainMenuPlugin.__init__(self, *args)
+
     def items(self, parent):
         return [ ShutdownItem(parent) ]
-
-

Reply via email to