Passed
Pull Request — dev (#943)
by
unknown
01:47
created

motorized_individual_travel_charging_infrastructure   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 44
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A unzip_file() 0 3 2
A download_zip() 0 8 3
A create_tables() 0 8 1
A get_tracbev_data() 0 7 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A MITChargingInfrastructure.__init__() 0 11 1
1
from __future__ import annotations
2
3
from pathlib import Path
4
import zipfile
5
6
from loguru import logger
7
import requests
8
9
from egon.data import config, db
10
from egon.data.datasets import Dataset
11
from egon.data.datasets.emobility.motorized_individual_travel_charging_infrastructure.db_classes import (  # noqa: E501
12
    EgonEmobChargingInfrastructure,
13
)
14
from egon.data.datasets.emobility.motorized_individual_travel_charging_infrastructure.infrastructure_allocation import (  # noqa: E501
15
    run_tracbev,
16
)
17
18
WORKING_DIR = Path(".", "charging_infrastructure").resolve()
19
DATASET_CFG = config.datasets()["charging_infrastructure"]
20
21
22
def create_tables() -> None:
23
    engine = db.engine()
24
    EgonEmobChargingInfrastructure.__table__.drop(bind=engine, checkfirst=True)
25
    EgonEmobChargingInfrastructure.__table__.create(
26
        bind=engine, checkfirst=True
27
    )
28
29
    logger.debug("Created tables.")
30
31
32
def download_zip(url: str, target: Path, chunk_size: int = 128) -> None:
33
    r = requests.get(url, stream=True)
34
35
    target.parent.mkdir(parents=True, exist_ok=True)
36
37
    with open(target, "wb") as fd:
38
        for chunk in r.iter_content(chunk_size=chunk_size):
39
            fd.write(chunk)
40
41
42
def unzip_file(source: Path, target: Path) -> None:
43
    with zipfile.ZipFile(source, "r") as zip_ref:
44
        zip_ref.extractall(target)
45
46
47
def get_tracbev_data() -> None:
48
    tracbev_cfg = DATASET_CFG["original_data"]["sources"]["tracbev"]
49
    file = WORKING_DIR / tracbev_cfg["file"]
50
51
    download_zip(url=tracbev_cfg["url"], target=file)
52
53
    unzip_file(source=file, target=WORKING_DIR)
54
55
56
class MITChargingInfrastructure(Dataset):
57
    def __init__(self, dependencies):
58
        super().__init__(
59
            name="MITChargingInfrastructure",
60
            version="0.0.1.dev",
61
            dependencies=dependencies,
62
            tasks=(
63
                {
64
                    create_tables,
65
                    get_tracbev_data,
66
                },
67
                run_tracbev,
68
            ),
69
        )
70