Create tmux segments

Refs #44.
This commit is contained in:
Kim Silkebækken 2013-01-11 13:37:22 +01:00
parent 7eb3bfde9c
commit 7e214a65b0
1 changed files with 88 additions and 4 deletions

View File

@ -2,10 +2,94 @@
import os
from powerline.lib.memoize import memoize
def user_name():
user_name = os.environ.get('USER')
# Weather condition code descriptions available at http://developer.yahoo.com/weather/#codes
weather_conditions_codes = {
u'': [25, 34],
u'': [24],
u'': [5, 6, 8, 9, 10, 11, 12, 35, 40, 45, 47],
u'': range(26, 30) + [44],
u'': [7] + range(13, 18) + [41, 42, 43, 46],
u'': range(0, 4) + range(37, 39),
u'': range(19, 23),
u'': [32, 36],
u'': [31, 33],
}
def date(format='%Y-%m-%d'):
from datetime import datetime
return datetime.now().strftime(format)
@memoize(600, persistent=True)
def external_ip(query_url='http://icanhazip.com/'):
import urllib2
try:
return urllib2.urlopen(query_url).read().strip()
except urllib2.HTTPError:
return
def system_load(format='{avg[0]:.1f}, {avg[1]:.1f}, {avg[2]:.1f}'):
from multiprocessing import cpu_count
averages = os.getloadavg()
normalized = averages[1] / cpu_count()
if normalized < 1:
gradient = 'system_load_good'
elif normalized < 2:
gradient = 'system_load_bad'
else:
gradient = 'system_load_ugly'
return {
'contents': user_name,
'highlight': 'user_name' if user_name != 'root' else ['user_name_root', 'user_name'],
'contents': format.format(avg=averages),
'highlight': [gradient, 'system_load']
}
def uptime(format='{days:02d}d {hours:02d}h {minutes:02d}m'):
# TODO: make this work with operating systems without /proc/uptime
try:
with open('/proc/uptime', 'r') as f:
seconds = int(float(f.readline().split()[0]))
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return format.format(days=int(days), hours=hours, minutes=minutes)
except IOError:
pass
@memoize(600, persistent=True)
def forecast(unit='c', location_query=None):
import json
import urllib
import urllib2
if not location_query:
try:
location = json.loads(urllib2.urlopen('http://freegeoip.net/json/' + external_ip()).read())
location_query = ','.join([location['city'], location['region_name'], location['country_name']])
except ValueError:
return None
query_data = {
'q':
'use "http://github.com/yql/yql-tables/raw/master/weather/weather.bylocation.xml" as we;'
'select * from we where location="{0}" and unit="{1}"'.format(location_query, unit),
'format': 'json'
}
url = 'http://query.yahooapis.com/v1/public/yql?' + urllib.urlencode(query_data)
response = json.loads(urllib2.urlopen(url).read())
condition = response['query']['results']['weather']['rss']['channel']['item']['condition']
condition_code = int(condition['code'])
icon = u''
for icon, codes in weather_conditions_codes.items():
if condition_code in codes:
break
return u'{0} {1}°{2}'.format(icon, condition['temp'], unit.upper())