gracefully handle absent extra_functionality file; add file to path

This commit is contained in:
Fabian Neumann 2024-01-04 09:00:31 +01:00
parent b4fb395158
commit d145758fb7
6 changed files with 13 additions and 13 deletions

View File

@ -28,6 +28,13 @@ def memory(w):
return int(factor * (10000 + 195 * int(w.clusters)))
def input_custom_extra_functionality(w):
path = config["solving"]["options"].get("custom_extra_functionality", False)
if path:
return workflow.source_path(path)
return []
# Check if the workflow has access to the internet by trying to access the HEAD of specified url
def has_internet_access(url="www.zenodo.org") -> bool:
import http.client as http_client

View File

@ -11,9 +11,7 @@ rule solve_network:
co2_sequestration_potential=config["sector"].get(
"co2_sequestration_potential", 200
),
custom_extra_functionality=workflow.source_path(
config["solving"]["options"].get("custom_extra_functionality", "")
),
custom_extra_functionality=input_custom_extra_functionality,
input:
network=RESOURCES + "networks/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}.nc",
config=RESULTS + "config.yaml",

View File

@ -88,9 +88,7 @@ rule solve_sector_network_myopic:
co2_sequestration_potential=config["sector"].get(
"co2_sequestration_potential", 200
),
custom_extra_functionality=workflow.source_path(
config["solving"]["options"].get("custom_extra_functionality", "")
),
custom_extra_functionality=input_custom_extra_functionality,
input:
network=RESULTS
+ "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc",

View File

@ -2,7 +2,6 @@
#
# SPDX-License-Identifier: MIT
rule solve_sector_network:
params:
solving=config["solving"],
@ -11,9 +10,7 @@ rule solve_sector_network:
co2_sequestration_potential=config["sector"].get(
"co2_sequestration_potential", 200
),
custom_extra_functionality=workflow.source_path(
config["solving"]["options"].get("custom_extra_functionality", "")
),
custom_extra_functionality=input_custom_extra_functionality,
input:
network=RESULTS
+ "prenetworks/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc",

View File

@ -118,9 +118,7 @@ rule solve_sector_network_perfect:
co2_sequestration_potential=config["sector"].get(
"co2_sequestration_potential", 200
),
custom_extra_functionality=workflow.source_path(
config["solving"]["options"].get("custom_extra_functionality", "")
),
custom_extra_functionality=input_custom_extra_functionality,
input:
network=RESULTS
+ "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_brownfield_all_years.nc",

View File

@ -30,6 +30,7 @@ import importlib
import logging
import os
import re
import sys
import numpy as np
import pandas as pd
@ -831,6 +832,7 @@ def extra_functionality(n, snapshots):
if snakemake.params.custom_extra_functionality:
source_path = snakemake.params.custom_extra_functionality
assert os.path.exists(source_path), f"{source_path} does not exist"
sys.path.append(os.path.dirname(source_path))
module_name = os.path.splitext(os.path.basename(source_path))[0]
module = importlib.import_module(module_name)
module.custom_extra_functionality(n, snapshots)