Skip to content

Commit

Permalink
Order import and add docstring to formatting script run fxn
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Oct 7, 2024
1 parent abc8432 commit 77ba1ef
Showing 1 changed file with 51 additions and 35 deletions.
86 changes: 51 additions & 35 deletions src/scribe_data/wikidata/query_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

import json
import os
import subprocess
import sys
from pathlib import Path
from urllib.error import HTTPError
import sys
import subprocess

from tqdm.auto import tqdm

Expand All @@ -35,6 +35,52 @@
from scribe_data.wikidata.wikidata_utils import sparql


def execute_formatting_script(formatting_file_path, output_dir):
"""
Executes a formatting script given a filepath and output directory for the process.
Parameters
----------
formatting_file_path : str
The formatting file to run.
output_dir : str
The output directory path for results.
Returns
-------
The results of the formatting script saved in the given output directory.
"""
# Determine the root directory of the project.
project_root = Path(__file__).parent.parent

if sys.platform.startswith("win"):
python_executable = sys.executable
pythonpath = str(project_root)

# Create environment with updated PYTHONPATH.
env = os.environ.copy()
if "PYTHONPATH" in env:
env["PYTHONPATH"] = f"{pythonpath};{env['PYTHONPATH']}"

else:
env["PYTHONPATH"] = pythonpath

# Use subprocess.run instead of os.system.
subprocess.run(
[python_executable, str(formatting_file_path), "--file-path", output_dir],
env=env,
check=True,
)

else:
# Unix-like systems (Linux, macOS).
subprocess.run(
["python3", str(formatting_file_path), "--file-path", output_dir],
check=True,
)


def query_data(
languages: str = None,
data_type: str = None,
Expand Down Expand Up @@ -264,39 +310,9 @@ def query_data(
/ target_type
/ f"format_{target_type}.py"
)
# Replace the original os.system call with:
execute_formatting_script(formatting_file_path, output_dir)

# os.system(f"python3 {formatting_file_path} --file-path {output_dir}")


def execute_formatting_script(formatting_file_path, output_dir):
# Determine the root directory of the project
project_root = Path(__file__).parent.parent

if sys.platform.startswith("win"):
python_executable = sys.executable
pythonpath = str(project_root)

# Create environment with updated PYTHONPATH
env = os.environ.copy()
if "PYTHONPATH" in env:
env["PYTHONPATH"] = f"{pythonpath};{env['PYTHONPATH']}"
else:
env["PYTHONPATH"] = pythonpath

# Use subprocess.run instead of os.system
subprocess.run(
[python_executable, str(formatting_file_path), "--file-path", output_dir],
env=env,
check=True,
)
else:
# Unix-like systems (Linux, macOS)
subprocess.run(
["python3", str(formatting_file_path), "--file-path", output_dir],
check=True,
)
execute_formatting_script(
formatting_file_path=formatting_file_path, output_dir=output_dir
)


if __name__ == "__main__":
Expand Down

0 comments on commit 77ba1ef

Please sign in to comment.