diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-02-17 01:20:03 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-02-17 01:20:03 +0100 |
commit | 73df5cdda541938e1891b650b3adfc1c317682fb (patch) | |
tree | b5f1f7c32a943cdf6fb103d3f6af7520f3abdf65 /src/__main__.py | |
parent | add tempurature mappings from redshift (diff) | |
download | blueshift-73df5cdda541938e1891b650b3adfc1c317682fb.tar.gz blueshift-73df5cdda541938e1891b650b3adfc1c317682fb.tar.bz2 blueshift-73df5cdda541938e1891b650b3adfc1c317682fb.tar.xz |
fix some minor errors
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/__main__.py')
-rwxr-xr-x | src/__main__.py | 122 |
1 files changed, 73 insertions, 49 deletions
diff --git a/src/__main__.py b/src/__main__.py index 083d0b0..45ba7db 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -23,7 +23,9 @@ import datetime ## Set global variables global DATADIR, i_size, o_size, r_curve, g_curve, b_curve, clip_result -global periodically, wait_period, monitor_controller, running +global periodically, wait_period, fadein_time, fadeout_time, fadein_steps, fadeout_steps +global monitor_controller, running, continuous_run +global signal_SIGTERM from solar import * @@ -40,6 +42,7 @@ def periodically(year, month, day, hour, minute, second, weekday, fade): fadeout_time = None fadein_steps = 100 fadeout_steps = 100 + start_over() if fade is None: negative(False, False, False) temperature(6500, lambda T : divide_by_maximum(series_d(T))) @@ -103,22 +106,22 @@ monitor_controller = lambda : randr() :()→void Function used by Blueshift on exit to apply reset colour curves ''' -fadein_time = 10 +fadein_time = 2 ''' :float? The number of seconds used to fade in on start, `None` for no fading ''' -fadeout_time = 10 +fadeout_time = 2 ''' :float? The number of seconds used to fade out on exit, `None` for no fading ''' -fadein_steps = 100 +fadein_steps = 10 ''' :int The number of steps in the fade in phase, if any ''' -fadeout_steps = 100 +fadeout_steps = 10 ''' :int The number of steps in the fade out phase, if any ''' @@ -129,52 +132,33 @@ running = True ''' -## Load extension and configurations via blueshiftrc -if config_file is None: - for file in ('$XDG_CONFIG_HOME/%/%rc', '$HOME/.config/%/%rc', '$HOME/.%rc', '/etc/%rc'): - file = file.replace('%', 'blueshift') - for arg in ('XDG_CONFIG_HOME', 'HOME'): - if '$' + arg in file: - if arg in os.environ: - file = file.replace('$' + arg, os.environ[arg].replace('$', '\0')) - else: - file = None - break - if file is not None: - file = file.replace('\0', '$') - if os.path.exists(file): - config_file = file - break -if config_file is not None: - code = None - with open(file, 'rb') as script: - code = script.read() - code = code.decode('utf8', 'error') + '\n' - code = compile(code, file, 'exec') - g, l = globals(), dict(locals()) - for key in l: - g[key] = l[key] - exec(code, g) -else: - print('No configuration file found') - sys.exit(1) - -## Run periodically if configured to -if periodically is not None: +def signal_SIGTERM(signum, frame): + global running + if not running: + running = False + start_over() + monitor_controller() + close_c_bindings() + sys.exit(0) + running = False + + +def continuous_run(): + ''' + Invoked to run continuously if `periodically` is not `None` + ''' + global running, wait_period, fadein_time, fadeout_time, fadein_steps, fadeout_steps def p(t, fade = None): wd = t.isocalendar()[2] periodically(t.year, t.month, t.day, t.hour, t.minute, t.second, wd, fade) + def sleep(seconds): + try: + time.sleep(seconds) + except KeyboardInterrupt: + signal_SIGTERM(0, None) - ## Catch TERM signal - def signal_SIGTERM(signum, frame): - if not running: - running = False - start_over() - monitor_controller() - close_c_bindings() - sys.exit(0) - running = False + ## Catch signals signal.signal(signal.SIGTERM, signal_SIGTERM) ## Fade in @@ -182,33 +166,73 @@ if periodically is not None: ftime = 0 if fadein_time is not None: dtime = fadein_time / fadein_steps + df = 1 / fadein_steps while running: - ftime += dtime + ftime += df if ftime > 1: break p(datetime.datetime.now(), ftime) + sleep(dtime) ## Run periodically if not early_exit: while running: p(datetime.datetime.now(), None) if running: - time.sleep(wait_period) + sleep(wait_period) ## Fade out if fadeout_time is not None: dtime = fadeout_time / fadeout_steps + df = 1 / fadeout_steps if early_exit: ftime = 1 while True: - ftime -= dtime + ftime -= df if ftime <= 0: break p(datetime.datetime.now(), -ftime) + sleep(dtime) ## Reset start_over() monitor_controller() + +## Load extension and configurations via blueshiftrc +if config_file is None: + for file in ('$XDG_CONFIG_HOME/%/%rc', '$HOME/.config/%/%rc', '$HOME/.%rc', '/etc/%rc'): + file = file.replace('%', 'blueshift') + for arg in ('XDG_CONFIG_HOME', 'HOME'): + if '$' + arg in file: + if arg in os.environ: + file = file.replace('$' + arg, os.environ[arg].replace('$', '\0')) + else: + file = None + break + if file is not None: + file = file.replace('\0', '$') + if os.path.exists(file): + config_file = file + break +if config_file is not None: + code = None + with open(file, 'rb') as script: + code = script.read() + code = code.decode('utf8', 'error') + '\n' + code = compile(code, file, 'exec') + g, l = globals(), dict(locals()) + for key in l: + g[key] = l[key] + exec(code, g) +else: + print('No configuration file found') + sys.exit(1) + + +## Run periodically if configured to +if periodically is not None: + continuous_run() + close_c_bindings() |