in src/composer/composer.py [0:0]
def __init__(self, composition):
''' construct a composition object with the specified fields '''
# shallow copy of obj attributes
items = composition.items() if isinstance(composition, dict) else composition.__dict__.items() if isinstance(composition, Composition) else None
if items is None:
raise ComposerError('Invalid argument', composition)
for k, v in items:
setattr(self, k, v)
combinator = composition['.combinator']()
if 'args' in combinator:
for arg in combinator['args']:
optional = arg.get('optional', False)
if arg['name'] not in composition and optional and 'type' in arg:
continue
if 'type' not in arg:
try:
value = composition.get(arg['name'], None if optional else undefined)
setattr(self, arg['name'], composer.task(value))
except Exception:
raise ComposerError('Invalid argument "'+arg['name']+'" in "'+composition['type']+' combinator"', value)
elif arg['type'] == 'name':
try:
setattr(self, arg['name'], parse_action_name(composition.get(arg['name'])))
except ComposerError as ce:
raise ComposerError(ce.message + ' in "'+composition['type']+' combinator"', composition.get(arg['name']))
elif arg['type'] == 'value':
if callable(composition.get(arg['name'])) or arg['name'] not in composition:
raise ComposerError('Invalid argument "' + arg['name']+'" in "'+ composition['type']+' combinator"', composition.get(arg['name']))
elif arg['type'] == 'object':
if not isinstance(composition.get(arg['name']), dict):
raise ComposerError('Invalid argument "' + arg['name']+'" in "'+ composition['type']+' combinator"', composition.get(arg['name']))
else:
if type(composition.get(arg['name'])).__name__ != arg['type']:
raise ComposerError('Invalid argument "' + arg['name']+'" in "'+ composition['type']+' combinator"', composition.get(arg['name']))
if 'components' in combinator:
self.components = list(map(composer.task, composition.get('components', [])))