Skip to content
This repository has been archived by the owner on Mar 11, 2023. It is now read-only.

Commit

Permalink
Init.
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Sep 24, 2012
0 parents commit 40cfb9c
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
__pycache__
*.pyc
58 changes: 58 additions & 0 deletions 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 changes: 61 additions & 0 deletions 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'

0 comments on commit 40cfb9c

Please sign in to comment.