From 01d5043106dbc8d5b0a63024d0c2a81b1ee63bb9 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 3 Mar 2014 14:08:00 +0100 Subject: add clock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- examples/clock | 18 +++++----- examples/plugin-test | 6 +++- src/plugins/clock.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 src/plugins/clock.py diff --git a/examples/clock b/examples/clock index 4984213..c7d9ac2 100644 --- a/examples/clock +++ b/examples/clock @@ -5,26 +5,28 @@ import time import threading +from plugins.clock import Clock + OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True +clock = Clock(format = '%Y-(%m)%b-%d %T, %a w%V, %Z', utc = False, sync_to = Clock.SECONDS) + + start_ = start def start(): start_() - def clock(): - while True: - time.sleep(0.1) - time.sleep(1 - (time.time() % 1)) - if redraw(): - get_display().flush() - async(clock) + def refresh(): + if redraw(): + get_display().flush() + async(lambda : clock.continuous_sync(refresh)) semaphore = threading.Semaphore() def redraw(): if semaphore.acquire(blocking = False): - text = spawn_read('date', '+%Y-(%m)%b-%d %T, %a w%V, %Z') + text = clock.read() bar.clear() bar.draw_coloured_text(0, 10, 0, 2, text) semaphore.release() diff --git a/examples/plugin-test b/examples/plugin-test index 619b6f5..9cf449c 100644 --- a/examples/plugin-test +++ b/examples/plugin-test @@ -16,13 +16,17 @@ from plugins.discstats import DiscStats from plugins.xdisplay import XDisplay from plugins.moc import MOC from plugins.cpu import CPU +from plugins.clock import Clock OUTPUT, HEIGHT, YPOS, TOP = 0, 24, 24, True +clock = Clock(format = '%Y-(%m)%b-%d %T, %a w%V, %Z') + + def redraw(): - date = spawn_read('date', '+%Y-(%m)%b-%d %T, %a w%V, %Z') + date = clock.read() uptime_ = Uptime() uptime = '%id %02i:%02i:%05.2f' % uptime_.uptime diff --git a/src/plugins/clock.py b/src/plugins/clock.py new file mode 100644 index 0000000..129269c --- /dev/null +++ b/src/plugins/clock.py @@ -0,0 +1,92 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +''' + +import time + +from util import * + + +class Clock: + ''' + Date and time information retrieval + ''' + + + SECONDS = 1 + ''' + :float Synchronisation value for syncing to seconds + ''' + + MINUTES = 60 + ''' + :float Synchronisation value for syncing to minutes + ''' + + + def __init__(self, format = '%Y-(%m)%b-%d %T, %a w%V, %Z', utc = False, sync_to = 1): + ''' + Constructor + + @param format:str The format as in the `date` command + @param utc:bool Show time in UTC, otherwise local time + @param sync_to:float The time parameter to sync to, the number of seconds between the reads + ''' + self.format = format + self.utc = utc + self.sync_to = sync_to + self.command = ['date'] + (['--utc'] if utc else []) + ['+' + format] + + + def read(self): + ''' + Reads the clock + + @return :str The time and date in the format specified at construction + ''' + return spawn_read(*(self.command)) + + + def sync(self): + ''' + Wait for the clock to reach the next synchronisation time point + ''' + time.sleep(self.sync_to - (time.time() % self.sync_to)) + + + def continuous_sync(self, function): + ''' + Wait for the clock to reach the next synchronisation time point + and the call a function and start over, i.e. sync in a loop forever + + @param function:()→void The function to call + ''' + while True: + time.sleep(self.sync_to - (time.time() % self.sync_to)) + function() + + + @staticmethod + def posix_time(): + ''' + Returns the current POSIX time + + @return :float The current POSIX time + ''' + return time.time() + -- cgit v1.2.3-70-g09d2