示例#1
0
def test_fail_with_with_429_from_engine():
    result = ServiceResult.failure(['ETEngine returned a 429'])

    with pytest.raises(ETMParseError) as excinfo:
        fail_with(result)

    assert excinfo.value.status_code == 429
示例#2
0
def get_context_values(scenario_id, inputs, queries):
    '''
    Gets all the requested inputs and queries from the ETM scenario
    May use two calls to achive this, as the queries and default inputs have different
    endpoints.

    scenario_id (int) - The ETM scenario ID
    inputs (list)     - The ETM inputs (sliders) where future and present values are requested from
    queries (list)    - ETM gqueries where future and present values are requested from

    Returns a dict with the inputs and queries as keys, with ETM future and present values
    in a nested dict
    '''
    values = {}

    if inputs:
        raw_result = InputsScenario.execute(scenario_id, *inputs)
        values = process(raw_result) if raw_result.successful else fail_with(raw_result)

    # We always need to access this because we need area and end year
    # (TODO: we can also get that from inputs)
    query_result = QueryScenario.execute(scenario_id, detailed=True, *queries)
    if not query_result.successful: return fail_with(raw_result)

    values.update(query_result.value)

    return values
示例#3
0
    def post(self):
        """
        Transform ESDL energy system description into an ETM scenario
        """
        args = context_parser.parse_args()
        start_situation = self.__get_start_situation(
            args['energy_system_start_situation'])

        # Set the ETM scenario as context
        scenario_id = self.__scenario_id(args['scenario_id'],
                                         start_situation.area)
        start_situation.set_context_scenario(scenario_id)

        with HaltGarbageCollection():
            end_situation = self.__get_as_situation(
                args['energy_system_end_situation'])

        # Compare start and end to create new settings for the context scenario
        sliders = start_situation.relative_change_to_for_context(
            end_situation).slider_settings

        result = SetScenarioSliders.execute(scenario_id, sliders)
        if not result.successful:
            fail_with(result)

        return {'scenario_id': scenario_id}
示例#4
0
 def __attach_esdl_to_etm(self, energy_system_title):
     ''' Attach ESDL file to scenario '''
     KPIHandler(self.energy_system_handler,
                self.scenario_id).add_kpis_to_esdl()
     result = AttachEsdlToEtengine.execute(
         self.scenario_id, self.energy_system_handler.get_as_stream(),
         energy_system_title)
     if not result.successful:
         fail_with(result)
示例#5
0
    def __create_new_scenario_id(self, area_code):
        '''
        Creates a new scenario in ETEngine. Sets the scenario id if succesful.
        '''
        result = CreateScenario.execute(None, {
            'area_code': area_code,
            'end_year': 2050
        })

        if result.successful:
            self.scenario_id = result.value
        else:
            fail_with(result)
示例#6
0
def setup_energy_system_handler_from_args(args):
    '''
    Returns an EnergySystemHandler based on a passed energy_system if there was one given,
    or else on the energy system that was attached to the ETM scenario.
    If there is no ESDL to be found, fails the whole request
    '''
    if args['energy_system']:
        return EnergySystemHandler.from_string(urllib.parse.unquote(args['energy_system']))

    result = FetchEsdlFromEtengine.execute(args['session_id'])
    if result.successful:
        return EnergySystemHandler.from_string(result.value)

    fail_with(result)
示例#7
0
    def __handle_create(self, result):
        if not result.successful:
            fail_with(result)

        return result.value
示例#8
0
    def __set_sliders_in_etm(self, slider_settings):
        ''' Set sliders in new scenario '''
        result = SetScenarioSliders.execute(self.scenario_id, slider_settings)

        if not result.successful:
            fail_with(result)