def __init__(self, configDict, importedAlready=None): """ Constructs the configuration from a dictionary of values. """ importedAlready = importedAlready or [] if constants.STATE_MACHINES_ATTRIBUTE not in configDict: raise exceptions.StateMachinesAttributeRequiredError() self.rootUrl = configDict.get(constants.ROOT_URL_ATTRIBUTE, constants.DEFAULT_ROOT_URL) if not self.rootUrl.endswith('/'): self.rootUrl += '/' self.machines = {} # import built-in machines self._importBuiltInMachines(importedAlready=importedAlready) for machineDict in configDict[constants.STATE_MACHINES_ATTRIBUTE]: # bring in all the imported machines if machineDict.get(constants.IMPORT_ATTRIBUTE): self._importYaml(machineDict[constants.IMPORT_ATTRIBUTE], importedAlready=importedAlready) continue machine = _MachineConfig(machineDict, rootUrl=self.rootUrl) if machine.name in self.machines: raise exceptions.MachineNameNotUniqueError(machine.name) # add the states for stateDict in machineDict.get( constants.MACHINE_STATES_ATTRIBUTE, []): machine.addState(stateDict) if not machine.initialState: raise exceptions.MachineHasNoInitialStateError(machine.name) if not machine.finalStates: raise exceptions.MachineHasNoFinalStateError(machine.name) # add the transitions (2-phase parsing :( ) for stateDict in machineDict.get( constants.MACHINE_STATES_ATTRIBUTE, []): for transDict in stateDict.get( constants.STATE_TRANSITIONS_ATTRIBUTE, []): machine.addTransition( transDict, stateDict[constants.STATE_NAME_ATTRIBUTE]) self.machines[machine.name] = machine
def __addMachinesFromImportedConfig(self, importedCofig): """ Adds new machines from an imported configuration. """ for machineName, machine in importedCofig.machines.items(): if machineName in self.machines: raise exceptions.MachineNameNotUniqueError(machineName) self.machines[machineName] = machine
def __init__(self, configDict, importedAlready=None, rootUrl=None, enableCapabilitiesCheck=None): """ Constructs the configuration from a dictionary of values. """ importedAlready = importedAlready or [] if constants.STATE_MACHINES_ATTRIBUTE not in configDict: raise exceptions.StateMachinesAttributeRequiredError() if rootUrl is None: self.rootUrl = configDict.get(constants.ROOT_URL_ATTRIBUTE, constants.DEFAULT_ROOT_URL) else: self.rootUrl = rootUrl if constants.ROOT_URL_ATTRIBUTE in configDict: message = 'Cannot specify "%s" in an imported .yaml file.' % constants.ROOT_URL_ATTRIBUTE raise exceptions.ConfigurationError(message) if enableCapabilitiesCheck is None: self.enableCapabilitiesCheck = configDict.get( constants.ENABLE_CAPABILITIES_CHECK_ATTRIBUTE, constants.DEFAULT_ENABLE_CAPABILITIES_CHECK) else: self.enableCapabilitiesCheck = enableCapabilitiesCheck if constants.ENABLE_CAPABILITIES_CHECK_ATTRIBUTE in configDict: message = 'Cannot specify "%s" in an imported .yaml file.' % \ constants.ENABLE_CAPABILITIES_CHECK_ATTRIBUTE raise exceptions.ConfigurationError(message) if not self.rootUrl.endswith('/'): self.rootUrl += '/' self.machines = {} # import built-in machines self._importBuiltInMachines(importedAlready=importedAlready) for machineDict in configDict[constants.STATE_MACHINES_ATTRIBUTE]: # bring in all the imported machines if machineDict.get(constants.IMPORT_ATTRIBUTE): self._importYaml(machineDict[constants.IMPORT_ATTRIBUTE], importedAlready=importedAlready) continue machine = _MachineConfig(machineDict, rootUrl=self.rootUrl) if machine.name in self.machines: raise exceptions.MachineNameNotUniqueError(machine.name) # add the states for stateDict in machineDict.get( constants.MACHINE_STATES_ATTRIBUTE, []): machine.addState(stateDict) if not machine.initialState: raise exceptions.MachineHasNoInitialStateError(machine.name) if not machine.finalStates: raise exceptions.MachineHasNoFinalStateError(machine.name) # add the transitions (2-phase parsing :( ) for stateDict in machineDict.get( constants.MACHINE_STATES_ATTRIBUTE, []): for transDict in stateDict.get( constants.STATE_TRANSITIONS_ATTRIBUTE, []): machine.addTransition( transDict, stateDict[constants.STATE_NAME_ATTRIBUTE]) self.machines[machine.name] = machine