From 9a764ed3c34f38c90dc15ccd71f78d9d30275f2f Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 16 Apr 2023 13:06:26 +0100 Subject: [PATCH] Uses current dir path for reading files --- lib/combine.py | 10 +++++++--- lib/download.py | 10 ++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/combine.py b/lib/combine.py index e21686e..64db191 100644 --- a/lib/combine.py +++ b/lib/combine.py @@ -2,7 +2,11 @@ import os import json # Get list of files in external-templates -files = os.listdir('../external-templates') +dir = os.path.dirname(os.path.abspath(__file__)) +templates_src_dir = os.path.join(dir, '../external-templates/') +template_dest_file = os.path.join(dir, '../templates.json') + +files = os.listdir(templates_src_dir) # Initialize empty list to store template objects templates = [] @@ -10,7 +14,7 @@ templates = [] # For each file in external-templates for file in files: # Open the file - with open('../external-templates/' + file) as f: + with open(templates_src_dir + file) as f: # Load the JSON into a variable data = json.load(f)['templates'] # Append the template object to the templates list @@ -26,5 +30,5 @@ fileData = { } # Open the templates.json file, and write results to it -with open('../templates.json', 'w') as f: +with open(template_dest_file, 'w') as f: json.dump(fileData, f, indent=2, sort_keys=False) diff --git a/lib/download.py b/lib/download.py index d10454e..e4a3fc0 100644 --- a/lib/download.py +++ b/lib/download.py @@ -2,8 +2,10 @@ import os import csv import requests -destination_dir = '../external-templates' -sources_list = '../sources.csv' +dir = os.path.dirname(os.path.abspath(__file__)) + +destination_dir = os.path.join(dir, '../external-templates') +sources_list = os.path.join(dir, '../sources.csv') # Downloads the file from a given URL, to the local destination def download(url: str, filename: str): @@ -23,7 +25,7 @@ def download(url: str, filename: str): # Gets list of URLs to download from CSV file def get_source_list(): sources=[] - with open('../sources.csv', mode='r') as file: + with open(sources_list, mode='r') as file: csvFile = csv.reader(file) for lines in csvFile:# sources.append(lines) @@ -33,6 +35,6 @@ def get_source_list(): if not os.path.exists(destination_dir): os.makedirs(destination_dir) -# # For each source, download the templates JSON file +# For each source, download the templates JSON file for sourceUrl in get_source_list(): download(sourceUrl[1], sourceUrl[0] + '.json')