-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:hotosm/osm-rawdata
Sync with main.
- Loading branch information
Showing
10 changed files
with
620 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (c) 2022, 2023 Humanitarian OpenStreetMap Team | ||
# This file is part of osm-rawdata. | ||
# | ||
# osm-rawdata is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# osm-rawdata is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with osm-rawdata. If not, see <https:#www.gnu.org/licenses/>. | ||
# | ||
|
||
version: "3" | ||
|
||
networks: | ||
net: | ||
name: osm-rawdata | ||
|
||
services: | ||
rawdata: | ||
image: "ghcr.io/hotosm/osm-rawdata:ci" | ||
build: | ||
target: ci | ||
container_name: osm-rawdata | ||
volumes: | ||
# Mount local package | ||
- ./osm_rawdata:/root/.local/lib/python3.10/site-packages/osm_rawdata | ||
# Mount local tests | ||
- ./tests:/data/tests | ||
depends_on: | ||
db: | ||
condition: service_healthy | ||
networks: | ||
- net | ||
restart: "unless-stopped" | ||
command: "pytest" | ||
|
||
db: | ||
image: "postgis/postgis:14-3.4-alpine" | ||
environment: | ||
- POSTGRES_USER=fmtm | ||
- POSTGRES_PASSWORD=testpass | ||
- POSTGRES_DB=underpass | ||
networks: | ||
- net | ||
restart: "unless-stopped" | ||
healthcheck: | ||
test: pg_isready -U fmtm -d underpass | ||
start_period: 5s | ||
interval: 10s | ||
timeout: 5s | ||
retries: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
# <[email protected]> | ||
|
||
import argparse | ||
import asyncio | ||
import json | ||
import logging | ||
import os | ||
|
@@ -28,13 +29,11 @@ | |
import zipfile | ||
from io import BytesIO | ||
from pathlib import Path | ||
from sys import argv | ||
from urllib.parse import urlparse | ||
|
||
import asyncpg | ||
import geojson | ||
import requests | ||
import asyncio | ||
import asyncpg | ||
from asyncpg import exceptions | ||
from geojson import Feature, FeatureCollection, Polygon | ||
from shapely import wkt | ||
from shapely.geometry import Polygon, shape | ||
|
@@ -48,39 +47,41 @@ | |
# Instantiate logger | ||
log = logging.getLogger(__name__) | ||
|
||
|
||
class DatabaseAccess(object): | ||
def __init__(self): | ||
"""This is a class to setup a database connection.""" | ||
self.pg = None | ||
self.dburi = None | ||
self.qc = None | ||
|
||
async def connect(self, | ||
dburi: str, | ||
): | ||
async def connect( | ||
self, | ||
dburi: str, | ||
): | ||
self.dburi = dict() | ||
uri = urlparse(dburi) | ||
if not uri.username: | ||
self.dburi['dbuser'] = os.getenv("PGUSER", default=None) | ||
if not self.dburi['dbuser']: | ||
log.error(f"You must specify the user name in the database URI, or set PGUSER") | ||
self.dburi["dbuser"] = os.getenv("PGUSER", default=None) | ||
if not self.dburi["dbuser"]: | ||
log.error("You must specify the user name in the database URI, or set PGUSER") | ||
else: | ||
self.dburi['dbuser'] = uri.username | ||
self.dburi["dbuser"] = uri.username | ||
if not uri.password: | ||
self.dburi['dbpass'] = os.getenv("PGPASSWORD", default=None) | ||
if not self.dburi['dbpass']: | ||
log.error(f"You must specify the user password in the database URI, or set PGPASSWORD") | ||
self.dburi["dbpass"] = os.getenv("PGPASSWORD", default=None) | ||
if not self.dburi["dbpass"]: | ||
log.error("You must specify the user password in the database URI, or set PGPASSWORD") | ||
else: | ||
self.dburi['dbpass'] = uri.password | ||
self.dburi["dbpass"] = uri.password | ||
if not uri.hostname: | ||
self.dburi['dbhost'] = os.getenv("PGHOST", default="localhost") | ||
self.dburi["dbhost"] = os.getenv("PGHOST", default="localhost") | ||
else: | ||
self.dburi['dbhost'] = uri.hostname | ||
self.dburi["dbhost"] = uri.hostname | ||
|
||
slash = uri.path.find('/') | ||
self.dburi['dbname'] = uri.path[slash + 1:] | ||
slash = uri.path.find("/") | ||
self.dburi["dbname"] = uri.path[slash + 1 :] | ||
connect = f"postgres://{self.dburi['dbuser']}:{ self.dburi['dbpass']}@{self.dburi['dbhost']}/{self.dburi['dbname']}" | ||
|
||
if self.dburi["dbname"] == "underpass": | ||
# Authentication data | ||
# self.auth = HTTPBasicAuth(self.user, self.passwd) | ||
|
@@ -292,11 +293,11 @@ async def createTable( | |
|
||
return True | ||
|
||
async def execute(self, | ||
sql: str, | ||
): | ||
""" | ||
Execute a raw SQL query and return the results. | ||
async def execute( | ||
self, | ||
sql: str, | ||
): | ||
"""Execute a raw SQL query and return the results. | ||
Args: | ||
sql (str): The SQL to execute | ||
|
@@ -442,17 +443,18 @@ def __init__( | |
# output: str = None | ||
): | ||
"""This is a client for a postgres database. | ||
Returns: | ||
(PostgresClient): An instance of this class | ||
""" | ||
super().__init__() | ||
self.qc = None | ||
|
||
async def loadConfig(self, | ||
config: str, | ||
): | ||
""" | ||
Load the JSON or YAML config file that defines the SQL query | ||
async def loadConfig( | ||
self, | ||
config: str, | ||
): | ||
"""Load the JSON or YAML config file that defines the SQL query | ||
Args: | ||
config (str): The filespec for the query config file | ||
|
@@ -535,6 +537,7 @@ async def execQuery( | |
collection = await self.queryRemote(request) | ||
return collection | ||
|
||
|
||
async def main(): | ||
"""This main function lets this class be run standalone by a bash script.""" | ||
parser = argparse.ArgumentParser( | ||
|
@@ -602,9 +605,9 @@ async def main(): | |
|
||
log.debug(f"Wrote {args.outfile}") | ||
|
||
|
||
if __name__ == "__main__": | ||
"""This is just a hook so this file can be run standalone during development.""" | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
loop.run_until_complete(main()) | ||
|
Oops, something went wrong.