aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-04-10 19:35:20 +0200
committerMattias Andrée <maandree@operamail.com>2014-04-10 19:35:20 +0200
commitb990a9af33cb4a9f4a6956849e58984b23e1d557 (patch)
treed924fe7bed2febd5c0ca991a8d933bd8998e1318
parentslightly change the name of the socket (diff)
downloadnightshift-b990a9af33cb4a9f4a6956849e58984b23e1d557.tar.gz
nightshift-b990a9af33cb4a9f4a6956849e58984b23e1d557.tar.bz2
nightshift-b990a9af33cb4a9f4a6956849e58984b23e1d557.tar.xz
parsing of options
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rwxr-xr-xsrc/nightshift.py104
1 files changed, 93 insertions, 11 deletions
diff --git a/src/nightshift.py b/src/nightshift.py
index 4a9a30b..96fac4f 100755
--- a/src/nightshift.py
+++ b/src/nightshift.py
@@ -66,22 +66,59 @@ def setproctitle(title):
setproctitle(sys.argv[0])
+red_args = None
+'''
+:list<str>? Raw arguments passed to redshift
+'''
+
+red_opts = []
+'''
+:list<str> Nightshift parsed options passed to redshift
+'''
+
+daemon = False
+'''
+:bool Whether or not to run as daemon
+'''
+
+kill = False
+'''
+:bool Whether or not to kill the redshift and the nightshift daemon
+'''
+
+toggle = False
+'''
+:bool Whether or not to toggle redshift
+'''
+
+status = False
+'''
+:bool Whether or not to get the current status
+'''
+
+
## Parse options
+add_to_red_opts = False
for arg in sys.argv[1:]:
- if arg in ('-V', '--version'):
+ if add_to_red_opts:
+ red_opts.append(arg)
+ add_to_red_opts = False
+ elif red_args is not None:
+ red_args.append(arg)
+ elif arg in ('-V', '--version', '-version'):
## Print the version of nightshift and of redshift
print('%s %s' % (PROGRAM_NAME, PROGRAM_VERSION))
Popen(['redshift', '-V'], stdout = sys.stdout).wait()
sys.exit(0)
- elif arg in ('-C', '--copyright'):
+ elif arg in ('-C', '--copyright', '-copyright'):
## Print copyright information
print(copyright[1 : -1])
sys.exit(0)
- elif arg in ('-W', '--warranty'):
+ elif arg in ('-W', '--warranty', '-warranty'):
## Print warranty disclaimer
print(copyright.split('\n\n')[-2])
sys.exit(0)
- elif arg in ('-h', '--help'):
+ elif arg in ('-h', '-?', '--help', '-help'):
## Display help message
text = '''USAGE: nightshift [OPTIONS...] [-- REDSHIFT-OPTIONS...]
@@ -94,7 +131,7 @@ for arg in sys.argv[1:]:
-W --warranty Show program warrantly disclaimer
-d --daemon Start as daemon
- -x --reset Reset mode (remove adjustment from screen)
+ -x --reset --kill Remove adjustment from screen
+x --toggle Temporarily disable or enable adjustments
-s --status Print status information
@@ -116,10 +153,52 @@ for arg in sys.argv[1:]:
indent = min([len(line) - len(line.lstrip()) for line in text if line.rstrip().startswith(' ')])
print('\n'.join([line[indent:] if line.startswith(' ') else line for line in text]))
sys.exit(0)
+ elif arg == '--':
+ red_args = []
else:
- ## Unrecognised option
- sys.stderr.write('%s: error: unrecognised option: %s\n' % (sys.argv[0], arg))
- sys.exit(1)
+ subargs = [arg]
+ if arg.startswith('-') and not arg.startswith('--'):
+ subargs = ['-' + letter for letter in arg[1:]]
+ elif arg.startswith('+') and not arg.startswith('++'):
+ subargs = ['+' + letter for letter in arg[1:]]
+ red_arg = ''
+ for arg in subargs:
+ if (add_to_red_opts is None) or add_to_red_opts:
+ add_to_red_opts = None
+ red_arg += arg[1]
+ elif arg in ('-d', '--daemon'):
+ daemon = True
+ elif arg in ('-x', '--reset', '--kill'):
+ kill = True
+ elif arg in ('+x', '--toggle'):
+ toggle = True
+ elif arg in ('-s', '--status'):
+ status = True
+ elif arg in ('-c', '--config'):
+ red_opts.append('-c')
+ add_to_red_opts = True
+ elif arg in ('-b', '--brightness'):
+ red_opts.append('-b')
+ add_to_red_opts = True
+ elif arg in ('-t', '--temperature'):
+ red_opts.append('-t')
+ add_to_red_opts = True
+ elif arg in ('-l', '--location'):
+ red_opts.append('-l')
+ add_to_red_opts = True
+ elif arg in ('-m', '--method'):
+ red_opts.append('-m')
+ add_to_red_opts = True
+ elif arg in ('-r', '--no-transition'):
+ red_opts.append('-r')
+ add_to_red_opts = True
+ else:
+ ## Unrecognised option
+ sys.stderr.write('%s: error: unrecognised option: %s\n' % (sys.argv[0], arg))
+ sys.exit(1)
+ if add_to_red_opts is None:
+ red_opts.append(red_arg)
+ add_to_red_opts = False
# Construct name of socket
@@ -145,8 +224,9 @@ if sock is None:
if pid == 0:
## Daemon (child)
- # Close stdin
+ # Close stdin and stdout
os.close(sys.stdin.fileno())
+ os.close(sys.stdout.fileno())
# Create server socket
os.unlink(socket_path)
@@ -190,7 +270,7 @@ if sock is None:
try:
proc = Popen(['redshift', '-v'], stdout = PIPE, stderr = open(os.devnull))
- red_brightness, red_period, red_temperature, red_running = 1, 1, 6500, True
+ red_brightness, red_period, red_temperature, red_running, red_status = 1, 1, 6500, True, True
red_condition = threading.Condition()
def read_status():
@@ -214,6 +294,8 @@ try:
red_period = float(value.split(' ')[1][1 : -1]) / 100
elif key == 'Color temperature':
red_temperature = float(value[:-1])
+ else key == 'Status':
+ red_status = value == 'Enabled'
except:
pass
red_condition.notify()
@@ -223,7 +305,7 @@ try:
thread = threading.Thread(target = read_status)
thread.setDaemon(True)
thread.start()
-
+
while red_running:
red_condition.acquire()
red_condition.wait()