From aa29b8dd8ca5d2a9f67f3e423367f81f9429e39f Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 16 Mar 2015 19:31:45 +0100 Subject: add examples: localutcclock, anytzclock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 11 +++++- examples/tricks/anytzclock.py | 78 ++++++++++++++++++++++++++++++++++++++++ examples/tricks/localutcclock.py | 64 +++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 examples/tricks/anytzclock.py create mode 100644 examples/tricks/localutcclock.py diff --git a/Makefile b/Makefile index 661d702..1520819 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,8 @@ PLUGIN_EXAMPLES = chase clock cpu cpuinfo cpuonline df discstats \ dentrystate inodestate files tzclock ropty ping \ inotify +TRICK_EXAMPLES = localutcclock anytzclock + EXAMPLES = mixed moderate test xmonad @@ -86,13 +88,18 @@ install-license: COPYING LICENSE.gpl3 LICENSE.agpl3 install -m644 $^ -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)" .PHONY: install-all-examples -install-all-examples: install-examples install-plugin-examples +install-all-examples: install-examples install-trick-examples install-plugin-examples .PHONY: install-examples install-examples: $(foreach F,$(EXAMPLES),examples/$(F)) install -dm755 -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples" install -m644 $^ -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples" +.PHONY: install-trick-examples +install-trick-examples: $(foreach F,$(EXAMPLES),examples/tricks/$(F)) + install -dm755 -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples/tricks" + install -m644 $^ -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples/tricks" + .PHONY: install-plugin-examples install-plugin-examples: $(foreach F,$(PLUGIN_EXAMPLES),examples/plugins/$(F)) install -dm755 -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples/plugins" @@ -115,6 +122,8 @@ uninstall: -rmdir -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)" -rm -- $(foreach F,$(PLUGIN_EXAMPLES),"$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples/plugins/$(F)") -rmdir -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples/plugins" + -rm -- $(foreach F,$(TRICK_EXAMPLES),"$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples/tricks/$(F)") + -rmdir -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples/tricks" -rm -- $(foreach F,$(EXAMPLES),"$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples/$(F)") -rmdir -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)/examples" -rmdir -- "$(DESTDIR)$(DOCDIR)/$(PKGNAME)" diff --git a/examples/tricks/anytzclock.py b/examples/tricks/anytzclock.py new file mode 100644 index 0000000..9ae2b05 --- /dev/null +++ b/examples/tricks/anytzclock.py @@ -0,0 +1,78 @@ +# -*- python -*- + +# A xpybar configuration example that demonstrates how you can +# write a configuration that lets you change the clock between +# any timezone you want using the keyboard or terminal. + +# This example requires the package python-posix_ipc, and the +# example command requires the package cmdipc. + +# Caveat: This example is only written to support local displays. +# If the host part of $DISPLAY contains a dot, this example will +# not work as expected. + +# Caveat: The IPC objects are not cleaned up on exit. +# After exiting you may want to run +# cmdipc -PQrk "/~${USER}.xpybar.$(echo "${DISPLAY}" | cut -d . -f 1)" + +# To switch between UTC and localtime run the command +# cmdipc -PQk "/~${USER}.xpybar.$(echo "${DISPLAY}" | cut -d . -f 1)" send -- tz ${THE_TIMEZONE_YOU_WANT} +# You may want to add an executable file in your ~/.local/bin +# (which should you include in your $PATH) containing the code +# #!/bin/sh +# exec cmdipc -PQk "/~${USER}.xpybar.$(echo "${DISPLAY}" | cut -d . -f 1)" send -- tz "$@" +# You can then add a hotkey to xbindkeys running that command +# with a timezone as a argument, or run it in the terminal. +# If the timezone is omitted, local time is used. + +import time +import threading + +from plugins.clock import Clock +from plugins.tzclock import TZClock + + +OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True + + +time_format = '%Y-(%m)%b-%d %T, %a w%V, %Z' + +local_clock = Clock(format = time_format, sync_to = Clock.SECONDS) +my_clock = local_clock + + +def mqueue_wait(): + import posix_ipc + global my_clock + qkey = '/~%s.xpybar.%s' % (os.environ['USER'], os.environ['DISPLAY'].split('.')[0]) + q = posix_ipc.MessageQueue(qkey, posix_ipc.O_CREAT, 0o600, 8, 128) + while True: + message = q.receive(None)[0].decode('utf-8', 'replace').split(' ') + if message[0] == 'tz': + old_clock = my_clock + tz = '/'.join(message[1:]) + try: + my_clock = local_clock if tz == '' else TZClock(timezone = tz, format = time_format) + except: + try: + my_clock = local_clock if tz == '' else TZClock(timezone = tz.upper(), format = time_format) + except: + print('%s: unknown typezone: %s' % (sys.argv[0], tz), file = sys.stderr) + continue + if old_clock is not local_clock: + del old_clock + bar.invalidate() # Optional, just to force redrawing before the next second + + +start_ = start +def start(): + start_() + async(mqueue_wait) + async(lambda : local_clock.continuous_sync(lambda : bar.invalidate())) + + +def redraw(): + text = '%s' % my_clock.read() + bar.clear() + bar.draw_coloured_text(0, 10, 0, 2, text) + diff --git a/examples/tricks/localutcclock.py b/examples/tricks/localutcclock.py new file mode 100644 index 0000000..129a1d1 --- /dev/null +++ b/examples/tricks/localutcclock.py @@ -0,0 +1,64 @@ +# -*- python -*- + +# A xpybar configuration example that demonstrates how you can +# write a configuration that lets you change the clock between +# local time and UTC using the keyboard. + +# This example requires the package python-posix_ipc, and the +# example command requires the package cmdipc. + +# Caveat: This example is only written to support local displays. +# If the host part of $DISPLAY contains a dot, this example will +# not work as expected. + +# Caveat: The IPC objects are not cleaned up on exit. +# After exiting you may want to run +# cmdipc -PQrk "/~${USER}.xpybar.$(echo "${DISPLAY}" | cut -d . -f 1)" + +# To switch between UTC and localtime run the command +# cmdipc -PQk "/~${USER}.xpybar.$(echo "${DISPLAY}" | cut -d . -f 1)" send -- clock +# You may want to add an executable file in your ~/.local/bin +# (which should you include in your $PATH) containing the code +# #!/bin/sh +# exec cmdipc -PQk "/~${USER}.xpybar.$(echo "${DISPLAY}" | cut -d . -f 1)" send -- clock +# You can then add a hotkey to xbindkeys running that command. + +import time +import threading + +from plugins.clock import Clock + + +OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True + + +local_clock = Clock(format = '%Y-(%m)%b-%d %T, %a w%V, %Z', utc = False, sync_to = Clock.SECONDS) +utc_clock = Clock(format = '%Y-(%m)%b-%d %T, %a w%V, %Z', utc = True) + +my_clock = local_clock + + +def mqueue_wait(): + import posix_ipc + global my_clock + qkey = '/~%s.xpybar.%s' % (os.environ['USER'], os.environ['DISPLAY'].split('.')[0]) + q = posix_ipc.MessageQueue(qkey, posix_ipc.O_CREAT, 0o600, 8, 128) + while True: + message = q.receive(None)[0].decode('utf-8', 'replace') + if message == 'clock': + my_clock = utc_clock if (my_clock is not utc_clock) else local_clock + bar.invalidate() # Optional, just to force redrawing before the next second + + +start_ = start +def start(): + start_() + async(mqueue_wait) + async(lambda : local_clock.continuous_sync(lambda : bar.invalidate())) + + +def redraw(): + text = '%s' % my_clock.read() + bar.clear() + bar.draw_coloured_text(0, 10, 0, 2, text) + -- cgit v1.2.3-70-g09d2