119 lines
4.1 KiB
Python
119 lines
4.1 KiB
Python
# factory.py
|
|
|
|
import events
|
|
from item import Item
|
|
import dmx
|
|
|
|
class MakeItems:
|
|
def __new__(cls, config):
|
|
items = {}
|
|
for item in config:
|
|
kwargs = {}
|
|
for key in config[item].keys():
|
|
if key == 'events':
|
|
kwargs[key] = MakeEvents(config[item][key])
|
|
else:
|
|
kwargs[key] = config[item][key]
|
|
items[item] = Item(**kwargs)
|
|
if hasattr(items[item], 'prepare'):
|
|
items[item].prepare()
|
|
return items
|
|
|
|
class MakeEvents:
|
|
def __new__(cls, config):
|
|
objects = []
|
|
for x in config:
|
|
for key in x:
|
|
args = []
|
|
kwargs = {}
|
|
if isinstance(x[key], list):
|
|
args = x[key]
|
|
elif isinstance(x[key], dict):
|
|
kwargs = x[key]
|
|
elif x[key] is None:
|
|
pass
|
|
else:
|
|
args[0] = x[key]
|
|
obj = getattr(events, key)(*args, **kwargs)
|
|
objects.append(obj)
|
|
return objects
|
|
|
|
# This just returns an array for now, we'll see if we need to make it into a class later.
|
|
class MakeSet:
|
|
def __new__(cls, config, items):
|
|
for item in config:
|
|
if item not in items:
|
|
raise KeyError('{} not in items'.format(item))
|
|
return config
|
|
|
|
class MakeFixtureTypes:
|
|
def __new__(cls, config):
|
|
|
|
def parse_range(src):
|
|
(a, b) = src.split('-')
|
|
return (int(a), int(b))
|
|
|
|
types = {}
|
|
for ftype in config:
|
|
channels = {}
|
|
for channel in config[ftype].keys():
|
|
dmxchannel = int(config[ftype][channel]['channel'])
|
|
finechannel = config[ftype][channel].get('fine-channel')
|
|
if finechannel is not None:
|
|
finechannel = int(finechannel)
|
|
fade = config[ftype][channel].get('fade', True)
|
|
rangesrc = config[ftype][channel]['range']
|
|
if isinstance(rangesrc, dict):
|
|
range = {}
|
|
for entry in rangesrc:
|
|
range[entry] = parse_range(rangesrc[entry])
|
|
else:
|
|
range = parse_range(rangesrc)
|
|
channels[channel] = {'channel' : dmxchannel, 'fine-channel' : finechannel, 'range' : range, 'fade' : fade}
|
|
types[ftype] = dmx.FixtureType(channels)
|
|
|
|
return types
|
|
|
|
class MakeFixtures:
|
|
def __new__(cls, config, fixture_types):
|
|
fixtures = {}
|
|
for fixture in config:
|
|
dmxch = config[fixture]['dmx']
|
|
type = config[fixture]['type']
|
|
if type not in fixture_types:
|
|
raise KeyError('{} not in fixture_types'.format(type))
|
|
fixtures[fixture] = dmx.Fixture(dmxch, fixture_types[type])
|
|
return fixtures
|
|
|
|
class MakeGroups:
|
|
def __new__(cls, config, fixtures):
|
|
groups = {}
|
|
for group in config:
|
|
fobjects = []
|
|
for fixture in config[group]:
|
|
if fixture not in fixtures:
|
|
raise KeyError('{} not in fixtures'.format(fixture))
|
|
fobjects.append(fixtures[fixture])
|
|
# XXX process modifiers
|
|
groups[group] = dmx.Group(fobjects)
|
|
return groups
|
|
|
|
class MakeScenes:
|
|
def __new__(cls, config, fixtures, groups):
|
|
scenes = {}
|
|
for scene in config:
|
|
objects = {}
|
|
for name in config[scene].keys():
|
|
if name in groups:
|
|
obj = groups[name]
|
|
elif name in fixtures:
|
|
obj = fixtures[name]
|
|
else:
|
|
raise KeyError('{} is not a group or a fixture'.format(name))
|
|
for channel in config[scene][name].keys():
|
|
if not obj.is_channel(channel):
|
|
raise KeyError('{} is not a channel within {}'.format(channel, name))
|
|
objects[name] = (obj, config[scene][name])
|
|
scenes[scene] = dmx.Scene(objects)
|
|
return scenes
|