Skip to content

Commit 42dd6a6

Browse files
authored
Add function to get data file path
1 parent 8a777b9 commit 42dd6a6

1 file changed

Lines changed: 37 additions & 4 deletions

File tree

findme.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
11
import json
22
import sys
3+
import os
34
import requests
45
from jsonschema import validate, ValidationError
56
from concurrent.futures import ThreadPoolExecutor, as_completed
67
from termcolor import colored
78

9+
def get_data_file_path(filename):
10+
"""Get the path to data files."""
11+
# Try current directory first
12+
if os.path.exists(filename):
13+
return filename
14+
15+
# Try script directory
16+
script_dir = os.path.dirname(os.path.abspath(__file__))
17+
data_path = os.path.join(script_dir, filename)
18+
if os.path.exists(data_path):
19+
return data_path
20+
21+
# Try installation directory
22+
try:
23+
import site
24+
for site_dir in site.getsitepackages():
25+
data_path = os.path.join(site_dir, filename)
26+
if os.path.exists(data_path):
27+
return data_path
28+
except:
29+
pass
30+
31+
raise FileNotFoundError(f"Cannot find {filename}")
32+
33+
834
def load_targets(json_file, schema_file):
935
"""Load and validate the target platforms from a JSON file."""
10-
with open(json_file, 'r') as file:
36+
try:
37+
json_path = get_data_file_path(json_file)
38+
schema_path = get_data_file_path(schema_file)
39+
except FileNotFoundError as e:
40+
print(f"Error: {e}")
41+
print("Make sure data.json and data.schema.json are in the same directory.")
42+
exit(1)
43+
44+
with open(json_path, 'r') as file:
1145
data = json.load(file)
1246

13-
14-
with open(schema_file, 'r') as schema:
47+
with open(schema_path, 'r') as schema:
1548
schema_data = json.load(schema)
1649
try:
1750
validate(instance=data, schema=schema_data)
@@ -152,4 +185,4 @@ def main():
152185

153186

154187
if __name__ == "__main__":
155-
main()
188+
main()

0 commit comments

Comments
 (0)