Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
due to https://bugs.launchpad.net/ubuntu/+source/redshift/+bug/868904 set redshift to use a manual location and configure this location automaticaly
#!/usr/bin/env python
import ConfigParser
import Geoclue
import os
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import sys
import time
redshift_conf_file = os.path.join(os.environ['HOME'], '.config/redshift.conf')
config = ConfigParser.RawConfigParser()
try:
config.read(redshift_conf_file)
except ConfigParser.ParsingError as e:
sys.exit('%s' % e)
latitude = longitude = '0.0000'
# Try initiating location for 20 seconds
start = time.time()
while time.time() - start < 20:
location = Geoclue.DiscoverLocation()
location_found = location.init()
if not location_found:
time.sleep(1)
else:
location_info = location.get_location_info()
latitude = '%0.4f' % location_info['latitude']
longitude = '%0.4f' % location_info['longitude']
if latitude != '0.0000' and longitude != '0.0000':
break
else:
location_found = False
time.sleep(1)
if not location_found:
# We exit without errors, we simply may have no network and we will not
# modify the location in redshift_conf_file.
print 'No Geoclue location found'
sys.exit(0)
# Set latitude and longitude manualy
if not config.has_section('manual'):
config.add_section('manual')
config.set('manual', 'lat', latitude)
config.set('manual', 'lon', longitude)
# We can't write directly to redshift_conf_file because redshift can't read
# properly formated ini files, so we have to ruin the nice format by
# removing spaces around the '=' signs :-/
fake_config_file = StringIO()
config.write(fake_config_file)
config_contents = fake_config_file.getvalue()
fake_config_file.close()
config_contents = config_contents.replace(' = ', '=')
with open(redshift_conf_file, 'w') as config_file:
config_file.write(config_contents)
@ygbourhis

This comment has been minimized.

Copy link
Owner Author

@ygbourhis ygbourhis commented Jan 16, 2016

For this file to work, create a ~/.config/redshift.conf file containing:

[redshift]
location-provider=manual

[manual]
lat=50.000
lon=3.000

And chmod a+x ~/bin/redshift-auto-set-manual-location above
Then configure your favourite window manager to launch redshift-auto-set-manual-location automaticaly (requires python-geoclue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment