Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Init.

  • Loading branch information...
commit 40cfb9cd84db09f3ab9ff7b645ae802ab47a2ef2 0 parents
@untitaker authored
Showing with 121 additions and 0 deletions.
  1. +2 −0  .gitignore
  2. +58 −0 README.rst
  3. +61 −0 kinky.py
2  .gitignore
@@ -0,0 +1,2 @@
+__pycache__
+*.pyc
58 README.rst
@@ -0,0 +1,58 @@
+=====
+Kinky
+=====
+
+Simple toolkit for custom conky-cli-like statusbar scripts, written in Python 3.
+
+Usage
+=====
+
+Create a file called `my_kinky.py`:
+
+.. code-block:: python
+
+ import os
+ import datetime
+ import time
+ from kinky import Item, StatusBar
+
+ class Clock(Item):
+ def run(self):
+ while self.running:
+ self.text = datetime.datetime.now().strftime('%H:%M')
+ time.sleep(60) # update interval: a minute
+
+ class Volume(Item):
+ def run(self):
+ while self.running:
+ # somehow get system volume and save it as a string in self.text
+ # self.text = "34%"
+ time.sleep(.3) # update interval: 0.3 seconds
+
+ statusbar = StatusBar()
+ statusbar.items = [Volume(),Clock()]
+ statusbar.between = '; '
+ statusbar.run()
+
+
+Every time an item writes to its text attribute, the statusbar will write a new
+line. Running this from the command line gives us::
+
+ $ python3 my_kinky.py
+
+ 34%; 20:15
+ 34%; 20:16
+ 34%; 20:17
+
+... because the Volume doesn't change, but the Clock triggers an update every minute.
+
+This is intended for usage with dzen2::
+
+ python -u mystatusbar.py | dzen2 # -u disables stdout buffering
+
+::
+
+License
+=======
+
+Kinky is released under the public domain.
61 kinky.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python3
+
+import threading
+import logging
+from subprocess import Popen, PIPE
+
+class StatusBar(object):
+ before = ' '
+ between = ' | '
+ after = ' '
+ items = None
+
+ def __init__(self):
+ self.items = []
+
+ def run(self):
+ for item in self.items:
+ logging.debug('Spawning: ' + item.__class__.__name__)
+ item.spawn(self)
+
+ def draw(self):
+ text = [item.text.replace('\n', '') for item in self.items if item.text is not None]
+ text = self.between.join(text).strip()
+ if text:
+ print(self.before + text + self.after)
+
+class Item(threading.Thread):
+ thread = None
+ bar = None
+ running = None
+ _text = None
+
+ @property
+ def text(self):
+ return self._text
+
+ @text.setter
+ def text(self, value):
+ self._text = value
+ self.bar.draw()
+
+ def spawn(self, bar):
+ self.bar = bar
+ self.running = True
+ self.start()
+
+ def kill(self):
+ self.running = False
+
+ def run(self):
+ raise NotImplementedError
+
+
+# UTILS
+def shell(command):
+ '''Executes a command on the shell and returns its output'''
+ return Popen(command, shell=True, stdout=PIPE).stdout.read().decode()
+
+def is_running(process):
+ '''Determines if a process with the given process name is running'''
+ return shell('ps -A | grep -c ' + process).strip() != '0'
Please sign in to comment.
Something went wrong with that request. Please try again.