Skip to content

Commit b5a70e9

Browse files
Merge pull request #356 from rustprooflabs/cleanup
Cleanup docblock, type hinting, remove mdbook-variables
2 parents d28ab76 + 719dfb2 commit b5a70e9

File tree

10 files changed

+67
-45
lines changed

10 files changed

+67
-45
lines changed

.github/workflows/deploy-book.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ jobs:
1818
echo "PATH=$HOME/.cargo/bin:$PATH" >> $GITHUB_ENV
1919
- name: Install mdbook
2020
run: |
21-
cargo install mdbook --version 0.4.32
22-
cargo install mdbook-variables --version 0.2.1
21+
cargo install mdbook
2322
- name: Deploy GitHub Pages
2423
run: |
2524
# This assumes your book is in the root of your repository.

docker/db.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Interacts with Postgres
1+
"""Module to interact with Postgres database.
22
"""
33
import logging
44
import os
@@ -13,7 +13,7 @@
1313
LOGGER = logging.getLogger('pgosm-flex')
1414

1515

16-
def connection_string(admin=False):
16+
def connection_string(admin: bool=False) -> str:
1717
"""Returns connection string to `db_name`.
1818
1919
Env vars for user/password defined by Postgres docker image.
@@ -63,7 +63,8 @@ def connection_string(admin=False):
6363

6464

6565
def pg_conn_parts() -> dict:
66-
"""Retrieves username/password from environment variables if they exist.
66+
"""Returns dictionary of connection parts based on environment variables
67+
if they exist.
6768
6869
Returns
6970
--------------------------
@@ -129,7 +130,8 @@ def wait_for_postgres():
129130
"""Ensures Postgres service is reliably ready for use.
130131
131132
Required b/c Postgres process in Docker gets restarted shortly
132-
after starting.
133+
after starting. Calls `sys.exit()` after `max_loops` reached
134+
indicating failure due to inability to connect.
133135
"""
134136
logger = logging.getLogger('pgosm-flex')
135137
logger.info('Checking for Postgres service to be available')
@@ -161,7 +163,7 @@ def wait_for_postgres():
161163
logger.info('Postgres instance ready')
162164

163165

164-
def pg_isready():
166+
def pg_isready() -> bool:
165167
"""Checks for Postgres to be available.
166168
167169
Uses pg_version_check() for simple approach.
@@ -184,7 +186,7 @@ def pg_isready():
184186

185187

186188
def log_pg_details():
187-
"""Logs non-sensitive Postgres connection details.
189+
"""Logs non-sensitive Postgres connection details to LOGGER.
188190
"""
189191
conn_parts = pg_conn_parts()
190192
pg_host = conn_parts['pg_host']

docker/geofabrik.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""This module handles the auto-file using Geofabrik's download service.
1+
"""This module handles the auto-file handling using Geofabrik's download service.
22
"""
33
import logging
44
import os
@@ -8,8 +8,9 @@
88
import helpers
99

1010

11-
def get_region_filename():
12-
"""Returns the filename needed to download/manage PBF files.
11+
def get_region_filename() -> str:
12+
"""Returns the filename needed to download/manage PBF files based on the
13+
region/subregion.
1314
1415
Returns
1516
----------------------
@@ -27,7 +28,7 @@ def get_region_filename():
2728
return filename
2829

2930

30-
def prepare_data(out_path):
31+
def prepare_data(out_path: str) -> str:
3132
"""Ensures the PBF file is available.
3233
3334
Checks if it already exists locally, download if needed,
@@ -70,13 +71,15 @@ def prepare_data(out_path):
7071
return pbf_file
7172

7273

73-
def pbf_download_needed(pbf_file_with_date, md5_file_with_date, pgosm_date):
74+
def pbf_download_needed(pbf_file_with_date: str, md5_file_with_date: str,
75+
pgosm_date: str) -> bool:
7476
"""Decides if the PBF/MD5 files need to be downloaded.
7577
7678
Parameters
7779
-------------------------------
7880
pbf_file_with_date : str
7981
md5_file_with_date : str
82+
pgosm_date : str
8083
8184
Returns
8285
--------------------------
@@ -110,7 +113,7 @@ def pbf_download_needed(pbf_file_with_date, md5_file_with_date, pgosm_date):
110113
return download_needed
111114

112115

113-
def get_pbf_url(region, subregion):
116+
def get_pbf_url(region: str, subregion: str) -> str:
114117
"""Returns the URL to the PBF for the region / subregion.
115118
116119
Parameters
@@ -132,7 +135,7 @@ def get_pbf_url(region, subregion):
132135
return pbf_url
133136

134137

135-
def download_data(region, subregion, pbf_file, md5_file):
138+
def download_data(region: str, subregion: str, pbf_file: str, md5_file: str):
136139
"""Downloads PBF and MD5 file using wget.
137140
138141
Parameters
@@ -166,7 +169,8 @@ def download_data(region, subregion, pbf_file, md5_file):
166169
)
167170

168171

169-
def archive_data(pbf_file, md5_file, pbf_file_with_date, md5_file_with_date):
172+
def archive_data(pbf_file: str, md5_file: str, pbf_file_with_date: str,
173+
md5_file_with_date: str):
170174
"""Copies `pbf_file` and `md5_file` to `pbf_file_with_date` and
171175
`md5_file_with_date`.
172176
@@ -190,8 +194,8 @@ def archive_data(pbf_file, md5_file, pbf_file_with_date, md5_file_with_date):
190194
shutil.copy2(md5_file, md5_file_with_date)
191195

192196

193-
194-
def unarchive_data(pbf_file, md5_file, pbf_file_with_date, md5_file_with_date):
197+
def unarchive_data(pbf_file: str, md5_file: str, pbf_file_with_date: str,
198+
md5_file_with_date: str):
195199
"""Copies `pbf_file_with_date` and `md5_file_with_date`
196200
to `pbf_file` and `md5_file`.
197201
@@ -218,7 +222,7 @@ def unarchive_data(pbf_file, md5_file, pbf_file_with_date, md5_file_with_date):
218222
shutil.copy2(md5_file_with_date, md5_file)
219223

220224

221-
def remove_latest_files(out_path):
225+
def remove_latest_files(out_path: str):
222226
"""Removes the PBF and MD5 file with -latest in the name.
223227
224228
Files are archived via prepare_data() before processing starts

docker/helpers.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Generic functions used in multiple modules.
1+
"""Generic functions and attributes used in multiple modules of PgOSM Flex.
22
"""
33
import datetime
44
import logging
@@ -14,19 +14,20 @@
1414
DEFAULT_SRID = '3857'
1515

1616

17-
def get_today():
17+
def get_today() -> str:
1818
"""Returns yyyy-mm-dd formatted string for today.
1919
20-
Retunrs
20+
Returns
2121
-------------------------
2222
today : str
2323
"""
2424
today = datetime.datetime.today().strftime('%Y-%m-%d')
2525
return today
2626

2727

28-
def run_command_via_subprocess(cmd, cwd, output_lines=[], print=False):
29-
"""Wraps around subprocess.Popen to run commands outside of Python. Prints
28+
def run_command_via_subprocess(cmd: list, cwd: str, output_lines: list=[],
29+
print: bool=False) -> int:
30+
"""Wraps around subprocess.Popen() to run commands outside of Python. Prints
3031
output as it goes, returns the status code from the command.
3132
3233
Parameters
@@ -66,12 +67,15 @@ def run_command_via_subprocess(cmd, cwd, output_lines=[], print=False):
6667
return status
6768

6869

69-
def verify_checksum(md5_file, path):
70-
"""If verfication fails calls `sys.exit()`
70+
def verify_checksum(md5_file: str, path: str):
71+
"""Verifies checksum of osm pbf file.
72+
73+
If verification fails calls `sys.exit()`
7174
7275
Parameters
7376
---------------------
7477
md5_file : str
78+
Filename of the MD5 file to verify the osm.pbf file.
7579
path : str
7680
Path to directory with `md5_file` to validate
7781
"""
@@ -146,7 +150,7 @@ def set_env_vars(region, subregion, srid, language, pgosm_date, layerset,
146150

147151

148152

149-
def get_region_combined(region, subregion):
153+
def get_region_combined(region: str, subregion: str) -> str:
150154
"""Returns combined region with optional subregion.
151155
152156
Parameters
@@ -167,7 +171,7 @@ def get_region_combined(region, subregion):
167171
return pgosm_region
168172

169173

170-
def get_git_info():
174+
def get_git_info() -> str:
171175
"""Provides git info in the form of the latest tag and most recent short sha
172176
173177
Sends info to logger and returns string.

docker/import_mode.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def okay_to_run(self, prior_import: dict) -> bool:
6666
6767
An empty dictionary (len==0) indicates no prior import.
6868
Only the replication key is specifically used
69+
70+
Returns
71+
-------------------
72+
okay_to_run : bool
6973
"""
7074
self.logger.debug(f'Checking if it is okay to run...')
7175
if self.force:
@@ -132,8 +136,13 @@ def set_run_post_sql(self):
132136
if self.update == 'append':
133137
self.run_post_sql = False
134138

135-
def as_json(self):
136-
"""Returns key details as a dictionary.
139+
def as_json(self) -> str:
140+
"""Packs key details as a dictionary passed through `json.dumps()`
141+
142+
Returns
143+
------------------------
144+
json_text : str
145+
Text representation of JSON object built using class attributes.
137146
"""
138147
self_as_dict = {'update': self.update,
139148
'replication': self.replication,

docker/osm2pgsql_recommendation.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
"""Used by PgOSM-Flex Docker image to get osm2pgsql command to run from
2-
the osm2pgsql-tuner API.
1+
"""Determine the appropriate osm2pgsql command to run from the osm2pgsql-tuner
2+
project.
33
"""
44
import logging
55
import os
66
import osm2pgsql_tuner as tuner
77

8-
import db
8+
import db, import_mode
99

1010
LOGGER = logging.getLogger('pgosm-flex')
1111

1212

13-
def osm2pgsql_recommendation(ram, pbf_filename, out_path, import_mode):
14-
"""Returns recommended osm2pgsql command.
13+
def osm2pgsql_recommendation(ram: float, pbf_filename: str, out_path: str,
14+
import_mode: import_mode.ImportMode) -> str:
15+
"""Returns recommended osm2pgsql command from the osm2pgsql-tuner
16+
Python module: https://pypi.org/project/osm2pgsql-tuner/
1517
1618
Recommendation from Python project.
1719
Public API available at https://osm2pgsql-tuner.com
@@ -45,8 +47,11 @@ def osm2pgsql_recommendation(ram, pbf_filename, out_path, import_mode):
4547
out_path)
4648
return osm2pgsql_cmd
4749

48-
def get_recommended_script(system_ram_gb, osm_pbf_gb, import_mode, pbf_filename,
49-
output_path):
50+
def get_recommended_script(system_ram_gb: float,
51+
osm_pbf_gb: float,
52+
import_mode:import_mode.ImportMode,
53+
pbf_filename: str,
54+
output_path: str) -> str:
5055
"""Generates recommended osm2pgsql command from osm2pgsql-tuner.
5156
5257
Parameters

docker/pgosm_flex.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env python3
2-
"""Python script to run PgOSM Flex within Docker container.
2+
"""Runs PgOSM Flex within Docker container.
33
44
Docker image available on Docker Hub
55
https://hub.docker.com/r/rustprooflabs/pgosm-flex
66
7-
Usage instructions:
8-
https://github.com/rustprooflabs/pgosm-flex/blob/main/docs/DOCKER-RUN.md
7+
Documentation available at https://pgosm-flex.com/
98
"""
109
import configparser
1110
import logging
@@ -39,7 +38,7 @@
3938
@click.option('--input-file',
4039
required=False,
4140
default=None,
42-
help='Set filename or absolute filepath to input osm.pbf file. Overrides default file handling, archiving, and MD5 checksum validation. Filename is assumed under /app/output unless absolute path is used.')
41+
help='Set filename or absolute filepath to input osm.pbf file. Overrides default file handling, archiving, and MD5 checksum validation. Filename is assumed to reference a file under /app/output unless absolute path is used.')
4342
@click.option('--layerset', required=True,
4443
default='default',
4544
help='Layerset to load. Defines name of included layerset unless --layerset-path is defined.')

docs/src/docker-build.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ docker build -t rustprooflabs/pgosm-flex .
6666
Tag with version.
6767

6868
```bash
69-
docker build -t rustprooflabs/pgosm-flex:{{ pgosm_flex_version }} .
69+
docker build -t rustprooflabs/pgosm-flex:0.10.0 .
7070
```
7171

7272
Push to Docker Hub.
7373

7474
```bash
7575
docker push rustprooflabs/pgosm-flex:latest
76-
docker push rustprooflabs/pgosm-flex:{{ pgosm_flex_version }}
76+
docker push rustprooflabs/pgosm-flex:0.10.0
7777
```
7878

7979

docs/src/replication.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ docker run --name pgosm -d --rm \
5454
-v /etc/localtime:/etc/localtime:ro \
5555
-e POSTGRES_PASSWORD=$POSTGRES_PASSWORD \
5656
-p 5433:5432 \
57-
-d rustprooflabs/pgosm-flex:{{ pgosm_flex_version }} \
57+
-d rustprooflabs/pgosm-flex:0.10.0 \
5858
-c max_connections=300
5959
```
6060

docs/src/troubleshooting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ docker run --name pgosm -d --rm \
7272
-v ~/pgosm-data:/app/output \
7373
-v /etc/localtime:/etc/localtime:ro \
7474
-e POSTGRES_PASSWORD=$POSTGRES_PASSWORD \
75-
-p 5433:5432 -d rustprooflabs/pgosm-flex:{{ pgosm_flex_version }} \
75+
-p 5433:5432 -d rustprooflabs/pgosm-flex \
7676
-c shared_buffers=1GB \
7777
-c work_mem=50MB \
7878
-c maintenance_work_mem=10GB \

0 commit comments

Comments
 (0)