Last active
January 3, 2018 16:31
-
-
Save ygbourhis/53f1ef5c6cb4b70ba910 to your computer and use it in GitHub Desktop.
due to https://bugs.launchpad.net/ubuntu/+source/redshift/+bug/868904 set redshift to use a manual location and configure this location automaticaly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For this file to work, create a ~/.config/redshift.conf file containing:
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)