Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/#59 move resource management to importlib resources #119

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/egon/data/airflow/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ def initdb():
subprocess.run(
["docker-compose", "up", "-d", "--build"],
#cwd=os.path.dirname(__file__),
files('egon.data.airflow'),
cwd=files('egon.data.airflow'),
)
7 changes: 6 additions & 1 deletion src/egon/data/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import os.path
import subprocess

from importlib_resources import files

import click
import yaml

import egon.data
import egon.data.airflow

from importlib_resources import files

@click.command(
add_help_option=False,
Expand Down Expand Up @@ -85,7 +88,9 @@ def serve(context):
@click.version_option(version=egon.data.__version__)
@click.pass_context
def main(context, **kwargs):
os.environ["AIRFLOW_HOME"] = os.path.dirname(egon.data.airflow.__file__)
#os.environ["AIRFLOW_HOME"] = os.path.dirname(egon.data.airflow.__file__)
os.environ["AIRFLOW_HOME"] = os.path.dirname(files(egon.data.airflow).joinpath('__init__.py'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line exceeds the 79 character limit. Also os.path.dirname just removes the '__init__.py' component, so files(egon.data.airflow) should suffice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the files(egon.data.airflow) yields TypeError: str expected, not PosixPath. So, I changed it to str(files(egon.data.airflow)). I hope adding str() is ok.

#os.environ["AIRFLOW_HOME"] = files('egon.data.airflow')
translations = {
"database": "POSTGRES_DB",
"database_password": "POSTGRES_PASSWORD",
Expand Down
4 changes: 3 additions & 1 deletion src/egon/data/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import egon

from importlib_resources import files

def datasets(config_file=None):
"""Return dataset configuration.
Expand All @@ -24,7 +25,8 @@ def datasets(config_file=None):

"""
if not config_file:
package_path = egon.data.__path__[0]
#package_path = egon.data.__path__[0]
package_path = files('egon.data')
config_file = os.path.join(package_path, "datasets.yml")

return yaml.load(open(config_file), Loader=yaml.SafeLoader)
Empty file.
14 changes: 11 additions & 3 deletions src/egon/data/importing/openstreetmap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
import egon.data.config
import egon.data.subprocess as subprocess

import egon.data.importing.openstreetmap as import_openstreetmap
from importlib_resources import files


def download_pbf_file():
"""Download OpenStreetMap `.pbf` file."""
data_config = egon.data.config.datasets()
osm_config = data_config["openstreetmap"]["original_data"]

target_file = os.path.join(
os.path.dirname(__file__), osm_config["target"]["path"]
#os.path.dirname(__file__),
files(import_openstreetmap),
osm_config["target"]["path"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason to split up the original line. Our automatic code formatting tool will join them again anyway.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

)

if not os.path.isfile(target_file):
Expand All @@ -50,7 +55,9 @@ def to_postgres(num_processes=4, cache_size=4096):
data_config = egon.data.config.datasets()
osm_config = data_config["openstreetmap"]["original_data"]
input_file = os.path.join(
os.path.dirname(__file__), osm_config["target"]["path"]
#os.path.dirname(__file__),
files(import_openstreetmap),
osm_config["target"]["path"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will also be joined when formatting code.

)

# Prepare osm2pgsql command
Expand All @@ -74,7 +81,8 @@ def to_postgres(num_processes=4, cache_size=4096):
" ".join(cmd),
shell=True,
env={"PGPASSWORD": docker_db_config["POSTGRES_PASSWORD"]},
cwd=os.path.dirname(__file__),
#cwd=os.path.dirname(__file__),
cwd=files(import_openstreetmap),
)


Expand Down
7 changes: 5 additions & 2 deletions src/egon/data/importing/vg250.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
from egon.data import db
import egon.data.config

from importlib_resources import files

def download_vg250_files():
"""Download VG250 (Verwaltungsgebiete) shape files."""
data_config = egon.data.config.datasets()
vg250_config = data_config["vg250"]["original_data"]

target_file = os.path.join(
os.path.dirname(__file__), vg250_config["target"]["path"]
#os.path.dirname(__file__), vg250_config["target"]["path"]
files(egon.data.importing), vg250_config["target"]["path"]
)

if not os.path.isfile(target_file):
Expand All @@ -44,7 +46,8 @@ def to_postgres():
db.execute_sql(f"CREATE SCHEMA IF NOT EXISTS {vg250_processed['schema']};")

zip_file = os.path.join(
os.path.dirname(__file__), vg250_orig["target"]["path"]
#os.path.dirname(__file__), vg250_orig["target"]["path"]
files(egon.data.importing), vg250_orig["target"]["path"]
)
engine_local_db = db.engine()

Expand Down
14 changes: 10 additions & 4 deletions src/egon/data/importing/zensus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from egon.data import db, subprocess
import egon.data.config

import egon.data.importing.zensus as import_zensus
from importlib_resources import files

def download_zensus_pop():
"""Download Zensus csv file on population per hectar grid cell."""
Expand All @@ -17,7 +19,8 @@ def download_zensus_pop():
]

target_file = os.path.join(
os.path.dirname(__file__), zensus_population_config["target"]["path"]
#os.path.dirname(__file__), zensus_population_config["target"]["path"]
files(import_zensus), zensus_population_config["target"]["path"]
)

if not os.path.isfile(target_file):
Expand All @@ -39,7 +42,8 @@ def download_zensus_misc():
url_path_map = list(zip(zensus_url, zensus_path))

for url, path in url_path_map:
target_file_misc = os.path.join(os.path.dirname(__file__), path)
#target_file_misc = os.path.join(os.path.dirname(__file__), path)
target_file_misc = os.path.join(files(import_zensus), path)

if not os.path.isfile(target_file_misc):
urlretrieve(url, target_file_misc)
Expand Down Expand Up @@ -110,7 +114,8 @@ def population_to_postgres():
zensus_population_orig = data_config["zensus_population"]["original_data"]
zensus_population_processed = data_config["zensus_population"]["processed"]
input_file = os.path.join(
os.path.dirname(__file__), zensus_population_orig["target"]["path"]
#os.path.dirname(__file__), zensus_population_orig["target"]["path"]
files(import_zensus), zensus_population_orig["target"]["path"]
)

# Read database configuration from docker-compose.yml
Expand Down Expand Up @@ -184,7 +189,8 @@ def zensus_misc_to_postgres():

for input_file, table in zensus_misc_processed["path_table_map"].items():
with zipfile.ZipFile(os.path.join(
os.path.dirname(__file__), input_file)) as zf:
#os.path.dirname(__file__), input_file)) as zf:
files(import_zensus), input_file)) as zf:
csvfiles = [n for n in zf.namelist() if n.lower()[-3:] == "csv"]
for filename in csvfiles:
zf.extract(filename)
Expand Down