Skip to content

Commit dc5688f

Browse files
committed
And... a bit more
1 parent efbaa8c commit dc5688f

3 files changed

Lines changed: 62 additions & 45 deletions

File tree

‎docker/database.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def start_import(
257257
pgosm_region: str
258258
, pgosm_date: str
259259
, srid: int
260-
, language: str
260+
, language: str | None
261261
, layerset: str
262262
, git_info: str
263263
, osm2pgsql_version: str
@@ -464,7 +464,7 @@ def get_db_conn(conn_string: str) -> psycopg.Connection:
464464
return conn
465465

466466

467-
def pgosm_after_import(flex_path: str) -> bool:
467+
def pgosm_after_import(flex_path: Path) -> bool:
468468
"""Runs post-processing SQL via Lua script.
469469
470470
Layerset logic is established via environment variable, must happen
@@ -476,7 +476,7 @@ def pgosm_after_import(flex_path: str) -> bool:
476476

477477
output = subprocess.run(cmds,
478478
text=True,
479-
cwd=flex_path,
479+
cwd=str(flex_path),
480480
check=False,
481481
stdout=subprocess.PIPE,
482482
stderr=subprocess.STDOUT)
@@ -561,7 +561,7 @@ def osm2pgsql_replication_finish(skip_nested: bool):
561561
cur.execute(sql_raw, params)
562562

563563

564-
def run_pg_dump(export_path: str, skip_qgis_style: bool):
564+
def run_pg_dump(export_path: Path, skip_qgis_style: bool):
565565
"""Runs `pg_dump` to save processed data to load into other PostGIS DBs.
566566
"""
567567
logger = logging.getLogger('pgosm-flex')
@@ -572,25 +572,25 @@ def run_pg_dump(export_path: str, skip_qgis_style: bool):
572572
logger.info(f'Running pg_dump (only {schema_name} schema)')
573573
cmds = ['pg_dump', '-d', conn_string,
574574
f'--schema={schema_name}',
575-
'-f', export_path]
575+
'-f', str(export_path)]
576576
else:
577577
logger.info(f'Running pg_dump ({schema_name} schema plus extras)')
578578
cmds = ['pg_dump', '-d', conn_string,
579579
f'--schema={schema_name}',
580580
'--schema=pgosm',
581581
'--schema=public',
582-
'-f', export_path]
582+
'-f', str(export_path)]
583583

584584
output = subprocess.run(cmds,
585585
text=True,
586586
capture_output=True,
587587
check=False)
588588
LOGGER.info(f'pg_dump complete, saved to {export_path}')
589589
LOGGER.debug(f'pg_dump output: \n {output.stderr}')
590-
fix_pg_dump_create_public(export_path)
590+
fix_pg_dump_create_public(export_path=export_path)
591591

592592

593-
def fix_pg_dump_create_public(export_path: str):
593+
def fix_pg_dump_create_public(export_path: Path):
594594
"""Using pg_dump with `--schema=public` results in
595595
a .sql script containing `CREATE SCHEMA public;`, nearly always breaks
596596
in target DB. Replaces with `CREATE SCHEMA IF NOT EXISTS public;`

‎docker/helpers.py‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
import json
55
import logging
66
from packaging.version import parse as parse_version
7-
import subprocess
87
import os
8+
import subprocess
9+
from typing import Any
910
from pathlib import Path
1011
import sys
1112
from time import sleep
1213
import git
1314
from git.exc import InvalidGitRepositoryError
1415

1516

16-
DEFAULT_SRID = '3857'
17+
DEFAULT_SRID = 3857
1718

1819

1920
def get_today() -> str:
@@ -106,7 +107,7 @@ def verify_checksum(md5_file: Path, path: Path):
106107
def set_env_vars(
107108
region: str | None
108109
, subregion: str | None
109-
, srid: str
110+
, srid: int
110111
, language: str | None
111112
, pgosm_date: str
112113
, layerset: str
@@ -147,9 +148,12 @@ def set_env_vars(
147148
os.environ['SKIP_NESTED'] = str(skip_nested)
148149

149150

150-
def get_region_combined(region: str, subregion: str | None) -> str:
151+
def get_region_combined(region: str | None, subregion: str | None) -> str:
151152
"""Returns combined region with optional subregion.
152153
"""
154+
if region is None:
155+
raise ValueError('Region is required for this function.')
156+
153157
if subregion is None:
154158
pgosm_region = f'{region}'
155159
else:
@@ -226,7 +230,7 @@ def __init__(
226230
self
227231
, replication: bool
228232
, replication_update: bool
229-
, update: str
233+
, update: str | None
230234
, force: bool
231235
):
232236
"""Computes two variables, `slim_no_drop` and `append_first_run`
@@ -246,7 +250,7 @@ def __init__(
246250
self.replication_update = replication_update
247251

248252
# The input via click should enforce this, still worth checking here
249-
valid_update_options = ['append', 'create', None]
253+
valid_update_options: list[str | None] = ['append', 'create', None]
250254

251255
if update not in valid_update_options:
252256
raise ValueError(f'Invalid option for --update. Valid options: {valid_update_options}')
@@ -258,7 +262,7 @@ def __init__(
258262
self.set_append_first_run()
259263
self.set_run_post_sql()
260264

261-
def okay_to_run(self, prior_import: dict) -> bool:
265+
def okay_to_run(self, prior_import: dict[str, Any]) -> bool:
262266
"""Determines if it is okay to run PgOSM Flex without fear of data loss.
263267
264268
This logic was along with the `--force` option to make it
@@ -292,7 +296,7 @@ def okay_to_run(self, prior_import: dict) -> bool:
292296
self.logger.debug('No prior import found, okay to proceed.')
293297
return True
294298

295-
prior_replication = prior_import['replication']
299+
prior_replication = bool(prior_import['replication'])
296300

297301
# Check PgOSM version using Git tags
298302
# If current version is lower than prior version from latest import, stop.

‎docker/pgosm_flex.py‎

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def run_pgosm_flex(
8787
, schema_name: str
8888
, skip_nested: bool
8989
, skip_qgis_style: bool
90-
, srid: str
90+
, srid: int
9191
, update: str | None
9292
):
9393
"""Run PgOSM Flex within Docker to automate osm2pgsql flex processing.
@@ -164,7 +164,7 @@ def run_pgosm_flex(
164164

165165
prior_import = database.get_prior_import(schema_name=schema_name)
166166

167-
if not import_mode.okay_to_run(prior_import):
167+
if not import_mode.okay_to_run(prior_import=prior_import):
168168
msg = 'Not okay to run PgOSM Flex. Exiting'
169169
logger.error(msg)
170170
sys.exit(msg)
@@ -178,16 +178,21 @@ def run_pgosm_flex(
178178
output_lines=vers_lines)
179179

180180
osm2pgsql_version = '\n'.join(vers_lines)
181-
import_id = database.start_import(pgosm_region=helpers.get_region_combined(region, subregion),
182-
pgosm_date=pgosm_date,
183-
srid=srid,
184-
language=language,
185-
layerset=layerset,
186-
git_info=helpers.get_git_info(),
187-
osm2pgsql_version=osm2pgsql_version,
188-
import_mode=import_mode,
189-
schema_name=schema_name,
190-
input_file=input_file)
181+
import_id = database.start_import(
182+
pgosm_region=helpers.get_region_combined(
183+
region=region
184+
, subregion=subregion
185+
),
186+
pgosm_date=pgosm_date,
187+
srid=srid,
188+
language=language,
189+
layerset=layerset,
190+
git_info=helpers.get_git_info(),
191+
osm2pgsql_version=osm2pgsql_version,
192+
import_mode=import_mode,
193+
schema_name=schema_name,
194+
input_file=input_file
195+
)
191196

192197
logger.info(f'Started import id {import_id}')
193198

@@ -257,7 +262,7 @@ def run_osm2pgsql_standard(
257262

258263
if not skip_nested:
259264
# Don't expect user to use --skip-nested when place isn't included
260-
skip_nested = check_layerset_skip_nested_place(flex_path)
265+
skip_nested = check_layerset_skip_nested_place(flex_path=flex_path)
261266

262267
post_processing = run_post_processing(flex_path=flex_path,
263268
skip_nested=skip_nested,
@@ -368,11 +373,11 @@ def get_paths() -> dict[str, Path]:
368373
return paths
369374

370375

371-
def get_export_filename(input_file: str) -> str:
376+
def get_export_filename(input_file: str | None) -> str:
372377
"""Returns the .sql filename to use for pg_dump.
373378
"""
374379
# always set internally, even with --input-file and no --region
375-
region = os.environ.get('PGOSM_REGION').replace('/', '-')
380+
region = os.environ.get('PGOSM_REGION', '').replace('/', '-')
376381
subregion = os.environ.get('PGOSM_SUBREGION')
377382
layerset = os.environ.get('PGOSM_LAYERSET')
378383
pgosm_date = os.environ.get('PGOSM_DATE')
@@ -392,13 +397,13 @@ def get_export_filename(input_file: str) -> str:
392397
return filename
393398

394399

395-
def get_export_full_path(out_path: str, export_filename: str) -> str:
400+
def get_export_full_path(out_path: Path, export_filename: str) -> Path:
396401
"""If `export_filename` is an absolute path, `out_path` is not considered.
397402
"""
398403
if os.path.isabs(export_filename):
399-
export_path = export_filename
404+
export_path = Path(export_filename)
400405
else:
401-
export_path = os.path.join(out_path, export_filename)
406+
export_path = out_path / export_filename
402407

403408
return export_path
404409

@@ -421,12 +426,12 @@ def run_osm2pgsql(osm2pgsql_command: str, flex_path: Path):
421426
logger.info('osm2pgsql completed')
422427

423428

424-
def check_layerset_skip_nested_place(flex_path: str) -> bool:
429+
def check_layerset_skip_nested_place(flex_path: Path) -> bool:
425430
"""If `place` layer is not included `skip_nested` should be true.
426431
427432
Parameters
428433
------------------------
429-
flex_path : str
434+
flex_path : Path
430435
Path to the .ini file for the defined layerset defining which layers
431436
to include.
432437
@@ -437,7 +442,7 @@ def check_layerset_skip_nested_place(flex_path: str) -> bool:
437442
"""
438443
logger = logging.getLogger('pgosm-flex')
439444

440-
include_place = layerset_include_place(flex_path)
445+
include_place = layerset_include_place(flex_path=flex_path)
441446

442447
logger.debug('--------------------------------------------')
443448
logger.debug(f'Place layer value: {type(include_place)}')
@@ -452,7 +457,7 @@ def check_layerset_skip_nested_place(flex_path: str) -> bool:
452457
return True
453458

454459

455-
def layerset_include_place(flex_path: str) -> bool:
460+
def layerset_include_place(flex_path: Path) -> bool:
456461
"""
457462
Returns
458463
---------------------
@@ -465,7 +470,7 @@ def layerset_include_place(flex_path: str) -> bool:
465470
layerset_path = os.environ.get('PGOSM_LAYERSET_PATH')
466471

467472
if layerset_path is None:
468-
layerset_path = os.path.join(flex_path, 'layerset')
473+
layerset_path = flex_path / 'layerset'
469474
logger.info(f'Using default layerset path {layerset_path}')
470475

471476
ini_file = os.path.join(layerset_path, f'{layerset}.ini')
@@ -490,7 +495,7 @@ def layerset_include_place(flex_path: str) -> bool:
490495

491496

492497
def run_post_processing(
493-
flex_path: str
498+
flex_path: Path
494499
, skip_nested: bool
495500
, import_mode: helpers.ImportMode
496501
, schema_name: str
@@ -523,12 +528,20 @@ def run_post_processing(
523528
return True
524529

525530

526-
def dump_database(input_file: str, out_path: str, pg_dump: bool, skip_qgis_style: bool):
531+
def dump_database(
532+
input_file: str | None
533+
, out_path: Path
534+
, pg_dump: bool
535+
, skip_qgis_style: bool
536+
):
527537
"""Runs pg_dump when necessary to export the processed OpenStreetMap data.
528538
"""
529539
if pg_dump:
530-
export_filename = get_export_filename(input_file)
531-
export_path = get_export_full_path(out_path, export_filename)
540+
export_filename = get_export_filename(input_file=input_file)
541+
export_path = get_export_full_path(
542+
out_path=out_path
543+
, export_filename=export_filename
544+
)
532545

533546
database.run_pg_dump(export_path=export_path,
534547
skip_qgis_style=skip_qgis_style)
@@ -557,11 +570,11 @@ def check_replication_exists() -> bool:
557570
return True
558571

559572

560-
def run_osm2pgsql_replication_init(pbf_path: str, pbf_filename: str):
573+
def run_osm2pgsql_replication_init(pbf_path: Path, pbf_filename: str):
561574
"""Runs osm2pgsql-replication init to support replication mode.
562575
"""
563576
logger = logging.getLogger('pgosm-flex')
564-
pbf_path = os.path.join(pbf_path, pbf_filename)
577+
pbf_path = pbf_path / pbf_filename
565578
init_cmd = 'osm2pgsql-replication init -d $PGOSM_CONN '
566579
init_cmd += f'--osm-file {pbf_path}'
567580
logger.debug(f'Initializing DB for replication with command:\n{init_cmd}')

0 commit comments

Comments
 (0)