Presets can be read from stdin and written to stdout

This commit is contained in:
Shy 2017-03-25 14:54:54 +01:00
parent 3696c8fe1c
commit 8cbb604ff0

View file

@ -237,7 +237,7 @@ class C4Room:
cmd = [] cmd = []
for light in self.lights: for light in self.lights:
if colorscheme.color_for(light.topic): if colorscheme.color_for(light.topic):
if magic != None: # Send color to ghost, but skip masters if magic != "none" and magic != '0': # Send color to ghost, but skip masters
if light.is_master: continue if light.is_master: continue
mode_id, error = Fluffy().mode_id(magic) mode_id, error = Fluffy().mode_id(magic)
@ -644,21 +644,24 @@ class ColorScheme:
def from_file(self, preset): def from_file(self, preset):
"""Load ColorScheme from file.""" """Load ColorScheme from file."""
import os if preset == '-':
cfg_dir = self._get_cfg_dir() fd = sys.stdin
if not cfg_dir: else:
print("Error: could not load preset!") import os
return None cfg_dir = self._get_cfg_dir()
if not cfg_dir:
print("Error: could not load preset!")
return None
# Expand preset name # Expand preset name
preset = self._expand_preset(preset) preset = self._expand_preset(preset)
# Try to open the preset file # Try to open the preset file
fn = os.path.join(cfg_dir, preset) fn = os.path.join(cfg_dir, preset)
try: try:
fd = open(fn) fd = open(fn)
except OSError: except OSError:
print("Error: could not load preset \"{}\" (file could not be accessed)!".format(preset)) print("Error: could not load preset \"{}\" (file could not be accessed)!".format(preset))
return None return None
# Parse the preset file # Parse the preset file
self.mapping = {} self.mapping = {}
@ -715,19 +718,22 @@ class ColorScheme:
print("I'm sorry Dave. I'm afraid I can't do that. The name \"{}\" is reserved. Please choose a different one.".format(name)) print("I'm sorry Dave. I'm afraid I can't do that. The name \"{}\" is reserved. Please choose a different one.".format(name))
return False return False
import os if name == '-':
cfg_dir = self._get_cfg_dir(create=True) # Create config dir if missing fd = sys.stdout
else:
import os
cfg_dir = self._get_cfg_dir(create=True) # Create config dir if missing
fn = os.path.join(cfg_dir, name) fn = os.path.join(cfg_dir, name)
try: try:
fd = open(fn, 'xt') fd = open(fn, 'xt')
except FileExistsError: except FileExistsError:
print("A preset with this name already exists, overwrite? [y/N]", print("A preset with this name already exists, overwrite? [y/N]",
end=' ', flush=True) end=' ', flush=True)
if sys.stdin.read(1).lower() == 'y': if sys.stdin.read(1).lower() == 'y':
fd = open(fn, 'wt') fd = open(fn, 'wt')
else: else:
return False return False
# Get current states # Get current states
c4 = C4Interface() c4 = C4Interface()
@ -751,8 +757,9 @@ class ColorScheme:
else: else:
fd.write("{} = {}\n".format(light.topic, light.color)) fd.write("{} = {}\n".format(light.topic, light.color))
fd.close() if name != '-':
print("Wrote preset \"{}\"".format(name)) fd.close()
print("Wrote preset \"{}\"".format(name))
class Fluffy: class Fluffy:
@ -773,7 +780,7 @@ class Fluffy:
return (self.modes[name.lower()], False) return (self.modes[name.lower()], False)
# Fallback # Fallback
return (1, True) return (0, True)
class RemotePresets: class RemotePresets:
@ -983,8 +990,8 @@ if __name__ == "__main__":
"-f", "--fnordcenter", type=str, dest="f_color", metavar="PRESET", "-f", "--fnordcenter", type=str, dest="f_color", metavar="PRESET",
help="apply local colorscheme PRESET to Fnordcenter") help="apply local colorscheme PRESET to Fnordcenter")
group_cl.add_argument( group_cl.add_argument(
"-m", "--magic", type=str, metavar="MODE", "-m", "--magic", type=str, default="fade", metavar="MODE",
help="EXPERIMENTAL: blend into preset (needs a running instance of fluffyd on the network). MODE is either \"fade\", \"wave\" or \"emp\".") help="EXPERIMENTAL: blend into preset (needs a running instance of fluffyd on the network). MODE is either \"fade\", \"wave\", \"emp\" or \"none\".")
group_cl.add_argument( group_cl.add_argument(
"-l", "--list-presets", action="store_true", "-l", "--list-presets", action="store_true",
help="list locally available presets") help="list locally available presets")