Skip to content

Commit 29eb6c7

Browse files
committed
refactor: reorganize imports and improve subprocess handling in transformation function
1 parent b7c37c8 commit 29eb6c7

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

middleware/main.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
The main script file for the FAIRagro basic middleware.
33
"""
4-
54
import os
65
import sys
76
from pathlib import Path
@@ -15,6 +14,11 @@
1514
import tempfile
1615

1716
import asyncio
17+
18+
from git_repo import GitRepo, GitRepoConfig
19+
from http_session import HttpSessionConfig
20+
from metadata_scraper import MetadataScraperConfig, scrape_repo
21+
1822
import aiofiles
1923
import pytz
2024
import yaml
@@ -40,9 +44,6 @@
4044
# Disable pylint warning that imports are not on top. But we need to adapt the import path before.
4145
# Is there another solution so packages next top the main script can be found?
4246
# pylint: disable=wrong-import-position
43-
from metadata_scraper import MetadataScraperConfig, scrape_repo
44-
from http_session import HttpSessionConfig
45-
from git_repo import GitRepo, GitRepoConfig
4647

4748

4849
def setup_opentelemetry(otlp_config: dict) -> None:
@@ -68,7 +69,8 @@ def setup_opentelemetry(otlp_config: dict) -> None:
6869
# Initialize OpenTelemetry for Tracing to OTLP endpoint
6970
trace.set_tracer_provider(
7071
TracerProvider(
71-
resource=Resource.create({"service.name": "FAIRagro middleware"}),
72+
resource=Resource.create(
73+
{"service.name": "FAIRagro middleware"}),
7274
active_span_processor=BatchSpanProcessor(
7375
OTLPSpanExporter(endpoint=endpoint)
7476
),
@@ -181,7 +183,8 @@ def setup_andconfig() -> dict:
181183
args = parser.parse_args()
182184

183185
if not os.path.isfile(args.config):
184-
raise FileNotFoundError(f"Config file {args.config} does not exist.")
186+
raise FileNotFoundError(
187+
f"Config file {args.config} does not exist.")
185188

186189
# load config
187190
with open(args.config, "r", encoding="utf-8") as f:
@@ -219,22 +222,20 @@ def transform_publisso_to_publisso_schemaorg():
219222
tmp_path = Path(tmp_file.name)
220223

221224
try:
222-
# Ejecutar jq en memoria
223-
p1 = subprocess.Popen(
224-
["jq", "-f", str(jq_script), str(input_file)], stdout=subprocess.PIPE
225-
)
226-
with open(tmp_path, "w", encoding="utf-8") as outfile:
227-
p2 = subprocess.Popen(
228-
["jq", "-s", "."], stdin=p1.stdout, stdout=outfile
229-
)
230-
p1.stdout.close() # Permite que p1 reciba SIGPIPE si p2 falla
231-
p2.communicate() # Espera a que termine
225+
# Ejecutar jq en memoria usando context managers so resources are cleaned up
226+
with subprocess.Popen(["jq", "-f", str(jq_script), str(input_file)], stdout=subprocess.PIPE
227+
) as p1:
228+
with open(tmp_path, "w", encoding="utf-8") as outfile:
229+
with subprocess.Popen(["jq", "-s", "."], stdin=p1.stdout, stdout=outfile) as p2:
230+
p1.stdout.close() # Permite que p1 reciba SIGPIPE si p2 falla
231+
p2.communicate() # Espera a que termine
232232

233233
# Reemplazar archivo original
234234
os.remove(input_file) # Eliminar input original
235235
tmp_path.rename(input_file) # Renombrar temp como input original
236236

237-
print(f"✅ Transformación completada, archivo actualizado: {input_file}")
237+
print(
238+
f"✅ Transformación completada, archivo actualizado: {input_file}")
238239

239240
except subprocess.CalledProcessError as e:
240241
print(f"❌ Error al ejecutar jq: {e}")
@@ -318,7 +319,8 @@ async def main():
318319
if sitemap["name"] == "openagrar":
319320
extract_thunen_from_openagrar_metadata()
320321
if git_repo:
321-
commit_to_git(scraper_config.url, git_repo, path, starttime)
322+
commit_to_git(scraper_config.url,
323+
git_repo, path, starttime)
322324

323325
if git_repo:
324326
git_repo.push()

0 commit comments

Comments
 (0)