Skip to content

Commit 9be0bf7

Browse files
committed
Ruff format
1 parent 82ea64b commit 9be0bf7

3 files changed

Lines changed: 198 additions & 242 deletions

File tree

pygeoapi/process/manager/postgresql.py

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
String,
4646
Table,
4747
text,
48-
update
48+
update,
4949
)
5050
from sqlalchemy.engine import Engine
5151
from sqlalchemy.orm import declarative_base, Session
@@ -54,7 +54,7 @@
5454
from pygeoapi.process.base import (
5555
JobNotFoundError,
5656
JobResultNotFoundError,
57-
ProcessorGenericError
57+
ProcessorGenericError,
5858
)
5959
from pygeoapi.process.manager.base import BaseManager
6060
from pygeoapi.provider.sql import get_engine, store_db_parameters
@@ -81,21 +81,21 @@ def __init__(self, manager_def: dict):
8181

8282
super().__init__(manager_def)
8383
self.is_async = True
84-
self.id_field = 'identifier'
84+
self.id_field = "identifier"
8585
self.supports_subscribing = True
86-
self.connection = manager_def['connection']
86+
self.connection = manager_def["connection"]
8787

88-
options = manager_def.get('options', {})
89-
self._store_db_parameters(manager_def['connection'], options)
88+
options = manager_def.get("options", {})
89+
self._store_db_parameters(manager_def["connection"], options)
9090
self._engine = get_engine(
91-
'postgresql+psycopg2',
91+
"postgresql+psycopg2",
9292
self.db_host,
9393
self.db_port,
9494
self.db_name,
9595
self.db_user,
9696
self._db_password,
9797
self.db_conn,
98-
**self.db_options
98+
**self.db_options,
9999
)
100100
self.table_output = self.output_dir is None
101101

@@ -104,16 +104,14 @@ def __init__(self, manager_def: dict):
104104
)
105105
self.c = self.table_model.c
106106
try:
107-
LOGGER.debug('Getting table model')
107+
LOGGER.debug("Getting table model")
108108

109109
except Exception as err:
110-
msg = 'Table model fetch failed'
111-
LOGGER.error(f'{msg}: {err}')
110+
msg = "Table model fetch failed"
111+
LOGGER.error(f"{msg}: {err}")
112112
raise ProcessorGenericError(msg)
113113

114-
def get_jobs(
115-
self, status: JobStatus = None, limit=None, offset=None
116-
) -> dict:
114+
def get_jobs(self, status: JobStatus = None, limit=None, offset=None) -> dict:
117115
"""
118116
Get jobs
119117
@@ -126,15 +124,15 @@ def get_jobs(
126124
and numberMatched
127125
"""
128126

129-
LOGGER.debug('Querying for jobs')
127+
LOGGER.debug("Querying for jobs")
130128
with Session(self._engine) as session:
131129
results = session.query(self.table_model)
132130

133131
if status is not None:
134132
results = results.filter(self.c.status == status.value)
135133

136134
jobs = [r._asdict() for r in results.all()]
137-
return {'jobs': jobs, 'numberMatched': len(jobs)}
135+
return {"jobs": jobs, "numberMatched": len(jobs)}
138136

139137
def add_job(self, job_metadata: dict) -> str:
140138
"""
@@ -145,20 +143,18 @@ def add_job(self, job_metadata: dict) -> str:
145143
:returns: identifier of added job
146144
"""
147145

148-
LOGGER.debug('Adding job')
146+
LOGGER.debug("Adding job")
149147
with Session(self._engine) as session:
150148
try:
151-
session.execute(
152-
insert(self.table_model).values(**job_metadata)
153-
)
149+
session.execute(insert(self.table_model).values(**job_metadata))
154150
session.commit()
155151
except Exception as err:
156152
session.rollback()
157-
msg = 'Insert failed'
158-
LOGGER.error(f'{msg}: {err}')
153+
msg = "Insert failed"
154+
LOGGER.error(f"{msg}: {err}")
159155
raise ProcessorGenericError(msg)
160156

161-
return job_metadata['identifier']
157+
return job_metadata["identifier"]
162158

163159
def update_job(self, job_id: str, update_dict: dict) -> bool:
164160
"""
@@ -172,7 +168,7 @@ def update_job(self, job_id: str, update_dict: dict) -> bool:
172168

173169
rowcount = 0
174170

175-
LOGGER.debug('Updating job')
171+
LOGGER.debug("Updating job")
176172
with Session(self._engine) as session:
177173
try:
178174
stmt = (
@@ -185,8 +181,8 @@ def update_job(self, job_id: str, update_dict: dict) -> bool:
185181
rowcount = result.rowcount
186182
except Exception as err:
187183
session.rollback()
188-
msg = 'Update failed'
189-
LOGGER.error(f'{msg}: {err}')
184+
msg = "Update failed"
185+
LOGGER.error(f"{msg}: {err}")
190186
raise ProcessorGenericError(msg)
191187

192188
return rowcount == 1
@@ -202,7 +198,7 @@ def get_job(self, job_id: str) -> dict:
202198
:returns: `dict` # `pygeoapi.process.manager.Job`
203199
"""
204200

205-
LOGGER.debug('Querying for job')
201+
LOGGER.debug("Querying for job")
206202
with Session(self._engine) as session:
207203
results = session.query(self.table_model).filter(
208204
self.c.identifier == job_id
@@ -230,21 +226,19 @@ def delete_job(self, job_id: str) -> bool:
230226

231227
# get result file if present for deletion
232228
job_result = self.get_job(job_id)
233-
location = job_result.get('location')
229+
location = job_result.get("location")
234230

235-
LOGGER.debug('Deleting job')
231+
LOGGER.debug("Deleting job")
236232
with Session(self._engine) as session:
237233
try:
238-
stmt = delete(self.table_model).where(
239-
self.c.identifier == job_id
240-
)
234+
stmt = delete(self.table_model).where(self.c.identifier == job_id)
241235
result = session.execute(stmt)
242236
session.commit()
243237
rowcount = result.rowcount
244238
except Exception as err:
245239
session.rollback()
246-
msg = 'Delete failed'
247-
LOGGER.error(f'{msg}: {err}')
240+
msg = "Delete failed"
241+
LOGGER.error(f"{msg}: {err}")
248242
raise ProcessorGenericError(msg)
249243

250244
# delete result file if present
@@ -270,36 +264,32 @@ def get_job_result(self, job_id: str) -> Tuple[str, Any]:
270264
"""
271265

272266
job_result = self.get_job(job_id)
273-
location = job_result.get('location')
274-
mimetype = job_result.get('mimetype')
275-
job_status = JobStatus[job_result['status']]
267+
location = job_result.get("location")
268+
mimetype = job_result.get("mimetype")
269+
job_status = JobStatus[job_result["status"]]
276270

277271
if job_status != JobStatus.successful:
278272
# Job is incomplete
279273
return (None,)
280274
if not location:
281-
LOGGER.warning(f'job {job_id!r} - unknown result location')
275+
LOGGER.warning(f"job {job_id!r} - unknown result location")
282276
raise JobResultNotFoundError()
283277
else:
284278
try:
285279
location = Path(location)
286-
if mimetype in (
287-
None,
288-
FORMAT_TYPES[F_JSON],
289-
FORMAT_TYPES[F_JSONLD]
290-
):
291-
with location.open('r', encoding='utf-8') as fh:
280+
if mimetype in (None, FORMAT_TYPES[F_JSON], FORMAT_TYPES[F_JSONLD]):
281+
with location.open("r", encoding="utf-8") as fh:
292282
result = json.load(fh)
293283
else:
294-
with location.open('rb') as fh:
284+
with location.open("rb") as fh:
295285
result = fh.read()
296286
except (TypeError, FileNotFoundError, json.JSONDecodeError):
297287
raise JobResultNotFoundError()
298288
else:
299289
return mimetype, result
300290

301291
def __repr__(self):
302-
return f'<PostgreSQLManager> {self.name}'
292+
return f"<PostgreSQLManager> {self.name}"
303293

304294

305295
@functools.cache
@@ -312,31 +302,31 @@ def get_table_model(
312302
schema = db_search_path[0]
313303

314304
Jobs = Table(
315-
'jobs',
305+
"jobs",
316306
Base.metadata,
317-
Column('identifier', String, primary_key=True, nullable=False),
307+
Column("identifier", String, primary_key=True, nullable=False),
318308
Column(
319-
'type',
309+
"type",
320310
String,
321311
nullable=False,
322-
server_default=text("'process'::character varying")
312+
server_default=text("'process'::character varying"),
323313
),
324-
Column('process_id', String, nullable=False),
325-
Column('created', DateTime),
326-
Column('started', DateTime),
327-
Column('finished', DateTime),
328-
Column('updated', DateTime),
329-
Column('status', String, nullable=False),
330-
Column('location', String),
331-
Column('mimetype', String),
332-
Column('message', String),
333-
Column('progress', Integer, nullable=False),
334-
schema=schema
314+
Column("process_id", String, nullable=False),
315+
Column("created", DateTime),
316+
Column("started", DateTime),
317+
Column("finished", DateTime),
318+
Column("updated", DateTime),
319+
Column("status", String, nullable=False),
320+
Column("location", String),
321+
Column("mimetype", String),
322+
Column("message", String),
323+
Column("progress", Integer, nullable=False),
324+
schema=schema,
335325
)
336326

337327
if table_output:
338-
Jobs.append_column(Column('output', LargeBinary))
328+
Jobs.append_column(Column("output", LargeBinary))
339329

340330
Base.metadata.create_all(engine, tables=[Jobs], checkfirst=True)
341331

342-
return Jobs
332+
return Jobs

0 commit comments

Comments
 (0)